KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jetty6 > cluster > wadi > builder > WADIJettyClusteringBuilder


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.jetty6.cluster.wadi.builder;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Set JavaDoc;
22
23 import javax.xml.namespace.QName JavaDoc;
24
25 import org.apache.geronimo.clustering.wadi.BasicWADISessionManager;
26 import org.apache.geronimo.clustering.wadi.WADISessionManagerConfigInfo;
27 import org.apache.geronimo.common.DeploymentException;
28 import org.apache.geronimo.deployment.DeploymentContext;
29 import org.apache.geronimo.deployment.NamespaceDrivenBuilder;
30 import org.apache.geronimo.deployment.service.EnvironmentBuilder;
31 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
32 import org.apache.geronimo.gbean.AbstractName;
33 import org.apache.geronimo.gbean.AbstractNameQuery;
34 import org.apache.geronimo.gbean.GBeanData;
35 import org.apache.geronimo.gbean.GBeanInfo;
36 import org.apache.geronimo.gbean.GBeanInfoBuilder;
37 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
38 import org.apache.geronimo.jetty6.JettyWebAppContext;
39 import org.apache.geronimo.jetty6.cluster.ClusteredSessionHandlerFactory;
40 import org.apache.geronimo.jetty6.cluster.wadi.WADIClusteredPreHandlerFactory;
41 import org.apache.geronimo.kernel.GBeanAlreadyExistsException;
42 import org.apache.geronimo.kernel.GBeanNotFoundException;
43 import org.apache.geronimo.kernel.config.Configuration;
44 import org.apache.geronimo.kernel.repository.Environment;
45 import org.apache.geronimo.naming.deployment.ENCConfigBuilder;
46 import org.apache.geronimo.schema.NamespaceElementConverter;
47 import org.apache.geronimo.schema.SchemaConversionUtils;
48 import org.apache.geronimo.xbeans.geronimo.GerClusteringWadiDocument;
49 import org.apache.geronimo.xbeans.geronimo.GerClusteringWadiType;
50 import org.apache.geronimo.xbeans.geronimo.naming.GerPatternType;
51 import org.apache.xmlbeans.QNameSet;
52 import org.apache.xmlbeans.XmlObject;
53
54 /**
55  *
56  * @version $Rev$ $Date$
57  */

58 public class WADIJettyClusteringBuilder implements NamespaceDrivenBuilder {
59     private static final QName JavaDoc CLUSTERING_WADI_QNAME = GerClusteringWadiDocument.type.getDocumentElementName();
60     private static final QNameSet CLUSTERING_WADI_QNAME_SET = QNameSet.singleton(CLUSTERING_WADI_QNAME);
61
62     private final int defaultSweepInterval;
63     private final int defaultNumPartitions;
64     private final AbstractNameQuery defaultRepManagerFactoryName;
65     private final AbstractNameQuery defaultRepStorageFactoryName;
66     private final AbstractNameQuery defaultBackingStrategyFactoryName;
67     private final AbstractNameQuery defaultDispatcherHolderName;
68     private final Environment defaultEnvironment;
69
70     public WADIJettyClusteringBuilder(int defaultSweepInterval,
71             int defaultNumPartitions,
72             AbstractNameQuery defaultRepManagerFactoryName,
73             AbstractNameQuery defaultRepStorageFactoryName,
74             AbstractNameQuery defaultBackingStrategyFactoryName,
75             AbstractNameQuery defaultDispatcherHolderName,
76             Environment defaultEnvironment) {
77         this.defaultSweepInterval = defaultSweepInterval;
78         this.defaultNumPartitions = defaultNumPartitions;
79         this.defaultRepManagerFactoryName = defaultRepManagerFactoryName;
80         this.defaultRepStorageFactoryName = defaultRepStorageFactoryName;
81         this.defaultBackingStrategyFactoryName = defaultBackingStrategyFactoryName;
82         this.defaultDispatcherHolderName = defaultDispatcherHolderName;
83         this.defaultEnvironment = defaultEnvironment;
84         SchemaConversionUtils.registerNamespaceConversions(Collections.singletonMap(CLUSTERING_WADI_QNAME.getLocalPart(), new NamespaceElementConverter(CLUSTERING_WADI_QNAME.getNamespaceURI())));
85     }
86
87     public void buildEnvironment(XmlObject container, Environment environment) throws DeploymentException {
88         if (getWadiClusterConfig(container) != null) {
89             EnvironmentBuilder.mergeEnvironments(environment, defaultEnvironment);
90         }
91     }
92
93     public void build(XmlObject container, DeploymentContext applicationContext, DeploymentContext moduleContext) throws DeploymentException {
94         GerClusteringWadiType clusteringWadiType = getWadiClusterConfig(container);
95         if (clusteringWadiType != null) {
96             GBeanData webModuleData = extractWebModule(moduleContext);
97             try {
98                 AbstractName sessionManagerName = addSessionManager(clusteringWadiType, webModuleData, moduleContext);
99                 addSessionHandlerFactory(moduleContext, webModuleData, sessionManagerName);
100                 addPreHandlerFactory(moduleContext, webModuleData, sessionManagerName);
101             } catch (GBeanAlreadyExistsException e) {
102                 throw new DeploymentException("Duplicate GBean", e);
103             }
104         }
105     }
106
107     private GBeanData extractWebModule(DeploymentContext moduleContext) throws DeploymentException {
108         Configuration configuration = moduleContext.getConfiguration();
109         AbstractNameQuery webModuleQuery = new AbstractNameQuery(configuration.getId(), Collections.EMPTY_MAP, Collections.singleton(JettyWebAppContext.class.getName()));
110         try {
111             return configuration.findGBeanData(webModuleQuery);
112         } catch (GBeanNotFoundException e) {
113             throw new DeploymentException("Could not locate web module gbean in web app configuration", e);
114         }
115     }
116
117     public QNameSet getSpecQNameSet() {
118         return QNameSet.EMPTY;
119     }
120
121     public QNameSet getPlanQNameSet() {
122         return CLUSTERING_WADI_QNAME_SET;
123     }
124
125     private GerClusteringWadiType getWadiClusterConfig(XmlObject container) throws DeploymentException {
126         XmlObject[] items = container.selectChildren(CLUSTERING_WADI_QNAME_SET);
127         if (items.length > 1) {
128             throw new DeploymentException("Unexpected count of clustering elements in geronimo plan " + items.length + " qnameset: " + CLUSTERING_WADI_QNAME_SET);
129         }
130         if (items.length == 1) {
131             return (GerClusteringWadiType) items[0].copy().changeType(GerClusteringWadiType.type);
132         }
133         return null;
134     }
135
136     private AbstractName addSessionManager(GerClusteringWadiType clustering, GBeanData webModuleData,
137             DeploymentContext moduleContext) throws GBeanAlreadyExistsException {
138         AbstractName name = moduleContext.getNaming().createChildName(moduleContext.getModuleName(),
139                 "WADISessionManager", NameFactory.GERONIMO_SERVICE);
140
141         GBeanData beanData = new GBeanData(name, BasicWADISessionManager.GBEAN_INFO);
142
143         setConfigInfo(clustering, webModuleData, beanData);
144         setReplicationManagerFactory(clustering, beanData);
145         setReplicaStorageFactory(clustering, beanData);
146         setBackingStrategyFactory(clustering, beanData);
147         setDispatcher(clustering, beanData);
148
149         moduleContext.addGBean(beanData);
150
151         return name;
152     }
153     
154     private void setConfigInfo(GerClusteringWadiType clustering, GBeanData webModuleData, GBeanData beanData) {
155         int sweepInterval = defaultSweepInterval;
156         if (clustering.isSetSweepInterval()) {
157             sweepInterval = clustering.getSweepInterval().intValue();
158         }
159         int numPartitions = defaultNumPartitions;
160         if (clustering.isSetNumPartitions()) {
161             numPartitions = clustering.getNumPartitions().intValue();
162         }
163         Integer JavaDoc sessionTimeout = (Integer JavaDoc) webModuleData.getAttribute(JettyWebAppContext.GBEAN_ATTR_SESSION_TIMEOUT);
164         if (null == sessionTimeout) {
165             throw new AssertionError JavaDoc();
166         }
167         
168         WADISessionManagerConfigInfo configInfo = new WADISessionManagerConfigInfo(
169                 beanData.getAbstractName().toURI(),
170                 sweepInterval,
171                 numPartitions,
172                 sessionTimeout.intValue());
173         beanData.setAttribute(BasicWADISessionManager.GBEAN_ATTR_WADI_CONFIG_INFO, configInfo);
174     }
175
176     private void setDispatcher(GerClusteringWadiType clustering, GBeanData beanData) {
177         Set JavaDoc patterns = new HashSet JavaDoc();
178         if (clustering.isSetDispatcher()) {
179             addAbstractNameQueries(patterns, clustering.getDispatcher().getPatternArray());
180         } else {
181             patterns.add(defaultDispatcherHolderName);
182         }
183         beanData.setReferencePatterns(BasicWADISessionManager.GBEAN_REF_DISPATCHER_HOLDER, patterns);
184     }
185
186     private void setBackingStrategyFactory(GerClusteringWadiType clustering, GBeanData beanData) {
187         Set JavaDoc patterns = new HashSet JavaDoc();
188         if (clustering.isSetBackingStrategyFactory()) {
189             addAbstractNameQueries(patterns, clustering.getBackingStrategyFactory().getPatternArray());
190         } else {
191             patterns.add(defaultBackingStrategyFactoryName);
192         }
193         beanData.setReferencePatterns(BasicWADISessionManager.GBEAN_REF_BACKING_STRATEGY_FACTORY, patterns);
194     }
195
196     private void setReplicaStorageFactory(GerClusteringWadiType clustering, GBeanData beanData) {
197         Set JavaDoc patterns = new HashSet JavaDoc();
198         if (clustering.isSetReplicaStorageFactory()) {
199             addAbstractNameQueries(patterns, clustering.getReplicaStorageFactory().getPatternArray());
200         } else {
201             patterns.add(defaultRepStorageFactoryName);
202         }
203         beanData.setReferencePatterns(BasicWADISessionManager.GBEAN_REF_REPLICA_STORAGE_FACTORY, patterns);
204     }
205
206     private void setReplicationManagerFactory(GerClusteringWadiType clustering, GBeanData beanData) {
207         Set JavaDoc patterns = new HashSet JavaDoc();
208         if (clustering.isSetReplicationManagerFactory()) {
209             addAbstractNameQueries(patterns, clustering.getReplicationManagerFactory().getPatternArray());
210         } else {
211             patterns.add(defaultRepManagerFactoryName);
212         }
213         beanData.setReferencePatterns(BasicWADISessionManager.GBEAN_REF_REPLICATION_MANAGER_FACTORY, patterns);
214     }
215
216     private AbstractName addPreHandlerFactory(DeploymentContext moduleContext,
217             GBeanData webModuleData, AbstractName sessionManagerName) throws GBeanAlreadyExistsException {
218         AbstractName name = moduleContext.getNaming().createChildName(moduleContext.getModuleName(),
219                 "WADIClusteredPreHandlerFactory", NameFactory.GERONIMO_SERVICE);
220
221         GBeanData beanData = new GBeanData(name, WADIClusteredPreHandlerFactory.GBEAN_INFO);
222         beanData.setReferencePattern(WADIClusteredPreHandlerFactory.GBEAN_REF_WADI_SESSION_MANAGER, sessionManagerName);
223
224         webModuleData.setReferencePattern(JettyWebAppContext.GBEAN_REF_PRE_HANDLER_FACTORY, name);
225
226         moduleContext.addGBean(beanData);
227
228         return name;
229     }
230
231     private AbstractName addSessionHandlerFactory(DeploymentContext moduleContext,
232             GBeanData webModuleData, AbstractName sessionManagerName) throws GBeanAlreadyExistsException {
233         AbstractName name = moduleContext.getNaming().createChildName(moduleContext.getModuleName(),
234                 "ClusteredSessionHandlerFactory", NameFactory.GERONIMO_SERVICE);
235
236         GBeanData beanData = new GBeanData(name, ClusteredSessionHandlerFactory.GBEAN_INFO);
237         beanData.setReferencePattern(ClusteredSessionHandlerFactory.GBEAN_REF_SESSION_MANAGER, sessionManagerName);
238
239         webModuleData.setReferencePattern(JettyWebAppContext.GBEAN_REF_SESSION_HANDLER_FACTORY, name);
240
241         moduleContext.addGBean(beanData);
242
243         return name;
244     }
245
246     private void addAbstractNameQueries(Set JavaDoc patterns, GerPatternType[] patternTypes) {
247         for (int i = 0; i < patternTypes.length; i++) {
248             AbstractNameQuery query = ENCConfigBuilder.buildAbstractNameQuery(patternTypes[i], null, null, null);
249             patterns.add(query);
250         }
251     }
252
253     public static final GBeanInfo GBEAN_INFO;
254
255     public static final String JavaDoc GBEAN_ATTR_DFT_SWEEP_INTERVAL = "defaultSweepInterval";
256     public static final String JavaDoc GBEAN_ATTR_DFT_NUM_PARTITIONS = "defaultNumPartitions";
257     public static final String JavaDoc GBEAN_ATTR_DFT_REP_MANAGER_FACTORY_NAME = "defaultReplicationManagerFactoryName";
258     public static final String JavaDoc GBEAN_ATTR_DFT_REP_STORAGE_FACTORY_NAME = "defaultReplicaStorageFactoryName";
259     public static final String JavaDoc GBEAN_ATTR_DFT_BACKING_STRATEGY_FACTORY_NAME = "defaultBackingStrategyFactoryName";
260     public static final String JavaDoc GBEAN_ATTR_DFT_DISPATCHER_HOLDER_NAME = "defaultDispatcherHolderName";
261     public static final String JavaDoc GBEAN_ATTR_DFT_ENVIRONMENT = "defaultEnvironment";
262
263     static {
264         GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("WADI Session Manager",
265                 WADIJettyClusteringBuilder.class,
266                 NameFactory.MODULE_BUILDER);
267
268         infoBuilder.addAttribute(GBEAN_ATTR_DFT_SWEEP_INTERVAL, int.class, true);
269         infoBuilder.addAttribute(GBEAN_ATTR_DFT_NUM_PARTITIONS, int.class, true);
270         infoBuilder.addAttribute(GBEAN_ATTR_DFT_REP_MANAGER_FACTORY_NAME, AbstractNameQuery.class, true);
271         infoBuilder.addAttribute(GBEAN_ATTR_DFT_REP_STORAGE_FACTORY_NAME, AbstractNameQuery.class, true);
272         infoBuilder.addAttribute(GBEAN_ATTR_DFT_BACKING_STRATEGY_FACTORY_NAME, AbstractNameQuery.class, true);
273         infoBuilder.addAttribute(GBEAN_ATTR_DFT_DISPATCHER_HOLDER_NAME, AbstractNameQuery.class, true);
274         infoBuilder.addAttribute(GBEAN_ATTR_DFT_ENVIRONMENT, Environment.class, true);
275
276         infoBuilder.setConstructor(new String JavaDoc[]{GBEAN_ATTR_DFT_SWEEP_INTERVAL,
277                 GBEAN_ATTR_DFT_NUM_PARTITIONS,
278                 GBEAN_ATTR_DFT_REP_MANAGER_FACTORY_NAME,
279                 GBEAN_ATTR_DFT_REP_STORAGE_FACTORY_NAME,
280                 GBEAN_ATTR_DFT_BACKING_STRATEGY_FACTORY_NAME,
281                 GBEAN_ATTR_DFT_DISPATCHER_HOLDER_NAME,
282                 GBEAN_ATTR_DFT_ENVIRONMENT});
283
284         GBEAN_INFO = infoBuilder.getBeanInfo();
285     }
286
287     public static GBeanInfo getGBeanInfo() {
288         return GBEAN_INFO;
289     }
290
291 }
292
Popular Tags