Home » geronimo-2.2-source-release » org.apache.geronimo.openejb » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.geronimo.openejb;
   18   
   19   import org.apache.geronimo.gbean.ReferenceCollection;
   20   import org.apache.geronimo.gbean.ReferenceCollectionEvent;
   21   import org.apache.geronimo.gbean.ReferenceCollectionListener;
   22   import org.apache.geronimo.gbean.GBeanLifecycle;
   23   import org.apache.geronimo.gbean.annotation.ParamAttribute;
   24   import org.apache.geronimo.gbean.annotation.ParamReference;
   25   import org.apache.geronimo.gbean.annotation.SpecialAttributeType;
   26   import org.apache.geronimo.gbean.annotation.ParamSpecial;
   27   import org.apache.geronimo.gbean.annotation.GBean;
   28   import org.apache.geronimo.j2ee.management.impl.InvalidObjectNameException;
   29   import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
   30   import org.apache.geronimo.kernel.ObjectNameUtil;
   31   import org.apache.geronimo.management.EJB;
   32   import org.apache.geronimo.management.EJBModule;
   33   import org.apache.geronimo.management.J2EEApplication;
   34   import org.apache.geronimo.management.J2EEServer;
   35   import org.apache.openejb.NoSuchApplicationException;
   36   import org.apache.openejb.UndeployException;
   37   import org.apache.openejb.DeploymentInfo;
   38   import org.apache.openejb.core.CoreDeploymentInfo;
   39   import org.apache.openejb.assembler.classic.EjbJarInfo;
   40   import org.slf4j.Logger;
   41   import org.slf4j.LoggerFactory;
   42   
   43   import javax.management.ObjectName;
   44   import java.util.ArrayList;
   45   import java.util.Collection;
   46   import java.util.Hashtable;
   47   import java.util.List;
   48   import java.util.HashMap;
   49   import java.util.Map;
   50   
   51   /**
   52    * @version $Revision: 831811 $ $Date: 2009-11-01 23:25:53 -0800 (Sun, 01 Nov 2009) $
   53    */
   54   
   55   @GBean(j2eeType = NameFactory.EJB_MODULE)
   56   public class EjbModuleImpl implements EJBModule, GBeanLifecycle {
   57       private static final Logger log = LoggerFactory.getLogger(EjbModuleImpl.class);
   58       private final J2EEServer server;
   59       private final J2EEApplication application;
   60       private final String deploymentDescriptor;
   61       private final String objectName;
   62       private final Map<String, EjbDeployment> ejbs = new HashMap<String, EjbDeployment>();
   63       private final ClassLoader classLoader;
   64   
   65       private final OpenEjbSystem openEjbSystem;
   66       private final EjbJarInfo ejbJarInfo;
   67   
   68       public EjbModuleImpl(@ParamSpecial(type = SpecialAttributeType.objectName) String objectName,
   69                            @ParamReference(name = "J2EEServer") J2EEServer server,
   70                            @ParamReference(name = "J2EEApplication") J2EEApplication application,
   71                            @ParamAttribute(name = "deploymentDescriptor") String deploymentDescriptor,
   72                            @ParamReference(name = "EJBCollection") Collection<? extends EjbDeployment> ejbs,
   73                            @ParamSpecial(type = SpecialAttributeType.classLoader) ClassLoader classLoader,
   74                            @ParamReference(name = "OpenEjbSystem") OpenEjbSystem openEjbSystem,
   75                            @ParamAttribute(name = "ejbJarInfo") EjbJarInfo ejbJarInfo) {
   76           this.objectName = objectName;
   77           ObjectName myObjectName = ObjectNameUtil.getObjectName(objectName);
   78           verifyObjectName(myObjectName);
   79   
   80           this.server = server;
   81           this.application = application;
   82           this.deploymentDescriptor = deploymentDescriptor;
   83           ((ReferenceCollection) ejbs).addReferenceCollectionListener(new ReferenceCollectionListener() {
   84   
   85               public void memberAdded(ReferenceCollectionEvent event) {
   86                   EjbDeployment ejb = (EjbDeployment) event.getMember();
   87                   addEjb(ejb);
   88               }
   89   
   90               public void memberRemoved(ReferenceCollectionEvent event) {
   91                   EjbDeployment ejb = (EjbDeployment) event.getMember();
   92                   removeEjb(ejb);
   93               }
   94           });
   95           for (EjbDeployment ejb : ejbs) {
   96               addEjb(ejb);
   97           }
   98   
   99           this.classLoader = classLoader;
  100   
  101           this.openEjbSystem = openEjbSystem;
  102           this.ejbJarInfo = ejbJarInfo;
  103       }
  104   
  105       private void removeEjb(EjbDeployment ejb) {
  106           GeronimoThreadContextListener.get().removeEjb(ejb.getDeploymentId());
  107           ejbs.remove(ejb.getDeploymentId());
  108       }
  109   
  110       private void addEjb(EjbDeployment ejb) {
  111           ejbs.put(ejb.getDeploymentId(), ejb);
  112           GeronimoThreadContextListener.get().addEjb(ejb);
  113       }
  114   
  115       public String getObjectName() {
  116           return objectName;
  117       }
  118   
  119       public boolean isStateManageable() {
  120           return true;
  121       }
  122   
  123       public boolean isStatisticsProvider() {
  124           return false;
  125       }
  126   
  127       public boolean isEventProvider() {
  128           return true;
  129       }
  130   
  131       public String getDeploymentDescriptor() {
  132           return deploymentDescriptor;
  133       }
  134   
  135       public String getServer() {
  136           return server.getObjectName();
  137       }
  138   
  139       public String getApplication() {
  140           if (application == null) {
  141               return null;
  142           }
  143           return application.getObjectName();
  144       }
  145   
  146       public String[] getJavaVMs() {
  147           return server.getJavaVMs();
  148       }
  149   
  150       public String[] getEjbs() {
  151           if (ejbs == null) {
  152               return new String[0];
  153           }
  154   
  155           ArrayList<EJB> copy;
  156           synchronized (ejbs) {
  157               copy = new ArrayList<EJB>(ejbs.values());
  158           }
  159   
  160           String[] result = new String[copy.size()];
  161           for (int i = 0; i < result.length; i++) {
  162               result[i] = (copy.get(i)).getObjectName();
  163           }
  164           return result;
  165       }
  166   
  167   
  168       public void doStart() throws Exception {
  169           openEjbSystem.createEjbJar(ejbJarInfo, classLoader);
  170           for (String deploymentId: ejbs.keySet()) {
  171               DeploymentInfo deploymentInfo = openEjbSystem.getDeploymentInfo(deploymentId);
  172               GeronimoThreadContextListener.get().getEjbDeployment((CoreDeploymentInfo) deploymentInfo);
  173           }
  174       }
  175   
  176       public void doStop() {
  177           try {
  178               openEjbSystem.removeEjbJar(ejbJarInfo, classLoader);
  179           } catch (NoSuchApplicationException e) {
  180               log.error("Module does not exist.", e);
  181           } catch (UndeployException e) {
  182               List<Throwable> causes = e.getCauses();
  183               log.error(e.getMessage() + ": Encountered " + causes.size() + " failures.");
  184               for (Throwable throwable : causes) {
  185                   log.info(throwable.toString(), throwable);
  186               }
  187           }
  188       }
  189   
  190       public void doFail() {
  191           doStop();
  192       }
  193   
  194       /**
  195        * ObjectName must match this pattern:
  196        * <p/>
  197        * domain:j2eeType=EJBModule,name=MyName,J2EEServer=MyServer,J2EEApplication=MyApplication
  198        */
  199       private void verifyObjectName(ObjectName objectName) {
  200           if (objectName.isPattern()) {
  201               throw new InvalidObjectNameException("ObjectName can not be a pattern", objectName);
  202           }
  203           Hashtable keyPropertyList = objectName.getKeyPropertyList();
  204           if (!"EJBModule".equals(keyPropertyList.get("j2eeType"))) {
  205               throw new InvalidObjectNameException("EJBModule object name j2eeType property must be 'EJBModule'", objectName);
  206           }
  207           if (!keyPropertyList.containsKey("name")) {
  208               throw new InvalidObjectNameException("EJBModule object must contain a name property", objectName);
  209           }
  210           if (!keyPropertyList.containsKey("J2EEServer")) {
  211               throw new InvalidObjectNameException("EJBModule object name must contain a J2EEServer property", objectName);
  212           }
  213           if (!keyPropertyList.containsKey("J2EEApplication")) {
  214               throw new InvalidObjectNameException("EJBModule object name must contain a J2EEApplication property", objectName);
  215           }
  216           if (keyPropertyList.size() != 4) {
  217               throw new InvalidObjectNameException("EJBModule object name can only have j2eeType, name, J2EEApplication, and J2EEServer properties", objectName);
  218           }
  219       }
  220   }

Home » geronimo-2.2-source-release » org.apache.geronimo.openejb » [javadoc | source]