KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > spring > deployment > SpringDeployer


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
23 package org.jboss.spring.deployment;
24
25 import java.io.File JavaDoc;
26 import java.net.URL JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import org.jboss.deployment.DeploymentException;
32 import org.jboss.deployment.DeploymentInfo;
33 import org.jboss.deployment.SubDeployer;
34 import org.jboss.deployment.SubDeployerSupport;
35 import org.jboss.spring.factory.BeanFactoryLoader;
36
37 /**
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  * @author <a HREF="mailto:ales.justin@genera-lynx.com">Ales Justin</a>
40  */

41 public abstract class SpringDeployer extends SubDeployerSupport implements
42       SubDeployer, SpringDeployerMBean
43 {
44
45    protected abstract BeanFactoryLoader createBeanFactoryLoader();
46
47    private BeanFactoryLoader beanFactoryLoader = createBeanFactoryLoader();
48
49    /**
50     * Default CTOR used to set default values to the Suffixes and RelativeOrder
51     * attributes. Those are read at subdeployer registration time by the MainDeployer
52     * to alter its SuffixOrder.
53     */

54    public SpringDeployer()
55    {
56       initializeMainDeployer();
57    }
58
59    protected void initializeMainDeployer()
60    {
61       setSuffixes(new String JavaDoc[]{".spring", "-spring.xml"});
62       setRelativeOrder(350); //after -ds, before ejb3
63
}
64
65    /**
66     * Returns true if this deployer can deploy the given DeploymentInfo.
67     *
68     * @return True if this deployer can deploy the given DeploymentInfo.
69     * @jmx:managed-operation
70     */

71    public boolean accepts(DeploymentInfo di)
72    {
73       String JavaDoc urlStr = di.url.toString();
74       return urlStr.endsWith(".spring") || urlStr.endsWith(".spring/") ||
75             urlStr.endsWith("-spring.xml");
76    }
77
78    /**
79     * Describe <code>init</code> method here.
80     *
81     * @param di a <code>DeploymentInfo</code> value
82     * @throws DeploymentException if an error occurs
83     * @jmx:managed-operation
84     */

85    public void init(DeploymentInfo di) throws DeploymentException
86    {
87       try
88       {
89          if (di.watch == null)
90          {
91             // resolve the watch
92
if (di.url.getProtocol().equals("file"))
93             {
94                File JavaDoc file = new File JavaDoc(di.url.getFile());
95                // If not directory we watch the package
96
if (!file.isDirectory())
97                {
98                   di.watch = di.url;
99                }
100                // If directory we watch the xml files
101
else
102                {
103                   di.watch = new URL JavaDoc(di.url, "META-INF/jboss-spring.xml");
104                }
105             }
106             else
107             {
108                // We watch the top only, no directory support
109
di.watch = di.url;
110             }
111          }
112       }
113       catch (Exception JavaDoc e)
114       {
115          log.error("failed to parse Spring context document: ", e);
116          throw new DeploymentException(e);
117       }
118       super.init(di);
119    }
120
121    /**
122     * Describe <code>create</code> method here.
123     *
124     * @param di a <code>DeploymentInfo</code> value
125     * @throws DeploymentException if an error occurs
126     * @jmx:managed-operation
127     */

128    public void create(DeploymentInfo di) throws DeploymentException
129    {
130       try
131       {
132          beanFactoryLoader.create(di);
133          emitNotification("Spring Deploy", di);
134          log.info("Deployed Spring: " + di.url);
135       }
136       catch (Exception JavaDoc e)
137       {
138          throw new DeploymentException(e);
139       }
140    }
141
142    /**
143     * The <code>start</code> method starts all the mbeans in this DeploymentInfo..
144     *
145     * @param di a <code>DeploymentInfo</code> value
146     * @throws DeploymentException if an error occurs
147     * @jmx:managed-operation
148     */

149    public void start(DeploymentInfo di) throws DeploymentException
150    {
151       beanFactoryLoader.start(di);
152    }
153
154    /**
155     * Undeploys the package at the url string specified. This will: Undeploy
156     * packages depending on this one. Stop, destroy, and unregister all the
157     * specified mbeans Unload this package and packages this package deployed
158     * via the classpath tag. Keep track of packages depending on this one that
159     * we undeployed so that they can be redeployed should this one be
160     * redeployed.
161     *
162     * @param di the <code>DeploymentInfo</code> value to stop.
163     * @jmx:managed-operation
164     */

165    public void stop(DeploymentInfo di)
166    {
167       log.info("Undeploying Spring: " + di.url);
168       try
169       {
170          beanFactoryLoader.stop(di);
171          emitNotification("Spring Undeploy", di);
172       }
173       catch (Exception JavaDoc e)
174       {
175          log.error("Failed to stop bean factory: " + di.url);
176       }
177    }
178
179    /**
180     * Describe <code>destroy</code> method here.
181     *
182     * @param di a <code>DeploymentInfo</code> value
183     * @jmx:managed-operation
184     */

185    public void destroy(DeploymentInfo di)
186    {
187       try
188       {
189          beanFactoryLoader.destroy(di);
190          emitNotification("Spring Destroy", di);
191       }
192       catch (DeploymentException e)
193       {
194          log.error("Failed to destroy deployer: " + di);
195       }
196    }
197
198    protected ObjectName JavaDoc getObjectName(MBeanServer JavaDoc server, ObjectName JavaDoc name)
199          throws MalformedObjectNameException JavaDoc
200    {
201       return name == null ? getDefaultObjectName() : name;
202    }
203
204    protected abstract ObjectName JavaDoc getDefaultObjectName();
205
206 }
207
Popular Tags