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

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with 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,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   package org.apache.geronimo.openejb.deployment.cluster;
   21   
   22   import java.net.URI;
   23   import java.net.URISyntaxException;
   24   import java.net.URL;
   25   import java.util.Collection;
   26   import java.util.Collections;
   27   import java.util.HashSet;
   28   import java.util.Set;
   29   import java.util.jar.JarFile;
   30   
   31   import javax.xml.namespace.QName;
   32   
   33   import org.apache.geronimo.clustering.wadi.BasicWADISessionManager;
   34   import org.apache.geronimo.clustering.wadi.WADISessionManagerConfigInfo;
   35   import org.apache.geronimo.common.DeploymentException;
   36   import org.apache.geronimo.deployment.DeploymentContext;
   37   import org.apache.geronimo.deployment.ModuleIDBuilder;
   38   import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
   39   import org.apache.geronimo.deployment.NamespaceDrivenBuilderCollection;
   40   import org.apache.geronimo.deployment.service.EnvironmentBuilder;
   41   import org.apache.geronimo.gbean.AbstractName;
   42   import org.apache.geronimo.gbean.AbstractNameQuery;
   43   import org.apache.geronimo.gbean.GBeanData;
   44   import org.apache.geronimo.gbean.GBeanInfo;
   45   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   46   import org.apache.geronimo.j2ee.deployment.EARContext;
   47   import org.apache.geronimo.j2ee.deployment.Module;
   48   import org.apache.geronimo.j2ee.deployment.ModuleBuilderExtension;
   49   import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
   50   import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
   51   import org.apache.geronimo.kernel.GBeanNotFoundException;
   52   import org.apache.geronimo.kernel.Naming;
   53   import org.apache.geronimo.kernel.config.ConfigurationStore;
   54   import org.apache.geronimo.kernel.repository.Environment;
   55   import org.apache.geronimo.naming.deployment.ENCConfigBuilder;
   56   import org.apache.geronimo.openejb.cluster.infra.BasicNetworkConnectorTrackerServiceHolder;
   57   import org.apache.geronimo.openejb.cluster.infra.NetworkConnectorMonitor;
   58   import org.apache.geronimo.openejb.cluster.stateful.deployment.ClusteredStatefulDeployment;
   59   import org.apache.geronimo.openejb.deployment.BasicEjbDeploymentGBeanNameBuilder;
   60   import org.apache.geronimo.openejb.deployment.EjbDeploymentGBeanNameBuilder;
   61   import org.apache.geronimo.openejb.deployment.EjbModule;
   62   import org.apache.geronimo.openejb.deployment.XmlUtil;
   63   import org.apache.geronimo.openejb.xbeans.ejbjar.OpenejbGeronimoEjbJarType;
   64   import org.apache.geronimo.schema.SchemaConversionUtils;
   65   import org.apache.geronimo.xbeans.geronimo.GerOpenejbClusteringWadiDocument;
   66   import org.apache.geronimo.xbeans.geronimo.GerOpenejbClusteringWadiType;
   67   import org.apache.geronimo.xbeans.geronimo.j2ee.GerClusteringDocument;
   68   import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
   69   import org.apache.openejb.jee.EjbJar;
   70   import org.apache.openejb.jee.EnterpriseBean;
   71   import org.apache.openejb.jee.SessionBean;
   72   import org.apache.openejb.jee.oejb2.GeronimoEjbJarType;
   73   import org.apache.xmlbeans.QNameSet;
   74   import org.apache.xmlbeans.XmlObject;
   75   
   76   /**
   77    *
   78    * @version $Rev:$ $Date:$
   79    */
   80   public class WADIOpenEJBClusteringBuilder implements ModuleBuilderExtension {
   81       private static final QName BASE_CLUSTERING_QNAME = GerClusteringDocument.type.getDocumentElementName();
   82       private static final QName CLUSTERING_WADI_QNAME = GerOpenejbClusteringWadiDocument.type.getDocumentElementName();
   83       private static final QNameSet CLUSTERING_WADI_QNAME_SET = QNameSet.singleton(CLUSTERING_WADI_QNAME);
   84       
   85       static {
   86           SchemaConversionUtils.registerNamespaceConversions(
   87               Collections.singletonMap(CLUSTERING_WADI_QNAME.getLocalPart(),
   88               new OpenEJBClusteringWADIConverter()));
   89       }
   90   
   91       private final String defaultClusteredStatefulContainerId;
   92       private final EjbDeploymentGBeanNameBuilder beanNameBuilder;
   93       private final int defaultSweepInterval;
   94       private final int defaultSessionTimeout;
   95       private final int defaultNumPartitions;
   96       private final AbstractNameQuery defaultBackingStrategyFactoryName;
   97       private final AbstractNameQuery defaultClusterName;
   98       private final AbstractNameQuery defaultNetworkConnectorName;
   99       private final Environment defaultEnvironment;
  100   
  101       public WADIOpenEJBClusteringBuilder(String defaultClusteredStatefulContainerId,
  102           int defaultSweepInterval,
  103           int defaultSessionTimeout,
  104           int defaultNumPartitions,
  105           AbstractNameQuery defaultBackingStrategyFactoryName,
  106           AbstractNameQuery defaultClusterName,
  107           AbstractNameQuery defaultNetworkConnectorName,        
  108           Environment defaultEnvironment) {
  109           if (null == defaultClusteredStatefulContainerId) {
  110               throw new IllegalArgumentException("defaultClusteredStatefulContainerId is required");
  111           } else if (defaultSweepInterval < 1) {
  112               throw new IllegalArgumentException("defaultSweepInterval is lower than 1");
  113           } else if (defaultSessionTimeout < 1) {
  114               throw new IllegalArgumentException("defaultSessionTimeout is lower than 1");
  115           } else if (defaultNumPartitions < 1) {
  116               throw new IllegalArgumentException("defaultNumPartitions is lower than 1");
  117           } else if (null == defaultBackingStrategyFactoryName) {
  118               throw new IllegalArgumentException("defaultBackingStrategyFactoryName is required");
  119           } else if (null == defaultClusterName) {
  120               throw new IllegalArgumentException("defaultClusterName is required");
  121           } else if (null == defaultEnvironment) {
  122               throw new IllegalArgumentException("defaultEnvironment is required");
  123           } else if (null == defaultNetworkConnectorName) {
  124               throw new IllegalArgumentException("defaultNetworkConnectorName is required");
  125           }
  126           this.defaultClusteredStatefulContainerId = defaultClusteredStatefulContainerId;
  127           this.defaultSweepInterval = defaultSweepInterval;
  128           this.defaultSessionTimeout = defaultSessionTimeout;
  129           this.defaultNumPartitions = defaultNumPartitions;
  130           this.defaultBackingStrategyFactoryName = defaultBackingStrategyFactoryName;
  131           this.defaultClusterName = defaultClusterName;
  132           this.defaultNetworkConnectorName = defaultNetworkConnectorName;
  133           this.defaultEnvironment = defaultEnvironment;
  134           
  135           beanNameBuilder = new BasicEjbDeploymentGBeanNameBuilder();
  136           
  137           new NamespaceDrivenBuilderCollection(Collections.<NamespaceDrivenBuilder>singleton(new NamespaceDrivenBuilder() {
  138               public void build(XmlObject container, DeploymentContext applicationContext, DeploymentContext moduleContext)
  139                       throws DeploymentException {
  140               }
  141   
  142               public void buildEnvironment(XmlObject container, Environment environment) throws DeploymentException {
  143               }
  144   
  145               public QNameSet getPlanQNameSet() {
  146                   return CLUSTERING_WADI_QNAME_SET;
  147               }
  148   
  149               public QNameSet getSpecQNameSet() {
  150                   return QNameSet.EMPTY;
  151               }
  152   
  153               public QName getBaseQName() {
  154                   return BASE_CLUSTERING_QNAME;
  155               }
  156   
  157            }));
  158       }
  159       
  160       public void addGBeans(EARContext earContext, Module module, ClassLoader cl, Collection repository)
  161               throws DeploymentException {
  162           EjbModule ejbModule = (EjbModule) module;
  163           
  164           OpenejbGeronimoEjbJarType geronimoEjbJarType = ejbModule.getVendorDD();
  165           GerOpenejbClusteringWadiType clusteringWadiType = getWadiClusterConfig(geronimoEjbJarType);
  166           if (clusteringWadiType != null) {
  167               AbstractName sessionManagerName = addSessionManager(clusteringWadiType, ejbModule, earContext);
  168   
  169               addNetworkConnectorMonitor(earContext, sessionManagerName);
  170   
  171               EjbJar ejbJar = ejbModule.getEjbJar();
  172               for (EnterpriseBean enterpriseBean : ejbJar.getEnterpriseBeans()) {
  173                   if (enterpriseBean instanceof SessionBean) {
  174                       SessionBean sessionBean = (SessionBean) enterpriseBean;
  175                       switch (sessionBean.getSessionType()) {
  176                           case STATEFUL:
  177                               replaceByClusteredDeploymentGBean(earContext,
  178                                   ejbModule,
  179                                   sessionManagerName,
  180                                   enterpriseBean);
  181                       }
  182                   }
  183               }
  184           }
  185       }
  186   
  187       protected void replaceByClusteredDeploymentGBean(EARContext earContext,
  188           EjbModule ejbModule,
  189           AbstractName sessionManagerName,
  190           EnterpriseBean enterpriseBean) throws DeploymentException {
  191           AbstractName name = beanNameBuilder.createEjbName(earContext, ejbModule, enterpriseBean);
  192           GBeanData beanInstance;
  193           try {
  194               beanInstance = earContext.getGBeanInstance(name);
  195               earContext.removeGBean(name);
  196           } catch (GBeanNotFoundException e) {
  197               throw new DeploymentException("No GBean [" + name + "]", e);
  198           }
  199           GBeanData clusteredDeploymentGBean = new GBeanData(beanInstance);
  200           clusteredDeploymentGBean.setGBeanInfo(ClusteredStatefulDeployment.GBEAN_INFO);
  201           clusteredDeploymentGBean.setReferencePattern(ClusteredStatefulDeployment.GBEAN_REF_SESSION_MANAGER, sessionManagerName);
  202           try {
  203               earContext.addGBean(clusteredDeploymentGBean);
  204           } catch (GBeanAlreadyExistsException e) {
  205               throw new DeploymentException("See nested", e);
  206           }
  207       }
  208   
  209       public void createModule(Module module,
  210           Object plan,
  211           JarFile moduleFile,
  212           String targetPath,
  213           URL specDDUrl,
  214           Environment environment,
  215           Object moduleContextInfo,
  216           AbstractName earName,
  217           Naming naming,
  218           ModuleIDBuilder idBuilder) throws DeploymentException {
  219           EjbModule ejbModule = (EjbModule) module;
  220           GeronimoEjbJarType tmpGeronimoEjbJarType = (GeronimoEjbJarType) ejbModule.getEjbModule().getAltDDs().get("geronimo-openejb.xml");
  221           OpenejbGeronimoEjbJarType geronimoEjbJarType = XmlUtil.convertToXmlbeans(tmpGeronimoEjbJarType);
  222           GerOpenejbClusteringWadiType clusteringWadiType = getWadiClusterConfig(geronimoEjbJarType);
  223           if (null == clusteringWadiType) {
  224               return;
  225           }
  226   
  227           EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment);
  228   
  229           ejbModule.getPreAutoConfigDeployer().add(new MapSFSBToContainerIDDeployer(defaultClusteredStatefulContainerId));
  230       }
  231   
  232       public void initContext(EARContext earContext, Module module, ClassLoader cl) throws DeploymentException {
  233       }
  234       
  235       public void installModule(JarFile earFile,
  236           EARContext earContext,
  237           Module module,
  238           Collection configurationStores,
  239           ConfigurationStore targetConfigurationStore,
  240           Collection repository) throws DeploymentException {
  241       }
  242   
  243       protected AbstractName addSessionManager(GerOpenejbClusteringWadiType clustering,
  244           EjbModule ejbModule,
  245           DeploymentContext moduleContext) throws DeploymentException {
  246           AbstractName name = newGBeanName(moduleContext, "WADISessionManager");
  247   
  248           GBeanData beanData = new GBeanData(name, BasicWADISessionManager.class);
  249   
  250           setConfigInfo(clustering, ejbModule, beanData);
  251           setCluster(clustering, beanData);
  252           setBackingStrategyFactory(clustering, beanData);
  253           setClusteredServiceHolders(moduleContext, beanData);
  254   
  255           addGBean(moduleContext, beanData);
  256   
  257           return name;
  258       }
  259       
  260       protected void setClusteredServiceHolders(DeploymentContext moduleContext, GBeanData beanData)
  261               throws DeploymentException {
  262           AbstractName name = newGBeanName(moduleContext, "NetworkConnectorTrackerHolder");
  263   
  264           GBeanData serviceHolder = new GBeanData(name, BasicNetworkConnectorTrackerServiceHolder.GBEAN_INFO);
  265           addGBean(moduleContext, serviceHolder);
  266           
  267           beanData.setReferencePattern(BasicWADISessionManager.GBEAN_REF_SERVICE_HOLDERS, name);
  268       }
  269   
  270       protected void addNetworkConnectorMonitor(DeploymentContext moduleContext, AbstractName sessionManagerName)
  271               throws DeploymentException {
  272           AbstractName name = newGBeanName(moduleContext, "NetworkConnectorMonitor");
  273   
  274           GBeanData networkConnectorMonitor = new GBeanData(name, NetworkConnectorMonitor.GBEAN_INFO);
  275           networkConnectorMonitor.setReferencePattern(NetworkConnectorMonitor.GBEAN_REF_NETWORK_CONNECTORS,
  276               defaultNetworkConnectorName);
  277           networkConnectorMonitor.setReferencePattern(NetworkConnectorMonitor.GBEAN_REF_EJB_DEP_ID_ACCESSOR,
  278               new AbstractNameQuery(ClusteredStatefulDeployment.class.getName()));
  279           networkConnectorMonitor.setReferencePattern(NetworkConnectorMonitor.GBEAN_REF_WADI_SESSION_MANAGER,
  280               sessionManagerName);
  281           
  282           addGBean(moduleContext, networkConnectorMonitor);
  283       }
  284   
  285       protected AbstractName newGBeanName(DeploymentContext moduleContext, String name) {
  286           return moduleContext.getNaming().createChildName(moduleContext.getModuleName(),
  287                   name,
  288                   GBeanInfoBuilder.DEFAULT_J2EE_TYPE);
  289       }
  290   
  291       protected void addGBean(DeploymentContext moduleContext, GBeanData beanData) throws DeploymentException {
  292           try {
  293               moduleContext.addGBean(beanData);
  294           } catch (GBeanAlreadyExistsException e) {
  295               throw new DeploymentException(e);
  296           }
  297       }
  298   
  299       protected void setCluster(GerOpenejbClusteringWadiType clustering, GBeanData beanData) {
  300           Set<AbstractNameQuery> patterns = new HashSet<AbstractNameQuery>();
  301           if (clustering.isSetCluster()) {
  302               addAbstractNameQueries(patterns, clustering.getCluster());
  303           } else {
  304               patterns.add(defaultClusterName);
  305           }
  306           beanData.setReferencePatterns(BasicWADISessionManager.GBEAN_REF_CLUSTER, patterns);
  307       }
  308   
  309       protected void setBackingStrategyFactory(GerOpenejbClusteringWadiType clustering, GBeanData beanData) {
  310           Set<AbstractNameQuery> patterns = new HashSet<AbstractNameQuery>();
  311           if (clustering.isSetBackingStrategyFactory()) {
  312               addAbstractNameQueries(patterns, clustering.getBackingStrategyFactory());
  313           } else {
  314               patterns.add(defaultBackingStrategyFactoryName);
  315           }
  316           beanData.setReferencePatterns(BasicWADISessionManager.GBEAN_REF_BACKING_STRATEGY_FACTORY, patterns);
  317       }
  318   
  319       protected void addAbstractNameQueries(Set<AbstractNameQuery> patterns, GerPatternType patternType) {
  320           AbstractNameQuery query = ENCConfigBuilder.buildAbstractNameQuery(patternType, null, null, null);
  321           patterns.add(query);
  322       }
  323   
  324       protected void setConfigInfo(GerOpenejbClusteringWadiType clustering, EjbModule ejbModule, GBeanData beanData) {
  325           int sweepInterval = getSweepInterval(clustering);
  326           int numPartitions = getNumberOfPartitions(clustering);
  327           Integer sessionTimeout = getSessionTimeout();
  328           boolean disableReplication = isDisableReplication(clustering);
  329           boolean deltaReplication = isDeltaReplication(clustering);
  330           
  331           String ejbModuleName = ejbModule.getName();
  332           URI serviceSpaceName;
  333           try {
  334               serviceSpaceName = new URI(ejbModuleName);
  335           } catch (URISyntaxException e) {
  336               AssertionError error = new AssertionError("contextPath [" + ejbModuleName + "] cannot be parsed as an URI.");
  337               throw (AssertionError) error.initCause(e);
  338           }
  339           
  340           WADISessionManagerConfigInfo configInfo = new WADISessionManagerConfigInfo(serviceSpaceName,
  341                   sweepInterval,
  342                   numPartitions,
  343                   sessionTimeout,
  344                   disableReplication,
  345                   deltaReplication);
  346           beanData.setAttribute(BasicWADISessionManager.GBEAN_ATTR_WADI_CONFIG_INFO, configInfo);
  347       }
  348   
  349       protected Integer getSessionTimeout() throws AssertionError {
  350           return defaultSessionTimeout;
  351       }
  352   
  353       protected int getSweepInterval(GerOpenejbClusteringWadiType clustering) {
  354           if (clustering.isSetSweepInterval()) {
  355               return clustering.getSweepInterval().intValue();
  356           }
  357           return defaultSweepInterval;
  358       }
  359   
  360       protected boolean isDeltaReplication(GerOpenejbClusteringWadiType clustering) {
  361           if (clustering.isSetDeltaReplication()) {
  362               return clustering.getDeltaReplication();
  363           }
  364           return false;
  365       }
  366   
  367       protected boolean isDisableReplication(GerOpenejbClusteringWadiType clustering) {
  368           if (clustering.isSetDisableReplication()) {
  369               return clustering.getDisableReplication();
  370           }
  371           return false;
  372       }
  373   
  374       protected int getNumberOfPartitions(GerOpenejbClusteringWadiType clustering) {
  375           if (clustering.isSetNumPartitions()) {
  376               return clustering.getNumPartitions().intValue();
  377           }
  378           return defaultNumPartitions;
  379       }
  380       
  381       protected GerOpenejbClusteringWadiType getWadiClusterConfig(XmlObject container) throws DeploymentException {
  382           XmlObject[] items = container.selectChildren(CLUSTERING_WADI_QNAME_SET);
  383           if (items.length > 1) {
  384               throw new DeploymentException("Unexpected count of clustering elements in geronimo plan " + items.length
  385                       + " qnameset: " + CLUSTERING_WADI_QNAME_SET);
  386           }
  387           if (items.length == 1) {
  388               return (GerOpenejbClusteringWadiType) items[0].copy().changeType(GerOpenejbClusteringWadiType.type);
  389           }
  390           return null;
  391       }
  392   
  393       public static final GBeanInfo GBEAN_INFO;
  394   
  395       public static final String GBEAN_ATTR_DFT_CLUSTERED_SFSB_CONT_ID = "defaultClusteredStatefulContainerId";
  396       public static final String GBEAN_ATTR_DFT_SWEEP_INTERVAL = "defaultSweepInterval";
  397       public static final String GBEAN_ATTR_DFT_SESSION_TIMEOUT = "defaultSessionTimeout";
  398       public static final String GBEAN_ATTR_DFT_NUM_PARTITIONS = "defaultNumPartitions";
  399       public static final String GBEAN_ATTR_DFT_BACKING_STRATEGY_FACTORY_NAME = "defaultBackingStrategyFactoryName";
  400       public static final String GBEAN_ATTR_DFT_CLUSTER_NAME = "defaultClusterName";
  401       public static final String GBEAN_ATTR_DFT_NETWORK_CONNECTOR_NAME = "defaultNetworkConnectorName";
  402       public static final String GBEAN_ATTR_DFT_ENVIRONMENT = "defaultEnvironment";
  403   
  404       static {
  405           GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("WADI OpenEJB Clusteting Builder",
  406                   WADIOpenEJBClusteringBuilder.class,
  407                   NameFactory.MODULE_BUILDER);
  408   
  409           infoBuilder.addAttribute(GBEAN_ATTR_DFT_CLUSTERED_SFSB_CONT_ID, String.class, true);
  410           infoBuilder.addAttribute(GBEAN_ATTR_DFT_SWEEP_INTERVAL, int.class, true);
  411           infoBuilder.addAttribute(GBEAN_ATTR_DFT_SESSION_TIMEOUT, int.class, true);
  412           infoBuilder.addAttribute(GBEAN_ATTR_DFT_NUM_PARTITIONS, int.class, true);
  413           infoBuilder.addAttribute(GBEAN_ATTR_DFT_BACKING_STRATEGY_FACTORY_NAME, AbstractNameQuery.class, true);
  414           infoBuilder.addAttribute(GBEAN_ATTR_DFT_CLUSTER_NAME, AbstractNameQuery.class, true);
  415           infoBuilder.addAttribute(GBEAN_ATTR_DFT_NETWORK_CONNECTOR_NAME, AbstractNameQuery.class, true);
  416           infoBuilder.addAttribute(GBEAN_ATTR_DFT_ENVIRONMENT, Environment.class, true);
  417   
  418           infoBuilder.setConstructor(new String[] { GBEAN_ATTR_DFT_CLUSTERED_SFSB_CONT_ID,
  419               GBEAN_ATTR_DFT_SWEEP_INTERVAL,
  420               GBEAN_ATTR_DFT_SESSION_TIMEOUT,
  421               GBEAN_ATTR_DFT_NUM_PARTITIONS,
  422               GBEAN_ATTR_DFT_BACKING_STRATEGY_FACTORY_NAME,
  423               GBEAN_ATTR_DFT_CLUSTER_NAME,
  424               GBEAN_ATTR_DFT_NETWORK_CONNECTOR_NAME,
  425               GBEAN_ATTR_DFT_ENVIRONMENT });
  426   
  427           GBEAN_INFO = infoBuilder.getBeanInfo();
  428       }
  429   
  430       public static GBeanInfo getGBeanInfo() {
  431           return GBEAN_INFO;
  432       }
  433   
  434   }

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