KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > plugins > deployers > helpers > AbstractComponentDeployer


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.deployers.plugins.deployers.helpers;
23
24 import java.util.Set JavaDoc;
25
26 import org.jboss.deployers.spi.DeploymentException;
27 import org.jboss.deployers.spi.deployer.DeploymentUnit;
28
29 /**
30  * AbstractComponentDeployer.
31  *
32  * @param <D> the deployment type
33  * @param <C> the component type
34  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
35  * @version $Revision: 1.1 $
36  */

37 public abstract class AbstractComponentDeployer<D, C> extends AbstractRealDeployer<D>
38 {
39    /** The component visitor */
40    private SimpleDeploymentVisitor<C> compVisitor;
41
42    /** The component type */
43    private Class JavaDoc<C> componentType;
44
45    /**
46     * Set the default relative order to COMPONENT_DEPLOYER
47     *
48     */

49    public AbstractComponentDeployer()
50    {
51       setRelativeOrder(COMPONENT_DEPLOYER);
52    }
53
54    /**
55     * Set the component visitor
56     *
57     * @param visitor the visitor
58     * @throws IllegalArgumentException if the visitor is null
59     */

60    protected void setComponentVisitor(SimpleDeploymentVisitor<C> visitor)
61    {
62       if (visitor == null)
63          throw new IllegalArgumentException JavaDoc("Null visitor");
64       this.compVisitor = visitor;
65       componentType = visitor.getVisitorType();
66       if (componentType == null)
67          throw new IllegalArgumentException JavaDoc("Null visitor type");
68    }
69
70    public void deploy(DeploymentUnit unit) throws DeploymentException
71    {
72       super.deploy(unit);
73       
74       try
75       {
76          deployComponents(unit);
77       }
78       catch (Throwable JavaDoc t)
79       {
80          undeployComponents(unit);
81          throw DeploymentException.rethrowAsDeploymentException("Error deploying: " + unit.getName(), t);
82       }
83    }
84    
85    public void undeploy(DeploymentUnit unit)
86    {
87       super.undeploy(unit);
88       undeployComponents(unit);
89    }
90
91    protected void deployComponents(DeploymentUnit unit) throws DeploymentException
92    {
93       if (compVisitor == null)
94          return;
95
96       Set JavaDoc<? extends C> components = unit.getAllMetaData(componentType);
97       for (C component : components)
98          compVisitor.deploy(unit, component);
99    }
100    
101    protected void undeployComponents(DeploymentUnit unit)
102    {
103       if (compVisitor == null)
104          return;
105       
106       Set JavaDoc<? extends C> components = unit.getAllMetaData(componentType);
107       for (C component : components)
108          compVisitor.undeploy(unit, component);
109    }
110 }
111
Popular Tags