KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > kernel > plugins > deployment > AbstractKernelDeployer


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.deployment;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedAction JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ListIterator JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import org.jboss.beans.metadata.spi.BeanMetaData;
33 import org.jboss.beans.metadata.spi.ClassLoaderMetaData;
34 import org.jboss.dependency.spi.ControllerContext;
35 import org.jboss.dependency.spi.ControllerMode;
36 import org.jboss.dependency.spi.ControllerState;
37 import org.jboss.dependency.spi.DependencyInfo;
38 import org.jboss.dependency.spi.DependencyItem;
39 import org.jboss.kernel.Kernel;
40 import org.jboss.kernel.plugins.dependency.AbstractKernelControllerContext;
41 import org.jboss.kernel.spi.dependency.KernelController;
42 import org.jboss.kernel.spi.dependency.KernelControllerContext;
43 import org.jboss.kernel.spi.deployment.KernelDeployment;
44 import org.jboss.logging.Logger;
45 import org.jboss.util.JBossStringBuilder;
46
47 /**
48  * An Kernel deployer.
49  *
50  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
51  * @author <a HREF="mailto:les.hazlewood@jboss.org">Les A. Hazlewood</a>
52  * @version $Revision: 57133 $
53  */

54 public class AbstractKernelDeployer
55 {
56    /** The log */
57    private static final Logger log = Logger.getLogger(AbstractKernelDeployer.class);
58    
59    /** The kernel */
60    protected Kernel kernel;
61
62    /** The controller */
63    protected KernelController controller;
64    
65    /** The required state */
66    protected ControllerState requiredState;
67    
68    /** The mode */
69    protected ControllerMode mode;
70    
71    /**
72     * Create a new kernel deployer
73     *
74     * @param kernel the kernel
75     */

76    public AbstractKernelDeployer(Kernel kernel)
77    {
78       this(kernel, null, null);
79    }
80
81    /**
82     * Create a new kernel deployer
83     *
84     * @param kernel the kernel
85     * @param requiredState the required state
86     * @param mode the controller mode
87     */

88    public AbstractKernelDeployer(final Kernel kernel, ControllerState requiredState, ControllerMode mode)
89    {
90       if (kernel == null)
91          throw new IllegalArgumentException JavaDoc("Null kernel");
92       this.kernel = kernel;
93       PrivilegedAction JavaDoc<Object JavaDoc> action = new PrivilegedAction JavaDoc<Object JavaDoc>()
94       {
95          public Object JavaDoc run()
96          {
97             controller = kernel.getController();
98             return null;
99          }
100       };
101       AccessController.doPrivileged(action);
102       this.requiredState = requiredState;
103       this.mode = mode;
104    }
105
106    /**
107     * Deploy a deployment
108     *
109     * @param deployment the deployment
110     * @throws Throwable for any error
111     */

112    public void deploy(final KernelDeployment deployment) throws Throwable JavaDoc
113    {
114       if (deployment.isInstalled())
115          throw new IllegalArgumentException JavaDoc("Already installed " + deployment.getName());
116
117       try
118       {
119          deployBeans(controller, deployment);
120          deployment.setInstalled(true);
121       }
122       catch (Throwable JavaDoc t)
123       {
124          undeploy(deployment);
125          throw t;
126       }
127    }
128
129    /**
130     * Change a deployments state
131     *
132     * @param deployment the deployment
133     * @param state the state
134     * @throws Throwable for any error
135     */

136    public void change(KernelDeployment deployment, ControllerState state) throws Throwable JavaDoc
137    {
138       if (deployment.isInstalled() == false)
139          throw new IllegalStateException JavaDoc("Not installed " + deployment.getName());
140
141       changeBeans(controller, deployment, state);
142    }
143    
144    /**
145     * Undeploy a deployment
146     *
147     * @param deployment the deployment
148     */

149    public void undeploy(KernelDeployment deployment)
150    {
151       undeployBeans(controller, deployment);
152       deployment.setInstalled(false);
153    }
154    
155    /**
156     * Validate all deployments
157     *
158     * @throws Throwable for any error
159     */

160    public void validate() throws Throwable JavaDoc
161    {
162       Set JavaDoc notInstalled = controller.getNotInstalled();
163       internalValidate(notInstalled);
164    }
165    
166    /**
167     * Validate a deployment
168     *
169     * @param deployment the deployment
170     * @throws Throwable for any error
171     */

172    public void validate(KernelDeployment deployment) throws Throwable JavaDoc
173    {
174       Set JavaDoc<KernelControllerContext> notInstalled = new HashSet JavaDoc<KernelControllerContext>(deployment.getInstalledContexts());
175       internalValidate(notInstalled);
176    }
177    
178    /**
179     * Validate a deployment
180     *
181     * @param notInstalled the not installed deployments
182     * @throws Throwable for any error
183     */

184    protected void internalValidate(Set JavaDoc notInstalled) throws Throwable JavaDoc
185    {
186       if (notInstalled.isEmpty() == false)
187       {
188          for (Iterator JavaDoc i = notInstalled.iterator(); i.hasNext();)
189          {
190             KernelControllerContext context = (KernelControllerContext) i.next();
191             if (context.getState().equals(context.getRequiredState()))
192                i.remove();
193          }
194          if (notInstalled.isEmpty() == false)
195          {
196             HashSet JavaDoc<KernelControllerContext> errors = new HashSet JavaDoc<KernelControllerContext>();
197             HashSet JavaDoc<KernelControllerContext> incomplete = new HashSet JavaDoc<KernelControllerContext>();
198             for (Iterator JavaDoc i = notInstalled.iterator(); i.hasNext();)
199             {
200                KernelControllerContext ctx = (KernelControllerContext) i.next();
201                if (ctx.getState().equals(ControllerState.ERROR))
202                   errors.add(ctx);
203                else
204                   incomplete.add(ctx);
205             }
206             JBossStringBuilder buffer = new JBossStringBuilder();
207             buffer.append("Incompletely deployed:\n");
208             if (errors.size() != 0)
209             {
210                buffer.append("\n*** DEPLOYMENTS IN ERROR: Name -> Error\n");
211                for (Iterator JavaDoc i = errors.iterator(); i.hasNext(); )
212                {
213                   KernelControllerContext ctx = (KernelControllerContext) i.next();
214                   buffer.append(ctx.getName()).append(" -> ").append(ctx.getError().toString()).append('\n');
215                }
216             }
217             if (incomplete.size() != 0)
218             {
219                buffer.append("\n*** DEPLOYMENTS MISSING DEPENDENCIES: Name -> Dependency{Required State:Actual State}\n");
220                for (Iterator JavaDoc i = incomplete.iterator(); i.hasNext();)
221                {
222                   KernelControllerContext ctx = (KernelControllerContext) i.next();
223                   buffer.append(ctx.getName()).append(" -> ");
224                   DependencyInfo dependsInfo = ctx.getDependencyInfo();
225                   Set JavaDoc depends = dependsInfo.getIDependOn(null);
226                   for (Iterator JavaDoc j = depends.iterator(); j.hasNext();)
227                   {
228                      DependencyItem item = (DependencyItem) j.next();
229                      buffer.append(item.getIDependOn()).append('{').append(item.getWhenRequired().getStateString());
230                      buffer.append(':');
231                      Object JavaDoc iDependOn = item.getIDependOn();
232                      if (iDependOn == null)
233                      {
234                         // FIXME needs to print something better than item.toString()
235
buffer.append("** UNRESOLVED " + item + " **");
236                      }
237                      else
238                      {
239                         ControllerContext other = controller.getContext(item.getIDependOn(), null);
240                         if (other == null)
241                            buffer.append("** NOT FOUND **");
242                         else
243                            buffer.append(other.getState().getStateString());
244                      }
245                      buffer.append('}');
246                      if (j.hasNext())
247                         buffer.append(", ");
248                   }
249                   buffer.append('\n');
250                }
251             }
252             throw new IllegalStateException JavaDoc(buffer.toString());
253          }
254       }
255    }
256
257    /**
258     * Deploy the beans in a deployment
259     *
260     * @param controller the controller
261     * @param deployment the deployment
262     * @throws Throwable for any error
263     */

264    protected void deployBeans(KernelController controller, KernelDeployment deployment) throws Throwable JavaDoc
265    {
266       List JavaDoc beans = deployment.getBeans();
267       if (beans != null)
268       {
269          for (int i = 0; i < beans.size(); ++i)
270          {
271             BeanMetaData metaData = (BeanMetaData) beans.get(i);
272             KernelControllerContext context = deployBean(controller, deployment, metaData);
273             deployment.addInstalledContext(context);
274          }
275       }
276    }
277
278    /**
279     * Deploy a bean
280     *
281     * @param controller the controller
282     * @param deployment the deployment
283     * @param bean the bean metadata
284     * @return the KernelControllerContext
285     * @throws Throwable for any error
286     */

287    protected KernelControllerContext deployBean(KernelController controller, KernelDeployment deployment, BeanMetaData bean) throws Throwable JavaDoc
288    {
289       KernelControllerContext context = new AbstractKernelControllerContext(null, bean, null);
290       if (requiredState != null)
291          context.setRequiredState(requiredState);
292       if (mode != null)
293          context.setMode(mode);
294       // Use any deployment classloader if present and the bean doesn't have one
295
ClassLoaderMetaData beanClassLoader = bean.getClassLoader();
296       if (beanClassLoader == null && deployment != null)
297       {
298          ClassLoaderMetaData deploymentClassLoader = deployment.getClassLoader();
299          if (deploymentClassLoader != null)
300             bean.setClassLoader(deploymentClassLoader);
301       }
302       controller.install(context);
303       return context;
304    }
305
306    /**
307     * Change the beans in a deployment
308     *
309     * @param controller the controller
310     * @param deployment the deployment
311     * @param state the state
312     * @throws Throwable for any error
313     */

314    protected void changeBeans(KernelController controller, KernelDeployment deployment, ControllerState state) throws Throwable JavaDoc
315    {
316       List JavaDoc contexts = deployment.getInstalledContexts();
317       if (contexts != null)
318       {
319          for (Iterator JavaDoc i = contexts.iterator(); i.hasNext();)
320          {
321             KernelControllerContext context = (KernelControllerContext) i.next();
322             changeBean(controller, context, state);
323          }
324       }
325    }
326
327    /**
328     * Change a bean
329     *
330     * @param controller the controller
331     * @param context the context
332     * @param state the state
333     * @throws Throwable for any error
334     */

335    protected void changeBean(KernelController controller, KernelControllerContext context, ControllerState state) throws Throwable JavaDoc
336    {
337       if (ControllerMode.MANUAL.equals(context.getMode()) && ControllerState.ERROR.equals(context.getState()) == false)
338          controller.change(context, state);
339    }
340
341    /**
342     * Undeploy the beans in a deployment
343     *
344     * @param controller the controller
345     * @param deployment the deployment
346     */

347    protected void undeployBeans(KernelController controller, KernelDeployment deployment)
348    {
349       List JavaDoc contexts = deployment.getInstalledContexts();
350       if (contexts.isEmpty() == false)
351       {
352          for (ListIterator JavaDoc i = contexts.listIterator(contexts.size()); i.hasPrevious();)
353          {
354             KernelControllerContext context = (KernelControllerContext) i.previous();
355             try
356             {
357                undeployBean(controller, context);
358                deployment.removeInstalledContext(context);
359             }
360             catch (Throwable JavaDoc ignored)
361             {
362                log.warn("Ignored error during uninstall of " + context, ignored);
363             }
364          }
365       }
366    }
367
368    /**
369     * Undeploy a bean
370     *
371     * @param controller the controller
372     * @param context the context of the bean
373     * @throws Throwable for any error
374     */

375    protected void undeployBean(KernelController controller, KernelControllerContext context) throws Throwable JavaDoc
376    {
377       controller.uninstall(context.getName());
378    }
379 }
380
Popular Tags