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.AxisFault;
025: import org.apache.axis2.deployment.util.PhasesInfo;
026: import org.apache.axis2.description.AxisModule;
027: import org.apache.axis2.description.AxisOperation;
028: import org.apache.axis2.description.AxisOperationFactory;
029: import org.apache.axis2.description.InOnlyAxisOperation;
030: import org.apache.axis2.description.PolicyInclude;
031: import org.apache.axis2.engine.AxisConfiguration;
032: import org.apache.axis2.engine.MessageReceiver;
033: import org.apache.axis2.i18n.Messages;
034: import org.apache.axis2.java.security.AccessController;
035: import org.apache.axis2.modules.Module;
036: import org.apache.axis2.util.Loader;
037:
038: import javax.xml.namespace.QName;
039: import javax.xml.stream.XMLStreamException;
040: import java.io.InputStream;
041: import java.io.StringWriter;
042: import java.security.PrivilegedActionException;
043: import java.security.PrivilegedExceptionAction;
044: import java.util.ArrayList;
045: import java.util.Iterator;
046:
047: /**
048: * Builds a module description from OM
049: */
050: public class ModuleBuilder extends DescriptionBuilder {
051: private AxisModule module;
052:
053: public ModuleBuilder(InputStream serviceInputStream,
054: AxisModule module, AxisConfiguration axisConfig) {
055: super (serviceInputStream, axisConfig);
056: this .module = module;
057: }
058:
059: private void loadModuleClass(AxisModule module,
060: String moduleClassName) throws DeploymentException {
061: Class moduleClass;
062:
063: try {
064: if ((moduleClassName != null)
065: && !"".equals(moduleClassName)) {
066: moduleClass = Loader.loadClass(module
067: .getModuleClassLoader(), moduleClassName);
068: final Class fmoduleClass = moduleClass;
069: final AxisModule fmodule = module;
070: try {
071: AccessController
072: .doPrivileged(new PrivilegedExceptionAction() {
073: public Object run()
074: throws IllegalAccessException,
075: InstantiationException {
076: Module new_module = (Module) fmoduleClass
077: .newInstance();
078: fmodule.setModule(new_module);
079: return null;
080: }
081: });
082: } catch (PrivilegedActionException e) {
083: throw e.getException();
084: }
085: }
086: } catch (Exception e) {
087: throw new DeploymentException(e.getMessage(), e);
088: }
089: }
090:
091: public void populateModule() throws DeploymentException {
092: try {
093: OMElement moduleElement = buildOM();
094: // Setting Module Class , if it is there
095: OMAttribute moduleClassAtt = moduleElement
096: .getAttribute(new QName(TAG_CLASS_NAME));
097:
098: if (moduleClassAtt != null) {
099: String moduleClass = moduleClassAtt.getAttributeValue();
100:
101: if ((moduleClass != null) && !"".equals(moduleClass)) {
102: loadModuleClass(module, moduleClass);
103: }
104: }
105:
106: // Get our name and version. If this is NOT present, we'll try to figure it out
107: // from the file name (e.g. "addressing-1.0.mar"). If the attribute is there, we
108: // always respect it.
109: //TODO: Need to check whether ths name is getting overridden by the file name of the MAR
110: OMAttribute nameAtt = moduleElement.getAttribute(new QName(
111: "name"));
112: if (nameAtt != null) {
113: String moduleName = nameAtt.getAttributeValue();
114: if (moduleName != null && !"".equals(moduleName)) {
115: module.setName(moduleName);
116: }
117: }
118:
119: // Process service description
120: OMElement descriptionElement = moduleElement
121: .getFirstChildWithName(new QName(TAG_DESCRIPTION));
122:
123: if (descriptionElement != null) {
124: OMElement descriptionValue = descriptionElement
125: .getFirstElement();
126:
127: if (descriptionValue != null) {
128: StringWriter writer = new StringWriter();
129:
130: descriptionValue.build();
131: descriptionValue.serialize(writer);
132: writer.flush();
133: module.setModuleDescription(writer.toString());
134: } else {
135: module.setModuleDescription(descriptionElement
136: .getText());
137: }
138: } else {
139: module
140: .setModuleDescription("module description not found");
141: }
142:
143: // setting the PolicyInclude
144:
145: // processing <wsp:Policy> .. </..> elements
146: Iterator policyElements = moduleElement
147: .getChildrenWithName(new QName(POLICY_NS_URI,
148: TAG_POLICY));
149:
150: if (policyElements != null && policyElements.hasNext()) {
151: processPolicyElements(PolicyInclude.AXIS_MODULE_POLICY,
152: policyElements, module.getPolicyInclude());
153: }
154:
155: // processing <wsp:PolicyReference> .. </..> elements
156: Iterator policyRefElements = moduleElement
157: .getChildrenWithName(new QName(POLICY_NS_URI,
158: TAG_POLICY_REF));
159:
160: if (policyRefElements != null && policyElements.hasNext()) {
161: processPolicyRefElements(
162: PolicyInclude.AXIS_MODULE_POLICY,
163: policyRefElements, module.getPolicyInclude());
164: }
165:
166: // processing Parameters
167: // Processing service level parameters
168: Iterator itr = moduleElement.getChildrenWithName(new QName(
169: TAG_PARAMETER));
170:
171: processParameters(itr, module, module.getParent());
172:
173: // process INFLOW
174: OMElement inFlow = moduleElement
175: .getFirstChildWithName(new QName(TAG_FLOW_IN));
176:
177: if (inFlow != null) {
178: module.setInFlow(processFlow(inFlow, module));
179: }
180:
181: OMElement outFlow = moduleElement
182: .getFirstChildWithName(new QName(TAG_FLOW_OUT));
183:
184: if (outFlow != null) {
185: module.setOutFlow(processFlow(outFlow, module));
186: }
187:
188: OMElement inFaultFlow = moduleElement
189: .getFirstChildWithName(new QName(TAG_FLOW_IN_FAULT));
190:
191: if (inFaultFlow != null) {
192: module.setFaultInFlow(processFlow(inFaultFlow, module));
193: }
194:
195: OMElement outFaultFlow = moduleElement
196: .getFirstChildWithName(new QName(TAG_FLOW_OUT_FAULT));
197:
198: if (outFaultFlow != null) {
199: module
200: .setFaultOutFlow(processFlow(outFaultFlow,
201: module));
202: }
203:
204: OMElement supportedPolicyNamespaces = moduleElement
205: .getFirstChildWithName(new QName(
206: TAG_SUPPORTED_POLICY_NAMESPACES));
207:
208: if (supportedPolicyNamespaces != null) {
209: module
210: .setSupportedPolicyNamespaces(processSupportedPolicyNamespaces(supportedPolicyNamespaces));
211: }
212:
213: /*
214: * Module description should contain a list of QName of the assertions that are local to the system.
215: * These assertions are not exposed to the outside.
216: */
217: OMElement localPolicyAssertionElement = moduleElement
218: .getFirstChildWithName(new QName(
219: "local-policy-assertions"));
220:
221: if (localPolicyAssertionElement != null) {
222: module
223: .setLocalPolicyAssertions(getLocalPolicyAssertionNames(localPolicyAssertionElement));
224: }
225:
226: // processing Operations
227: Iterator op_itr = moduleElement
228: .getChildrenWithName(new QName(TAG_OPERATION));
229: ArrayList operations = processOperations(op_itr);
230:
231: for (int i = 0; i < operations.size(); i++) {
232: AxisOperation operation = (AxisOperation) operations
233: .get(i);
234:
235: module.addOperation(operation);
236: }
237: } catch (XMLStreamException e) {
238: throw new DeploymentException(e);
239: }
240: }
241:
242: private ArrayList processOperations(Iterator operationsIterator)
243: throws DeploymentException {
244: ArrayList operations = new ArrayList();
245:
246: while (operationsIterator.hasNext()) {
247: OMElement operation = (OMElement) operationsIterator.next();
248:
249: //getting operation name
250: OMAttribute op_name_att = operation.getAttribute(new QName(
251: ATTRIBUTE_NAME));
252:
253: if (op_name_att == null) {
254: throw new DeploymentException(Messages
255: .getMessage(Messages.getMessage(
256: DeploymentErrorMsgs.INVALID_OP,
257: "operation name missing")));
258: }
259:
260: OMAttribute op_mep_att = operation.getAttribute(new QName(
261: TAG_MEP));
262: String mepURL = null;
263: AxisOperation op_descrip;
264:
265: if (op_mep_att != null) {
266: mepURL = op_mep_att.getAttributeValue();
267: }
268:
269: if (mepURL == null) {
270:
271: // assuming in-out MEP
272: op_descrip = new InOnlyAxisOperation();
273: } else {
274: try {
275: op_descrip = AxisOperationFactory
276: .getOperationDescription(mepURL);
277: } catch (AxisFault axisFault) {
278: throw new DeploymentException(
279: Messages
280: .getMessage(Messages
281: .getMessage(
282: DeploymentErrorMsgs.OPERATION_PROCESS_ERROR,
283: axisFault
284: .getMessage())));
285: }
286: }
287:
288: String opname = op_name_att.getAttributeValue();
289:
290: op_descrip.setName(new QName(opname));
291:
292: // Operation Parameters
293: Iterator parameters = operation
294: .getChildrenWithName(new QName(TAG_PARAMETER));
295: processParameters(parameters, op_descrip, module);
296:
297: //To process wsamapping;
298: processActionMappings(operation, op_descrip);
299:
300: // setting the MEP of the operation
301: // loading the message receivers
302: OMElement receiverElement = operation
303: .getFirstChildWithName(new QName(
304: TAG_MESSAGE_RECEIVER));
305:
306: if (receiverElement != null) {
307: MessageReceiver messageReceiver = loadMessageReceiver(
308: module.getModuleClassLoader(), receiverElement);
309: op_descrip.setMessageReceiver(messageReceiver);
310: } else {
311: // setting default message receiver
312: MessageReceiver msgReceiver = loadDefaultMessageReceiver(
313: mepURL, null);
314: op_descrip.setMessageReceiver(msgReceiver);
315: }
316: // Process Module Refs
317: Iterator modules = operation.getChildrenWithName(new QName(
318: TAG_MODULE));
319: processOperationModuleRefs(modules, op_descrip);
320:
321: // processing <wsp:Policy> .. </..> elements
322: Iterator policyElements = operation
323: .getChildrenWithName(new QName(POLICY_NS_URI,
324: TAG_POLICY));
325:
326: if (policyElements != null && policyElements.hasNext()) {
327: processPolicyElements(
328: PolicyInclude.AXIS_MODULE_OPERATION_POLICY,
329: policyElements, op_descrip.getPolicyInclude());
330: }
331:
332: // processing <wsp:PolicyReference> .. </..> elements
333: Iterator policyRefElements = operation
334: .getChildrenWithName(new QName(POLICY_NS_URI,
335: TAG_POLICY_REF));
336:
337: if (policyRefElements != null && policyElements.hasNext()) {
338: processPolicyRefElements(
339: PolicyInclude.AXIS_MODULE_OPERATION_POLICY,
340: policyRefElements, module.getPolicyInclude());
341: }
342:
343: // setting Operation phase
344: PhasesInfo info = axisConfig.getPhasesInfo();
345: try {
346: info.setOperationPhases(op_descrip);
347: } catch (AxisFault axisFault) {
348: throw new DeploymentException(axisFault);
349: }
350:
351: // adding the operation
352: operations.add(op_descrip);
353: }
354:
355: return operations;
356: }
357: }
|