KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > deployment > AspectDeployer


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.aop.deployment;
23
24 import java.io.File JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.Notification JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32
33 import org.jboss.aop.AspectAnnotationLoader;
34 import org.jboss.aop.AspectManager;
35 import org.jboss.aop.AspectXmlLoader;
36 import org.jboss.deployment.DeploymentException;
37 import org.jboss.deployment.DeploymentInfo;
38 import org.jboss.deployment.DeploymentState;
39 import org.jboss.deployment.SubDeployer;
40 import org.jboss.deployment.SubDeployerSupport;
41 import org.jboss.util.file.ArchiveBrowser;
42 import org.jboss.util.file.ClassFileFilter;
43
44 /**
45  * Deployer for Aspects
46  *
47  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
48  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
49  * @version $Revision: 57924 $
50  * @jmx:mbean name="jboss.aspect:AspectDeployer"
51  * extends="org.jboss.deployment.SubDeployerMBean"
52  */

53 public class AspectDeployer
54 extends SubDeployerSupport
55 implements SubDeployer, AspectDeployerMBean
56 {
57
58    /**
59     * Default CTOR used to set default values to the Suffixes and RelativeOrder
60     * attributes. Those are read at subdeployer registration time by the MainDeployer
61     * to alter its SuffixOrder.
62     */

63    public AspectDeployer()
64    {
65       initializeMainDeployer();
66    }
67
68    /**
69     * Set the suffixes and relative order attributes.
70     *
71     * Those are read at subdeployer registration time by the MainDeployer
72     * to update its SuffixOrder list.
73     */

74    protected void initializeMainDeployer()
75    {
76       setSuffixes(new String JavaDoc[]{".aop", "-aop.xml"});
77       setRelativeOrder(100);
78    }
79
80    /**
81     * Returns true if this deployer can deploy the given DeploymentInfo.
82     *
83     * @return True if this deployer can deploy the given DeploymentInfo.
84     * @jmx:managed-operation
85     */

86    public boolean accepts(DeploymentInfo di)
87    {
88       String JavaDoc urlStr = di.url.toString();
89       return urlStr.endsWith(".aop") || urlStr.endsWith(".aop/") ||
90       urlStr.endsWith("-aop.xml");
91    }
92
93    /**
94     * Describe <code>init</code> method here.
95     *
96     * @param di a <code>DeploymentInfo</code> value
97     * @throws DeploymentException if an error occurs
98     * @jmx:managed-operation
99     */

100    public void init(DeploymentInfo di)
101    throws DeploymentException
102    {
103       try
104       {
105          if (di.watch == null)
106          {
107             // resolve the watch
108
if (di.url.getProtocol().equals("file"))
109             {
110                File JavaDoc file = new File JavaDoc(di.url.getFile());
111
112                // If not directory we watch the package
113
if (!file.isDirectory())
114                {
115                   di.watch = di.url;
116                }
117                // If directory we watch the xml files
118
else
119                {
120                   di.watch = new URL JavaDoc(di.url, "META-INF/jboss-aop.xml");
121                }
122             }
123             else
124             {
125                // We watch the top only, no directory support
126
di.watch = di.url;
127             }
128          }
129       }
130       catch (Exception JavaDoc e)
131       {
132          log.error("failed to parse AOP document: ", e);
133          throw new DeploymentException(e);
134       }
135       super.init(di);
136    }
137
138    /**
139     * Describe <code>create</code> method here.
140     *
141     * @param di a <code>DeploymentInfo</code> value
142     * @throws DeploymentException if an error occurs
143     * @jmx:managed-operation
144     */

145    public void create(DeploymentInfo di)
146    throws DeploymentException
147    {
148       ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
149       try
150       {
151          URL JavaDoc docURL = getDocUrl(di);
152          ClassLoader JavaDoc scl = getScopedClassLoader(di, docURL);
153
154          if (scl != null)
155          {
156             log.info("AOP deployment is scoped using classloader " + scl);
157          }
158          
159          Thread.currentThread().setContextClassLoader(di.ucl);
160          if (!di.isXML)
161          {
162             Iterator JavaDoc it = ArchiveBrowser.getBrowser(di.localUrl, new ClassFileFilter());
163             AspectManager manager = (scl != null) ? AspectManager.instance(scl) : AspectManager.instance();
164             AspectAnnotationLoader loader = new AspectAnnotationLoader(manager);
165             loader.setClassLoader(scl);
166             loader.deployInputStreamIterator(it);
167          }
168          
169          AspectXmlLoader.deployXML(docURL, scl);
170          Notification JavaDoc msg = new Notification JavaDoc("AOP Deploy", this, getNextNotificationSequenceNumber());
171          sendNotification(msg);
172          log.debug("Deployed AOP: " + di.url);
173       }
174       catch (Exception JavaDoc ex)
175       {
176          ex.printStackTrace();
177          throw new DeploymentException(ex);
178       }
179       finally
180       {
181          Thread.currentThread().setContextClassLoader(old);
182       }
183    }
184
185    /**
186     * The <code>start</code> method starts all the mbeans in this DeploymentInfo..
187     *
188     * @param di a <code>DeploymentInfo</code> value
189     * @throws DeploymentException if an error occurs
190     * @jmx:managed-operation
191     */

192    public void start(DeploymentInfo di) throws DeploymentException
193    {
194    }
195
196    /**
197     * Undeploys the package at the url string specified. This will: Undeploy
198     * packages depending on this one. Stop, destroy, and unregister all the
199     * specified mbeans Unload this package and packages this package deployed
200     * via the classpath tag. Keep track of packages depending on this one that
201     * we undeployed so that they can be redeployed should this one be
202     * redeployed.
203     *
204     * @param di the <code>DeploymentInfo</code> value to stop.
205     * @jmx:managed-operation
206     */

207    public void stop(DeploymentInfo di)
208    //throws DeploymentException
209
{
210       // Can happen on shutdown with a nested .aop module
211
// which is first stopped then undeployed.
212
if (di.state != DeploymentState.STARTED)
213       {
214          log.debug("Ignoring request to stop '" + di.url + "', current state: " + di.state);
215          return;
216       }
217       
218       log.debug("undeploying document " + di.url);
219       ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
220       try
221       {
222          Thread.currentThread().setContextClassLoader(di.ucl);
223          if (!di.isXML)
224          {
225             Iterator JavaDoc it = ArchiveBrowser.getBrowser(di.localUrl, new ClassFileFilter());
226             AspectAnnotationLoader loader = new AspectAnnotationLoader(AspectManager.instance());
227             loader.undeployInputStreamIterator(it);
228
229          }
230
231          URL JavaDoc docURL = getDocUrl(di);
232          //long start = System.currentTimeMillis();
233
AspectXmlLoader.undeployXML(docURL);
234          AspectManager.instance().unregisterClassLoader(di.ucl);
235
236          /*
237          System.out.println("************************");
238          System.out.println("undeploy took: " + (System.currentTimeMillis() - start));
239          System.out.println("************************");
240          */

241          Notification JavaDoc msg = new Notification JavaDoc("AOP Undeploy", this, getNextNotificationSequenceNumber());
242          sendNotification(msg);
243       }
244       catch (Exception JavaDoc ex)
245       {
246          log.error("failed to stop", ex);
247       }
248       finally
249       {
250          Thread.currentThread().setContextClassLoader(old);
251       }
252    }
253
254    /**
255     * Describe <code>destroy</code> method here.
256     *
257     * @param di a <code>DeploymentInfo</code> value
258     * @jmx:managed-operation
259     */

260    public void destroy(DeploymentInfo di)
261    //throws DeploymentException
262
{
263    }
264
265    /**
266     * The startService method gets the mbeanProxies for MainDeployer
267     * and ServiceController, used elsewhere.
268     *
269     * @throws Exception if an error occurs
270     */

271    protected void startService() throws Exception JavaDoc
272    {
273       super.startService();
274    }
275
276    protected ObjectName JavaDoc getObjectName(MBeanServer JavaDoc server, ObjectName JavaDoc name)
277    throws MalformedObjectNameException JavaDoc
278    {
279       return name == null ? OBJECT_NAME : name;
280    }
281
282    private URL JavaDoc getDocUrl(DeploymentInfo di) throws DeploymentException
283    {
284       URL JavaDoc docURL = di.localUrl;
285       if (di.isXML == false)
286          docURL = di.localCl.findResource("META-INF/jboss-aop.xml");
287       // Validate that the descriptor was found
288
if (docURL == null)
289          throw new DeploymentException("Failed to find META-INF/jboss-aop.xml");
290       return docURL;
291    }
292    
293    private ClassLoader JavaDoc getScopedClassLoader(DeploymentInfo di, URL JavaDoc docUrl)
294    {
295       //Scoped AOP deployments are only available when deployed as part of a scoped sar, ear etc.
296
//It can contain an aop.xml file, or it can be part of a .aop file
297
//Linking a standalone -aop.xml file onto a scoped deployment is not possible at the moment
298
if (JBossScopedClassLoaderHelper.isScopedClassLoader(di.ucl))
299       {
300          return di.ucl;
301       }
302       
303       return null;
304    }
305 }
Popular Tags