KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > plugins > deployer > AbstractDeploymentUnit


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.deployer;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import org.jboss.deployers.plugins.attachments.AbstractAttachments;
33 import org.jboss.deployers.plugins.structure.ComponentDeploymentContext;
34 import org.jboss.deployers.spi.DeploymentException;
35 import org.jboss.deployers.spi.attachments.Attachments;
36 import org.jboss.deployers.spi.classloader.ClassLoaderFactory;
37 import org.jboss.deployers.spi.deployer.DeploymentUnit;
38 import org.jboss.deployers.spi.structure.DeploymentContext;
39 import org.jboss.logging.Logger;
40 import org.jboss.virtual.VirtualFile;
41
42 /**
43  * AbstractDeploymentUnit.<p>
44  *
45  * This is just a wrapper to the deployment context that
46  * restricts people from "poking" behind the scenes.
47  *
48  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
49  * @author Scott.Stark@jboss.org
50  * @version $Revision: 1.1 $
51  */

52 public class AbstractDeploymentUnit extends AbstractAttachments
53    implements DeploymentUnit, Serializable JavaDoc
54 {
55    private static final Logger log = Logger.getLogger(AbstractDeploymentUnit.class);
56    private static final long serialVersionUID = 1;
57
58    /** The deployment context */
59    private DeploymentContext deploymentContext;
60    
61    /**
62     * Create a new AbstractDeploymentUnit.
63     *
64     * @param deploymentContext the deployment context
65     */

66    public AbstractDeploymentUnit(DeploymentContext deploymentContext)
67    {
68       if (deploymentContext == null)
69          throw new IllegalArgumentException JavaDoc("Null deployment context");
70       this.deploymentContext = deploymentContext;
71    }
72    
73    public String JavaDoc getName()
74    {
75       return deploymentContext.getName();
76    }
77    
78    public String JavaDoc getSimpleName()
79    {
80       return deploymentContext.getSimpleName();
81    }
82
83    /**
84     * Get the path of this deployment relative to the top of the
85     * deployment based on the vfs paths.
86     *
87     * @return the top-level deployment relative path
88     */

89    public String JavaDoc getRelativePath()
90    {
91       return deploymentContext.getRelativePath();
92    }
93
94    /**
95     * Find a child of the deployment root.
96     * @param name - relative path of the file to find
97     * @return the file if found, null otherwise.
98     */

99    public VirtualFile getFile(String JavaDoc name)
100    {
101       VirtualFile root = deploymentContext.getRoot();
102       VirtualFile file = null;
103       try
104       {
105          file = root.findChild(name);
106       }
107       catch(Exception JavaDoc e)
108       {
109          if( log.isTraceEnabled() )
110             log.trace("Failed to find: "+name, e);
111       }
112       return file;
113    }
114
115    public ClassLoader JavaDoc getClassLoader()
116    {
117       ClassLoader JavaDoc cl = deploymentContext.getClassLoader();
118       if (cl == null)
119          throw new IllegalStateException JavaDoc("ClassLoader has not been set");
120       deploymentContext.deployed();
121       return cl;
122    }
123
124    public boolean createClassLoader(ClassLoaderFactory factory) throws DeploymentException
125    {
126       return deploymentContext.createClassLoader(factory);
127    }
128    
129    public VirtualFile getMetaDataFile(String JavaDoc name)
130    {
131       return deploymentContext.getMetaDataFile(name);
132    }
133
134    public List JavaDoc<VirtualFile> getMetaDataFiles(String JavaDoc name, String JavaDoc suffix)
135    {
136       return deploymentContext.getMetaDataFiles(name, suffix);
137    }
138
139    public DeploymentUnit addComponent(String JavaDoc name)
140    {
141       ComponentDeploymentContext component = new ComponentDeploymentContext(name, deploymentContext);
142       AbstractDeploymentUnit unit = new AbstractDeploymentUnit(component);
143       component.setDeploymentUnit(unit);
144       deploymentContext.addComponent(component);
145       return unit;
146    }
147
148    public boolean removeComponent(String JavaDoc name)
149    {
150       if (name == null)
151          throw new IllegalArgumentException JavaDoc("Null name");
152       
153       for (DeploymentContext component : deploymentContext.getComponents())
154       {
155          if (name.equals(component.getName()))
156             return deploymentContext.removeComponent(component);
157       }
158       return false;
159    }
160
161    public Map JavaDoc<String JavaDoc, Object JavaDoc> getAttachments()
162    {
163       DeploymentContext parent = deploymentContext.getParent();
164       if (deploymentContext.isComponent() == false)
165          parent = null;
166       HashMap JavaDoc<String JavaDoc, Object JavaDoc> result = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
167       if (parent != null)
168          result.putAll(parent.getTransientAttachments().getAttachments());
169       result.putAll(deploymentContext.getTransientAttachments().getAttachments());
170       if (parent != null)
171          result.putAll(parent.getTransientManagedObjects().getAttachments());
172       result.putAll(deploymentContext.getTransientManagedObjects().getAttachments());
173       if (parent != null)
174          result.putAll(parent.getPredeterminedManagedObjects().getAttachments());
175       result.putAll(deploymentContext.getPredeterminedManagedObjects().getAttachments());
176       if (result.isEmpty() == false)
177          deploymentContext.deployed();
178       return Collections.unmodifiableMap(result);
179    }
180
181    public Object JavaDoc addAttachment(String JavaDoc name, Object JavaDoc attachment)
182    {
183       deploymentContext.deployed();
184       return deploymentContext.getTransientAttachments().addAttachment(name, attachment);
185    }
186
187    public Object JavaDoc getAttachment(String JavaDoc name)
188    {
189       DeploymentContext parent = deploymentContext.getParent();
190       if (deploymentContext.isComponent() == false)
191          parent = null;
192       Object JavaDoc result = deploymentContext.getPredeterminedManagedObjects().getAttachment(name);
193       if (result != null)
194       {
195          deploymentContext.deployed();
196          return result;
197       }
198       if (parent != null)
199       {
200          result = parent.getPredeterminedManagedObjects().getAttachment(name);
201          if (result != null)
202          {
203             deploymentContext.deployed();
204             return result;
205          }
206       }
207       result = deploymentContext.getTransientManagedObjects().getAttachment(name);
208       if (result != null)
209       {
210          deploymentContext.deployed();
211          return result;
212       }
213       if (parent != null)
214       {
215          result = parent.getTransientManagedObjects().getAttachment(name);
216          if (result != null)
217          {
218             deploymentContext.deployed();
219             return result;
220          }
221       }
222       result = deploymentContext.getTransientAttachments().getAttachment(name);
223       if (result != null)
224       {
225          deploymentContext.deployed();
226          return result;
227       }
228       if (parent != null)
229       {
230          result = parent.getTransientAttachments().getAttachment(name);
231          if (result != null)
232          {
233             deploymentContext.deployed();
234             return result;
235          }
236       }
237       return null;
238    }
239
240    public boolean isAttachmentPresent(String JavaDoc name)
241    {
242       DeploymentContext parent = deploymentContext.getParent();
243       if (deploymentContext.isComponent() == false)
244          parent = null;
245       if (deploymentContext.getPredeterminedManagedObjects().isAttachmentPresent(name))
246       {
247          deploymentContext.deployed();
248          return true;
249       }
250       if (parent != null && parent.getPredeterminedManagedObjects().isAttachmentPresent(name))
251       {
252          deploymentContext.deployed();
253          return true;
254       }
255       if (deploymentContext.getTransientManagedObjects().isAttachmentPresent(name))
256       {
257          deploymentContext.deployed();
258          return true;
259       }
260       if (parent != null && parent.getTransientAttachments().isAttachmentPresent(name))
261       {
262          deploymentContext.deployed();
263          return true;
264       }
265       if (deploymentContext.getTransientAttachments().isAttachmentPresent(name))
266       {
267          deploymentContext.deployed();
268          return true;
269       }
270       if (parent != null && parent.getTransientAttachments().isAttachmentPresent(name))
271       {
272          deploymentContext.deployed();
273          return true;
274       }
275       return false;
276    }
277
278    public Object JavaDoc removeAttachment(String JavaDoc name)
279    {
280       return deploymentContext.getTransientAttachments().removeAttachment(name);
281    }
282
283    public Attachments getTransientManagedObjects()
284    {
285       return deploymentContext.getTransientManagedObjects();
286    }
287
288    @SuppressWarnings JavaDoc("unchecked")
289    // TODO optimize
290
public <T> Set JavaDoc<? extends T> getAllMetaData(Class JavaDoc<T> type)
291    {
292       if (type == null)
293          throw new IllegalArgumentException JavaDoc("Null type");
294       
295       Set JavaDoc<T> result = new HashSet JavaDoc<T>();
296       Map JavaDoc<String JavaDoc, Object JavaDoc> attachments = getAttachments();
297       for (Object JavaDoc object : attachments.values())
298       {
299          if (type.isInstance(object))
300             result.add((T) object);
301       }
302       if (result.isEmpty() == false)
303          deploymentContext.deployed();
304       return result;
305    }
306
307    @Deprecated JavaDoc
308    public DeploymentContext getDeploymentContext()
309    {
310       deploymentContext.deployed();
311       return deploymentContext;
312    }
313 }
314
Popular Tags