1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.backsource.axis;
21 import java.util.ArrayList;
22 import java.io.InputStream;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25
26 import javax.management.ObjectName;
27 import javax.management.MBeanServer;
28 import javax.management.MalformedObjectNameException;
29
30 import org.w3c.dom.Document;
31 import org.w3c.dom.Element;
32
33 import org.apache.axis.client.AdminClient;
34 import org.apache.axis.utils.Options;
35
36 import org.backsource.jmx.ServiceMBeanSupport;
37 import org.backsource.jmx.ObjectNameFactory;
38 import org.backsource.utils.xml.DocumentUtil;
39 import org.backsource.utils.xml.XmlException;
40 import org.backsource.utils.io.DOMInputStream;
41
42 import org.backsource.axis.DeployerServiceJMXMBean;
43 /***
44 * MBean wich deployes and undeployes webservices, at starts and stop.
45 * <p>Here is an example mbean setup:</p>
46 <?xml version="1.0" encoding="iso-8859-1"?>
47 <pre><server>
48 <mbean code="org.backsource.axis.DeployerServiceJMX" name="axis:service=Deployer,name=VV">
49 <attribute name="Port">8080</attribute>
50 <attribute name="ServletPath">axistest/services/AdminService</attribute>
51 <attribute name="Wsdd"><deployment
52 name="JMX.net.test"
53 targetNameSpace="http://net.jboss.org/jmx/test"
54 xmlns="http://xml.apache.org/axis/wsdd/"
55 xmlns:jmx="http://net.jboss.org/jmx/test"
56 xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
57 <service name="DeliverEvents" provider="Handler">
58 <parameter name="handlerClass" value="org.backsource.axis.MBeanProxyProvider"/>
59 <parameter name="ObjectName" value="ProtocolHandler:handlerName=vvWs"/>
60 <parameter name="InterfaceName" value="org.backsource.amsterdam.plugins.axis.AxisEntrance"/>
61 <parameter name="allowedMethods" value="handleMessage"/>
62 <operation name="handleMessage" qname="tt:DeliverEvents" returnQName="DeliverEventsReturn" xmlns:tt="http://localhost:8080/axistest">
63 <parameter name="events" qname="Events" type="xsd:base64Binary"/>
64 </operation>
65 <requestFlow>
66 <handler type="java:org.backsource.axis.UserPasswordAutenticationHandler">
67 <parameter name="allowedUser" value="VV"/>
68 <parameter name="allowedPassword" value="VV1"/>
69 </handler>
70 </requestFlow>
71 </service>
72 </deployment>
73
74 </attribute>
75 <!-- When embedding axis undeployment is not needed-->
76 <attribute name="WsddUndep"><undeployment name="JMX.net.test" xmlns="http://xml.apache.org/axis/wsdd/">
77 <service name="DeliverEvents"/>
78 </undeployment>
79 </attribute>
80
81 </mbean>
82 </server>
83 </pre>
84 *
85 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
86 * @version $Revision: 1.1.1.1 $
87 * @jmx:mbean name="axis:service=Deployer,name=ServiceName" extends="org.backsource.jmx.ServiceMBean"
88 */
89
90 public class DeployerServiceJMX extends ServiceMBeanSupport implements DeployerServiceJMXMBean{
91 public static final ObjectName OBJECT_NAME
92 = ObjectNameFactory.create("in:service=VV,name=DeliverEvents");
93 protected ObjectName name = OBJECT_NAME;
94 protected MBeanServer server;
95 int port;
96 String user;
97 String password;
98 Element wsddEl;
99 String wsddFileName;
100 Element wsddUndepEl;
101 String wsddUndepFileName;
102 String wsddServiceName;
103 AdminClient admin;
104 Options opts;
105 String servletPath;
106 public DeployerServiceJMX (){
107
108 }
109 protected ObjectName getObjectName(MBeanServer server, ObjectName name)
110 throws MalformedObjectNameException
111 {
112 this.server = server;
113 if (name != null) {
114 this.name = name;
115 }
116
117 return this.name;
118 }
119 /***
120 * Set the port of the webapp container.
121 * @jmx:managed-attribute
122 */
123 public void setPort(int port) {
124 this.port = port;
125 }
126 /***
127 * @jmx:managed-attribute
128 */
129 public int getPort() {
130 return port;
131 }
132 /***
133 * Set the servlet path of the deployed axis admin service.
134 * @jmx:managed-attribute
135 */
136 public void setServletPath(String servletPath) {
137 this.servletPath = servletPath;
138 }
139 /***
140 * @jmx:managed-attribute
141 */
142 public String getServletPath() {
143 return servletPath;
144 }
145 /***
146 *
147 * @jmx:managed-attribute
148 */
149 public void setPassport(String password) {
150 this.password = password;
151 }
152 /***
153 * @jmx:managed-attribute
154 */
155 public void setUser(String user) {
156 this.user = user;
157 }
158 /***
159 * @jmx:managed-attribute
160 */
161 public String getUser() {
162 return user;
163 }
164
165 /***
166 * Set the deployment wsdd as xml.
167 * @jmx:managed-attribute
168 */
169 public void setWsdd(Element wsddEl) {
170 this.wsddEl = wsddEl;
171 }
172
173 /***
174 * @jmx:managed-attribute
175 */
176 public Element getWsdd() {
177 return wsddEl;
178 }
179 /***
180 * Set the filename of a wsdd file to use.
181 * @jmx:managed-attribute
182 */
183 public void setWsddFileName(String wsddFileName) {
184 this.wsddFileName = wsddFileName;
185 }
186 /***
187 * @jmx:managed-attribute
188 */
189 public String getWsddFileName() {
190 return wsddFileName;
191 }
192 /***
193 * Set wsdd undeployment xml.
194 * @jmx:managed-attribute
195 */
196 public void setWsddUndep(Element wsddUndepEl) {
197 this.wsddUndepEl = wsddUndepEl;
198 }
199
200 public Element getWsddUndep() {
201 return wsddUndepEl;
202 }
203 /***
204 * Set the filename of a wsdd undeoloyment file.
205 * @jmx:managed-attribute
206 */
207 public void setWsddUndepFileName(String wsddUndepFileName) {
208 this.wsddUndepFileName = wsddUndepFileName;
209 }
210 /***
211 * @jmx:managed-attribute
212 */
213 public String getWsddUndepFileName() {
214 return wsddUndepFileName;
215 }
216 /***
217 * if the deployed service only contains one service and no global handlers, use this.
218 * @jmx:managed-attribute
219 */
220 public void setWsddServiceName(String serviceName) {
221 this.wsddServiceName = serviceName;
222 }
223
224
225 /***
226 * @jmx:managed-attribute
227 */
228 public String getWsddServiceName() {
229 return wsddServiceName;
230 }
231
232 /***
233 * Start service ans deploy any give service, try with includes xml first, then file.
234 */
235 protected void startService() throws Exception {
236 opts = new Options( getPropertiesAsArgs() );
237 opts.setDefaultURL("http://localhost:8080/axis/services/AdminService");
238
239 admin = new AdminClient();
240 admin.processOpts(opts);
241
242 InputStream is = getWsddFromElement(wsddEl);
243 if ( is == null) {
244 is = getWsddFromFile( wsddFileName );
245 }
246 if ( is != null) {
247 deploy(is);
248 }
249
250
251 }
252
253 /***
254 * Stop and undeploy any webservices. if a name is given, that service is deployed, othervise embedded xml and last file is tested.
255 */
256 protected void stopService() throws Exception {
257 if ( admin != null) {
258 if ( wsddServiceName != null) {
259 undeployService(wsddServiceName);
260 } else {
261 InputStream is = getWsddFromElement(wsddUndepEl);
262 if ( is == null) {
263 is = getWsddFromFile( wsddUndepFileName );
264 }
265 if ( is != null) {
266 undeploy(is);
267 } else if ( wsddEl != null || wsddFileName != null) {
268 log.warn("We had services deployed, but no undeploy infromation, could not undeploy");
269 }
270
271
272 }
273
274
275 }
276
277 }
278
279 protected InputStream getWsddFromElement(Element wsddEl) throws XmlException {
280 if ( wsddEl == null) {
281 return null;
282 }
283 return new DOMInputStream( DocumentUtil.createDocument( wsddEl) );
284
285 }
286
287 protected InputStream getWsddFromFile(String fileName) throws IOException {
288 if ( fileName == null) {
289 return null;
290 }
291
292
293 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
294 if ( is == null) {
295 is = new FileInputStream(fileName);
296 }
297 return is;
298 }
299
300 protected String[] getPropertiesAsArgs() {
301 ArrayList args = new ArrayList();
302 if ( port != -1) {
303 args.add("-p");
304 args.add(""+port);
305 }
306 if ( user != null) {
307 args.add("-u");
308 args.add(user);
309 }
310 if ( password != null) {
311 args.add("-p");
312 args.add(password);
313 }
314 if ( servletPath != null) {
315 args.add("-s");
316 args.add(servletPath);
317 }
318
319 return (String[])args.toArray( new String[ args.size() ] );
320 }
321 /***
322 * @jmx:managed-operation
323 */
324 public void deploy(String fileName) throws Exception {
325 deploy( getWsddFromFile(fileName));
326 }
327 /***
328 * @jmx:managed-operation
329 */
330 public void deploy(InputStream wsdd) throws Exception{
331 process(wsdd);
332 }
333
334 /***
335 * @jmx:managed-operation
336 */
337 public void undeploy(String fileName) throws Exception {
338 deploy( getWsddFromFile(fileName));
339 }
340 /***
341 * @jmx:managed-operation
342 */
343 public void undeploy(InputStream wsdd) throws Exception{
344 process(wsdd);
345 }
346 /***
347 * @jmx:managed-operation
348 */
349 public void process(InputStream wsdd) throws Exception{
350 if ( admin == null) {
351 throw new IllegalStateException("No admin client set up");
352 }
353 if ( wsdd == null) {
354 throw new IllegalArgumentException("wsdd not allowed to be null");
355 }
356 admin.process(opts,wsdd);
357 }
358
359 /***
360 * @jmx:managed-operation
361 */
362 public void undeployService(String serviceName) throws Exception{
363 if ( admin == null) {
364 throw new IllegalStateException("No admin client set up");
365 }
366 if ( serviceName == null) {
367 throw new IllegalArgumentException("Service name not allowed to be null");
368 }
369 admin.undeployService(serviceName);
370 }
371 }