Home » geronimo-2.2-source-release » org.apache.geronimo.tomcat.cluster.wadi.builder » [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.tomcat.cluster.wadi.builder;
   21   
   22   import java.net.URI;
   23   import java.net.URISyntaxException;
   24   import java.util.ArrayList;
   25   import java.util.Collections;
   26   import java.util.HashSet;
   27   import java.util.Iterator;
   28   import java.util.List;
   29   import java.util.Set;
   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.NamespaceDrivenBuilder;
   38   import org.apache.geronimo.deployment.service.EnvironmentBuilder;
   39   import org.apache.geronimo.gbean.AbstractName;
   40   import org.apache.geronimo.gbean.AbstractNameQuery;
   41   import org.apache.geronimo.gbean.GBeanData;
   42   import org.apache.geronimo.gbean.annotation.GBean;
   43   import org.apache.geronimo.gbean.annotation.ParamAttribute;
   44   import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
   45   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   46   import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
   47   import org.apache.geronimo.kernel.GBeanNotFoundException;
   48   import org.apache.geronimo.kernel.Jsr77Naming;
   49   import org.apache.geronimo.kernel.config.Configuration;
   50   import org.apache.geronimo.kernel.repository.Artifact;
   51   import org.apache.geronimo.kernel.repository.Dependency;
   52   import org.apache.geronimo.kernel.repository.Environment;
   53   import org.apache.geronimo.naming.deployment.ENCConfigBuilder;
   54   import org.apache.geronimo.schema.SchemaConversionUtils;
   55   import org.apache.geronimo.tomcat.TomcatWebAppContext;
   56   import org.apache.geronimo.tomcat.cluster.ClusteredManagerRetriever;
   57   import org.apache.geronimo.tomcat.cluster.wadi.WADIClusteredValveRetriever;
   58   import org.apache.geronimo.xbeans.geronimo.GerTomcatClusteringWadiDocument;
   59   import org.apache.geronimo.xbeans.geronimo.GerTomcatClusteringWadiType;
   60   import org.apache.geronimo.xbeans.geronimo.j2ee.GerClusteringDocument;
   61   import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
   62   import org.apache.xmlbeans.QNameSet;
   63   import org.apache.xmlbeans.XmlObject;
   64   
   65   /**
   66    *
   67    * @version $Rev:$ $Date:$
   68    */
   69   @GBean(name="WADITomcatClusteringBuilder", j2eeType=NameFactory.MODULE_BUILDER)
   70   public class WADITomcatClusteringBuilder implements NamespaceDrivenBuilder {
   71       private static final QName BASE_CLUSTERING_QNAME = GerClusteringDocument.type.getDocumentElementName();
   72       private static final QName CLUSTERING_WADI_QNAME = GerTomcatClusteringWadiDocument.type.getDocumentElementName();
   73       private static final QNameSet CLUSTERING_WADI_QNAME_SET = QNameSet.singleton(CLUSTERING_WADI_QNAME);
   74   
   75       static {
   76           SchemaConversionUtils.registerNamespaceConversions(
   77               Collections.singletonMap(CLUSTERING_WADI_QNAME.getLocalPart(),
   78               new TomcatClusteringWADIConverter()));
   79       }
   80       
   81       private final int defaultSweepInterval;
   82       private final int defaultSessionTimeout;
   83       private final int defaultNumPartitions;
   84       private final AbstractNameQuery defaultBackingStrategyFactoryName;
   85       private final AbstractNameQuery defaultClusterName;
   86       private final Artifact artifactToRemoveFromEnvironment;
   87       private final Environment defaultEnvironment;
   88       
   89       public WADITomcatClusteringBuilder(@ParamAttribute(name=GBEAN_ATTR_DFT_SWEEP_INTERVAL) int defaultSweepInterval,
   90           @ParamAttribute(name=GBEAN_ATTR_DFT_SESSION_TIMEOUT) int defaultSessionTimeout,
   91           @ParamAttribute(name=GBEAN_ATTR_DFT_NUM_PARTITIONS) int defaultNumPartitions,
   92           @ParamAttribute(name=GBEAN_ATTR_DFT_BACKING_STRATEGY_FACTORY_NAME) AbstractNameQuery defaultBackingStrategyFactoryName,
   93           @ParamAttribute(name=GBEAN_ATTR_DFT_CLUSTER_NAME) AbstractNameQuery defaultClusterName,
   94           @ParamAttribute(name=GBEAN_ATTR_ARTIFACT_TO_REMOVE) Artifact artifactToRemoveFromEnvironment,
   95           @ParamAttribute(name=GBEAN_ATTR_DFT_ENVIRONMENT) Environment defaultEnvironment) {
   96           if (defaultSweepInterval < 1) {
   97               throw new IllegalArgumentException("defaultSweepInterval is lower than 1");
   98           } else if (defaultSessionTimeout < 1) {
   99               throw new IllegalArgumentException("defaultSessionTimeout is lower than 1");
  100           } else if (defaultNumPartitions < 1) {
  101               throw new IllegalArgumentException("defaultNumPartitions is lower than 1");
  102           } else if (null == defaultBackingStrategyFactoryName) {
  103               throw new IllegalArgumentException("defaultBackingStrategyFactoryName is required");
  104           } else if (null == defaultClusterName) {
  105               throw new IllegalArgumentException("defaultClusterName is required");
  106           } else if (null == artifactToRemoveFromEnvironment) {
  107               throw new IllegalArgumentException("artifactToRemoveFromEnvironment is required");
  108           } else if (null == defaultEnvironment) {
  109               throw new IllegalArgumentException("defaultEnvironment is required");
  110           }
  111           this.defaultSweepInterval = defaultSweepInterval;
  112           this.defaultSessionTimeout = defaultSessionTimeout;
  113           this.defaultNumPartitions = defaultNumPartitions;
  114           this.defaultBackingStrategyFactoryName = defaultBackingStrategyFactoryName;
  115           this.defaultClusterName = defaultClusterName;
  116           this.artifactToRemoveFromEnvironment = artifactToRemoveFromEnvironment;
  117           this.defaultEnvironment = defaultEnvironment;
  118       }
  119   
  120       public void buildEnvironment(XmlObject container, Environment environment) throws DeploymentException {
  121           if (getWadiClusterConfig(container) != null) {
  122               filterDependencies(environment);
  123               EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment);
  124           }
  125       }
  126   
  127       public void build(XmlObject container, DeploymentContext applicationContext, DeploymentContext moduleContext)
  128               throws DeploymentException {
  129           GerTomcatClusteringWadiType clusteringWadiType = getWadiClusterConfig(container);
  130           if (clusteringWadiType != null) {
  131               GBeanData webModuleData = extractWebModule(moduleContext);
  132               try {
  133                   AbstractName sessionManagerName = addSessionManager(clusteringWadiType, webModuleData, moduleContext);
  134                   addClusteredManagerRetriever(moduleContext, webModuleData, sessionManagerName);
  135                   addClusteredValveRetriever(moduleContext, webModuleData, sessionManagerName);
  136               } catch (GBeanAlreadyExistsException e) {
  137                   throw new DeploymentException("Duplicate GBean", e);
  138               }
  139           }
  140       }
  141   
  142       protected void filterDependencies(Environment environment) {
  143           List<Dependency> dependencies = environment.getDependencies();
  144           dependencies = new ArrayList<Dependency>(dependencies);
  145           for (Iterator<Dependency> iterator = dependencies.iterator(); iterator.hasNext();) {
  146               Dependency dependency = iterator.next();
  147               Artifact dependencyArtifact = dependency.getArtifact();
  148               if (artifactToRemoveFromEnvironment.matches(dependencyArtifact)) {
  149                   iterator.remove();
  150               }
  151           }
  152           environment.setDependencies(dependencies);
  153       }
  154   
  155       protected GBeanData extractWebModule(DeploymentContext moduleContext) throws DeploymentException {
  156           AbstractNameQuery webModuleQuery = createTomcatWebAppContextNameQuery(moduleContext);
  157           Configuration configuration = moduleContext.getConfiguration();
  158           try {
  159               return configuration.findGBeanData(webModuleQuery);
  160           } catch (GBeanNotFoundException e) {
  161               throw new DeploymentException("Could not locate web module gbean in web app configuration", e);
  162           }
  163       }
  164   
  165       protected AbstractNameQuery createTomcatWebAppContextNameQuery(DeploymentContext moduleContext) {
  166           String name = moduleContext.getModuleName().getNameProperty(Jsr77Naming.J2EE_NAME);
  167           return new AbstractNameQuery(null,
  168               Collections.singletonMap(Jsr77Naming.J2EE_NAME, name),
  169               Collections.singleton(TomcatWebAppContext.class.getName()));
  170       }
  171   
  172       public QNameSet getSpecQNameSet() {
  173           return QNameSet.EMPTY;
  174       }
  175   
  176       public QNameSet getPlanQNameSet() {
  177           return CLUSTERING_WADI_QNAME_SET;
  178       }
  179   
  180       public QName getBaseQName() {
  181           return BASE_CLUSTERING_QNAME;
  182       }
  183   
  184       protected GerTomcatClusteringWadiType getWadiClusterConfig(XmlObject container) throws DeploymentException {
  185           XmlObject[] items = container.selectChildren(CLUSTERING_WADI_QNAME_SET);
  186           if (items.length > 1) {
  187               throw new DeploymentException("Unexpected count of clustering elements in geronimo plan " + items.length
  188                       + " qnameset: " + CLUSTERING_WADI_QNAME_SET);
  189           }
  190           if (items.length == 1) {
  191               return (GerTomcatClusteringWadiType) items[0].copy().changeType(GerTomcatClusteringWadiType.type);
  192           }
  193           return null;
  194       }
  195   
  196       protected AbstractName addSessionManager(GerTomcatClusteringWadiType clustering,
  197               GBeanData webModuleData,
  198               DeploymentContext moduleContext) throws GBeanAlreadyExistsException {
  199           AbstractName name = newGBeanName(moduleContext, "WADISessionManager");
  200   
  201           GBeanData beanData = new GBeanData(name, BasicWADISessionManager.class);
  202   
  203           setConfigInfo(clustering, webModuleData, beanData);
  204           setCluster(clustering, beanData);
  205           setBackingStrategyFactory(clustering, beanData);
  206   
  207           addGBean(moduleContext, beanData);
  208   
  209           return name;
  210       }
  211   
  212       protected void setConfigInfo(GerTomcatClusteringWadiType clustering, GBeanData webModuleData, GBeanData beanData) {
  213           int sweepInterval = getSweepInterval(clustering);
  214           int numPartitions = getNumberOfPartitions(clustering);
  215           Integer sessionTimeout = getSessionTimeout(webModuleData);
  216           boolean disableReplication = isDisableReplication(clustering);
  217           boolean deltaReplication = isDeltaReplication(clustering);
  218           
  219           String contextPath = (String) webModuleData.getAttribute("contextPath");
  220           URI serviceSpaceName;
  221           try {
  222               serviceSpaceName = new URI(contextPath);
  223           } catch (URISyntaxException e) {
  224               AssertionError error = new AssertionError("contextPath [" + contextPath + "] cannot be parsed as an URI.");
  225               throw (AssertionError) error.initCause(e);
  226           }
  227           
  228           WADISessionManagerConfigInfo configInfo = new WADISessionManagerConfigInfo(serviceSpaceName,
  229                   sweepInterval,
  230                   numPartitions,
  231                   sessionTimeout.intValue(),
  232                   disableReplication,
  233                   deltaReplication);
  234           beanData.setAttribute(BasicWADISessionManager.GBEAN_ATTR_WADI_CONFIG_INFO, configInfo);
  235       }
  236   
  237       protected Integer getSessionTimeout(GBeanData webModuleData) throws AssertionError {
  238           return defaultSessionTimeout;
  239       }
  240   
  241       protected boolean isDeltaReplication(GerTomcatClusteringWadiType clustering) {
  242           if (clustering.isSetDeltaReplication()) {
  243               return clustering.getDeltaReplication();
  244           }
  245           return false;
  246       }
  247   
  248       protected boolean isDisableReplication(GerTomcatClusteringWadiType clustering) {
  249           if (clustering.isSetDisableReplication()) {
  250               return clustering.getDisableReplication();
  251           }
  252           return false;
  253       }
  254   
  255       protected int getNumberOfPartitions(GerTomcatClusteringWadiType clustering) {
  256           if (clustering.isSetNumPartitions()) {
  257               return clustering.getNumPartitions().intValue();
  258           }
  259           return defaultNumPartitions;
  260       }
  261   
  262       protected int getSweepInterval(GerTomcatClusteringWadiType clustering) {
  263           if (clustering.isSetSweepInterval()) {
  264               return clustering.getSweepInterval().intValue();
  265           }
  266           return defaultSweepInterval;
  267       }
  268   
  269       protected void setCluster(GerTomcatClusteringWadiType clustering, GBeanData beanData) {
  270           Set patterns = new HashSet();
  271           if (clustering.isSetCluster()) {
  272               addAbstractNameQueries(patterns, clustering.getCluster());
  273           } else {
  274               patterns.add(defaultClusterName);
  275           }
  276           beanData.setReferencePatterns(BasicWADISessionManager.GBEAN_REF_CLUSTER, patterns);
  277       }
  278   
  279       protected void setBackingStrategyFactory(GerTomcatClusteringWadiType clustering, GBeanData beanData) {
  280           Set patterns = new HashSet();
  281           if (clustering.isSetBackingStrategyFactory()) {
  282               addAbstractNameQueries(patterns, clustering.getBackingStrategyFactory());
  283           } else {
  284               patterns.add(defaultBackingStrategyFactoryName);
  285           }
  286           beanData.setReferencePatterns(BasicWADISessionManager.GBEAN_REF_BACKING_STRATEGY_FACTORY, patterns);
  287       }
  288   
  289       protected AbstractName addClusteredValveRetriever(DeploymentContext moduleContext,
  290               GBeanData webModuleData,
  291               AbstractName sessionManagerName) throws GBeanAlreadyExistsException {
  292           AbstractName name = newGBeanName(moduleContext, "WADIClusteredValveRetriever");
  293   
  294           GBeanData beanData = new GBeanData(name, WADIClusteredValveRetriever.class);
  295           beanData.setReferencePattern(WADIClusteredValveRetriever.GBEAN_REF_WADI_SESSION_MANAGER, sessionManagerName);
  296   
  297           webModuleData.setReferencePattern(TomcatWebAppContext.GBEAN_REF_CLUSTERED_VALVE_RETRIEVER, name);
  298   
  299           addGBean(moduleContext, beanData);
  300   
  301           return name;
  302       }
  303   
  304       protected AbstractName addClusteredManagerRetriever(DeploymentContext moduleContext,
  305               GBeanData webModuleData,
  306               AbstractName sessionManagerName) throws GBeanAlreadyExistsException {
  307           AbstractName name = newGBeanName(moduleContext, "ClusteredManagerRetriever");
  308   
  309           GBeanData beanData = new GBeanData(name, ClusteredManagerRetriever.class);
  310           beanData.setReferencePattern(ClusteredManagerRetriever.GBEAN_REF_SESSION_MANAGER, sessionManagerName);
  311   
  312           webModuleData.setReferencePattern(TomcatWebAppContext.GBEAN_REF_MANAGER_RETRIEVER, name);
  313   
  314           addGBean(moduleContext, beanData);
  315   
  316           return name;
  317       }
  318       
  319       protected void addGBean(DeploymentContext moduleContext, GBeanData beanData) throws GBeanAlreadyExistsException {
  320           moduleContext.addGBean(beanData);
  321       }
  322   
  323       protected AbstractName newGBeanName(DeploymentContext moduleContext, String name) {
  324           return moduleContext.getNaming().createChildName(moduleContext.getModuleName(),
  325                   name,
  326                   GBeanInfoBuilder.DEFAULT_J2EE_TYPE);
  327       }
  328   
  329       protected void addAbstractNameQueries(Set patterns, GerPatternType patternType) {
  330           AbstractNameQuery query = ENCConfigBuilder.buildAbstractNameQuery(patternType, null, null, null);
  331           patterns.add(query);
  332       }
  333   
  334       public static final String GBEAN_ATTR_DFT_SWEEP_INTERVAL = "defaultSweepInterval";
  335       public static final String GBEAN_ATTR_DFT_SESSION_TIMEOUT = "defaultSessionTimeout";
  336       public static final String GBEAN_ATTR_DFT_NUM_PARTITIONS = "defaultNumPartitions";
  337       public static final String GBEAN_ATTR_DFT_BACKING_STRATEGY_FACTORY_NAME = "defaultBackingStrategyFactoryName";
  338       public static final String GBEAN_ATTR_DFT_CLUSTER_NAME = "defaultClusterName";
  339       public static final String GBEAN_ATTR_ARTIFACT_TO_REMOVE = "artifactToRemoveFromEnvironment";
  340       public static final String GBEAN_ATTR_DFT_ENVIRONMENT = "defaultEnvironment";
  341   }

Home » geronimo-2.2-source-release » org.apache.geronimo.tomcat.cluster.wadi.builder » [javadoc | source]