KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > kernel > plugins > dependency > DescribeAction


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.kernel.plugins.dependency;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.jboss.beans.info.spi.BeanInfo;
29 import org.jboss.beans.metadata.spi.AnnotationMetaData;
30 import org.jboss.beans.metadata.spi.BeanMetaData;
31 import org.jboss.beans.metadata.spi.PropertyMetaData;
32 import org.jboss.dependency.plugins.AbstractDependencyItem;
33 import org.jboss.dependency.spi.ControllerContext;
34 import org.jboss.dependency.spi.ControllerState;
35 import org.jboss.dependency.spi.DependencyInfo;
36 import org.jboss.kernel.Kernel;
37 import org.jboss.kernel.spi.config.KernelConfigurator;
38 import org.jboss.kernel.spi.dependency.KernelController;
39 import org.jboss.kernel.spi.dependency.KernelControllerContext;
40 import org.jboss.kernel.spi.metadata.MutableMetaDataContext;
41 import org.jboss.metadata.spi.repository.MetaDataRepository;
42 import org.jboss.repository.spi.KernelRepository;
43 import org.jboss.repository.spi.MetaDataContext;
44 import org.jboss.repository.spi.MetaDataContextFactory;
45
46 /**
47  * DescribeAction.
48  *
49  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
50  * @version $Revision: 58238 $
51  */

52 public class DescribeAction extends KernelControllerContextAction
53 {
54    public void installAction(KernelControllerContext context) throws Throwable JavaDoc
55    {
56       KernelController controller = (KernelController) context.getController();
57       Kernel kernel = controller.getKernel();
58       KernelConfigurator configurator = kernel.getConfigurator();
59
60       BeanMetaData metaData = context.getBeanMetaData();
61       if (metaData.getBean() != null)
62       {
63          BeanInfo info = configurator.getBeanInfo(metaData);
64          context.setBeanInfo(info);
65
66          info = addAnnotations(context, metaData, info);
67
68          DependencyInfo depends = context.getDependencyInfo();
69          // add custom dependencies (e.g. AOP layer).
70
List JavaDoc<Object JavaDoc> dependencies = info.getDependencies();
71          if (dependencies != null)
72          {
73             for (Object JavaDoc dependencyName : dependencies)
74             {
75                AbstractDependencyItem dependency = new AbstractDependencyItem(metaData.getName(), dependencyName, ControllerState.INSTANTIATED, ControllerState.INSTALLED);
76                depends.addIDependOn(dependency);
77             }
78          }
79       }
80    }
81
82    public void uninstallAction(KernelControllerContext context)
83    {
84       context.setMetaDataContext(null);
85       context.setBeanInfo(null);
86    }
87
88    /**
89     * Adds annotations to the bean. If annotations are added, returns the bean info for the instance
90     * @return The class bean info if no annotations exist or the instance bean info if annotations exist
91     */

92    private BeanInfo addAnnotations(KernelControllerContext context, BeanMetaData beanMetaData, BeanInfo beanInfo)
93    {
94       MutableMetaDataContext metaCtx = addClassAnnotations(context, beanMetaData, beanInfo);
95       addPropertyAnnotations(metaCtx, context, beanMetaData, beanInfo);
96       return context.getBeanInfo();
97    }
98
99    private MutableMetaDataContext addClassAnnotations(KernelControllerContext context, BeanMetaData beanMetaData, BeanInfo beanInfo)
100    {
101       Set JavaDoc<AnnotationMetaData> annotations = beanMetaData.getAnnotations();
102
103       MutableMetaDataContext metaCtx = null;
104
105       if (annotations != null && annotations.size() > 0)
106       {
107          metaCtx = getMetaDataContext(context);
108          if (metaCtx != null)
109          {
110             metaCtx.addAnnotations(annotations);
111          }
112       }
113
114       return metaCtx;
115    }
116
117    private MutableMetaDataContext addPropertyAnnotations(MutableMetaDataContext metaCtx, KernelControllerContext context, BeanMetaData beanMetaData, BeanInfo beanInfo)
118    {
119       Set JavaDoc properties = beanMetaData.getProperties();
120
121       if (properties != null && properties.size() > 0)
122       {
123          for (Iterator JavaDoc it = properties.iterator() ; it.hasNext() ; )
124          {
125             PropertyMetaData property = (PropertyMetaData)it.next();
126             Set JavaDoc propertyAnnotations = property.getAnnotations();
127             if (propertyAnnotations != null && propertyAnnotations.size() > 0)
128             {
129                if (metaCtx == null)
130                {
131                   metaCtx = getMetaDataContext(context);
132                }
133                if (metaCtx != null)
134                {
135                   Set JavaDoc propertyInfos = beanInfo.getProperties();
136                   if (propertyInfos != null && propertyInfos.size() > 0)
137                   {
138                      metaCtx.addPropertyAnnotations(property.getName(), beanInfo.getProperties(), propertyAnnotations);
139                   }
140                }
141             }
142          }
143       }
144
145       return metaCtx;
146    }
147
148    private MutableMetaDataContext getMetaDataContext(KernelControllerContext context)
149    {
150       //TODO: Hardcoding this doesn't feel right...
151
ControllerContext repCtx = context.getController().getContext("Repository", ControllerState.INSTALLED);
152
153       if (repCtx == null)
154       {
155          log.warn("You have defined annotations for bean '" + context.getName() + "', but no MetaDataRepository has been installed under the name 'Repository'");
156          return null;
157       }
158
159       MetaDataRepository repository = (MetaDataRepository)repCtx.getTarget();
160       MetaDataContextFactory metaFactory = context.getBeanInfo().getMetaDataContextFactory();
161       ClassLoader JavaDoc beanLoader = context.getBeanInfo().getClassInfo().getType().getClassLoader();
162       MetaDataContext metaCtx = metaFactory.getMetaDataContext(beanLoader, repository, (String JavaDoc)context.getName());
163       
164       if (metaCtx instanceof MutableMetaDataContext == false)
165       {
166          throw new RuntimeException JavaDoc("MetaDataContext must be mutable");
167       }
168          
169       context.setMetaDataContext(metaCtx);
170
171       return (MutableMetaDataContext)metaCtx;
172    }
173
174 }
Popular Tags