KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileNotFoundException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 import javassist.scopedpool.ScopedClassPoolFactory;
33
34 import javax.management.Attribute JavaDoc;
35 import javax.management.AttributeNotFoundException JavaDoc;
36 import javax.management.InstanceNotFoundException JavaDoc;
37 import javax.management.InvalidAttributeValueException JavaDoc;
38 import javax.management.MBeanException JavaDoc;
39 import javax.management.Notification JavaDoc;
40 import javax.management.ObjectName JavaDoc;
41 import javax.management.ReflectionException JavaDoc;
42 import org.jboss.aop.AspectManager;
43 import org.jboss.aop.AspectNotificationHandler;
44 import org.jboss.aop.AspectXmlLoader;
45 import org.jboss.aop.ClassLoaderValidation;
46 import org.jboss.aop.ClassicWeavingStrategy;
47 import org.jboss.aop.Deployment;
48 import org.jboss.aop.SuperClassesFirstWeavingStrategy;
49 import org.jboss.aop.hook.JDK14Transformer;
50 import org.jboss.aop.hook.JDK14TransformerManager;
51 import org.jboss.aop.instrument.InstrumentorFactory;
52 import org.jboss.aop.instrument.TransformerCommon;
53 import org.jboss.mx.server.ServerConstants;
54 import org.jboss.mx.util.ObjectNameFactory;
55 import org.jboss.system.ServiceMBeanSupport;
56 import org.jboss.system.server.ServerConfig;
57
58 /**
59  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
60  * @version $Revision: 57942 $
61  * @jmx:mbean extends="org.jboss.system.ServiceMBean"
62  */

63 public class AspectManagerService
64         extends ServiceMBeanSupport
65         implements AspectManagerServiceMBean, AspectNotificationHandler
66 {
67    
68    static {
69       //pre-load necessary classes so that we avoid NoClassDefFoundErrors on JRockit when using the RepositoryClassloader hook
70
//When AspectManager.translate() is called the first time, these classes have not been loaded yet, and this is what causes
71
//JRockit to get confused
72
TransformerCommon common = new TransformerCommon();
73       SuperClassesFirstWeavingStrategy strategy1 = new SuperClassesFirstWeavingStrategy();
74       ClassicWeavingStrategy strategy2 = new ClassicWeavingStrategy();
75       
76    }
77    
78    public static final ObjectName JavaDoc DEFAULT_LOADER_REPOSITORY = ObjectNameFactory.create(ServerConstants.DEFAULT_LOADER_NAME);
79
80    // Attributes ---------------------------------------------------
81

82    boolean created = false;
83    protected File JavaDoc tmpClassesDir;
84    protected boolean enableTransformer = false;
85    protected boolean enableLoadtimeWeaving = false;
86    protected boolean suppressTransformationErrors = true;
87    protected boolean suppressReferenceErrors = true;
88    protected String JavaDoc exclude;
89    protected String JavaDoc include;
90    protected String JavaDoc ignore;
91    private String JavaDoc baseXml = "base-aop.xml";
92
93    // Static -------------------------------------------------------
94

95    // Constructors -------------------------------------------------
96
public AspectManagerService()
97    {
98    }
99    
100    public AspectManagerService(String JavaDoc baseXml)
101    {
102       this.baseXml = baseXml;
103    }
104
105    // Public -------------------------------------------------------
106

107    protected ScopedClassPoolFactory createFactory() throws Exception JavaDoc
108    {
109       return new JBossClassPoolFactory(tmpClassesDir);
110    }
111
112    protected ClassLoaderValidation createClassLoaderValidation()
113    {
114       return new JBossClassLoaderValidator();
115    }
116
117    protected void createService()
118            throws Exception JavaDoc
119    {
120       // Set a default tmp classes dir to the jboss tmp dir/aopclasses
121
if (tmpClassesDir == null)
122       {
123          String JavaDoc jbossTmpDir = System.getProperty(ServerConfig.SERVER_TEMP_DIR);
124          if (jbossTmpDir == null)
125             jbossTmpDir = System.getProperty("java.io.tmpdir");
126          tmpClassesDir = new File JavaDoc(jbossTmpDir, "aopdynclasses");
127       }
128       // Validate the the tmp dir exists
129
if (tmpClassesDir.exists() == false && tmpClassesDir.mkdirs() == false)
130          throw new FileNotFoundException JavaDoc("Failed to create tmpClassesDir: " + tmpClassesDir.getAbsolutePath());
131       AspectManager.setClassPoolFactory(createFactory());
132       
133       AspectManager.classLoaderValidator = createClassLoaderValidation();
134       // Add the tmp classes dir to our UCL classpath
135

136       Deployment.searchClasspath = false; // turn off dynamic finding of DDs
137
AspectManager.suppressTransformationErrors = suppressTransformationErrors;
138       if (enableTransformer && enableLoadtimeWeaving) throw new RuntimeException JavaDoc("Cannot set both EnableTransformer and EnableLoadtimeWeaving");
139       if (enableTransformer)
140       {
141          attachDeprecatedTranslator();
142       }
143       if (enableLoadtimeWeaving)
144       {
145          attachTranslator();
146       }
147       created = true;
148       AspectManager.notificationHandler = this;
149       
150       AspectManager.scopedCLHelper = new JBossScopedClassLoaderHelper();
151       
152       baseAop();
153    }
154
155    protected void baseAop()
156    {
157       ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
158       URL JavaDoc base = cl.getResource(baseXml);
159       try
160       {
161          if (base != null)
162          {
163             AspectXmlLoader.deployXML(base);
164          }
165          else
166          {
167             System.out.println("Could not find " + baseXml + " file in the resources of " + cl);
168          }
169       }
170       catch (Exception JavaDoc e)
171       {
172          System.out.println("Error loading " + baseXml + " file" + e);
173       }
174    }
175
176    protected void attachDeprecatedTranslator()
177    {
178       log.warn("EnableTransformer has been deprecated, please use EnableLoadtimeWeaving. See docs for more details");
179       AspectManager mgr = AspectManager.instance();
180       try
181       {
182          server.setAttribute(DEFAULT_LOADER_REPOSITORY, new Attribute JavaDoc("Translator", mgr));
183       }
184       catch (InstanceNotFoundException JavaDoc e)
185       {
186          throw new RuntimeException JavaDoc(e);
187       }
188       catch (AttributeNotFoundException JavaDoc e)
189       {
190          throw new RuntimeException JavaDoc(e);
191       }
192       catch (InvalidAttributeValueException JavaDoc e)
193       {
194          throw new RuntimeException JavaDoc(e);
195       }
196       catch (MBeanException JavaDoc e)
197       {
198          throw new RuntimeException JavaDoc(e);
199       }
200       catch (ReflectionException JavaDoc e)
201       {
202          throw new RuntimeException JavaDoc(e);
203       }
204    }
205
206    protected void detachDeprecatedTranslator()
207    {
208       try
209       {
210          server.setAttribute(DEFAULT_LOADER_REPOSITORY, new Attribute JavaDoc("Translator", null));
211       }
212       catch (InstanceNotFoundException JavaDoc e)
213       {
214          throw new RuntimeException JavaDoc(e);
215       }
216       catch (AttributeNotFoundException JavaDoc e)
217       {
218          throw new RuntimeException JavaDoc(e);
219       }
220       catch (InvalidAttributeValueException JavaDoc e)
221       {
222          throw new RuntimeException JavaDoc(e);
223       }
224       catch (MBeanException JavaDoc e)
225       {
226          throw new RuntimeException JavaDoc(e);
227       }
228       catch (ReflectionException JavaDoc e)
229       {
230          throw new RuntimeException JavaDoc(e);
231       }
232    }
233
234    protected void attachTranslator()
235    {
236       JDK14TransformerManager.transformer = new JDK14Transformer()
237       {
238          public byte[] transform(ClassLoader JavaDoc loader, String JavaDoc classname, byte[] classBytes)
239          {
240             try
241             {
242                //Make sure that we use the correct classloader, in order to get the correct domain if it is a scoped loader
243
return AspectManager.instance(loader).translate(classname, loader, classBytes);
244             }
245             catch (Exception JavaDoc e)
246             {
247                throw new RuntimeException JavaDoc(e);
248             }
249          }
250       };
251    }
252
253    protected void detachTranslator()
254    {
255       JDK14TransformerManager.transformer = null;
256    }
257
258    public void attachClass(String JavaDoc classname)
259    {
260       Notification JavaDoc msg = new Notification JavaDoc("AOP class attached", this, getNextNotificationSequenceNumber());
261       msg.setUserData(classname);
262       sendNotification(msg);
263    }
264
265    protected void startService()
266            throws Exception JavaDoc
267    {
268    }
269
270    protected void stopService()
271    {
272    }
273
274    public boolean getPrune()
275    {
276       return AspectManager.getPrune();
277    }
278
279    public void setPrune(boolean prune)
280    {
281       AspectManager.setPrune(prune);
282    }
283
284    public String JavaDoc getExclude()
285    {
286       return exclude;
287    }
288
289    public void setExclude(String JavaDoc exclude)
290    {
291       this.exclude = exclude;
292       ArrayList JavaDoc list = new ArrayList JavaDoc();
293       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(exclude, ",");
294       while (tokenizer.hasMoreTokens())
295       {
296          list.add(tokenizer.nextToken().trim());
297       }
298       AspectManager.instance().setExclude(list);
299    }
300
301    public String JavaDoc getInclude()
302    {
303       return include;
304    }
305
306    public void setInclude(String JavaDoc include)
307    {
308       this.include = include;
309       ArrayList JavaDoc list = new ArrayList JavaDoc();
310       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(include, ",");
311       while (tokenizer.hasMoreTokens())
312       {
313          list.add(tokenizer.nextToken().trim());
314       }
315       AspectManager.instance().setInclude(list);
316    }
317
318    public String JavaDoc getIgnore()
319    {
320       return ignore;
321    }
322
323    public void setIgnore(String JavaDoc ignore)
324    {
325       this.ignore = ignore;
326       ArrayList JavaDoc list = new ArrayList JavaDoc();
327       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(ignore, ",");
328       while (tokenizer.hasMoreTokens())
329       {
330          list.add(tokenizer.nextToken().trim());
331       }
332       AspectManager.instance().setIgnore(list);
333    }
334
335    
336    /**
337     * The temporary directory to which dyn class files are written
338     *
339     * @jmx:managed-attribute
340     */

341    public File JavaDoc getTmpClassesDir()
342    {
343       return tmpClassesDir;
344    }
345
346    /**
347     * The temporary directory to which dyn class files are written
348     *
349     * @jmx:managed-attribute
350     */

351    public void setTmpClassesDir(File JavaDoc tmpClassesDir)
352    {
353       this.tmpClassesDir = tmpClassesDir;
354    }
355
356    /**
357     * Set the verbosity of aop logging. It doesn't use log4j
358     *
359     * @jmx:managed-attribute
360     */

361    public boolean getVerbose()
362    {
363       return AspectManager.verbose;
364    }
365
366    /**
367     * Set the verbosity of aop logging. It doesn't use log4j
368     *
369     * @jmx:managed-attribute
370     */

371    public void setVerbose(boolean verbose)
372    {
373       AspectManager.verbose = verbose;
374    }
375
376    /**
377     * Use aop optimizations. Optional just in case there is a bug
378     *
379     * @jmx:managed-attribute
380     */

381    public boolean getOptimized()
382    {
383       return AspectManager.optimize;
384    }
385
386    /**
387     * Use aop optimizations. Optional just in case there is a bug
388     *
389     * @jmx:managed-attribute
390     */

391    public void setOptimized(boolean verbose)
392    {
393       AspectManager.optimize = verbose;
394    }
395
396    /**
397     * @jmx:managed-attribute
398     */

399    public boolean getSuppressTransformationErrors()
400    {
401       return suppressTransformationErrors;
402    }
403
404    /**
405     * @jmx:managed-attribute
406     */

407    public void setSuppressTransformationErrors(boolean suppressTransformationErrors)
408    {
409       this.suppressTransformationErrors = suppressTransformationErrors;
410       AspectManager.suppressTransformationErrors = suppressTransformationErrors;
411    }
412
413    /**
414     * @jmx:managed-attribute
415     */

416    public boolean getSuppressReferenceErrors()
417    {
418       return suppressReferenceErrors;
419    }
420
421    /**
422     * @jmx:managed-attribute
423     */

424    public void setSuppressReferenceErrors(boolean suppressReferenceErrors)
425    {
426       this.suppressReferenceErrors = suppressReferenceErrors;
427       AspectManager.suppressReferenceErrors = suppressReferenceErrors;
428    }
429
430    /**
431     * The temporary directory to which dyn class files are written
432     *
433     * @jmx:managed-attribute
434     */

435    public boolean getEnableTransformer()
436    {
437       return enableTransformer;
438    }
439
440    /**
441     * The temporary directory to which dyn class files are written
442     *
443     * @jmx:managed-operation
444     */

445    public String JavaDoc interceptorFactories()
446    {
447       Map JavaDoc factories = AspectManager.instance().getInterceptorFactories();
448       Iterator JavaDoc it = factories.keySet().iterator();
449       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("");
450       while (it.hasNext())
451       {
452          buffer.append(it.next() + "<br>");
453       }
454       return buffer.toString();
455    }
456
457    /**
458     * The temporary directory to which dyn class files are written
459     *
460     * @jmx:managed-operation
461     */

462    public String JavaDoc aspectDefinitions()
463    {
464       Map JavaDoc factories = AspectManager.instance().getAspectDefinitions();
465       Iterator JavaDoc it = factories.keySet().iterator();
466       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("");
467       while (it.hasNext())
468       {
469          buffer.append(it.next() + "<br>");
470       }
471       return buffer.toString();
472    }
473
474    /**
475     * @jmx:managed-operation
476     */

477    public String JavaDoc introductions()
478    {
479       Map JavaDoc factories = AspectManager.instance().getInterfaceIntroductions();
480       Iterator JavaDoc it = factories.keySet().iterator();
481       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("");
482       while (it.hasNext())
483       {
484          buffer.append(it.next() + "<br>");
485       }
486       return buffer.toString();
487    }
488
489    /**
490     * The temporary directory to which dyn class files are written
491     *
492     * @jmx:managed-operation
493     */

494    public String JavaDoc stacks()
495    {
496       Map JavaDoc factories = AspectManager.instance().getInterceptorStacks();
497       Iterator JavaDoc it = factories.keySet().iterator();
498       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("");
499       while (it.hasNext())
500       {
501          buffer.append(it.next() + "<br>");
502       }
503       return buffer.toString();
504    }
505
506    /**
507     * The temporary directory to which dyn class files are written
508     *
509     * @jmx:managed-operation
510     */

511    public String JavaDoc bindings()
512    {
513       Map JavaDoc factories = AspectManager.instance().getBindings();
514       Iterator JavaDoc it = factories.keySet().iterator();
515       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("");
516       while (it.hasNext())
517       {
518          buffer.append(it.next() + "<br>");
519       }
520       return buffer.toString();
521    }
522
523    /**
524     * The temporary directory to which dyn class files are written
525     *
526     * @jmx:managed-operation
527     */

528    public String JavaDoc registeredClassLoaders()
529    {
530       Map JavaDoc factories = AspectManager.instance().getRegisteredCLs();
531       Iterator JavaDoc it = factories.keySet().iterator();
532       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("");
533       while (it.hasNext())
534       {
535          buffer.append(it.next() + "<br>");
536       }
537       return buffer.toString();
538    }
539
540    /**
541     * The temporary directory to which dyn class files are written
542     *
543     * @jmx:managed-attribute
544     */

545    public void setEnableTransformer(boolean enableTransformer)
546    {
547       // Testsuite uses enableTransformer, we may be testing new loadtime features though.
548

549       if (enableLoadtimeWeaving)
550       {
551          log.warn("enabledLoadtimeWeaving alread set");
552          return;
553       }
554       if (this.enableTransformer == enableTransformer) return;
555       if (this.getState() == STARTED)
556       {
557          if (enableTransformer)
558          {
559             attachDeprecatedTranslator();
560          }
561          else
562          {
563             detachDeprecatedTranslator();
564          }
565       }
566       this.enableTransformer = enableTransformer;
567    }
568
569    public boolean getEnableLoadtimeWeaving()
570    {
571       return enableLoadtimeWeaving;
572    }
573
574    /**
575     * The temporary directory to which dyn class files are written
576     *
577     * @jmx:managed-attribute
578     */

579    public void setEnableLoadtimeWeaving(boolean enableTransformer)
580    {
581       if (this.enableLoadtimeWeaving == enableTransformer) return;
582       if (this.getState() == STARTED)
583       {
584          if (enableTransformer)
585          {
586             attachTranslator();
587          }
588          else
589          {
590             detachTranslator();
591          }
592       }
593       this.enableLoadtimeWeaving = enableTransformer;
594    }
595    
596    public String JavaDoc getInstrumentor()
597    {
598       return InstrumentorFactory.getInstrumentorName();
599    }
600    
601    public void setInstrumentor(String JavaDoc instrumentor)
602    {
603       InstrumentorFactory.initialise(instrumentor);
604    }
605
606 }
607
Popular Tags