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.synapse.transport;
021:
022: import org.apache.axis2.description.*;
023: import org.apache.axis2.Constants;
024: import org.apache.axis2.engine.ListenerManager;
025: import org.apache.synapse.transport.vfs.VFSTransportListener;
026: import org.apache.axis2.context.ConfigurationContext;
027: import org.apache.axis2.context.ConfigurationContextFactory;
028: import org.apache.axis2.wsdl.WSDLConstants;
029: import org.apache.axis2.receivers.RawXMLINOutMessageReceiver;
030: import org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver;
031:
032: import javax.xml.namespace.QName;
033: import java.io.File;
034: import java.util.List;
035: import java.util.Iterator;
036:
037: /**
038: * Base class for transport util servers used in unit testing
039: */
040: public abstract class UtilsTransportServer {
041:
042: private ListenerManager listnMgr = null;
043: private ConfigurationContext cfgCtx = null;
044:
045: public void start(TransportInDescription trpInDesc,
046: TransportOutDescription trpDescOut) throws Exception {
047:
048: // create a dummy repository
049: File file = makeCleanPath("target/axis2/repository");
050:
051: cfgCtx = ConfigurationContextFactory
052: .createConfigurationContextFromFileSystem(file
053: .getAbsolutePath());
054:
055: // remove http transport
056: cfgCtx.getAxisConfiguration().getTransportsIn().remove("http");
057:
058: // start given transport
059: listnMgr = new ListenerManager();
060: listnMgr.init(cfgCtx);
061: cfgCtx.setTransportManager(listnMgr);
062: //listnMgr.addListener(trpInDesc, false);
063:
064: trpDescOut.getSender().init(cfgCtx, trpDescOut);
065: cfgCtx.getAxisConfiguration().addTransportOut(trpDescOut);
066: //trpInDesc.getReceiver().init(cfgCtx, trpInDesc);
067: listnMgr.addListener(trpInDesc, false);
068: listnMgr.start();
069: }
070:
071: public void start() throws Exception {
072: }
073:
074: public void stop() throws Exception {
075: listnMgr.stop();
076: }
077:
078: /**
079: * Deploy the standard Echo service with the custom parameters passed in
080: * @param name the service name to assign
081: * @param parameters the parameters for the service
082: * @throws Exception
083: */
084: public void deployEchoService(String name, List parameters)
085: throws Exception {
086:
087: AxisService service = new AxisService(name);
088: service.setClassLoader(Thread.currentThread()
089: .getContextClassLoader());
090: service.addParameter(new Parameter(Constants.SERVICE_CLASS,
091: Echo.class.getName()));
092:
093: // add operation echoOMElement
094: AxisOperation axisOp = new InOutAxisOperation(new QName(
095: "echoOMElement"));
096: axisOp.setMessageReceiver(new RawXMLINOutMessageReceiver());
097: axisOp.setStyle(WSDLConstants.STYLE_RPC);
098: service.addOperation(axisOp);
099: service.mapActionToOperation(Constants.AXIS2_NAMESPACE_URI
100: + "/echoOMElement", axisOp);
101:
102: // add operation echoOMElementNoResponse
103: axisOp = new InOutAxisOperation(new QName(
104: "echoOMElementNoResponse"));
105: axisOp.setMessageReceiver(new RawXMLINOnlyMessageReceiver());
106: axisOp.setStyle(WSDLConstants.STYLE_RPC);
107: service.addOperation(axisOp);
108: service.mapActionToOperation(Constants.AXIS2_NAMESPACE_URI
109: + "/echoOMElementNoResponse", axisOp);
110:
111: Iterator iter = parameters.iterator();
112: while (iter.hasNext()) {
113: service.addParameter((Parameter) iter.next());
114: }
115:
116: cfgCtx.getAxisConfiguration().addService(service);
117: }
118:
119: /**
120: * Make sure the given path exists by creating it if required
121: * @param path
122: * @return
123: * @throws Exception
124: */
125: protected File makePath(String path) throws Exception {
126: File file = new File(path);
127: if (!file.exists()) {
128: if (!file.mkdirs()) {
129: throw new Exception("Couldn't create directory : "
130: + file.getPath());
131: }
132: }
133: return file;
134: }
135:
136: /**
137: * Delete the path if it exists and, create it
138: * @param path
139: * @return
140: * @throws Exception
141: */
142: protected File makeCleanPath(String path) throws Exception {
143: File file = new File(path);
144: if (!file.exists()) {
145: if (!file.mkdirs()) {
146: throw new Exception("Couldn't create directory : "
147: + file.getPath());
148: }
149: } else {
150: // delete any existing
151: recursivelydelete(file);
152: if (file.exists()) {
153: throw new Exception("Couldn't delete directory : "
154: + file.getPath());
155: }
156: if (!file.mkdirs()) {
157: throw new Exception("Couldn't create directory : "
158: + file.getPath());
159: }
160: }
161: return file;
162: }
163:
164: private void recursivelydelete(File file) {
165:
166: File[] children = file.listFiles();
167: if (children != null && children.length > 0) {
168: for (int i = 0; i < children.length; i++) {
169: recursivelydelete(children[i]);
170: }
171: }
172: file.delete();
173: }
174: }
|