View Javadoc

1   /*
2    * Copyright (c) 2003 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: DeployerServiceJMX.java,v 1.1.1.1 2004/05/19 12:26:41 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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>&lt;server&gt;
48     &lt;mbean code="org.backsource.axis.DeployerServiceJMX" name="axis:service=Deployer,name=VV"&gt;
49      &lt;attribute name="Port"&gt;8080&lt;/attribute&gt;
50      &lt;attribute name="ServletPath"&gt;axistest/services/AdminService&lt;/attribute&gt;
51      &lt;attribute name="Wsdd"&gt;&lt;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"&gt;
57          &lt;service name="DeliverEvents" provider="Handler"&gt;
58            &lt;parameter name="handlerClass" value="org.backsource.axis.MBeanProxyProvider"/&gt;
59            &lt;parameter name="ObjectName" value="ProtocolHandler:handlerName=vvWs"/&gt;
60            &lt;parameter name="InterfaceName" value="org.backsource.amsterdam.plugins.axis.AxisEntrance"/&gt;
61            &lt;parameter name="allowedMethods" value="handleMessage"/&gt;
62            &lt;operation name="handleMessage" qname="tt:DeliverEvents" returnQName="DeliverEventsReturn" xmlns:tt="http://localhost:8080/axistest"&gt;
63              &lt;parameter name="events" qname="Events" type="xsd:base64Binary"/&gt;
64            &lt;/operation&gt;
65            &lt;requestFlow&gt;
66              &lt;handler type="java:org.backsource.axis.UserPasswordAutenticationHandler"&gt;
67                &lt;parameter name="allowedUser" value="VV"/&gt;
68                &lt;parameter name="allowedPassword" value="VV1"/&gt;
69              &lt;/handler&gt;
70            &lt;/requestFlow&gt;
71          &lt;/service&gt;
72        &lt;/deployment&gt;
73  
74      &lt;/attribute&gt;
75  &lt;!-- When embedding axis undeployment is not needed--&gt;
76      &lt;attribute name="WsddUndep"&gt;&lt;undeployment name="JMX.net.test" xmlns="http://xml.apache.org/axis/wsdd/"&gt;
77          &lt;service name="DeliverEvents"/&gt;
78        &lt;/undeployment&gt;
79      &lt;/attribute&gt;
80  
81    &lt;/mbean&gt;
82  &lt;/server&gt;
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       } // end of if 
246       if ( is != null) {
247          deploy(is);
248       } // end of if ()
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             } // end of if 
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             } // end of else if ()
270 
271             
272          } // end of if ()
273          
274          
275       } // end of if ()
276       
277    }
278 
279    protected InputStream getWsddFromElement(Element wsddEl) throws XmlException {
280       if ( wsddEl == null) {
281          return null;
282       } // end of if ()
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       } // end of if ()
291       
292       // Classpath first
293       InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
294       if ( is == null) {
295          is = new FileInputStream(fileName);
296       } // end of if ()
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       } // end of if ()
306       if ( user != null) {
307          args.add("-u");
308          args.add(user);
309       } // end of if ()
310       if ( password != null) {
311          args.add("-p");
312          args.add(password);
313       } // end of if ()
314       if ( servletPath != null) {
315          args.add("-s");
316          args.add(servletPath);
317       } // end of if ()
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       } // end of if ()
353       if ( wsdd == null) {
354          throw new IllegalArgumentException("wsdd not allowed to be null");
355       } // end of if ()
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       } // end of if ()
366       if ( serviceName == null) {
367          throw new IllegalArgumentException("Service name not allowed to be null");
368       } // end of if ()
369       admin.undeployService(serviceName);
370    }
371 }// DeployerServiceJMX