PageRenderTime 90ms CodeModel.GetById 29ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 0ms

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

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

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