PageRenderTime 949ms CodeModel.GetById 181ms app.highlight 649ms RepoModel.GetById 106ms app.codeStats 1ms

/factory/factory-core/src/main/java/org/qualipso/factory/eventqueue/EventQueueServiceBean.java

https://github.com/benzen/Ulysse
Java | 1369 lines | 918 code | 173 blank | 278 comment | 221 complexity | 34ef442262b0c1cc29e4c6ded716c080 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/*
   2 * Qualipso Funky Factory
   3 * Copyright (C) 2006-2010 INRIA
   4 * http://www.inria.fr - molli@loria.fr
   5 *
   6 * This is free software; you can redistribute it and/or modify it
   7 * under the terms of the GNU Lesser General Public License version 3
   8 * as published by the Free Software Foundation. See the GNU
   9 * Lesser General Public License in LGPL.txt for more details.
  10 *
  11 * You should have received a copy of the GNU Lesser General Public
  12 * License along with this software; if not, write to the Free
  13 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  14 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  15 *
  16 * Initial authors :
  17 *
  18 *Elamri Firas
  19 *Yuksel Huriye
  20 *Secondary authors :
  21 *Amrou Mohanned Khalifa
  22 *Yiqing Li
  23 */
  24package org.qualipso.factory.eventqueue;
  25
  26import java.util.ArrayList;
  27import java.util.Date;
  28import java.util.Iterator;
  29import java.util.List;
  30import java.util.UUID;
  31
  32import javax.annotation.Resource;
  33import javax.ejb.EJB;
  34import javax.ejb.SessionContext;
  35import javax.ejb.Stateless;
  36import javax.jws.WebMethod;
  37import javax.jws.WebService;
  38import javax.jws.soap.SOAPBinding;
  39import javax.jws.soap.SOAPBinding.Style;
  40import javax.persistence.EntityManager;
  41import javax.persistence.PersistenceContext;
  42import javax.persistence.Query;
  43
  44import org.apache.commons.logging.Log;
  45import org.apache.commons.logging.LogFactory;
  46import org.jboss.ejb3.annotation.SecurityDomain;
  47import org.jboss.ws.annotation.EndpointConfig;
  48import org.jboss.wsf.spi.annotation.WebContext;
  49import org.qualipso.factory.FactoryException;
  50import org.qualipso.factory.FactoryNamingConvention;
  51import org.qualipso.factory.FactoryResource;
  52import org.qualipso.factory.FactoryResourceIdentifier;
  53import org.qualipso.factory.FactoryResourceProperty;
  54import org.qualipso.factory.binding.BindingService;
  55import org.qualipso.factory.binding.PathHelper;
  56import org.qualipso.factory.core.CoreServiceException;
  57import org.qualipso.factory.eventqueue.entity.PersistentEvent;
  58import org.qualipso.factory.eventqueue.entity.EventQueue;
  59import org.qualipso.factory.eventqueue.entity.Rule;
  60import org.qualipso.factory.membership.MembershipService;
  61import org.qualipso.factory.notification.NotificationService;
  62import org.qualipso.factory.security.pap.PAPService;
  63import org.qualipso.factory.security.pap.PAPServiceHelper;
  64import org.qualipso.factory.security.pep.AccessDeniedException;
  65import org.qualipso.factory.security.pep.PEPService;
  66import org.qualipso.factory.security.pep.PEPServiceException;
  67
  68/**
  69 * Implementation of the ClockService. Provides a time service for the factory.
  70 * 
  71 * @author <a href="mailto:christophe.bouthier@loria.fr">Christophe Bouthier</a>
  72 * @author Nicolas HENRY
  73 * @author Marlène HANTZ
  74 * @date 27 July 2009
  75 */
  76@Stateless(name = EventQueueService.SERVICE_NAME, mappedName = FactoryNamingConvention.SERVICE_PREFIX + EventQueueService.SERVICE_NAME)
  77@WebService(endpointInterface = "org.qualipso.factory.eventqueue.EventQueueService", targetNamespace = FactoryNamingConvention.SERVICE_NAMESPACE + EventQueueService.SERVICE_NAME, serviceName = EventQueueService.SERVICE_NAME)
  78@WebContext(contextRoot = FactoryNamingConvention.WEB_SERVICE_CORE_MODULE_CONTEXT, urlPattern = FactoryNamingConvention.WEB_SERVICE_URL_PATTERN_PREFIX + EventQueueService.SERVICE_NAME)
  79@SOAPBinding(style = Style.RPC)
  80@SecurityDomain(value = "JBossWSDigest")
  81@EndpointConfig(configName = "Standard WSSecurity Endpoint")
  82public class EventQueueServiceBean implements EventQueueService {
  83
  84    private static Log logger = LogFactory.getLog(EventQueueServiceBean.class);
  85
  86    private BindingService binding;
  87    private PEPService pep;
  88    private PAPService pap;
  89    private NotificationService notification;
  90    private MembershipService membership;
  91    private SessionContext ctx;
  92    private EntityManager em;
  93
  94    public EventQueueServiceBean() {
  95    }
  96
  97    /**
  98     * cette methode positionne l Entity Manager
  99     * 
 100     * @param em
 101     *            Entity Manager
 102     */
 103    @PersistenceContext
 104    public void setEntityManager(EntityManager em) {
 105        this.em = em;
 106    }
 107
 108    /**
 109     * cette methode retourne l Entity Manager
 110     * 
 111     * @return em l'entity manager
 112     */
 113    public EntityManager getEntityManager() {
 114        return this.em;
 115    }
 116
 117    /**
 118     * cette methode positionne la Session Context
 119     * 
 120     * @param ctx
 121     *            la Session Context
 122     */
 123    @Resource
 124    public void setSessionContext(SessionContext ctx) {
 125        this.ctx = ctx;
 126    }
 127
 128    /**
 129     * cette methode retourne la Session Context
 130     * 
 131     * @return la Session Context
 132     */
 133    public SessionContext getSessionContext() {
 134        return this.ctx;
 135    }
 136
 137    /**
 138     * cette methode positionne le Binding Service
 139     * 
 140     * @param binding
 141     *            le Binding Service
 142     * 
 143     */
 144    @EJB
 145    public void setBindingService(BindingService binding) {
 146        this.binding = binding;
 147    }
 148
 149    /**
 150     * 
 151     *cette methode retourne le Binding Service
 152     * 
 153     * @return le Binding Service
 154     */
 155    public BindingService getBindingService() {
 156        return this.binding;
 157    }
 158
 159    /**
 160     * cette methode positionne le PEP Service
 161     * 
 162     * @param pep
 163     *            le PEP Service
 164     * 
 165     */
 166    @EJB
 167    public void setPEPService(PEPService pep) {
 168        this.pep = pep;
 169    }
 170
 171    /**
 172     * cette methode retourne le PEP Service
 173     * 
 174     * @return PEP Service
 175     */
 176    public PEPService getPEPService() {
 177        return this.pep;
 178    }
 179
 180    /**
 181     * cette methode positionne le PAP Service
 182     * 
 183     * @param pap
 184     *            le PAP Service
 185     * 
 186     */
 187    @EJB
 188    public void setPAPService(PAPService pap) {
 189        this.pap = pap;
 190    }
 191
 192    /**
 193     * cette methode retourne le PAP Service
 194     * 
 195     * @return PAPS ervice
 196     */
 197    public PAPService getPAPService() {
 198        return this.pap;
 199    }
 200
 201    /**
 202     * cette methode positionne le Notification Service
 203     * 
 204     * @param notification
 205     *            le Notification Service
 206     * 
 207     */
 208    @EJB
 209    public void setNotificationService(NotificationService notification) {
 210        this.notification = notification;
 211    }
 212
 213    /**
 214     * cette methode retourne le Notification Service
 215     * 
 216     * @return le Service Notification
 217     */
 218    public NotificationService getNotificationService() {
 219        return this.notification;
 220    }
 221
 222    /**
 223     * cette methode positionne le Membership Service
 224     * 
 225     * @param membership
 226     *            le Membership Service
 227     * 
 228     */
 229    @EJB
 230    public void setMembershipService(MembershipService membership) {
 231        this.membership = membership;
 232    }
 233
 234    /**
 235     * cette methode retourne le Membership Service
 236     * 
 237     * @return le Membership Service
 238     */
 239    public MembershipService getMembershipService() {
 240        return this.membership;
 241    }
 242
 243    /**
 244     * cette methode retourne les evenements contenu dans la queue sinon une
 245     * exception si la queue n est pas trouve
 246     * 
 247     * @param name
 248     *            le nom de la queue
 249     * @return returne un tableau des evenements contenu dans la queue
 250     */
 251    @Override
 252    public PersistentEvent[] getEvents(String path) throws EventQueueServiceException {
 253
 254        FactoryResourceIdentifier identifier;
 255        try {
 256            identifier = binding.lookup(path);
 257            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 258                String caller = membership.getProfilePathForConnectedIdentifier();
 259                pep.checkSecurity(caller, path, "read");
 260
 261                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 262                if (eventqueue == null) {
 263                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 264                }
 265
 266                PersistentEvent[] evs = new PersistentEvent[eventqueue.getEvents().size()];
 267
 268                /*
 269                 * ArrayList<Event> evts = new ArrayList<Event>();
 270                 * ArrayList<Event> allEvts = eventqueue.getEvents(); for (Event
 271                 * event : allEvts) { try{
 272                 * pep.checkSecurity(caller,event.getFromResource(), "read");
 273                 * evts.add(event); }catch(PEPServiceException e){
 274                 * 
 275                 * } }
 276                 */
 277
 278                return eventqueue.getEvents().toArray(evs);
 279
 280            } else {
 281                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 282            }
 283        } catch (Exception e) {
 284            logger.error("unable to get events", e);
 285            ctx.setRollbackOnly();
 286            throw new EventQueueServiceException("unable to get events", e);
 287        }
 288    }
 289
 290    /**
 291     * cette méthode permet de creer une nouvelle queue a partir de son nom
 292     * sinon retourne une exception si la queue n a pas pu etre cree la queue
 293     * est rendu persistante et bindee par la suite
 294     * 
 295     * @param name
 296     *            le nom de la nouvelle queue a creer
 297     * 
 298     */
 299    @Override
 300    public void createEventQueue(String path) throws EventQueueServiceException {
 301        logger.info("createEventQueue(...) called");
 302        logger.debug("params : path=" + path);
 303        try {
 304
 305            String caller = membership.getProfilePathForConnectedIdentifier();
 306            pep.checkSecurity(caller, PathHelper.getParentPath(path), "create");
 307            EventQueue evq = new EventQueue();
 308            evq.setId(UUID.randomUUID().toString());
 309            evq.setEvents(new ArrayList<PersistentEvent>());
 310            em.persist(evq);
 311
 312            binding.bind(evq.getFactoryResourceIdentifier(), path);
 313
 314            String policyId = UUID.randomUUID().toString();
 315            pap.createPolicy(policyId, PAPServiceHelper.addRuleToPolicy(PAPServiceHelper.buildOwnerPolicy(policyId, caller, path), "", new String[] { "create",
 316                    "read", "update" }));
 317            binding.setProperty(path, FactoryResourceProperty.OWNER, caller);
 318            binding.setProperty(path, FactoryResourceProperty.POLICY_ID, policyId);
 319            
 320            //TODO notification...
 321            //TODO indexation...
 322
 323        } catch (Exception e) {
 324            logger.error("unable to create an event queue", e);
 325            ctx.setRollbackOnly();
 326            throw new EventQueueServiceException("unable to create an event queue", e);
 327        }
 328    }
 329
 330    /**
 331     * cette methode place un evenement dans la queue associe au name sinon
 332     * retourne une exception si l evenement n est pas ajoute
 333     * 
 334     * @param name
 335     *            le nom de l'event queue
 336     * @param event
 337     *            l evenement a pusher dans la queue
 338     * 
 339     */
 340    @Override
 341    public void pushEvent(String path, PersistentEvent event) throws EventQueueServiceException {
 342
 343        FactoryResourceIdentifier identifier;
 344        try {
 345            if (event == null) {
 346                throw new IllegalArgumentException("event can't be null");
 347            }
 348            identifier = binding.lookup(path);
 349            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 350                String caller = membership.getProfilePathForConnectedIdentifier();
 351                pep.checkSecurity(caller, path, "update");
 352
 353                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 354                if (eventqueue == null) {
 355                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 356                }
 357
 358                eventqueue.getEvents().add(event);
 359
 360                em.persist(eventqueue);
 361            } else {
 362                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 363            }
 364        } catch (Exception e) {
 365            logger.error("unable to pushEvent an event in the event queue", e);
 366            ctx.setRollbackOnly();
 367            throw new EventQueueServiceException("unable to pushEvent an event in the event queue", e);
 368        }
 369    }
 370
 371    /**
 372     * cette methode retourne l event queue associe a path sinon une exception
 373     * si l'event queue n'est pas trouve
 374     * 
 375     * @param path
 376     *            le path de l event queue
 377     * @return event queue associé a path
 378     */
 379    @Override
 380    public FactoryResource findResource(String path) throws FactoryException {
 381        logger.info("findResource(...) called");
 382        logger.debug("params : path=" + path);
 383
 384        FactoryResourceIdentifier identifier = binding.lookup(path);
 385
 386        if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 387            String caller = membership.getProfilePathForConnectedIdentifier();
 388            pep.checkSecurity(caller, path, "read");
 389
 390            EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 391            if (eventqueue == null) {
 392                throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 393            }
 394
 395            return eventqueue;
 396        }
 397
 398        throw new EventQueueServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 399    }
 400
 401    @Override
 402    public PersistentEvent getLastEvent(String path) throws EventQueueServiceException {
 403
 404        FactoryResourceIdentifier identifier;
 405        try {
 406            identifier = binding.lookup(path);
 407            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 408                String caller = membership.getProfilePathForConnectedIdentifier();
 409                pep.checkSecurity(caller, path, "read");
 410
 411                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 412                if (eventqueue == null) {
 413                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 414                }
 415
 416                int size = eventqueue.getEvents().size();
 417                PersistentEvent[] evq = new PersistentEvent[size];
 418                evq = eventqueue.getEvents().toArray(evq);
 419                PersistentEvent result = evq[size - 1];
 420                return result;
 421
 422            } else {
 423                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 424            }
 425        } catch (Exception e) {
 426            logger.error("unable to get Last Event", e);
 427            ctx.setRollbackOnly();
 428            throw new EventQueueServiceException("unable to get Last Event", e);
 429        }
 430
 431    }
 432
 433    /**
 434     * cette methode retourne le Resource Type List
 435     * 
 436     * @return Resource Type List
 437     */
 438    @Override
 439    public String[] getResourceTypeList() {
 440        return RESOURCE_TYPE_LIST;
 441    }
 442
 443    /**
 444     * cette methode retourne le nom du service
 445     * 
 446     * @return le nom du cervice
 447     */
 448    @Override
 449    public String getServiceName() {
 450        return SERVICE_NAME;
 451    }
 452
 453    @Override
 454    public void deleteEvent(String path, PersistentEvent e) throws EventQueueServiceException {
 455
 456        FactoryResourceIdentifier identifier;
 457
 458        try {
 459            identifier = binding.lookup(path);
 460            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 461                String caller = membership.getProfilePathForConnectedIdentifier();
 462                pep.checkSecurity(caller, path, "update");
 463
 464                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 465                if (eventqueue == null) {
 466                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 467                }
 468                /*
 469                 * int size = eventqueue.getEvents().size(); Event[] evq = new
 470                 * Event[size]; evq = eventqueue.getEvents().toArray(evq);
 471                 */
 472                ArrayList<PersistentEvent> newEventList = eventqueue.getEvents();
 473                if (newEventList.remove(e)) {
 474                    eventqueue.setEvents(newEventList);
 475                    em.merge(eventqueue);
 476                } else {
 477                    throw new EventQueueServiceException("this event doesn't exist in this queue");
 478                }
 479
 480            } else {
 481                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 482            }
 483        } catch (Exception ex) {
 484            logger.error("unable to delete Event", ex);
 485            ctx.setRollbackOnly();
 486            throw new EventQueueServiceException("unable to delete Event", ex);
 487        }
 488
 489    }
 490
 491    /**
 492     * 
 493     * return an array of event that have or contains the same ressourceType
 494     * 
 495     * @param path
 496     *            path of the EventQueue
 497     * @param ressourceType
 498     *            the type of resource event
 499     * @param substring
 500     *            true if typeRssource contains parameter typeRessource, false
 501     *            if typeRssource contains exactly the parameter typeRessource
 502     * @return array of event
 503     * @throws EventQueueServiceException
 504     */
 505    @WebMethod
 506    public PersistentEvent[] findEventByRessourceType(String path, String ressourceType, boolean substring) throws EventQueueServiceException {
 507
 508        FactoryResourceIdentifier identifier;
 509        try {
 510            // li des cas exceptionnelles
 511            if ((path == null) || (ressourceType == null))
 512                throw new EventQueueServiceException("Le parametre n'est pas correct verifier le path ou le ressourceType");
 513
 514            identifier = binding.lookup(path);
 515            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 516                String caller = membership.getProfilePathForConnectedIdentifier();
 517                pep.checkSecurity(caller, path, "read");
 518
 519                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 520                if (eventqueue == null) {
 521                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 522                }
 523
 524                ArrayList<PersistentEvent> resultContains = new ArrayList<PersistentEvent>();
 525                ArrayList<PersistentEvent> resultEquals = new ArrayList<PersistentEvent>();
 526
 527                ArrayList<PersistentEvent> listEvent = eventqueue.getEvents();
 528                Iterator<PersistentEvent> it = listEvent.iterator();
 529
 530                while (it.hasNext()) {
 531                    PersistentEvent ev = it.next();
 532
 533                    if (substring == true && ev.getResourceType().contains(ressourceType)) {
 534                        resultContains.add(ev);
 535
 536                    }
 537
 538                    if (substring == false && ev.getResourceType().equals(ressourceType)) {
 539                        resultEquals.add(ev);
 540                    }
 541                }
 542
 543                if (substring == true) {
 544                    return (PersistentEvent[]) (resultContains.toArray());
 545                } else {
 546                    return (PersistentEvent[]) (resultEquals.toArray());
 547                }
 548            }
 549
 550            else {
 551                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 552            }
 553        } catch (Exception e) {
 554            logger.error("unable to  find Event By Ressource Type", e);
 555            ctx.setRollbackOnly();
 556            throw new EventQueueServiceException("unable to  find Event By Ressource Type", e);
 557        }
 558
 559    }
 560
 561    /**
 562     * return an array of event that have or contains the same thrower
 563     * 
 564     * @param path
 565     *            path of the EventQueue
 566     * @param thrower
 567     *            thrower of event
 568     * @param substring
 569     *            true if thrower contains the string parameter thrower , false
 570     *            if thrower contains exactly the parameter thrower.
 571     * @return array of event (return an array of event that have or contains
 572     *         the same thrower)
 573     * @throws EventQueueServiceException
 574     */
 575    @WebMethod
 576    public PersistentEvent[] findEventBythrower(String path, String thrower, boolean substring) throws EventQueueServiceException {
 577
 578        FactoryResourceIdentifier identifier;
 579        try {
 580            identifier = binding.lookup(path);
 581            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 582                String caller = membership.getProfilePathForConnectedIdentifier();
 583                pep.checkSecurity(caller, path, "read");
 584
 585                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 586                if (eventqueue == null) {
 587                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 588                }
 589
 590                ArrayList<PersistentEvent> resultContains = new ArrayList<PersistentEvent>();
 591                ArrayList<PersistentEvent> resultFalse = new ArrayList<PersistentEvent>();
 592
 593                ArrayList<PersistentEvent> listEvent = eventqueue.getEvents();
 594                Iterator<PersistentEvent> it = listEvent.iterator();
 595
 596                while (it.hasNext()) {
 597                    PersistentEvent ev = it.next();
 598
 599                    if (substring == true && ev.getThrowedBy().contains(thrower)) {
 600                        resultContains.add(ev);
 601                    }
 602
 603                    if (substring == false && ev.getThrowedBy().equals(thrower)) {
 604                        resultFalse.add(ev);
 605                    }
 606                }
 607
 608                if (substring == true) {
 609                    PersistentEvent[] tab = new PersistentEvent[resultContains.size()];
 610                    int i = 0;
 611                    while (i < resultContains.size()) {
 612                        tab[i] = resultContains.get(i);
 613                        i++;
 614                    }
 615                    return tab;
 616                } else {
 617                    PersistentEvent[] tab = new PersistentEvent[resultFalse.size()];
 618                    int i = 0;
 619                    while (i < resultFalse.size()) {
 620                        tab[i] = resultFalse.get(i);
 621                        i++;
 622                    }
 623                    return tab;
 624                }
 625            }
 626
 627            else {
 628                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 629            }
 630        } catch (Exception e) {
 631            logger.error("unable to find Event By thrower", e);
 632            ctx.setRollbackOnly();
 633            throw new EventQueueServiceException("unable to find Event By thrower", e);
 634        }
 635    }
 636
 637    /**
 638     * 
 639     * @param path
 640     *            path of the EventQueue
 641     * @param fromRessource
 642     *            fromRessource of event
 643     * @param substring
 644     *            true if fromRessource contains the string parameter
 645     *            fromRessource , false if thrower contains exactly the
 646     *            parameter fromRessource.
 647     * @return array of event ( return an array of event that have or contains
 648     *         the same fromRessource )
 649     * @throws EventQueueServiceException
 650     */
 651    @WebMethod
 652    public PersistentEvent[] findEventByFromRessource(String path, String fromRessource, boolean substring) throws EventQueueServiceException {
 653        FactoryResourceIdentifier identifier;
 654        try {
 655            // li des cas exceptionnelles
 656            if ((path == null) || (fromRessource == null))
 657                throw new EventQueueServiceException("Le parametre n'est pas correct verifier le path ou fromRessource");
 658
 659            identifier = binding.lookup(path);
 660            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 661                String caller = membership.getProfilePathForConnectedIdentifier();
 662                pep.checkSecurity(caller, path, "read");
 663
 664                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 665                if (eventqueue == null) {
 666                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 667                }
 668
 669                ArrayList<PersistentEvent> resultContains = new ArrayList<PersistentEvent>();
 670                ArrayList<PersistentEvent> resultFalse = new ArrayList<PersistentEvent>();
 671
 672                ArrayList<PersistentEvent> listEvent = eventqueue.getEvents();
 673                Iterator<PersistentEvent> it = listEvent.iterator();
 674
 675                while (it.hasNext()) {
 676                    PersistentEvent ev = it.next();
 677
 678                    if (substring == true && ev.getFromResource().contains(fromRessource)) {
 679                        resultContains.add(ev);
 680                    }
 681
 682                    if (substring == false && ev.getFromResource().equals(fromRessource)) {
 683                        resultFalse.add(ev);
 684                    }
 685                }
 686
 687                if (substring == true) {
 688                    PersistentEvent[] tab = new PersistentEvent[resultContains.size()];
 689                    int i = 0;
 690                    while (i < resultContains.size()) {
 691                        tab[i] = resultContains.get(i);
 692                        i++;
 693                    }
 694                    return tab;
 695                } else {
 696                    PersistentEvent[] tab = new PersistentEvent[resultFalse.size()];
 697                    int i = 0;
 698                    while (i < resultFalse.size()) {
 699                        tab[i] = resultFalse.get(i);
 700                        i++;
 701                    }
 702                    return tab;
 703                }
 704            }
 705
 706            else {
 707                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 708            }
 709        } catch (Exception e) {
 710            logger.error("unable to find Event By from Ressource", e);
 711            ctx.setRollbackOnly();
 712            throw new EventQueueServiceException("unable to find Event By from Ressource", e);
 713        }
 714    }
 715
 716    /**
 717     * 
 718     * @param path
 719     *            path of the EventQueue
 720     * @param date
 721     *            date of the event
 722     * @return array of event ( return an array of event having the same date )
 723     * @throws EventQueueServiceException
 724     */
 725    @WebMethod
 726    public PersistentEvent[] findEventByDate(String path, Date date) throws EventQueueServiceException {
 727        FactoryResourceIdentifier identifier;
 728        try {
 729            identifier = binding.lookup(path);
 730            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 731                String caller = membership.getProfilePathForConnectedIdentifier();
 732                pep.checkSecurity(caller, path, "read");
 733
 734                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 735                if (eventqueue == null) {
 736                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 737                }
 738
 739                ArrayList<PersistentEvent> result = new ArrayList<PersistentEvent>();
 740
 741                ArrayList<PersistentEvent> listEvent = eventqueue.getEvents();
 742                Iterator<PersistentEvent> it = listEvent.iterator();
 743
 744                while (it.hasNext()) {
 745                    PersistentEvent ev = it.next();
 746                    if (ev.getDate().equals(date)) {
 747                        result.add(ev);
 748                    }
 749
 750                }
 751
 752                PersistentEvent[] tab = new PersistentEvent[result.size()];
 753                int i = 0;
 754                while (i < result.size()) {
 755                    tab[i] = result.get(i);
 756                    i++;
 757                }
 758                // return (Event[]) (result.toArray());
 759                return tab;
 760            }
 761
 762            else {
 763                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 764            }
 765        } catch (Exception e) {
 766            logger.error("unable to find Event By Date", e);
 767            ctx.setRollbackOnly();
 768            throw new EventQueueServiceException("unable to find Event By Date", e);
 769        }
 770
 771    }
 772
 773    /**
 774     * 
 775     * @param path
 776     *            path of the EventQueue
 777     * @param date
 778     *            date of the event
 779     * @return array of event (return an array of event having date lesser or
 780     *         equal of the parameter date )
 781     * @throws EventQueueServiceException
 782     */
 783
 784    @WebMethod
 785    public PersistentEvent[] findEventByDateInf(String path, Date date) throws EventQueueServiceException {
 786        FactoryResourceIdentifier identifier;
 787        try {
 788            identifier = binding.lookup(path);
 789            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 790                String caller = membership.getProfilePathForConnectedIdentifier();
 791                pep.checkSecurity(caller, path, "read");
 792
 793                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 794                if (eventqueue == null) {
 795                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 796                }
 797
 798                ArrayList<PersistentEvent> result = new ArrayList<PersistentEvent>();
 799
 800                ArrayList<PersistentEvent> listEvent = eventqueue.getEvents();
 801                Iterator<PersistentEvent> it = listEvent.iterator();
 802
 803                while (it.hasNext()) {
 804                    PersistentEvent ev = (PersistentEvent) it.next();
 805                    if (ev.getDate().before(date) || ev.getDate().equals(date)) {
 806                        result.add(ev);
 807                    }
 808
 809                }// while
 810
 811                // return (Event[]) (result.toArray());
 812
 813                PersistentEvent[] tab = new PersistentEvent[result.size()];
 814                int i = 0;
 815                while (i < result.size()) {
 816                    tab[i] = result.get(i);
 817                    i++;
 818                }
 819                return tab;
 820            }
 821
 822            else {
 823                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 824            }
 825        } catch (Exception e) {
 826            logger.error("unable to find Event By DateSup", e);
 827            ctx.setRollbackOnly();
 828            throw new EventQueueServiceException("unable to find Event By DateSup", e);
 829        }
 830    }
 831
 832    /**
 833     * 
 834     * @param path
 835     *            path of the EventQueue
 836     * @param date
 837     *            date date of the event
 838     * @return array of event (return an array of event having date superior or
 839     *         equal of the parameter date )
 840     * @throws EventQueueServiceException
 841     */
 842    @WebMethod
 843    public PersistentEvent[] findEventByDateSup(String path, Date date) throws EventQueueServiceException {
 844        FactoryResourceIdentifier identifier;
 845        try {
 846            identifier = binding.lookup(path);
 847            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 848                String caller = membership.getProfilePathForConnectedIdentifier();
 849                pep.checkSecurity(caller, path, "read");
 850
 851                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 852                if (eventqueue == null) {
 853                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 854                }
 855
 856                ArrayList<PersistentEvent> result = new ArrayList<PersistentEvent>();
 857
 858                ArrayList<PersistentEvent> listEvent = eventqueue.getEvents();
 859                Iterator<PersistentEvent> it = listEvent.iterator();
 860
 861                while (it.hasNext()) {
 862                    PersistentEvent ev = it.next();
 863                    if (ev.getDate().after(date) || ev.getDate().equals(date)) {
 864                        result.add(ev);
 865                    }
 866
 867                }// while
 868
 869                PersistentEvent[] tab = new PersistentEvent[result.size()];
 870                int i = 0;
 871                while (i < result.size()) {
 872                    tab[i] = result.get(i);
 873                    i++;
 874                }
 875                return tab;
 876            }
 877
 878            else {
 879                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 880            }
 881        } catch (Exception e) {
 882            logger.error("unable to find Event By DateInf", e);
 883            ctx.setRollbackOnly();
 884            throw new EventQueueServiceException("unable to find Event By DateInf", e);
 885        }
 886    }
 887
 888    /**
 889     * 
 890     * @param path
 891     *            path of the EventQueue
 892     * @param date1
 893     *            date of the event
 894     * @param date2
 895     *            date of the event
 896     * @return array of event (return an array of event having date between
 897     *         date1 and date2 where date1<= date2)
 898     * @throws EventQueueServiceException
 899     */
 900    @WebMethod
 901    public PersistentEvent[] findEventByDateBetween(String path, Date date1, Date date2) throws EventQueueServiceException {
 902        FactoryResourceIdentifier identifier;
 903        try {
 904            identifier = binding.lookup(path);
 905            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 906                String caller = membership.getProfilePathForConnectedIdentifier();
 907                pep.checkSecurity(caller, path, "read");
 908
 909                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 910                if (eventqueue == null) {
 911                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 912                }
 913
 914                ArrayList<PersistentEvent> result = new ArrayList<PersistentEvent>();
 915
 916                ArrayList<PersistentEvent> listEvent = eventqueue.getEvents();
 917                Iterator<PersistentEvent> it = listEvent.iterator();
 918                while (it.hasNext()) {
 919                    PersistentEvent ev = it.next();
 920
 921                    // correction d'un bug concernant date1 et date2 et inversé
 922                    // if (((ev.getDate().before(date1)) ||
 923                    // (ev.getDate().equals(date1))) &&
 924                    // ((ev.getDate().after(date2)) ||
 925                    // (ev.getDate().equals(date2)))) {
 926                    // result.add(ev);
 927                    // }
 928
 929                    if ((ev.getDate().getTime() >= date1.getTime()) && (ev.getDate().getTime() <= date2.getTime())) {
 930                        result.add(ev);
 931                    }
 932
 933                }// while
 934
 935                PersistentEvent[] tab = new PersistentEvent[result.size()];
 936                int i = 0;
 937                while (i < result.size()) {
 938                    tab[i] = result.get(i);
 939                    i++;
 940                }
 941                return tab;
 942            }
 943
 944            else {
 945                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
 946            }
 947        } catch (Exception e) {
 948            logger.error("unable to find Event By Date Between", e);
 949            ctx.setRollbackOnly();
 950            throw new EventQueueServiceException("unable to find Event By Date Between", e);
 951        }
 952    }
 953
 954    /**
 955     * 
 956     * @param path
 957     * @param eventType
 958     * @param substring
 959     * @return
 960     * @throws EventQueueServiceException
 961     */
 962    @WebMethod
 963    public PersistentEvent[] findEventByEventType(String path, String eventType, boolean substring) throws EventQueueServiceException {
 964        FactoryResourceIdentifier identifier;
 965        try {
 966            // li des cas exceptionnelles
 967            if ((path == null) || (eventType == null))
 968                throw new EventQueueServiceException("Le parametre n'est pas correct . Verifier le path ou l'eventType");
 969
 970            identifier = binding.lookup(path);
 971            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
 972                String caller = membership.getProfilePathForConnectedIdentifier();
 973                pep.checkSecurity(caller, path, "read");
 974
 975                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
 976                if (eventqueue == null) {
 977                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
 978                }
 979
 980                ArrayList<PersistentEvent> resultContains = new ArrayList<PersistentEvent>();
 981                ArrayList<PersistentEvent> resultEquals = new ArrayList<PersistentEvent>();
 982
 983                ArrayList<PersistentEvent> listEvent = eventqueue.getEvents();
 984                Iterator<PersistentEvent> it = listEvent.iterator();
 985
 986                while (it.hasNext()) {
 987                    PersistentEvent ev = it.next();
 988                    if ((substring == true) && (ev.getEventType().contains(eventType))) {
 989                        resultContains.add(ev);
 990                    }
 991
 992                    if ((substring == false) && (ev.getEventType().equals(eventType))) {
 993                        resultEquals.add(ev);
 994                    }
 995                }// while
 996
 997                if (substring == true) {
 998                    PersistentEvent[] tab = new PersistentEvent[resultContains.size()];
 999                    int i = 0;
1000                    while (i < resultContains.size()) {
1001                        tab[i] = resultContains.get(i);
1002                        i++;
1003                    }
1004                    return tab;
1005                } else {
1006                    PersistentEvent[] tab = new PersistentEvent[resultEquals.size()];
1007                    int i = 0;
1008                    while (i < resultEquals.size()) {
1009                        tab[i] = resultEquals.get(i);
1010                        i++;
1011                    }
1012                    return tab;
1013                }
1014            }
1015
1016            else {
1017                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
1018            }
1019        } catch (Exception e) {
1020            logger.error("unable to find Event By EventType", e);
1021            ctx.setRollbackOnly();
1022            throw new EventQueueServiceException("unable to find Event By EventType", e);
1023        }
1024    }
1025
1026    public PersistentEvent[] findObjectEvent(String path, PersistentEvent event) throws EventQueueServiceException {
1027
1028        FactoryResourceIdentifier identifier;
1029        try {
1030            identifier = binding.lookup(path);
1031            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
1032                String caller = membership.getProfilePathForConnectedIdentifier();
1033                pep.checkSecurity(caller, path, "read");
1034
1035                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
1036                if (eventqueue == null) {
1037                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
1038                }
1039
1040                ArrayList<PersistentEvent> result = new ArrayList<PersistentEvent>();
1041                ArrayList<PersistentEvent> listEvent = eventqueue.getEvents();
1042                Iterator<PersistentEvent> it = listEvent.iterator();
1043
1044                while (it.hasNext()) {
1045                    PersistentEvent ev = it.next();
1046                    if (ev.equals(event)) {
1047                        result.add(ev);
1048                    }
1049                }
1050
1051                PersistentEvent[] tab = new PersistentEvent[result.size()];
1052                int i = 0;
1053                while (i < result.size()) {
1054                    tab[i] = result.get(i);
1055                    i++;
1056                }
1057                return tab;
1058            }
1059
1060            else {
1061                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
1062            }
1063        } catch (Exception e) {
1064            logger.error("unable to find Object Event", e);
1065            ctx.setRollbackOnly();
1066            throw new EventQueueServiceException("unable to find Object Event", e);
1067        }
1068    }
1069
1070    @Override
1071    public void removeQueue(String path) throws EventQueueServiceException {
1072
1073        FactoryResourceIdentifier identifier;
1074        try {
1075            identifier = binding.lookup(path);
1076            if (identifier.getType().equals(EventQueue.RESOURCE_NAME)) {
1077                String caller = membership.getProfilePathForConnectedIdentifier();
1078                pep.checkSecurity(caller, path, "update");
1079
1080                EventQueue eventqueue = em.find(EventQueue.class, identifier.getId());
1081                if (eventqueue == null) {
1082                    throw new EventQueueServiceException("unable to find an event queue for id " + identifier.getId());
1083                }
1084
1085                em.remove(eventqueue);
1086                binding.unbind(path);
1087            } else {
1088                throw new CoreServiceException("Resource " + identifier + " is not managed by Event Queue Service");
1089            }
1090        } catch (Exception e) {
1091            logger.error("unable to remove  event queue", e);
1092            ctx.setRollbackOnly();
1093            throw new EventQueueServiceException("unable to create an event queue", e);
1094        }
1095
1096    }
1097
1098    @WebMethod
1099    public PersistentEvent[] findEventBySimpleParameter(String path, String eventType, String thrower, String resourceType, String fromRessource, Date date,
1100            boolean dateSup, boolean dateInf) throws EventQueueServiceException {
1101        return null;
1102    }
1103
1104    @WebMethod
1105    public PersistentEvent[] findEventByComposedParameter(String path, String eventType, String thrower, String resourceType, String fromRessource, Date date1, Date date2)
1106            throws EventQueueServiceException {
1107        return null;
1108    }
1109
1110    @Override
1111    public void generateXML(String path) {
1112        // TODO Auto-generated method stub
1113
1114    }
1115
1116    @Override
1117    public Rule[] list() throws EventQueueServiceException {
1118        logger.debug("list() called");
1119        Query q = em.createQuery("select r from Rule r");
1120        List<?> l = q.getResultList();
1121        Rule[] tab = new Rule[l.size()];
1122        tab = l.toArray(tab);
1123        return tab;
1124    }
1125
1126    @Override
1127    public Rule[] listByRE(String subjectre, String objectre, String targetre, String queue) throws EventQueueServiceException {
1128        logger.debug("listByRE(...) called");
1129
1130        if ((subjectre == null) && (objectre == null) && (targetre == null) && (queue == null))
1131            throw new EventQueueServiceException("Incorrect args, all args are null");
1132        Query q = em.createQuery("SELECT r FROM Rule r WHERE 0=0 " + (subjectre != null ? "AND r.subjectre=:subjectre" : "") + " "
1133                + (objectre != null ? "AND r.objectre=:objectre" : "") + " " + (targetre != null ? "AND r.targetre=:targetre" : "") + " "
1134                + (queue != null ? "AND r.queuePath =:queue" : ""));
1135        if (subjectre != null) {
1136            q.setParameter("subjectre", subjectre);
1137        }
1138        if (objectre != null) {
1139            q.setParameter("objectre", objectre);
1140        }
1141        if (targetre != null) {
1142            q.setParameter("targetre", targetre);
1143        }
1144        if (queue != null) {
1145            q.setParameter("queue", queue);
1146        }
1147        List<?> l = q.getResultList();
1148        Rule[] tab = new Rule[l.size()];
1149        tab = l.toArray(tab);
1150        return tab;
1151
1152    }
1153
1154    @Override
1155    public Rule[] listBy(String subject, String object, String target, String queuere) throws EventQueueServiceException {
1156        logger.debug("listRE(...) called");
1157        if (queuere == null)
1158            throw new EventQueueServiceException("Incorrect arg, queuere should not be null");
1159        Query q = em.createQuery("SELECT r FROM Rule r");
1160        List<?> l = q.getResultList();
1161        List<Rule> lres = new ArrayList<Rule>();
1162        for (Object r : l) {
1163            Rule rule = (Rule) r;
1164            boolean b1, b2, b3, b4;
1165            b1 = subject == null || rule.matchBySubjectRE(subject);
1166            b2 = object == null || rule.matchByObjectRE(object);
1167            b3 = target == null || rule.matchByTargetRE(target);
1168            b4 = rule.matchByQueue(queuere);
1169            if (b1 && b2 && b3 && b4) {
1170                lres.add(rule);
1171            }
1172        }
1173        Rule[] tab = new Rule[lres.size()];
1174        tab = lres.toArray(tab);
1175        return tab;
1176    }
1177
1178    @Override
1179    public Rule[] listByQueue(String queue) throws EventQueueServiceException {
1180        logger.debug("listByQueue(String queue) called");
1181        if (queue == null)
1182            throw new EventQueueServiceException("Incorrect arg, targetre should not be null");
1183        Query q = em.createQuery("SELECT r FROM Rule r WHERE r.queuePath =:queue");
1184        q.setParameter("queue", queue);
1185        List<?> l = q.getResultList();
1186        Rule[] tab = new Rule[l.size()];
1187        tab = l.toArray(tab);
1188        return tab;
1189    }
1190
1191    @Override
1192    public Rule[] listByQueueRE(String queuere) throws EventQueueServiceException {
1193        logger.debug("listByQueueRE(String queuere) called");
1194        if (queuere == null)
1195            throw new EventQueueServiceException("Incorrect arg, queuere should not be null");
1196        Query q = em.createQuery("SELECT r FROM Rule r");
1197        List<?> l = q.getResultList();
1198        List<Rule> lres = new ArrayList<Rule>();
1199        for (Object r : l) {
1200            Rule rule = (Rule) r;
1201            if (rule.matchByQueue(queuere)) {
1202                lres.add(rule);
1203            }
1204        }
1205        Rule[] tab = new Rule[lres.size()];
1206        tab = lres.toArray(tab);
1207        return tab;
1208    }
1209
1210    @Override
1211    public Rule[] listBySubjectRE(String subjectre) throws EventQueueServiceException {
1212        logger.debug("listBySubjectRE(String subjectre) called");
1213        if (subjectre == null)
1214            throw new EventQueueServiceException("Incorrect arg, subject should not be null");
1215        Query q = em.createQuery("SELECT r FROM Rule r WHERE r.subjectre =:subject");
1216        q.setParameter("subject", subjectre);
1217        List<?> l = q.getResultList();
1218        Rule[] tab = new Rule[l.size()];
1219        tab = l.toArray(tab);
1220        return tab;
1221    }
1222
1223    @Override
1224    public Rule[] listBySubject(String subject) throws EventQueueServiceException {
1225        logger.debug("listBySubject(String subject) called");
1226        if (subject == null)
1227            throw new EventQueueServiceException("Incorrect arg, subject should not be null");
1228        Query q = em.createQuery("SELECT r FROM Rule r");
1229        List<?> l = q.getResultList();
1230        List<Rule> lres = new ArrayList<Rule>();
1231        for (Object r : l) {
1232            Rule rule = (Rule) r;
1233            if (rule.matchBySubjectRE(subject)) {
1234                lres.add(rule);
1235            }
1236        }
1237        Rule[] tab = new Rule[lres.size()];
1238        tab = lres.toArray(tab);
1239        return tab;
1240    }
1241
1242    @Override
1243    public Rule[] listByObjectRE(String objectre) throws EventQueueServiceException {
1244        logger.debug("listByObjectRE(String objectre) called");
1245        if (objectre == null)
1246            throw new EventQueueServiceException("Incorrect arg, object should not be null");
1247        Query q = em.createQuery("SELECT r FROM Rule r WHERE r.objectre =:object");
1248        q.setParameter("object", objectre);
1249        List<?> l = q.getResultList();
1250        Rule[] tab = new Rule[l.size()];
1251        tab = l.toArray(tab);
1252        return tab;
1253    }
1254
1255    @Override
1256    public Rule[] listByObject(String object) throws EventQueueServiceException {
1257        logger.debug("listByObject(String object) called");
1258        if (object == null)
1259            throw new EventQueueServiceException("Incorrect arg, object should not be null");
1260        Query q = em.createQuery("SELECT r FROM Rule r");
1261        List<?> l = q.getResultList();
1262        List<Rule> lres = new ArrayList<Rule>();
1263        for (Object r : l) {
1264            Rule rule = (Rule) r;
1265            if (rule.matchByObjectRE(object)) {
1266                lres.add(rule);
1267            }
1268        }
1269        Rule[] tab = new Rule[lres.size()];
1270        tab = lres.toArray(tab);
1271        return tab;
1272    }
1273
1274    @Override
1275    public Rule[] listByTargetRE(String targetre) throws EventQueueServiceException {
1276        logger.debug("listByTargetRE(String targetre) called");
1277        if (targetre == null)
1278            throw new EventQueueServiceException("Incorrect arg, target should not be null");
1279        Query q = em.createQuery("SELECT r FROM Rule r WHERE r.targetre =:target");
1280        q.setParameter("target", targetre);
1281        List<?> l = q.getResultList();
1282        Rule[] tab = new Rule[l.size()];
1283        tab = l.toArray(tab);
1284        return tab;
1285    }
1286
1287    @Override
1288    public Rule[] listByTarget(String target) throws EventQueueServiceException {
1289        logger.debug("listByTarget(String target) called");
1290        if (target == null)
1291            throw new EventQueueServiceException("Incorrect arg, target should not be null");
1292        Query q = em.createQuery("SELECT r FROM Rule r");
1293        List<?> l = q.getResultList();
1294        List<Rule> lres = new ArrayList<Rule>();
1295        for (Object r : l) {
1296            Rule rule = (Rule) r;
1297            if (rule.matchByTargetRE(target)) {
1298                lres.add(rule);
1299            }
1300        }
1301        Rule[] tab = new Rule[lres.size()];
1302        tab = lres.toArray(tab);
1303        return tab;
1304    }
1305
1306    @Override
1307    public void register(String subjectre, String objectre, String targetre, String queuePath) throws EventQueueServiceException {
1308        logger.info("register(...) called");
1309
1310        String caller = "";
1311        try {
1312            caller = membership.getProfilePathForConnectedIdentifier();
1313            pep.checkSecurity(caller, queuePath, "update");
1314        } catch (PEPServiceException e) {
1315            e.printStackTrace();
1316            throw new EventQueueServiceException("unable to add rule on the queue " + queuePath);
1317        } catch (AccessDeniedException

Large files files are truncated, but you can click here to view the full file