KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > plugins > structure > vfs > war > WARStructure


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
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.war;
23
24 import java.io.IOException JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.jboss.deployers.plugins.structure.ContextInfoImpl;
28 import org.jboss.deployers.plugins.structure.vfs.AbstractStructureDeployer;
29 import org.jboss.deployers.spi.structure.vfs.StructureMetaData;
30 import org.jboss.deployers.spi.structure.vfs.StructuredDeployers;
31 import org.jboss.virtual.VirtualFile;
32 import org.jboss.virtual.VirtualFileFilter;
33 import org.jboss.virtual.VisitorAttributes;
34 import org.jboss.virtual.plugins.vfs.helpers.SuffixMatchFilter;
35
36 /**
37  * WARStructure.
38  *
39  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 1.1 $
41  */

42 public class WARStructure extends AbstractStructureDeployer
43 {
44    /** The default filter which allows jars/jar directories */
45    public static final VirtualFileFilter DEFAULT_WEB_INF_LIB_FILTER =
46       new SuffixMatchFilter(".jar", VisitorAttributes.DEFAULT);
47    
48    /** The web-inf/lib filter */
49    private VirtualFileFilter webInfLibFilter = DEFAULT_WEB_INF_LIB_FILTER;
50
51    /**
52     * Sets the default relative order 1000.
53     *
54     */

55    public WARStructure()
56    {
57       setRelativeOrder(1000);
58    }
59
60    /**
61     * Get the webInfLibFilter.
62     *
63     * @return the webInfLibFilter.
64     */

65    public VirtualFileFilter getWebInfLibFilter()
66    {
67       return webInfLibFilter;
68    }
69
70    /**
71     * Set the webInfLibFilter.
72     *
73     * @param webInfLibFilter the webInfLibFilter.
74     * @throws IllegalArgumentException for a null filter
75     */

76    public void setWebInfLibFilter(VirtualFileFilter webInfLibFilter)
77    {
78       if (webInfLibFilter == null)
79          throw new IllegalArgumentException JavaDoc("Null filter");
80       this.webInfLibFilter = webInfLibFilter;
81    }
82
83    public boolean determineStructure(VirtualFile root, StructureMetaData metaData, StructuredDeployers deployers)
84    {
85       try
86       {
87          if (root.isLeaf() == false)
88          {
89             // We require either a WEB-INF or the name ends in .war
90
if (root.getName().endsWith(".war") == false)
91             {
92                try
93                {
94                   root.findChild("WEB-INF");
95                   log.trace("... ok - directory has a WEB-INF subdirectory");
96                }
97                catch (IOException JavaDoc e)
98                {
99                   log.trace("... no - doesn't look like a war and no WEB-INF subdirectory.");
100                   return false;
101                }
102             }
103             else
104             {
105                log.trace("... ok - name ends in .war.");
106             }
107
108             ContextInfoImpl context = new ContextInfoImpl(root.getPathName());
109             // The metadata path is WEB-INF
110
context.setMetaDataPath("WEB-INF");
111
112             // Add the war manifest classpath entries
113
addClassPath(root, root, false, true, context);
114             try
115             {
116                // The classpath is WEB-INF/classes
117
VirtualFile classes = root.findChild("WEB-INF/classes");
118                // Add the war manifest classpath entries
119
addClassPath(root, classes, true, false, context);
120             }
121             catch(IOException JavaDoc e)
122             {
123                log.trace("No WEB-INF/classes for: " + root.getPathName());
124             }
125             // and the top level jars in WEB-INF/lib
126
try
127             {
128                VirtualFile webinfLib = root.findChild("WEB-INF/lib");
129                List JavaDoc<VirtualFile> archives = webinfLib.getChildren(webInfLibFilter);
130                for (VirtualFile jar : archives)
131                {
132                   addClassPath(root, jar, true, true, context);
133                }
134             }
135             catch (IOException JavaDoc ignored)
136             {
137                log.trace("No WEB-INF/lib for: " + root.getPathName());
138             }
139             metaData.addContext(context);
140
141             // There are no subdeployments for wars
142
return true;
143          }
144          else
145          {
146             log.trace("... no - not a directory or an archive.");
147             return false;
148          }
149       }
150       catch (Exception JavaDoc e)
151       {
152          log.warn("Error determining structure: " + root.getName(), e);
153          return false;
154       }
155    }
156 }
157
Popular Tags