KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > varia > deployment > BeanShellSubDeployer


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.varia.deployment;
23
24 import java.io.File JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.Arrays JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31
32 import org.jboss.deployment.DeploymentException;
33 import org.jboss.deployment.DeploymentInfo;
34 import org.jboss.deployment.SubDeployerSupport;
35 import org.jboss.mx.util.MBeanProxyExt;
36 import org.jboss.mx.util.ObjectNameConverter;
37 import org.jboss.system.ServiceControllerMBean;
38
39 /**
40  * A deployer that takes a bean shell script file and creates a JBoss
41  * MBean service wrapper for the script.
42  *
43  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
44  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
45  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>.
46  * @version $Revision: 41492 $
47  *
48  * @jmx.mbean name="jboss.system:service=BeanShellSubDeployer"
49  * extends="org.jboss.deployment.SubDeployerMBean"
50  */

51 public class BeanShellSubDeployer extends SubDeployerSupport
52    implements BeanShellSubDeployerMBean
53 {
54    // Constants -----------------------------------------------------
55

56    //public static final String BEANSHELL_EXTENSION = ".bsh";
57
public static final String JavaDoc BASE_SCRIPT_OBJECT_NAME = "jboss.scripts:type=BeanShell";
58
59    /** The suffixes we accept, along with their relative order */
60    private static final String JavaDoc[] DEFAULT_ENHANCED_SUFFIXES = new String JavaDoc[] {
61          "800:.bsh"
62    };
63    
64    // Attributes ----------------------------------------------------
65

66    protected ServiceControllerMBean serviceController;
67
68    // Static --------------------------------------------------------
69

70    // Constructors --------------------------------------------------
71

72    /**
73     * Default contructor used to set default values to the Suffixes and RelativeOrder
74     * attributes. Those are read at subdeployer registration time by the MainDeployer
75     * to alter its SuffixOrder.
76     */

77    public BeanShellSubDeployer()
78    {
79       setEnhancedSuffixes(DEFAULT_ENHANCED_SUFFIXES);
80    }
81    
82    // Public --------------------------------------------------------
83

84    // Z implementation ----------------------------------------------
85

86    // ServiceMBeanSupport overrides ---------------------------------------------------
87

88    /**
89     * Get a reference to the ServiceController
90     */

91    protected void startService() throws Exception JavaDoc
92    {
93       serviceController = (ServiceControllerMBean)
94          MBeanProxyExt.create(ServiceControllerMBean.class,
95            ServiceControllerMBean.OBJECT_NAME, server);
96
97       // register with MainDeployer
98
super.startService();
99    }
100
101    // SubDeployerSupport overrides ---------------------------------------------------
102

103    protected void processNestedDeployments(DeploymentInfo di) throws DeploymentException
104    {
105       // no sub-deployment!
106
}
107
108    /**
109     * Returns true if this deployer can deploy the given DeploymentInfo.
110     *
111     * @return True if this deployer can deploy the given DeploymentInfo.
112     *
113     * @jmx:managed-operation
114     */

115    public boolean accepts(DeploymentInfo sdi)
116    {
117       return super.accepts(sdi);
118    }
119
120    /**
121     * Describe <code>init</code> method here.
122     *
123     * @param di a <code>DeploymentInfo</code> value
124     * @exception DeploymentException if an error occurs
125     * @jmx:managed-operation
126     */

127    public void init(DeploymentInfo di)
128       throws DeploymentException
129    {
130       super.init(di);
131       di.watch = di.url;
132    }
133
134    /**
135     * Describe <code>create</code> method here.
136     *
137     * @param di a <code>DeploymentInfo</code> value
138     * @exception DeploymentException if an error occurs
139     * @jmx:managed-operation
140     */

141    public void create(DeploymentInfo di)
142       throws DeploymentException
143    {
144       try
145       {
146          // install the MBeans in this descriptor
147
log.debug("Deploying BeanShell script, create step: url " + di.url);
148          
149          String JavaDoc lURL = di.url.toString();
150          int lIndex = lURL.lastIndexOf( "/" );
151          di.shortName = lURL.substring( lIndex >= 0 ? lIndex + 1 : 0 );
152                   
153          BeanShellScript script = new BeanShellScript (di);
154          ObjectName JavaDoc bshScriptName = script.getPreferedObjectName();
155          ObjectName JavaDoc[] depends = script.getDependsServices();
156          
157          if (bshScriptName == null)
158          {
159             bshScriptName = ObjectNameConverter.convert(
160                BASE_SCRIPT_OBJECT_NAME + ",url=" + di.url);
161          }
162
163          di.deployedObject = bshScriptName;
164          try
165          {
166             server.unregisterMBean(bshScriptName);
167          } catch(Exception JavaDoc e) { log.info(e);}
168          server.registerMBean(script, bshScriptName);
169
170          log.debug( "Deploying: " + di.url );
171
172          // Init application
173
if (depends == null)
174             serviceController.create(bshScriptName);
175          else
176             serviceController.create(bshScriptName, Arrays.asList(depends));
177          super.create(di);
178       }
179       catch (Exception JavaDoc e)
180       {
181          destroy(di);
182          DeploymentException de = new DeploymentException("create operation failed for script "
183             + di.url, e);
184          throw de;
185       }
186    }
187
188    public synchronized void start(DeploymentInfo di)
189       throws DeploymentException
190    {
191       try
192       {
193          // Start application
194
log.debug( "start script, deploymentInfo: " + di +
195                     ", short name: " + di.shortName +
196                     ", parent short name: " +
197                     (di.parent == null ? "no parent" : di.parent.shortName) );
198
199          serviceController.start(di.deployedObject);
200
201          log.debug( "Deployed: " + di.url );
202          super.start(di);
203       }
204       catch (Exception JavaDoc e)
205       {
206          throw new DeploymentException( "Could not deploy " + di.url, e );
207       }
208    }
209
210    public void stop(DeploymentInfo di)
211       throws DeploymentException
212    {
213       try
214       {
215          serviceController.stop(di.deployedObject);
216          super.stop(di);
217       }
218       catch (Exception JavaDoc e)
219       {
220          throw new DeploymentException( "problem stopping ejb module: " +
221             di.url, e );
222       }
223    }
224
225    public void destroy(DeploymentInfo di)
226       throws DeploymentException
227    {
228       try
229       {
230          serviceController.destroy( di.deployedObject );
231          serviceController.remove( di.deployedObject );
232          super.destroy(di);
233       }
234       catch (Exception JavaDoc e)
235       {
236          throw new DeploymentException( "problem destroying BSH Script: " +
237             di.url, e );
238       }
239    }
240
241    /** Create a bsh deployment given the script content and name. This creates
242     * a temp file using File.createTempFile(scriptName, ".bsh") and then
243     * deploys this script via the main deployer.
244     *
245     * @param bshScript the bsh script content
246     * @param scriptName the bsh script name to use
247     * @return the URL of the temporary file used as the deployment script
248     *
249     * @throws DeploymentException thrown on failure to create the bsh
250     * script or deploy it.
251     *
252     * @jmx:managed-operation
253     */

254    public URL JavaDoc createScriptDeployment(String JavaDoc bshScript, String JavaDoc scriptName)
255       throws DeploymentException
256    {
257       try
258       {
259          File JavaDoc scriptFile = File.createTempFile(scriptName, ".bsh");
260          FileWriter JavaDoc fw = new FileWriter JavaDoc(scriptFile);
261          fw.write(bshScript);
262          fw.close();
263
264          URL JavaDoc scriptURL = scriptFile.toURL();
265          mainDeployer.deploy(scriptURL);
266          return scriptURL;
267       }
268       catch(IOException JavaDoc e)
269       {
270          throw new DeploymentException("Failed to deploy: "+scriptName, e);
271       }
272    }
273
274    // Package protected ---------------------------------------------
275

276    // Protected -----------------------------------------------------
277

278    // Private -------------------------------------------------------
279

280    // Inner classes -------------------------------------------------
281

282 }
283
Popular Tags