Save This Page
Home » openejb-3.1.2-src » org.apache.openejb.spring » [javadoc | source]
    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   package org.apache.openejb.spring;
   19   
   20   import java.util.ArrayList;
   21   import java.util.List;
   22   import java.util.Map;
   23   import java.util.Set;
   24   import java.util.TreeMap;
   25   import java.util.TreeSet;
   26   import java.util.Map.Entry;
   27   import javax.annotation.PreDestroy;
   28   import javax.annotation.PostConstruct;
   29   
   30   import org.apache.openejb.DeploymentInfo;
   31   import org.apache.openejb.OpenEJBException;
   32   import org.apache.openejb.core.CoreDeploymentInfo;
   33   import org.apache.openejb.assembler.classic.AppInfo;
   34   import org.apache.openejb.assembler.classic.Assembler;
   35   import org.apache.openejb.assembler.classic.EjbJarInfo;
   36   import org.apache.openejb.assembler.classic.EnterpriseBeanInfo;
   37   import org.apache.openejb.assembler.classic.JndiBuilder;
   38   import org.apache.openejb.assembler.classic.JndiBuilder.JndiNameStrategy;
   39   import org.apache.openejb.loader.SystemInstance;
   40   import org.apache.openejb.util.LogCategory;
   41   import org.apache.openejb.util.Logger;
   42   import org.springframework.beans.BeansException;
   43   import org.springframework.context.ApplicationContext;
   44   import org.springframework.context.ApplicationContextAware;
   45   import org.springframework.context.ConfigurableApplicationContext;
   46   
   47   public abstract class AbstractApplication implements ApplicationContextAware {
   48       private static final Logger logger = Logger.getInstance(LogCategory.OPENEJB_STARTUP, ClassPathApplication.class);
   49       private final Map<String, JndiNameStrategy> nameStrategies = new TreeMap<String, JndiNameStrategy>();
   50   
   51       protected final List<AppInfo> applications = new ArrayList<AppInfo>();
   52       protected OpenEJB openEJB;
   53       protected boolean started = false;
   54       protected boolean export = true;
   55       private ConfigurableApplicationContext applicationContext;
   56   
   57       public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
   58           this.applicationContext = (ConfigurableApplicationContext) applicationContext;
   59       }
   60   
   61       public OpenEJB getOpenEJB() {
   62           return openEJB;
   63       }
   64   
   65       public void setOpenEJB(OpenEJB openEJB) {
   66           this.openEJB = openEJB;
   67       }
   68   
   69       public boolean isExport() {
   70           return export;
   71       }
   72   
   73       public void setExport(boolean export) {
   74           this.export = export;
   75       }
   76   
   77       protected abstract List<AppInfo> loadApplications() throws OpenEJBException;
   78   
   79       @PostConstruct
   80       public void start() throws OpenEJBException {
   81           // check if openejb already started
   82           if (openEJB == null) {
   83               Map map = applicationContext.getBeansOfType(OpenEJB.class);
   84               if (!map.isEmpty()) {
   85                   openEJB = (OpenEJB) map.values().iterator().next();
   86               }
   87           }
   88           if (openEJB == null) {
   89               openEJB = new OpenEJB();
   90               applicationContext.getBeanFactory().registerSingleton("openejb.internalOpenEJB", openEJB);
   91               openEJB.setApplicationContext(applicationContext);
   92               openEJB.start();
   93           }
   94           openEJB.deployApplication(this);
   95       }
   96   
   97       // Do not make this the Spring start method
   98       protected void deployApplication() throws OpenEJBException {
   99           // Ok someone made this the OpenEJB start method... ignore this deploy call
  100           if (openEJB != null && openEJB.isStarting()) return;
  101   
  102           if (started) return;
  103           started = true;
  104   
  105           // load the applications
  106           List<AppInfo> appInfos = loadApplications();
  107   
  108           // deploy the applications
  109           Assembler assembler = getAssembler();
  110           for (AppInfo appInfo : appInfos) {
  111               try {
  112                   List<DeploymentInfo> deployments = assembler.createApplication(appInfo, assembler.createAppClassLoader(appInfo), true);
  113                   if (export) {
  114                       for (DeploymentInfo deployment : deployments) {
  115                           JndiNameStrategy strategy = createStrategy(appInfo, deployments, deployment);
  116                           Map<String, EJB> bindings = getEjbBindings(strategy, (CoreDeploymentInfo) deployment);
  117                           for (Entry<String, EJB> entry : bindings.entrySet()) {
  118                               String beanName = entry.getKey();
  119                               if (!applicationContext.containsBean(beanName)) {
  120                                   EJB ejb = entry.getValue();
  121                                   applicationContext.getBeanFactory().registerSingleton(beanName, ejb);
  122                                   logger.info("Exported EJB " + deployment.getEjbName() + " with interface " + entry.getValue().getInterface().getName() + " to Spring bean " + entry.getKey());
  123                               }
  124                           }
  125                       }
  126                   }
  127                   applications.add(appInfo);
  128               } catch (Exception e) {
  129                   if (e instanceof OpenEJBException) {
  130                       throw (OpenEJBException) e;
  131                   }
  132                   throw new OpenEJBException("Error starting application " + appInfo.jarPath, e);
  133               }
  134           }
  135       }
  136   
  137       public Map<String, EJB> getEjbBindings(JndiNameStrategy strategy, CoreDeploymentInfo deployment) {
  138           strategy.begin(deployment);
  139   
  140           Map<String, EJB> bindings = new TreeMap<String, EJB>();
  141   
  142           Class remoteHome = deployment.getHomeInterface();
  143           if (remoteHome != null) {
  144               String externalName = strategy.getName(remoteHome, JndiNameStrategy.Interface.REMOTE_HOME);
  145               bindings.put(externalName, new EJB(deployment, remoteHome));
  146           }
  147   
  148   
  149           Class localHome = deployment.getLocalHomeInterface();
  150           if (localHome != null) {
  151               String externalName = strategy.getName(localHome, JndiNameStrategy.Interface.LOCAL_HOME);
  152               bindings.put(externalName, new EJB(deployment, remoteHome));
  153           }
  154   
  155           for (Class businessLocal : deployment.getBusinessLocalInterfaces()) {
  156               String externalName = strategy.getName(businessLocal, JndiNameStrategy.Interface.BUSINESS_LOCAL);
  157               bindings.put(externalName, new EJB(deployment, businessLocal));
  158           }
  159   
  160           for (Class businessRemote : deployment.getBusinessRemoteInterfaces()) {
  161               String externalName = strategy.getName(businessRemote, JndiNameStrategy.Interface.BUSINESS_REMOTE);
  162               bindings.put(externalName, new EJB(deployment, businessRemote));
  163           }
  164   
  165   //        if (MessageListener.class.equals(deployment.getMdbInterface())) {
  166   //            String name = deployment.getDeploymentID().toString();
  167   //            bindings.put(name, MessageListener.class);
  168   //        }
  169   
  170           strategy.end();
  171   
  172           return bindings;
  173       }
  174   
  175       public JndiNameStrategy createStrategy(AppInfo appInfo, List<DeploymentInfo> deployments, DeploymentInfo deployment) throws OpenEJBException {
  176           JndiNameStrategy strategy = nameStrategies.get(deployment.getModuleID());
  177           if (strategy != null) {
  178               return strategy;
  179           }
  180   
  181           String deploymentId = (String) deployment.getDeploymentID();
  182           for (EjbJarInfo ejbJar : appInfo.ejbJars) {
  183               if (ejbJar.moduleId.equals(deployment.getModuleID())) {
  184                   Set<String> moduleDeploymentIds = new TreeSet<String>();
  185                   for (EnterpriseBeanInfo enterpriseBean : ejbJar.enterpriseBeans) {
  186                       moduleDeploymentIds.add(enterpriseBean.ejbDeploymentId);
  187                   }
  188                   Map<String, DeploymentInfo> moduleDeployments = new TreeMap<String, DeploymentInfo>();
  189                   for (DeploymentInfo deploymentInfo : deployments) {
  190                       if (moduleDeploymentIds.contains(deploymentId)) {
  191                           moduleDeployments.put((String) deploymentInfo.getDeploymentID(), deploymentInfo);
  192                       }
  193                   }
  194                   strategy = JndiBuilder.createStrategy(ejbJar, moduleDeployments);
  195                   for (String moduleDeploymentId : moduleDeploymentIds) {
  196                       nameStrategies.put(moduleDeploymentId, strategy);
  197                   }
  198                   return strategy;
  199               }
  200           }
  201           throw new OpenEJBException("Can not find EjbJarInfo " + deployment.getModuleID() + " for EJB " + deploymentId);
  202       }
  203   
  204       @PreDestroy
  205       public void stop() {
  206           if (!started) return;
  207           started = false;
  208   
  209           for (AppInfo application : applications) {
  210               try {
  211                   getAssembler().destroyApplication(application.jarPath);
  212               } catch (Exception e) {
  213                   logger.error("Error stopping application " + application.jarPath, e);
  214               }
  215           }
  216       }
  217   
  218       protected Assembler getAssembler() {
  219               return SystemInstance.get().getComponent(Assembler.class);
  220       }
  221   }

Save This Page
Home » openejb-3.1.2-src » org.apache.openejb.spring » [javadoc | source]