KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > Deployment


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;
23
24 import org.jboss.util.file.ArchiveBrowser;
25 import org.jboss.util.file.ClassFileFilter;
26
27 import java.io.File JavaDoc;
28 import java.io.FilenameFilter JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34 /**
35  * Comment
36  *
37  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
38  * @version $Revision: 37406 $
39  */

40 public class Deployment
41 {
42    public static boolean searchClasspath = true;
43
44    public static void deploy()
45    {
46       try
47       {
48
49          deployThroughClassAnnotations();
50          preconfigThroughClassPath();
51          preconfigThroughSystemProperty();
52       }
53       catch (Exception JavaDoc e)
54       {
55          e.printStackTrace();
56       }
57    }
58
59    /**
60     * This is for running standalone. AspectManager will automatically search classpath for resources
61     * of jboss-aop.xml and load them. The AspectManagerService will turn this off if you are running
62     * from within JBoss.
63     */

64    public static void preconfigThroughClassPath()
65    {
66       String JavaDoc search = System.getProperty("jboss.aop.search.classpath", null);
67       if (search != null) searchClasspath = (new Boolean JavaDoc(search)).booleanValue();
68       if (AspectManager.verbose) System.out.println("[debug] jboss.aop.search.classpath: '" + search + "' " + searchClasspath);
69       if (searchClasspath)
70       {
71          try
72          {
73             Enumeration JavaDoc en = Thread.currentThread().getContextClassLoader().getResources("META-INF/jboss-aop.xml");
74             while (en.hasMoreElements())
75             {
76                URL JavaDoc url = (URL JavaDoc) en.nextElement();
77                if (AspectManager.verbose) System.out.println("[deploying] " + url);
78                AspectXmlLoader.deployXML(url);
79             }
80          }
81          catch (Exception JavaDoc ex)
82          {
83             throw new RuntimeException JavaDoc(ex);
84          }
85       }
86    }
87
88    /**
89     * Load XML files through a system property value jboss.aop.path
90     */

91    public static void deployThroughClassAnnotations()
92    {
93       String JavaDoc path = System.getProperty("jboss.aop.class.path", null);
94       if (path == null)
95       {
96          if (AspectManager.verbose) System.out.println("[debug] jboss.aop.class.path is NULL");
97          return;
98       }
99       if (AspectManager.verbose) System.out.println("[debug] jboss.aop.class.path: " + path);
100       StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(path, File.pathSeparator);
101       while (t.hasMoreTokens())
102       {
103          String JavaDoc token = t.nextToken();
104          File JavaDoc f = new File JavaDoc(token);
105          if (!f.exists())
106          {
107             System.err.println("[error] Unable to find jboss.aop.class.path: " + f.getName());
108          }
109          try
110          {
111             URL JavaDoc url = f.toURL();
112             Iterator JavaDoc it = ArchiveBrowser.getBrowser(url, new ClassFileFilter());
113             AspectAnnotationLoader loader = new AspectAnnotationLoader(AspectManager.instance());
114             loader.deployInputStreamIterator(it);
115          }
116          catch (Exception JavaDoc ex)
117          {
118             ex.printStackTrace();
119             if (ex instanceof RuntimeException JavaDoc)
120                throw (RuntimeException JavaDoc) ex;
121             else
122                throw new RuntimeException JavaDoc("[error] failed to load aop class path: " + f.toString(), ex);
123          }
124       }
125    }
126
127    /**
128     * Load XML files through a system property value jboss.aop.path
129     */

130    public static void preconfigThroughSystemProperty()
131    {
132       String JavaDoc path = System.getProperty("jboss.aop.path", null);
133       if (AspectManager.verbose) System.out.println("[debug] jboss.aop.path: " + path);
134       if (path == null) return;
135       StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(path, File.pathSeparator);
136       int j = 0;
137       while (t.hasMoreTokens())
138       {
139          String JavaDoc token = t.nextToken();
140          if (AspectManager.verbose) System.out.println("jboss.aop.path[" + j + "]: " + token);
141          File JavaDoc f = new File JavaDoc(token);
142          try
143          {
144             if (f.isDirectory())
145             {
146                FilenameFilter JavaDoc filter = new FilenameFilter JavaDoc()
147                {
148                   public boolean accept(File JavaDoc dir, String JavaDoc name)
149                   {
150                      return name.endsWith("aop.xml");
151                   }
152                };
153                File JavaDoc[] files = f.listFiles(filter);
154                for (int i = 0; i < files.length; i++)
155                {
156                   deployXmlFile(files[i]);
157                }
158             }
159             else
160             {
161                deployXmlFile(f);
162             }
163          }
164          catch (Exception JavaDoc ex)
165          {
166             ex.printStackTrace();
167             if (ex instanceof RuntimeException JavaDoc)
168                throw (RuntimeException JavaDoc) ex;
169             else
170                throw new RuntimeException JavaDoc("[error] failed to load aop path: " + f.toString(), ex);
171          }
172       }
173    }
174
175    private static void deployXmlFile(File JavaDoc f) throws Exception JavaDoc
176    {
177       URL JavaDoc url = f.toURL();
178       if (AspectManager.verbose) System.out.println("[deploying] " + url);
179       AspectXmlLoader.deployXML(url);
180    }
181 }
182
Popular Tags