KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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 java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.jboss.beans.metadata.plugins.AbstractBeanMetaData;
29 import org.jboss.beans.metadata.plugins.AbstractDependencyValueMetaData;
30 import org.jboss.beans.metadata.plugins.AbstractInstallMetaData;
31 import org.jboss.beans.metadata.plugins.AbstractParameterMetaData;
32 import org.jboss.beans.metadata.plugins.AbstractPropertyMetaData;
33 import org.jboss.beans.metadata.plugins.ThisValueMetaData;
34 import org.jboss.beans.metadata.plugins.factory.GenericBeanFactoryMetaData;
35 import org.jboss.beans.metadata.spi.BeanMetaData;
36 import org.jboss.beans.metadata.spi.BeanMetaDataFactory;
37 import org.jboss.beans.metadata.spi.InstallMetaData;
38 import org.jboss.beans.metadata.spi.MetaDataVisitorNode;
39 import org.jboss.beans.metadata.spi.ParameterMetaData;
40 import org.jboss.beans.metadata.spi.PropertyMetaData;
41
42 /**
43  * AspectBeanMetaDataFactory.
44  *
45  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
46  * @version $Revision: 58260 $
47  */

48 public class AspectBeanMetaDataFactory extends GenericBeanFactoryMetaData implements BeanMetaDataFactory
49 {
50    /** Unless specified use the bean with this name as the aspect manager */
51    final static String JavaDoc DEFAULT_ASPECT_MANAGER = "AspectManager";
52    
53    /** The pointcut */
54    private String JavaDoc pointcut;
55    
56    /** The bean name of the aspect manager to use */
57    private String JavaDoc managerBean = DEFAULT_ASPECT_MANAGER;
58    
59    /** The property of the aspect manager bean, if any, containing the aspect manager */
60    private String JavaDoc managerProperty;
61    
62    /**
63     * Get the pointcut.
64     *
65     * @return the pointcut.
66     */

67    public String JavaDoc getPointcut()
68    {
69       return pointcut;
70    }
71
72    /**
73     * Set the pointcut.
74     *
75     * @param pointcut the pointcut.
76     */

77    public void setPointcut(String JavaDoc pointcut)
78    {
79       this.pointcut = pointcut;
80    }
81
82
83    public String JavaDoc getManager()
84    {
85       return managerBean;
86    }
87
88    public void setManagerBean(String JavaDoc managerBean)
89    {
90       this.managerBean = managerBean;
91    }
92
93    public String JavaDoc getManagerProperty()
94    {
95       return managerProperty;
96    }
97
98    public void setManagerProperty(String JavaDoc aspectManagerProperty)
99    {
100       this.managerProperty = aspectManagerProperty;
101    }
102    
103    public List JavaDoc<BeanMetaData> getBeans()
104    {
105       ArrayList JavaDoc<BeanMetaData> result = new ArrayList JavaDoc<BeanMetaData>();
106
107
108       result.add(this);
109       
110       String JavaDoc aspectName = name + "$Aspect";
111       AbstractBeanMetaData aspect = new AbstractBeanMetaData();
112       aspect.setName(aspectName);
113       aspect.setBean("org.jboss.aop.microcontainer.beans.Aspect");
114       aspect.addProperty(getAspectManagerPropertyMetaData("manager"));
115       result.add(aspect);
116       
117       String JavaDoc aspectBindingName = name + "$AspectBinding";
118       AbstractBeanMetaData aspectBinding = new AbstractBeanMetaData();
119       aspectBinding.setName(aspectBindingName);
120       aspectBinding.setBean("org.jboss.aop.microcontainer.beans.AspectBinding");
121       aspectBinding.addProperty(new AbstractPropertyMetaData("pointcut", pointcut));
122       aspectBinding.addProperty(new AbstractPropertyMetaData("aspect", new AbstractDependencyValueMetaData(aspectName, "definition")));
123       aspectBinding.addProperty(getAspectManagerPropertyMetaData("manager"));
124       result.add(aspectBinding);
125       
126       if (hasInjectedBeans())
127       {
128          configureWithDependencies(aspect, aspectBinding);
129       }
130       else
131       {
132          configureNoDependencies(aspect, aspectBinding);
133       }
134       
135       
136       return result;
137    }
138    
139    protected PropertyMetaData getAspectManagerPropertyMetaData(String JavaDoc name)
140    {
141       return new AbstractPropertyMetaData(name, new AbstractDependencyValueMetaData(managerBean, managerProperty));
142    }
143    
144    private void configureWithDependencies(AbstractBeanMetaData aspect, AbstractBeanMetaData aspectBinding)
145    {
146       aspect.addProperty(new AbstractPropertyMetaData("adviceBean", name));
147       
148       AbstractInstallMetaData installAspect = new AbstractInstallMetaData();
149       installAspect.setBean(aspect.getName());
150       installAspect.setMethodName("install");
151       ArrayList JavaDoc<ParameterMetaData> parameters = new ArrayList JavaDoc<ParameterMetaData>();
152       parameters.add(new AbstractParameterMetaData(new ThisValueMetaData()));
153       installAspect.setParameters(parameters);
154       
155       AbstractInstallMetaData installBinding = new AbstractInstallMetaData();
156       installBinding.setBean(aspectBinding.getName());
157       installBinding.setMethodName("rebind");
158       parameters = new ArrayList JavaDoc<ParameterMetaData>();
159       parameters.add(new AbstractParameterMetaData(new AbstractDependencyValueMetaData(aspect.getName(), "definition")));
160       installBinding.setParameters(parameters);
161
162       List JavaDoc<InstallMetaData> installs = getInstalls();
163       if (installs == null)
164          installs = new ArrayList JavaDoc<InstallMetaData>();
165       installs.add(installAspect);
166       installs.add(installBinding);
167       setInstalls(installs);
168       
169       AbstractInstallMetaData uninstallAspect = new AbstractInstallMetaData();
170       uninstallAspect.setBean(aspect.getName());
171       uninstallAspect.setMethodName("uninstall");
172
173       List JavaDoc<InstallMetaData> uninstalls = getUninstalls();
174       if (uninstalls == null)
175          uninstalls = new ArrayList JavaDoc<InstallMetaData>();
176       uninstalls.add(uninstallAspect);
177       setUninstalls(uninstalls);
178       
179    }
180    
181    private void configureNoDependencies(AbstractBeanMetaData aspect, AbstractBeanMetaData aspectBinding)
182    {
183       aspect.addProperty(new AbstractPropertyMetaData("advice", new AbstractDependencyValueMetaData(name)));
184    }
185    
186    
187    private boolean hasInjectedBeans()
188    {
189       ArrayList JavaDoc<AbstractDependencyValueMetaData> dependencies = new ArrayList JavaDoc<AbstractDependencyValueMetaData>();
190       getDependencies(dependencies, this);
191       
192       for (AbstractDependencyValueMetaData dep : dependencies)
193       {
194          if(!((String JavaDoc)dep.getValue()).startsWith("jboss.kernel:service="))
195          {
196             return true;
197          }
198       }
199       return false;
200    }
201    
202    private void getDependencies(ArrayList JavaDoc<AbstractDependencyValueMetaData> dependencies, MetaDataVisitorNode node)
203    {
204       Iterator JavaDoc children = node.getChildren();
205       
206       if (children != null)
207       {
208          while (children.hasNext())
209          {
210             MetaDataVisitorNode child = (MetaDataVisitorNode)children.next();
211             if (child instanceof AbstractDependencyValueMetaData)
212             {
213                dependencies.add((AbstractDependencyValueMetaData)child);
214             }
215             getDependencies(dependencies, child);
216          }
217       }
218    }
219
220 }
221
Popular Tags