KickJava   Java API By Example, From Geeks To Geeks.

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


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

51 public class AbstractDeploymentContext
52    implements DeploymentContext, Serializable JavaDoc
53 {
54    private static final long serialVersionUID = 1;
55
56    /** The log */
57    protected Logger log = Logger.getLogger(getClass());
58    
59    /** The name */
60    private String JavaDoc name;
61
62    /** Whether the structure is determined */
63    private StructureDetermined structureDetermined = StructureDetermined.NO;
64
65    /** The deployment state */
66    private DeploymentState state;
67    
68    /** The deployment unit */
69    private DeploymentUnit unit;
70
71    /** The root */
72    private VirtualFile root;
73    
74    /** The meta data location */
75    private VirtualFile metaDataLocation;
76    
77    /** The class paths */
78    private List JavaDoc<VirtualFile> classPath;
79    
80    /** The class loader */
81    private transient ClassLoader JavaDoc classLoader;
82
83    /** The class loader factory for this deployment */
84    private transient ClassLoaderFactory classLoaderFactory;
85    
86    /** Whether this is a candidate deployment */
87    private boolean candidate;
88
89    /** Whether this deployment was processed */
90    private boolean deployed;
91    
92    /** The parent context */
93    private DeploymentContext parent;
94
95    /** The child contexts */
96    private Set JavaDoc<DeploymentContext> children = new CopyOnWriteArraySet JavaDoc<DeploymentContext>();
97
98    /** The component contexts */
99    private Set JavaDoc<DeploymentContext> components = new CopyOnWriteArraySet JavaDoc<DeploymentContext>();
100    
101    /** The predtermined managed objects */
102    private Attachments predeterminedManagedObjects = new AttachmentsImpl();
103    
104    /** The attachments */
105    private transient Attachments transientAttachments = new AttachmentsImpl();
106    
107    /** The managed objects */
108    private transient Attachments transientManagedObjects = new AttachmentsImpl();
109    
110    /** Throwable */
111    private Throwable JavaDoc problem;
112    
113    /**
114     * Get the deployment name
115     *
116     * @param file the file
117     * @return the name;
118     */

119    public static String JavaDoc getDeploymentName(VirtualFile file)
120    {
121       if (file == null)
122          throw new IllegalArgumentException JavaDoc("Null file");
123       try
124       {
125          URI JavaDoc uri = file.toURI();
126          return uri.toString();
127       }
128       catch (Exception JavaDoc e)
129       {
130          throw new IllegalArgumentException JavaDoc("File does not have a valid uri: " + file, e);
131       }
132    }
133
134    /**
135     * Create a new AbstractDeploymentContext.
136     *
137     * @param name the name
138     * @throws IllegalArgumentException if the name is null
139     */

140    public AbstractDeploymentContext(String JavaDoc name)
141    {
142       this(name, false);
143    }
144
145    /**
146     * Create a new AbstractDeploymentContext.
147     *
148     * @param name the name
149     * @param candidate whether this is a candidate
150     * @throws IllegalArgumentException if the name is null
151     */

152    public AbstractDeploymentContext(String JavaDoc name, boolean candidate)
153    {
154       if (name == null)
155          throw new IllegalArgumentException JavaDoc("Null name");
156       this.name = name;
157       this.candidate = candidate;
158    }
159
160    /**
161     * Create a new AbstractDeploymentContext.
162     *
163     * @param name the name
164     * @param parent the parent
165     * @throws IllegalArgumentException if the name or parent is null
166     */

167    public AbstractDeploymentContext(String JavaDoc name, DeploymentContext parent)
168    {
169       this(name, false, parent);
170    }
171
172    /**
173     * Create a new AbstractDeploymentContext.
174     *
175     * @param name the name
176     * @param candidate whether this is a candidate
177     * @param parent the parent
178     * @throws IllegalArgumentException if the name or parent is null
179     */

180    public AbstractDeploymentContext(String JavaDoc name, boolean candidate, DeploymentContext parent)
181    {
182       this(name, candidate);
183       if (parent == null)
184          throw new IllegalArgumentException JavaDoc("Null parent");
185       setParent(parent);
186    }
187    
188    /**
189     * Create a new AbstractDeploymentContext.
190     *
191     * @param root the root
192     * @throws IllegalArgumentException if the file/root is null
193     */

194    public AbstractDeploymentContext(VirtualFile root)
195    {
196       this(getDeploymentName(root), false);
197       setRoot(root);
198    }
199    
200    /**
201     * Create a new AbstractDeploymentContext.
202     *
203     * @param root the root
204     * @param candidate whether this is a candidate
205     * @throws IllegalArgumentException if the file/root is null
206     */

207    public AbstractDeploymentContext(VirtualFile root, boolean candidate)
208    {
209       this(getDeploymentName(root), candidate);
210       setRoot(root);
211    }
212    
213    /**
214     * Create a new AbstractDeploymentContext.
215     *
216     * @param root the root
217     * @param candidate whether this is a candidate
218     * @param parent the parent
219     * @throws IllegalArgumentException if the file/root or parent is null
220     */

221    public AbstractDeploymentContext(VirtualFile root, boolean candidate, DeploymentContext parent)
222    {
223       this(getDeploymentName(root), candidate, parent);
224       setRoot(root);
225    }
226
227    public String JavaDoc getName()
228    {
229       return name;
230    }
231
232    /**
233     * Get the simple vfs name of the deployment unit. This is the simple
234     * name of the virtual file .
235     *
236     * vfs path ------------------- relative path
237     * deploy/some.ear "some.ear"
238     * deploy/some.ear/x.ejb "x.ejb"
239     * deploy/some.ear/y.sar "y.sar"
240     * deploy/some.ear/y.sar/z.rar "z.rar"
241     * @return the deployment unit simple path
242     */

243    public String JavaDoc getSimpleName()
244    {
245       VirtualFile unitVF = getRoot();
246       return unitVF.getName();
247    }
248
249    /**
250     * Get the path of this deployment relative to the top of the
251     * deployment based on the vfs paths.
252     *
253     * @return the top-level deployment relative path
254     */

255    public String JavaDoc getRelativePath()
256    {
257       VirtualFile unitVF = getRoot();
258       VirtualFile topVF = unitVF;
259       DeploymentContext ctx = getParent();
260       while( ctx != null )
261       {
262          topVF = ctx.getRoot();
263          ctx = ctx.getParent();
264       }
265       String JavaDoc unitPath = unitVF.getPathName();
266       String JavaDoc topPath = topVF.getPathName();
267       String JavaDoc relativePath = unitPath.substring(topPath.length());
268       return relativePath;
269    }
270
271    public StructureDetermined getStructureDetermined()
272    {
273       return structureDetermined;
274    }
275
276    public void setStructureDetermined(StructureDetermined determined)
277    {
278       if (determined == null)
279          throw new IllegalArgumentException JavaDoc("Null determined");
280       this.structureDetermined = determined;
281    }
282    
283    public boolean isCandidate()
284    {
285       return candidate;
286    }
287
288    public DeploymentState getState()
289    {
290       return state;
291    }
292
293    public void setState(DeploymentState state)
294    {
295       this.state = state;
296    }
297
298    public DeploymentUnit getDeploymentUnit()
299    {
300       if (unit == null)
301          throw new IllegalStateException JavaDoc("Deployment unit has not been set");
302       return unit;
303    }
304
305    public void setDeploymentUnit(DeploymentUnit unit)
306    {
307       this.unit = unit;
308    }
309
310    public VirtualFile getRoot()
311    {
312       return root;
313    }
314
315    /**
316     * Set the root location
317     *
318     * @param root the root
319     */

320    public void setRoot(VirtualFile root)
321    {
322       this.root = root;
323    }
324    
325    public void setMetaDataPath(String JavaDoc path)
326    {
327       if (path == null)
328          setMetaDataLocation(null);
329       try
330       {
331          setMetaDataLocation(root.findChild(path));
332       }
333       catch (IOException JavaDoc e)
334       {
335          log.debug("Meta data path does not exist: root=" + root.getPathName() + " path=" + path);
336       }
337    }
338
339    public VirtualFile getMetaDataLocation()
340    {
341       return metaDataLocation;
342    }
343
344    public void setMetaDataLocation(VirtualFile location)
345    {
346       this.metaDataLocation = location;
347       if (log.isTraceEnabled() && location != null)
348          log.trace("MetaData location for " + root.getPathName() + " is " + location.getPathName());
349    }
350
351    public ClassLoader JavaDoc getClassLoader()
352    {
353       return classLoader;
354    }
355    
356    public void setClassLoader(ClassLoader JavaDoc classLoader)
357    {
358       this.classLoader = classLoader;
359       if (classLoader != null)
360          log.trace("ClassLoader for " + name + " is " + classLoader);
361    }
362    
363    public boolean createClassLoader(ClassLoaderFactory factory) throws DeploymentException
364    {
365       if (factory == null)
366          throw new IllegalArgumentException JavaDoc("Null factory");
367       
368       ClassLoader JavaDoc cl = getClassLoader();
369       if (cl != null)
370          return false;
371
372       try
373       {
374          cl = factory.createClassLoader(this);
375          if (cl != null)
376          {
377             setClassLoader(cl);
378             this.classLoaderFactory = factory;
379          }
380       }
381       catch (Throwable JavaDoc t)
382       {
383          throw DeploymentException.rethrowAsDeploymentException("Error creating classloader for " + getName(), t);
384       }
385       return true;
386    }
387
388    public void removeClassLoader()
389    {
390       if (classLoaderFactory == null)
391          return;
392       try
393       {
394          classLoaderFactory.removeClassLoader(this);
395       }
396       catch (Throwable JavaDoc t)
397       {
398          log.warn("Error removing classloader for " + getName(), t);
399       }
400       classLoaderFactory = null;
401       setClassLoader(null);
402    }
403
404    
405    public List JavaDoc<VirtualFile> getClassPath()
406    {
407       return classPath;
408    }
409    
410    public void setClassPath(List JavaDoc<VirtualFile> paths)
411    {
412       this.classPath = paths;
413       if (log.isTraceEnabled() && paths != null)
414          log.trace("ClassPath for " + root.getPathName() + " is " + VFSUtils.getPathsString(paths));
415    }
416
417    public boolean isTopLevel()
418    {
419       return parent == null;
420    }
421
422    public DeploymentContext getTopLevel()
423    {
424       DeploymentContext result = this;
425       DeploymentContext parent = getParent();
426       while (parent != null)
427       {
428          result = parent;
429          parent = parent.getParent();
430       }
431       return result;
432    }
433    
434    public DeploymentContext getParent()
435    {
436       return parent;
437    }
438
439    public void setParent(DeploymentContext parent)
440    {
441       if (parent != null && this.parent != null)
442          throw new IllegalStateException JavaDoc("Context already has a parent " + getName());
443       this.parent = parent;
444    }
445
446    public Set JavaDoc<DeploymentContext> getChildren()
447    {
448       return Collections.unmodifiableSet(children);
449    }
450
451    public void addChild(DeploymentContext child)
452    {
453       if (child == null)
454          throw new IllegalArgumentException JavaDoc("Null child");
455       children.add(child);
456    }
457
458    public boolean removeChild(DeploymentContext child)
459    {
460       if (child == null)
461          throw new IllegalArgumentException JavaDoc("Null child");
462       return children.remove(child);
463    }
464
465    public boolean isComponent()
466    {
467       return false;
468    }
469    
470    public Set JavaDoc<DeploymentContext> getComponents()
471    {
472       return Collections.unmodifiableSet(components);
473    }
474
475    public void addComponent(DeploymentContext component)
476    {
477       if (component == null)
478          throw new IllegalArgumentException JavaDoc("Null component");
479       deployed();
480       components.add(component);
481       log.debug("Added component " + component.getName() + " to " + getName());
482    }
483
484    public boolean removeComponent(DeploymentContext component)
485    {
486       if (component == null)
487          throw new IllegalArgumentException JavaDoc("Null component");
488
489       Set JavaDoc<DeploymentContext> componentComponents = component.getComponents();
490       if (componentComponents.isEmpty() == false)
491          log.warn("Removing component " + name + " which still has components " + componentComponents);
492       boolean result = components.remove(component);
493       if (result)
494          log.debug("Removed component " + component.getName() + " from " + getName());
495       return result;
496    }
497
498    public void visit(DeploymentContextVisitor visitor) throws DeploymentException
499    {
500       if (visitor == null)
501          throw new IllegalArgumentException JavaDoc("Null visitor");
502
503       visit(this, visitor);
504    }
505    
506    /**
507     * Visit a context
508     *
509     * @param context the context
510     * @param visitor the visitor
511     * @throws DeploymentException for any error
512     */

513    private void visit(DeploymentContext context, DeploymentContextVisitor visitor) throws DeploymentException
514    {
515       visitor.visit(context);
516       try
517       {
518          Set JavaDoc<DeploymentContext> children = context.getChildren();
519          if (children.isEmpty())
520             return;
521          
522          DeploymentContext[] childContexts = children.toArray(new DeploymentContext[children.size()]);
523          for (int i = 0; i < childContexts.length; ++i)
524          {
525             if (childContexts[i] == null)
526                throw new IllegalStateException JavaDoc("Null child context for " + context.getName() + " children=" + children);
527             try
528             {
529                visit(childContexts[i], visitor);
530             }
531             catch (Throwable JavaDoc t)
532             {
533                for (int j = i-1; j >= 0; --j)
534                   visitError(childContexts[j], visitor, true);
535                throw DeploymentException.rethrowAsDeploymentException("Error visiting: " + childContexts[i].getName(), t);
536             }
537          }
538       }
539       catch (Throwable JavaDoc t)
540       {
541          visitError(context, visitor, false);
542          throw DeploymentException.rethrowAsDeploymentException("Error visiting: " + context.getName(), t);
543       }
544    }
545
546    /**
547     * Unwind the visit invoking the previously visited context's error handler
548     *
549     * @param context the context
550     * @param visitor the visitor
551     * @param visitChildren whether to visit the children
552     * @throws DeploymentException for any error
553     */

554    private void visitError(DeploymentContext context, DeploymentContextVisitor visitor, boolean visitChildren) throws DeploymentException
555    {
556       if (visitChildren)
557       {
558          Set JavaDoc<DeploymentContext> children = context.getChildren();
559          if (children.isEmpty())
560             return;
561          
562          for (DeploymentContext child : children)
563          {
564             try
565             {
566                visitError(child, visitor, true);
567             }
568             catch (Throwable JavaDoc t)
569             {
570                log.warn("Error during visit error: " + child.getName(), t);
571             }
572          }
573          
574       }
575       try
576       {
577          visitor.error(context);
578       }
579       catch (Throwable JavaDoc t)
580       {
581          log.warn("Error during visit error: " + context.getName(), t);
582       }
583    }
584
585    public Attachments getPredeterminedManagedObjects()
586    {
587       return predeterminedManagedObjects;
588    }
589    
590    public Attachments getTransientManagedObjects()
591    {
592       return transientManagedObjects;
593    }
594    
595    public Attachments getTransientAttachments()
596    {
597       return transientAttachments;
598    }
599
600    public Throwable JavaDoc getProblem()
601    {
602       return problem;
603    }
604
605    public void setProblem(Throwable JavaDoc problem)
606    {
607       this.problem = problem;
608    }
609
610    public VirtualFile getMetaDataFile(String JavaDoc name)
611    {
612       if (name == null)
613          throw new IllegalArgumentException JavaDoc("Null name");
614       try
615       {
616          // There isn't a metadata location so let's see whether the root matches.
617
if (metaDataLocation == null)
618          {
619             // It has to be a plain file
620
if (root != null && root.isLeaf())
621             {
622                String JavaDoc fileName = root.getName();
623                if (fileName.equals(name))
624                   return root;
625             }
626             
627             // No match
628
return null;
629          }
630          // Look in the meta data location
631
VirtualFile result = metaDataLocation.findChild(name);
632          if (result != null)
633             deployed();
634          return result;
635       }
636       catch (Exception JavaDoc e)
637       {
638          log.trace("Error retrieving meta data: " + name, e);
639          return null;
640       }
641    }
642
643    public List JavaDoc<VirtualFile> getMetaDataFiles(String JavaDoc name, String JavaDoc suffix)
644    {
645       if (name == null && suffix == null)
646          throw new IllegalArgumentException JavaDoc("Null name and suffix");
647       try
648       {
649          // There isn't a metadata location so let's see whether the root matches.
650
// i.e. the top level is an xml
651
if (metaDataLocation == null)
652          {
653             // It has to be a plain file
654
if (root != null && root.isLeaf())
655             {
656                String JavaDoc fileName = root.getName();
657                if (name != null && fileName.equals(name))
658                   return Collections.singletonList(root);
659                if (suffix != null && fileName.endsWith(suffix))
660                   return Collections.singletonList(root);
661             }
662             
663             // No match
664
return Collections.emptyList();
665          }
666          // Look in the meta data location
667
List JavaDoc<VirtualFile> result = metaDataLocation.getChildren(new MetaDataMatchFilter(name, suffix));
668          if (result != null && result.isEmpty() == false)
669             deployed();
670          return result;
671       }
672       catch (Exception JavaDoc e)
673       {
674          log.debug("Error retrieving meta data: name=" + name + " suffix=" + suffix, e);
675          return Collections.emptyList();
676       }
677    }
678    
679    public boolean isDeployed()
680    {
681       return deployed;
682    }
683    
684    public void deployed()
685    {
686       deployed = true;
687    }
688
689    public void reset()
690    {
691       if (structureDetermined != StructureDetermined.PREDETERMINED)
692          children.clear();
693       else
694       {
695          for (DeploymentContext child : children)
696             child.reset();
697       }
698       components.clear();
699       deployed = false;
700       
701       classLoader = null;
702       transientManagedObjects.clear();
703       transientAttachments.clear();
704    }
705
706    public String JavaDoc toString()
707    {
708       StringBuilder JavaDoc buffer = new StringBuilder JavaDoc();
709       buffer.append(getClass().getSimpleName());
710       buffer.append('@');
711       buffer.append(System.identityHashCode(this));
712       buffer.append('{').append(name).append('}');
713       return buffer.toString();
714    }
715 }
716
Popular Tags