001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.deployment;
021:
022: import org.apache.axiom.om.OMAttribute;
023: import org.apache.axiom.om.OMElement;
024: import org.apache.axis2.clustering.ClusterManager;
025: import org.apache.axis2.clustering.configuration.ConfigurationManager;
026: import org.apache.axis2.clustering.configuration.ConfigurationManagerListener;
027: import org.apache.axis2.clustering.context.ContextManager;
028: import org.apache.axis2.clustering.context.ContextManagerListener;
029: import org.apache.axis2.engine.AxisConfiguration;
030: import org.apache.axis2.i18n.Messages;
031:
032: import javax.xml.namespace.QName;
033: import java.io.InputStream;
034: import java.util.ArrayList;
035: import java.util.Iterator;
036: import java.util.List;
037:
038: /**
039: * Builds a service description from OM
040: */
041: public class ClusterBuilder extends DescriptionBuilder {
042:
043: // private static final Log log = LogFactory.getLog(ClusterBuilder.class);
044:
045: public ClusterBuilder(AxisConfiguration axisConfig) {
046: this .axisConfig = axisConfig;
047: }
048:
049: public ClusterBuilder(InputStream serviceInputStream,
050: AxisConfiguration axisConfig) {
051: super (serviceInputStream, axisConfig);
052: }
053:
054: /**
055: * Populates service from corresponding OM.
056: */
057: public void buildCluster(OMElement clusterElement)
058: throws DeploymentException {
059:
060: OMAttribute classNameAttr = clusterElement
061: .getAttribute(new QName(TAG_CLASS_NAME));
062: if (classNameAttr == null) {
063: throw new DeploymentException(Messages.getMessage(
064: "classAttributeNotFound", TAG_CLUSTER));
065: }
066:
067: String className = classNameAttr.getAttributeValue();
068: ClusterManager clusterManager;
069: try {
070: Class clazz;
071: try {
072: clazz = Class.forName(className);
073: } catch (ClassNotFoundException e) {
074: throw new DeploymentException(Messages.getMessage(
075: "clusterImplNotFound", className));
076: }
077: clusterManager = (ClusterManager) clazz.newInstance();
078:
079: clusterManager.setConfigurationContext(configCtx);
080:
081: //loading the parameters.
082: processParameters(clusterElement
083: .getChildrenWithName(new QName(TAG_PARAMETER)),
084: clusterManager, null);
085:
086: //loading the ConfigurationManager
087: loadConfigManager(clusterElement, clusterManager);
088:
089: // loading the ContextManager
090: loadContextManager(clusterElement, clusterManager);
091:
092: axisConfig.setClusterManager(clusterManager);
093: } catch (InstantiationException e) {
094: throw new DeploymentException(Messages
095: .getMessage("cannotLoadClusterImpl"));
096: } catch (IllegalAccessException e) {
097: throw new DeploymentException(e);
098: }
099: }
100:
101: private void loadContextManager(OMElement clusterElement,
102: ClusterManager clusterManager) throws DeploymentException,
103: InstantiationException, IllegalAccessException {
104: OMElement contextManagerEle = clusterElement
105: .getFirstChildWithName(new QName(TAG_CONTEXT_MANAGER));
106: if (contextManagerEle != null) {
107:
108: // Load & set the ContextManager class
109: OMAttribute classNameAttr = contextManagerEle
110: .getAttribute(new QName(ATTRIBUTE_CLASS));
111: if (classNameAttr == null) {
112: throw new DeploymentException(Messages.getMessage(
113: "classAttributeNotFound", TAG_CONTEXT_MANAGER));
114: }
115:
116: String className = classNameAttr.getAttributeValue();
117:
118: Class clazz;
119: try {
120: clazz = Class.forName(className);
121: } catch (ClassNotFoundException e) {
122: throw new DeploymentException(Messages.getMessage(
123: "clusterImplNotFound", className));
124: }
125: ContextManager contextManager = (ContextManager) clazz
126: .newInstance();
127: clusterManager.setContextManager(contextManager);
128:
129: // Load & set the ContextManagerListener
130: OMElement listenerEle = contextManagerEle
131: .getFirstChildWithName(new QName(TAG_LISTENER));
132: if (listenerEle != null) {
133: classNameAttr = listenerEle.getAttribute(new QName(
134: TAG_CLASS_NAME));
135: if (classNameAttr == null) {
136: throw new DeploymentException(Messages.getMessage(
137: "classAttributeNotFound", TAG_LISTENER));
138: }
139: className = classNameAttr.getAttributeValue();
140: try {
141: clazz = Class.forName(className);
142: } catch (ClassNotFoundException e) {
143: throw new DeploymentException(Messages.getMessage(
144: "clusterImplNotFound", className));
145: }
146: ContextManagerListener listener = (ContextManagerListener) clazz
147: .newInstance();
148: contextManager.setContextManagerListener(listener);
149: } else {
150: throw new DeploymentException(Messages
151: .getMessage("contextManagerListenerIsNull"));
152: }
153:
154: //loading the parameters.
155: processParameters(contextManagerEle
156: .getChildrenWithName(new QName(TAG_PARAMETER)),
157: contextManager, null);
158:
159: // Load the replication patterns to be excluded. We load the following structure.
160: /*<replication>
161: <defaults>
162: <exclude name="foo.bar.*"/>
163: </defaults>
164: <context class="org.apache.axis2.context.ConfigurationContext">
165: <exclude name="my.sandesha.*"/>
166: </context>
167: <context class="org.apache.axis2.context.ServiceGroupContext">
168: <exclude name="my.sandesha.*"/>
169: </context>
170: <context class="org.apache.axis2.context.ServiceContext">
171: <exclude name="my.sandesha.*"/>
172: </context>
173: </replication>*/
174: OMElement replicationEle = contextManagerEle
175: .getFirstChildWithName(new QName(TAG_REPLICATION));
176: if (replicationEle != null) {
177: // Process defaults
178: OMElement defaultsEle = replicationEle
179: .getFirstChildWithName(new QName(TAG_DEFAULTS));
180: if (defaultsEle != null) {
181: List defaults = new ArrayList();
182: for (Iterator iter = defaultsEle
183: .getChildrenWithName(new QName(TAG_EXCLUDE)); iter
184: .hasNext();) {
185: OMElement excludeEle = (OMElement) iter.next();
186: OMAttribute nameAtt = excludeEle
187: .getAttribute(new QName(ATTRIBUTE_NAME));
188: defaults.add(nameAtt.getAttributeValue());
189: }
190: contextManager.setReplicationExcludePatterns(
191: TAG_DEFAULTS, defaults);
192: }
193:
194: // Process specifics
195: for (Iterator iter = replicationEle
196: .getChildrenWithName(new QName(TAG_CONTEXT)); iter
197: .hasNext();) {
198: OMElement contextEle = (OMElement) iter.next();
199: String ctxClassName = contextEle.getAttribute(
200: new QName(ATTRIBUTE_CLASS))
201: .getAttributeValue();
202: List excludes = new ArrayList();
203: for (Iterator iter2 = contextEle
204: .getChildrenWithName(new QName(TAG_EXCLUDE)); iter2
205: .hasNext();) {
206: OMElement excludeEle = (OMElement) iter2.next();
207: OMAttribute nameAtt = excludeEle
208: .getAttribute(new QName(ATTRIBUTE_NAME));
209: excludes.add(nameAtt.getAttributeValue());
210: }
211: contextManager.setReplicationExcludePatterns(
212: ctxClassName, excludes);
213: }
214: }
215: }
216: }
217:
218: private void loadConfigManager(OMElement clusterElement,
219: ClusterManager clusterManager) throws DeploymentException,
220: InstantiationException, IllegalAccessException {
221: OMElement configManagerEle = clusterElement
222: .getFirstChildWithName(new QName(
223: TAG_CONFIGURATION_MANAGER));
224: if (configManagerEle != null) {
225: OMAttribute classNameAttr = configManagerEle
226: .getAttribute(new QName(ATTRIBUTE_CLASS));
227: if (classNameAttr == null) {
228: throw new DeploymentException(Messages.getMessage(
229: "classAttributeNotFound",
230: TAG_CONFIGURATION_MANAGER));
231: }
232:
233: String className = classNameAttr.getAttributeValue();
234: Class clazz;
235: try {
236: clazz = Class.forName(className);
237: } catch (ClassNotFoundException e) {
238: throw new DeploymentException(Messages.getMessage(
239: "clusterImplNotFound", className));
240: }
241:
242: ConfigurationManager configurationManager = (ConfigurationManager) clazz
243: .newInstance();
244: clusterManager
245: .setConfigurationManager(configurationManager);
246:
247: OMElement listenerEle = configManagerEle
248: .getFirstChildWithName(new QName(TAG_LISTENER));
249: if (listenerEle != null) {
250: classNameAttr = listenerEle.getAttribute(new QName(
251: TAG_CLASS_NAME));
252: if (classNameAttr == null) {
253: throw new DeploymentException(Messages.getMessage(
254: "clusterImplNotFound", TAG_LISTENER));
255: }
256:
257: className = classNameAttr.getAttributeValue();
258: try {
259: clazz = Class.forName(className);
260: } catch (ClassNotFoundException e) {
261: throw new DeploymentException(
262: Messages
263: .getMessage("configurationManagerListenerIsNull"));
264: }
265: ConfigurationManagerListener listener = (ConfigurationManagerListener) clazz
266: .newInstance();
267: listener.setConfigurationContext(configCtx);
268: configurationManager
269: .setConfigurationManagerListener(listener);
270: } else {
271: throw new DeploymentException(
272: Messages
273: .getMessage("configurationManagerListenerIsNull"));
274: }
275:
276: //updating the ConfigurationManager with the new ConfigurationContext
277: configurationManager.setConfigurationContext(configCtx);
278:
279: //loading the parameters.
280: processParameters(configManagerEle
281: .getChildrenWithName(new QName(TAG_PARAMETER)),
282: configurationManager, null);
283: }
284: }
285: }
|