KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > advice > GenericInterceptorFactory


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.advice;
23
24 import org.jboss.aop.Advisor;
25 import org.jboss.aop.joinpoint.Joinpoint;
26 import org.jboss.util.xml.XmlLoadable;
27 import org.w3c.dom.Element JavaDoc;
28
29 public class GenericInterceptorFactory implements InterceptorFactory
30 {
31    private Class JavaDoc clazz = null;
32    private String JavaDoc classname;
33    private Element JavaDoc element;
34    private boolean deployed = true;
35    private String JavaDoc name;
36
37    private static volatile int counter = 0;
38
39    public GenericInterceptorFactory(String JavaDoc classname, Element JavaDoc element)
40    {
41       this.name = classname + (counter++);
42       this.classname = classname;
43       this.element = element;
44    }
45
46    public GenericInterceptorFactory(Class JavaDoc clazz)
47    {
48       this.clazz = clazz;
49       this.classname = clazz.getName();
50    }
51
52    public String JavaDoc getName()
53    {
54       return name;
55    }
56
57    public AspectDefinition getAspect()
58    {
59       return null;
60    }
61
62    public String JavaDoc getAdvice()
63    {
64       return "invoke";
65    }
66    public void undeploy() { deployed = false; }
67
68    public boolean isDeployed()
69    {
70       return deployed;
71    }
72
73    public String JavaDoc getClassName() { return classname; }
74
75    public Interceptor create(Advisor advisor, Joinpoint joinpoint)
76    {
77       try
78       {
79          synchronized (this)
80          {
81             if (clazz == null)
82             {
83                clazz = Thread.currentThread().getContextClassLoader().loadClass(classname);
84             }
85          }
86          Interceptor interceptor = (Interceptor)clazz.newInstance();
87          if (interceptor instanceof XmlLoadable && element != null)
88          {
89             ((XmlLoadable)interceptor).importXml(element);
90          }
91          return interceptor;
92       }
93       catch (Exception JavaDoc re)
94       {
95          if (re instanceof RuntimeException JavaDoc) throw (RuntimeException JavaDoc)re;
96          throw new RuntimeException JavaDoc(re);
97       }
98    }
99
100    public boolean equals(Object JavaDoc obj)
101    {
102       if (this == obj) return true;
103       if (!(obj instanceof GenericInterceptorFactory)) return false;
104       if (!this.classname.equals(((GenericInterceptorFactory)obj).classname)) return false;
105       
106       return true;
107    }
108 }
109
110
Popular Tags