KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import org.jboss.beans.info.spi.BeanInfo;
28 import org.jboss.beans.metadata.spi.BeanMetaData;
29 import org.jboss.beans.metadata.spi.ParameterMetaData;
30 import org.jboss.joinpoint.spi.JoinpointException;
31 import org.jboss.joinpoint.spi.MethodJoinpoint;
32 import org.jboss.kernel.Kernel;
33 import org.jboss.kernel.plugins.config.Configurator;
34 import org.jboss.kernel.spi.config.KernelConfigurator;
35 import org.jboss.kernel.spi.dependency.KernelController;
36 import org.jboss.kernel.spi.dependency.KernelControllerContext;
37 import org.jboss.repository.spi.MetaDataContext;
38 import org.jboss.reflect.spi.AnnotationValue;
39 import org.jboss.reflect.spi.MethodInfo;
40
41 /**
42  * LifecycleAction.
43  *
44  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
45  * @version $Revision: 57859 $
46  */

47 public abstract class LifecycleAction extends KernelControllerContextAction
48 {
49    /**
50     * Create a new AbstractLifecycleAction.
51     */

52    public LifecycleAction()
53    {
54    }
55
56    /**
57     * Get the install method
58     *
59     * @param beanMetaData
60     * @return the method
61     */

62    public abstract String JavaDoc getInstallMethod(BeanMetaData beanMetaData);
63
64    /**
65     * Get install default method name
66     *
67     * @return install annotation name
68     */

69    public abstract String JavaDoc getDefaultInstallMethod();
70
71    /**
72     * Get install annotation class name
73     *
74     * @return install annotation name
75     */

76    public abstract String JavaDoc getInstallAnnotation();
77
78    /**
79     * Get the install parameters
80     *
81     * @param beanMetaData
82     * @return the parameters
83     */

84    public abstract List JavaDoc<ParameterMetaData> getInstallParameters(BeanMetaData beanMetaData);
85
86    /**
87     * Get the uninstall method
88     *
89     * @param beanMetaData
90     * @return the method
91     */

92    public abstract String JavaDoc getUninstallMethod(BeanMetaData beanMetaData);
93
94    /**
95     * Get uninstall default method name
96     *
97     * @return install annotation name
98     */

99    public abstract String JavaDoc getDefaultUninstallMethod();
100
101    /**
102     * Get uninstall annotation class name
103     *
104     * @return uninstall annotation name
105     */

106    public abstract String JavaDoc getUninstallAnnotation();
107
108    /**
109     * Get the uninstall parameters
110     *
111     * @param beanMetaData
112     * @return the parameters
113     */

114    public abstract List JavaDoc<ParameterMetaData> getUninstallParameters(BeanMetaData beanMetaData);
115
116    public void installAction(KernelControllerContext context) throws Throwable JavaDoc
117    {
118       boolean trace = log.isTraceEnabled();
119
120       KernelController controller = (KernelController) context.getController();
121       Kernel kernel = controller.getKernel();
122       KernelConfigurator configurator = kernel.getConfigurator();
123
124       Object JavaDoc target = context.getTarget();
125       BeanInfo info = context.getBeanInfo();
126       BeanMetaData metaData = context.getBeanMetaData();
127       String JavaDoc method = getInstallMethod(context);
128       List JavaDoc<ParameterMetaData> parameters = getInstallParameters(context);
129       MethodJoinpoint joinpoint;
130       try
131       {
132          ClassLoader JavaDoc cl = Configurator.getClassLoader(metaData);
133          joinpoint = configurator.getMethodJoinPoint(info, cl, method, parameters, false, true);
134       }
135       catch (JoinpointException ignored)
136       {
137          if (trace)
138          {
139             if (parameters == null)
140                log.trace("No " + method + " method for " + context);
141             else
142                log.trace("No " + method + parameters + " method for " + context);
143          }
144          return;
145       }
146       joinpoint.setTarget(target);
147       dispatchJoinPoint(context, joinpoint);
148    }
149
150    public void uninstallAction(KernelControllerContext context)
151    {
152       boolean trace = log.isTraceEnabled();
153
154       KernelController controller = (KernelController) context.getController();
155       Kernel kernel = controller.getKernel();
156       KernelConfigurator configurator = kernel.getConfigurator();
157
158       Object JavaDoc target = context.getTarget();
159       BeanInfo info = context.getBeanInfo();
160       BeanMetaData metaData = context.getBeanMetaData();
161       String JavaDoc method = getUninstallMethod(context);
162       List JavaDoc<ParameterMetaData> parameters = getUninstallParameters(context);
163       MethodJoinpoint joinpoint;
164       try
165       {
166          ClassLoader JavaDoc cl = Configurator.getClassLoader(metaData);
167          joinpoint = configurator.getMethodJoinPoint(info, cl, method, parameters, false, true);
168          joinpoint.setTarget(target);
169          dispatchJoinPoint(context, joinpoint);
170       }
171       catch (JoinpointException ignored)
172       {
173          if (trace)
174          {
175             if (parameters == null)
176                log.trace("No " + method + " method for " + context);
177             else
178                log.trace("No " + method + parameters + " method for " + context);
179          }
180       }
181       catch (Throwable JavaDoc throwable)
182       {
183          log.warn("Error during " + method, throwable);
184       }
185    }
186
187    /**
188     * Get the install method
189     *
190     * @param context
191     * @return the method
192     */

193    public String JavaDoc getInstallMethod(KernelControllerContext context)
194    {
195       String JavaDoc installMethod = getInstallMethod(context.getBeanMetaData());
196       if (installMethod != null)
197       {
198          return installMethod;
199       }
200       BeanInfo beanInfo = context.getBeanInfo();
201       Set JavaDoc<MethodInfo> methods = beanInfo.getMethods();
202       if (methods != null)
203       {
204          for (MethodInfo mi : methods)
205          {
206             if (mi.isAnnotationPresent(getInstallAnnotation()))
207             {
208                return mi.getName();
209             }
210          }
211       }
212       return getDefaultInstallMethod();
213    }
214
215    /**
216     * Get the install parameters
217     *
218     * @param context
219     * @return the parameters
220     */

221    public List JavaDoc<ParameterMetaData> getInstallParameters(KernelControllerContext context)
222    {
223       // todo some parameter support
224
return getInstallParameters(context.getBeanMetaData());
225    }
226
227    /**
228     * Get the uninstall method
229     *
230     * @param context
231     * @return the method
232     */

233    public String JavaDoc getUninstallMethod(KernelControllerContext context)
234    {
235       String JavaDoc uninstallMethod = getUninstallMethod(context.getBeanMetaData());
236       if (uninstallMethod != null)
237       {
238          return uninstallMethod;
239       }
240       BeanInfo beanInfo = context.getBeanInfo();
241       Set JavaDoc<MethodInfo> methods = beanInfo.getMethods();
242       if (methods != null)
243       {
244          for (MethodInfo mi : methods)
245          {
246             if (mi.isAnnotationPresent(getUninstallAnnotation()))
247             {
248                return mi.getName();
249             }
250          }
251       }
252       return getDefaultUninstallMethod();
253    }
254
255    /**
256     * Get the uninstall parameters
257     *
258     * @param context the context
259     * @return the parameters
260     */

261    public List JavaDoc<ParameterMetaData> getUninstallParameters(KernelControllerContext context)
262    {
263       // todo some parameter support
264
return getUninstallParameters(context.getBeanMetaData());
265    }
266
267 }
Popular Tags