KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > microcontainer > beans > Aspect


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.microcontainer.beans;
23
24 import org.jboss.aop.AspectManager;
25 import org.jboss.aop.advice.AspectDefinition;
26 import org.jboss.aop.advice.GenericAspectFactory;
27 import org.jboss.aop.advice.Scope;
28 import org.jboss.aop.advice.ScopeUtil;
29 import org.jboss.aop.instrument.Untransformable;
30 import org.jboss.beans.metadata.plugins.factory.GenericBeanFactory;
31 import org.jboss.dependency.spi.Controller;
32 import org.jboss.kernel.Kernel;
33 import org.jboss.kernel.spi.dependency.KernelControllerContext;
34 import org.jboss.kernel.spi.dependency.KernelControllerContextAware;
35 import org.jboss.logging.Logger;
36 import org.jboss.util.id.GUID;
37
38 /**
39  * An Aspect.
40  *
41  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
42  * @version $Revision: 57321 $
43  */

44 public class Aspect implements KernelControllerContextAware, Untransformable
45 {
46    private static final Logger log = Logger.getLogger(Aspect.class);
47    
48    protected AspectManager manager;
49    
50    protected String JavaDoc adviceName = GUID.asString();
51    
52    protected String JavaDoc adviceBean;
53    
54    protected String JavaDoc scope;
55    
56    protected ManagedAspectDefinition definition;
57    
58    protected GenericBeanFactory advice;
59
60    protected Controller controller;
61
62    /** The name of this bean */
63    protected String JavaDoc myname;
64    
65    /** The name of the AspectDefinition. If not lazy, use myname. If lazy, use adviceBean */
66    protected String JavaDoc aspectDefName;
67    
68    protected Kernel kernel;
69    
70    /**
71     * Get the adviceName.
72     *
73     * @return the adviceName.
74     */

75    public String JavaDoc getAdviceName()
76    {
77       return adviceName;
78    }
79
80    /**
81     * Set the adviceName.
82     *
83     * @param adviceName The adviceName to set.
84     */

85    public void setAdviceName(String JavaDoc adviceName)
86    {
87       this.adviceName = adviceName;
88    }
89
90    /**
91     * Get the adviceBean.
92     *
93     * @return the adviceBean.
94     */

95    public String JavaDoc getAdviceBean()
96    {
97       return adviceBean;
98    }
99
100    /**
101     * Set the adviceBean.
102     *
103     * @param adviceBean the adviceBean.
104     */

105    public void setAdviceBean(String JavaDoc adviceBean)
106    {
107       this.adviceBean = adviceBean;
108    }
109
110    /**
111     * Get the definition.
112     *
113     * @return the definition.
114     */

115    public AspectDefinition getDefinition()
116    {
117       return definition;
118    }
119
120    /**
121     * Get the manager.
122     *
123     * @return the manager.
124     */

125    public AspectManager getManager()
126    {
127       return manager;
128    }
129
130    /**
131     * Set the manager.
132     *
133     * @param manager The manager to set.
134     */

135    public void setManager(AspectManager manager)
136    {
137       this.manager = manager;
138    }
139    
140    /**
141     * Get the advice.
142     *
143     * @return the advice.
144     */

145    public GenericBeanFactory getAdvice()
146    {
147       return advice;
148    }
149
150    /**
151     * Set the advice.
152     *
153     * @param advice The advice to set.
154     */

155    public void setAdvice(GenericBeanFactory advice)
156    {
157       this.advice = advice;
158    }
159
160    /**
161     * Get the scope.
162     *
163     * @return the scope.
164     */

165    public String JavaDoc getScope()
166    {
167       return scope;
168    }
169
170    /**
171     * Set the scope.
172     *
173     * @param scope The scope to set.
174     */

175    public void setScope(String JavaDoc scope)
176    {
177       this.scope = scope;
178    }
179
180    public void setKernelControllerContext(KernelControllerContext context) throws Exception JavaDoc
181    {
182       myname = (String JavaDoc)context.getName();
183       controller = context.getController();
184       kernel = context.getKernel();
185    }
186
187    public void unsetKernelControllerContext(KernelControllerContext context) throws Exception JavaDoc
188    {
189       controller = null;
190    }
191
192    public void install(GenericBeanFactory factory) throws Exception JavaDoc
193    {
194       this.advice = factory;
195       start();
196    }
197    
198    public void start() throws Exception JavaDoc
199    {
200       if (definition == null)
201       {
202          aspectDefName = (adviceBean != null) ? adviceBean : myname;
203          if (manager == null)
204             throw new IllegalArgumentException JavaDoc("Null manager");
205          Scope theScope = ScopeUtil.parse(scope);
206          if (advice != null)
207          {
208             definition = new ManagedAspectDefinition(aspectDefName, theScope, new GenericBeanAspectFactory(adviceName, advice));
209          }
210          else if (adviceBean != null && controller != null)
211          {
212             definition = new ManagedAspectDefinition(aspectDefName, theScope, new GenericBeanAspectFactory(aspectDefName, advice), false);
213          }
214          else
215          {
216             definition = new ManagedAspectDefinition(aspectDefName, theScope, new GenericAspectFactory(adviceName, null));
217          }
218          manager.addAspectDefinition(definition);
219       }
220       
221       if (adviceBean != null && advice != null)
222       {
223          definition.setDeployed(true);
224          GenericBeanAspectFactory factory = (GenericBeanAspectFactory)definition.getFactory();
225          factory.setBeanFactory(advice);
226       }
227       log.debug("Bound aspect " + aspectDefName + "; deployed:" + definition.isDeployed());
228    }
229    
230    
231    public void uninstall() throws Exception JavaDoc
232    {
233       stop();
234    }
235    
236    
237    public void stop()
238    {
239       aspectDefName = (adviceBean != null) ? adviceBean : myname;
240       log.debug("Unbinding aspect " + aspectDefName);
241       manager.removeAspectDefinition(aspectDefName);
242       if (definition != null)
243       {
244          definition.undeploy();
245          definition = null;
246       }
247    }
248 }
Popular Tags