KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > plugins > structure > ComponentDeploymentContext


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.structure;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.concurrent.CopyOnWriteArraySet JavaDoc;
29
30 import org.jboss.deployers.plugins.attachments.AttachmentsImpl;
31 import org.jboss.deployers.spi.DeploymentException;
32 import org.jboss.deployers.spi.attachments.Attachments;
33 import org.jboss.deployers.spi.classloader.ClassLoaderFactory;
34 import org.jboss.deployers.spi.deployer.DeploymentUnit;
35 import org.jboss.deployers.spi.structure.DeploymentContext;
36 import org.jboss.deployers.spi.structure.DeploymentContextVisitor;
37 import org.jboss.deployers.spi.structure.DeploymentState;
38 import org.jboss.deployers.spi.structure.StructureDetermined;
39 import org.jboss.logging.Logger;
40 import org.jboss.virtual.VirtualFile;
41
42 /**
43  * AbstractDeploymentContext.
44  *
45  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
46  * @version $Revision: 1.1 $
47  */

48 public class ComponentDeploymentContext
49    implements DeploymentContext, Serializable JavaDoc
50 {
51    private static final long serialVersionUID = 1;
52
53    /** The log */
54    protected Logger log = Logger.getLogger(getClass());
55    
56    /** The name */
57    private String JavaDoc name;
58    
59    /** The deployment unit */
60    private DeploymentUnit unit;
61
62    /** The parent context */
63    private DeploymentContext parent;
64
65    /** The component contexts */
66    private Set JavaDoc<DeploymentContext> components = new CopyOnWriteArraySet JavaDoc<DeploymentContext>();
67    
68    /** The attachments */
69    private transient Attachments transientAttachments = new AttachmentsImpl();
70    
71    /** The managed objects */
72    private transient Attachments transientManagedObjects = new AttachmentsImpl();
73
74    /**
75     * Create a new ComponentDeploymentContext.
76     *
77     * @param name the name
78     * @param parent the parent
79     * @throws IllegalArgumentException if the name or parent is null
80     */

81    public ComponentDeploymentContext(String JavaDoc name, DeploymentContext parent)
82    {
83       if (name == null)
84          throw new IllegalArgumentException JavaDoc("Null name");
85       if (parent == null)
86          throw new IllegalArgumentException JavaDoc("Null parent");
87       this.name = name;
88       this.parent = parent;
89    }
90
91    public String JavaDoc getName()
92    {
93       return name;
94    }
95
96    public String JavaDoc getSimpleName()
97    {
98       return parent.getSimpleName();
99    }
100    public String JavaDoc getRelativePath()
101    {
102       return parent.getRelativePath();
103    }
104
105    public StructureDetermined getStructureDetermined()
106    {
107       return parent.getStructureDetermined();
108    }
109
110    public void setStructureDetermined(StructureDetermined determined)
111    {
112       throw new UnsupportedOperationException JavaDoc("Not supported for components");
113    }
114    
115    public boolean isCandidate()
116    {
117       return parent.isCandidate();
118    }
119
120    public DeploymentState getState()
121    {
122       return parent.getState();
123    }
124
125    public void setState(DeploymentState state)
126    {
127       parent.setState(state);
128    }
129
130    public DeploymentUnit getDeploymentUnit()
131    {
132       if (unit == null)
133          throw new IllegalStateException JavaDoc("Deployment unit has not been set");
134       return unit;
135    }
136
137    public void setDeploymentUnit(DeploymentUnit unit)
138    {
139       this.unit = unit;
140    }
141
142    public VirtualFile getRoot()
143    {
144       return parent.getRoot();
145    }
146
147    /**
148     * Set the root location
149     *
150     * @param root the root
151     */

152    public void setRoot(VirtualFile root)
153    {
154       throw new UnsupportedOperationException JavaDoc("Not supported for components");
155    }
156    
157    public void setMetaDataPath(String JavaDoc path)
158    {
159       throw new UnsupportedOperationException JavaDoc("Not supported for components");
160    }
161
162    public VirtualFile getMetaDataLocation()
163    {
164       return parent.getMetaDataLocation();
165    }
166
167    public void setMetaDataLocation(VirtualFile location)
168    {
169       throw new UnsupportedOperationException JavaDoc("Not supported for components");
170    }
171
172    public ClassLoader JavaDoc getClassLoader()
173    {
174       return parent.getClassLoader();
175    }
176    
177    public void setClassLoader(ClassLoader JavaDoc classLoader)
178    {
179       throw new UnsupportedOperationException JavaDoc("Not supported for components");
180    }
181    
182    public boolean createClassLoader(ClassLoaderFactory factory) throws DeploymentException
183    {
184       return false;
185    }
186
187    public void removeClassLoader()
188    {
189    }
190    
191    public List JavaDoc<VirtualFile> getClassPath()
192    {
193       return parent.getClassPath();
194    }
195    
196    public void setClassPath(List JavaDoc<VirtualFile> paths)
197    {
198       throw new UnsupportedOperationException JavaDoc("Not supported for components");
199    }
200
201    public boolean isTopLevel()
202    {
203       return false;
204    }
205
206    public DeploymentContext getTopLevel()
207    {
208       return parent.getTopLevel();
209    }
210    
211    public DeploymentContext getParent()
212    {
213       return parent;
214    }
215
216    public void setParent(DeploymentContext parent)
217    {
218       throw new UnsupportedOperationException JavaDoc("Not supported for components");
219    }
220
221    public Set JavaDoc<DeploymentContext> getChildren()
222    {
223       return Collections.emptySet();
224    }
225
226    public void addChild(DeploymentContext child)
227    {
228       throw new UnsupportedOperationException JavaDoc("Not supported for components");
229    }
230
231    public boolean removeChild(DeploymentContext child)
232    {
233       throw new UnsupportedOperationException JavaDoc("Not supported for components");
234    }
235
236    public boolean isComponent()
237    {
238       return true;
239    }
240
241    public Set JavaDoc<DeploymentContext> getComponents()
242    {
243       return Collections.unmodifiableSet(components);
244    }
245
246    public void addComponent(DeploymentContext component)
247    {
248       if (component == null)
249          throw new IllegalArgumentException JavaDoc("Null component");
250       components.add(component);
251    }
252
253    public boolean removeComponent(DeploymentContext component)
254    {
255       if (component == null)
256          throw new IllegalArgumentException JavaDoc("Null component");
257       return components.remove(component);
258    }
259
260    public void visit(DeploymentContextVisitor visitor) throws DeploymentException
261    {
262       if (visitor == null)
263          throw new IllegalArgumentException JavaDoc("Null visitor");
264
265       visit(this, visitor);
266    }
267    
268    /**
269     * Visit a context
270     *
271     * @param context the context
272     * @param visitor the visitor
273     * @throws DeploymentException for any error
274     */

275    private void visit(DeploymentContext context, DeploymentContextVisitor visitor) throws DeploymentException
276    {
277       visitor.visit(context);
278       try
279       {
280          Set JavaDoc<DeploymentContext> children = context.getChildren();
281          if (children.isEmpty())
282             return;
283          
284          DeploymentContext[] childContexts = children.toArray(new DeploymentContext[children.size()]);
285          for (int i = 0; i < childContexts.length; ++i)
286          {
287             if (childContexts[i] == null)
288                throw new IllegalStateException JavaDoc("Null child context for " + context.getName() + " children=" + children);
289             try
290             {
291                visit(childContexts[i], visitor);
292             }
293             catch (Throwable JavaDoc t)
294             {
295                for (int j = i-1; j >= 0; --j)
296                   visitError(childContexts[j], visitor, true);
297                throw DeploymentException.rethrowAsDeploymentException("Error visiting: " + childContexts[i].getName(), t);
298             }
299          }
300       }
301       catch (Throwable JavaDoc t)
302       {
303          visitError(context, visitor, false);
304          throw DeploymentException.rethrowAsDeploymentException("Error visiting: " + context.getName(), t);
305       }
306    }
307
308    /**
309     * Unwind the visit invoking the previously visited context's error handler
310     *
311     * @param context the context
312     * @param visitor the visitor
313     * @param visitChildren whether to visit the children
314     * @throws DeploymentException for any error
315     */

316    private void visitError(DeploymentContext context, DeploymentContextVisitor visitor, boolean visitChildren) throws DeploymentException
317    {
318       if (visitChildren)
319       {
320          Set JavaDoc<DeploymentContext> children = context.getChildren();
321          if (children.isEmpty())
322             return;
323          
324          for (DeploymentContext child : children)
325          {
326             try
327             {
328                visitError(child, visitor, true);
329             }
330             catch (Throwable JavaDoc t)
331             {
332                log.warn("Error during visit error: " + child.getName(), t);
333             }
334          }
335          
336       }
337       try
338       {
339          visitor.error(context);
340       }
341       catch (Throwable JavaDoc t)
342       {
343          log.warn("Error during visit error: " + context.getName(), t);
344       }
345    }
346
347    public Attachments getPredeterminedManagedObjects()
348    {
349       return parent.getPredeterminedManagedObjects();
350    }
351    
352    public Attachments getTransientManagedObjects()
353    {
354       return transientManagedObjects;
355    }
356    
357    public Attachments getTransientAttachments()
358    {
359       return transientAttachments;
360    }
361
362    public Throwable JavaDoc getProblem()
363    {
364       return parent.getProblem();
365    }
366
367    public void setProblem(Throwable JavaDoc problem)
368    {
369       parent.setProblem(problem);
370    }
371
372    public VirtualFile getMetaDataFile(String JavaDoc name)
373    {
374       return parent.getMetaDataFile(name);
375    }
376
377    public List JavaDoc<VirtualFile> getMetaDataFiles(String JavaDoc name, String JavaDoc suffix)
378    {
379       return parent.getMetaDataFiles(name, suffix);
380    }
381    
382    public void deployed()
383    {
384       parent.deployed();
385    }
386
387    public boolean isDeployed()
388    {
389       return parent.isDeployed();
390    }
391
392    public void reset()
393    {
394       components.clear();
395       
396       transientManagedObjects.clear();
397       transientAttachments.clear();
398    }
399
400    public String JavaDoc toString()
401    {
402       StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
403       buffer.append(getClass().getSimpleName());
404       buffer.append('@');
405       buffer.append(System.identityHashCode(this));
406       buffer.append('{').append(name).append('}');
407       return buffer.toString();
408    }
409 }
410
Popular Tags