KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > plugins > structure > vfs > AbstractStructureDeployer


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.vfs;
23
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.jboss.deployers.plugins.structure.ClassPathInfoImpl;
29 import org.jboss.deployers.plugins.structure.vfs.jar.JARCandidateStructureVisitorFactory;
30 import org.jboss.deployers.spi.structure.vfs.ClassPathInfo;
31 import org.jboss.deployers.spi.structure.vfs.ContextInfo;
32 import org.jboss.deployers.spi.structure.vfs.StructureDeployer;
33 import org.jboss.deployers.spi.structure.vfs.StructureMetaData;
34 import org.jboss.deployers.spi.structure.vfs.StructuredDeployers;
35 import org.jboss.logging.Logger;
36 import org.jboss.virtual.VFSUtils;
37 import org.jboss.virtual.VirtualFile;
38 import org.jboss.virtual.VirtualFileVisitor;
39 import org.jboss.virtual.VisitorAttributes;
40
41 /**
42  * AbstractStructureDeployer.<p>
43  *
44  * We don't care about the order by default.
45  *
46  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
47  * @version $Revision: 1.1 $
48  */

49 public abstract class AbstractStructureDeployer implements StructureDeployer
50 {
51    /** The log */
52    protected Logger log = Logger.getLogger(getClass());
53    private int relativeOrder = Integer.MAX_VALUE;
54
55    /** The candidate structure visitor factory */
56    private CandidateStructureVisitorFactory candidateStructureVisitorFactory = JARCandidateStructureVisitorFactory.INSTANCE;
57
58    public int getRelativeOrder()
59    {
60       return relativeOrder;
61    }
62    public void setRelativeOrder(int order)
63    {
64       this.relativeOrder = order;
65    }
66    
67
68    /**
69     * Get the candidateStructureVisitorFactory.
70     *
71     * @return the candidateStructureVisitorFactory.
72     */

73    public CandidateStructureVisitorFactory getCandidateStructureVisitorFactory()
74    {
75       return candidateStructureVisitorFactory;
76    }
77
78    /**
79     * Set the candidateStructureVisitorFactory.
80     *
81     * @param candidateStructureVisitorFactory the candidateStructureVisitorFactory.
82     * @throws IllegalArgumentException for a null candidate structure
83     */

84    public void setCandidateStructureVisitorFactory(CandidateStructureVisitorFactory candidateStructureVisitorFactory)
85    {
86       if (candidateStructureVisitorFactory == null)
87          throw new IllegalArgumentException JavaDoc("Null candidateStructureVisitorFactory");
88       this.candidateStructureVisitorFactory = candidateStructureVisitorFactory;
89    }
90
91    public abstract boolean determineStructure(VirtualFile root,
92          StructureMetaData metaData, StructuredDeployers deployers);
93
94    /**
95     * See if a file corresponds to a top-level deployment.
96     *
97     * @param file
98     * @param metaData
99     * @return
100     */

101    public boolean isTopLevel(VirtualFile file, StructureMetaData metaData)
102       throws IOException JavaDoc
103    {
104       // See if this is a top-level by checking the parent
105
VirtualFile parent = file.getParent();
106       String JavaDoc parentPath = parent != null ? parent.getPathName() : null;
107       boolean isTopLevel = parentPath == null || metaData.getContext(parentPath) == null;
108       return isTopLevel;
109    }
110
111    /**
112     * Add an entry to the context classpath.
113     *
114     * @param root - the root file the classpath entry should be relative to
115     * @param entry - the candidate file to add as a classpath entry
116     * @param includeEntry - a flag indicating if the entry should be added to
117     * the classpath
118     * @param includeRootManifestCP - a flag indicating if the entry metainf
119     * manifest classpath should be included.
120     * @param context - the context to populate
121     * @throws IOException
122     */

123    protected void addClassPath(VirtualFile root, VirtualFile entry,
124          boolean includeEntry, boolean includeRootManifestCP,
125          ContextInfo context)
126       throws IOException JavaDoc
127    {
128       // Add the manifest locations
129
List JavaDoc<VirtualFile> paths = new ArrayList JavaDoc<VirtualFile>();
130       if( includeEntry )
131          paths.add(entry);
132       String JavaDoc rootPath = root.getPathName();
133       if( includeRootManifestCP && entry.isLeaf() == false )
134       {
135          try
136          {
137             VFSUtils.addManifestLocations(entry, paths);
138          }
139          catch(Exception JavaDoc e)
140          {
141             if( log.isTraceEnabled() )
142                log.trace("Failed to add manifest locations", e);
143          }
144       }
145       // Add to any existing classpath
146
List JavaDoc<ClassPathInfo> pathInfo = new ArrayList JavaDoc<ClassPathInfo>();
147       if( context.getClassPath() != null )
148          pathInfo.addAll(context.getClassPath());
149       // Translate from VirtualFile to root relative paths
150
for(VirtualFile vf : paths)
151       {
152          // Set the path relative to the root
153
String JavaDoc cp = vf.getPathName();
154          if( !"".equals(rootPath) && cp.startsWith(rootPath) )
155          {
156             if( cp.length() == rootPath.length() )
157                cp = "";
158             else
159                cp = cp.substring(rootPath.length()+1);
160          }
161          ClassPathInfoImpl cpi = new ClassPathInfoImpl(cp);
162          pathInfo.add(cpi);
163       }
164       context.setClassPath(pathInfo);
165    }
166
167    /**
168     * Add all children as candidates
169     *
170     * @param parent the parent context
171     * @throws Exception for any error
172     */

173    protected void addAllChildren(VirtualFile parent, StructureMetaData metaData, StructuredDeployers deployers)
174       throws Exception JavaDoc
175    {
176       addChildren(parent, metaData, deployers, null);
177    }
178
179    /**
180     * Add all children as candidates
181     *
182     * @param parent the parent context
183     * @param attributes the visitor attributes uses {@link VisitorAttributes#DEFAULT} when null
184     * @throws Exception for any error
185     */

186    protected void addChildren(VirtualFile parent, StructureMetaData metaData, StructuredDeployers deployers,
187          VisitorAttributes attributes) throws Exception JavaDoc
188    {
189       if (parent == null)
190          throw new IllegalArgumentException JavaDoc("Null parent");
191       
192       VirtualFileVisitor visitor = candidateStructureVisitorFactory.createVisitor(parent, metaData, deployers, attributes);
193       parent.visit(visitor);
194    }
195 }
196
Popular Tags