KickJava   Java API By Example, From Geeks To Geeks.

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


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;
23
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.jboss.deployers.plugins.structure.AbstractDeploymentContext;
31 import org.jboss.deployers.spi.DeploymentException;
32 import org.jboss.deployers.spi.structure.DeploymentContext;
33 import org.jboss.deployers.spi.structure.vfs.ClassPathInfo;
34 import org.jboss.deployers.spi.structure.vfs.ContextInfo;
35 import org.jboss.deployers.spi.structure.vfs.StructureBuilder;
36 import org.jboss.deployers.spi.structure.vfs.StructureMetaData;
37 import org.jboss.logging.Logger;
38 import org.jboss.virtual.VFS;
39 import org.jboss.virtual.VFSUtils;
40 import org.jboss.virtual.VirtualFile;
41 import org.jboss.virtual.VisitorAttributes;
42 import org.jboss.virtual.plugins.vfs.helpers.SuffixMatchFilter;
43
44 /**
45  * The default StructureBuilder. It translates a StructureMetaData instance
46  * into a DeploymentContext tree.
47  *
48  * @author Scott.Stark@jboss.org
49  * @version $Revision:$
50  */

51 public class DefaultStructureBuilder
52    implements StructureBuilder
53 {
54    private static Logger log = Logger.getLogger(DefaultStructureBuilder.class);
55
56    public void populateContext(DeploymentContext context, StructureMetaData metaData)
57       throws DeploymentException
58     {
59       HashMap JavaDoc<String JavaDoc, DeploymentContext> contextMap = new HashMap JavaDoc<String JavaDoc, DeploymentContext>();
60       // Validate that the root file has a valid ContextInfo
61
VirtualFile root = context.getRoot();
62       ContextInfo rootInfo = metaData.getContext(root.getPathName());
63       if( rootInfo == null )
64          throw new DeploymentException("Failed to find ContextInfo for context root: "+root);
65
66       // Process the context from the root down
67
VFS vfs = root.getVFS();
68       contextMap.put(root.getPathName(), context);
69       try
70       {
71          for(ContextInfo info : metaData.getContexts())
72          {
73             String JavaDoc vfsPath = info.getVfsPath();
74             VirtualFile vf = vfs.findChild(vfsPath);
75             ContextInfo parentInfo = info.getParent();
76             String JavaDoc parentPath = parentInfo != null ? parentInfo.getVfsPath() : "";
77             DeploymentContext ctx = contextMap.get(vfsPath);
78             DeploymentContext parent = contextMap.get(parentPath);
79             if( ctx == null )
80             {
81                if( parent != null )
82                {
83                   ctx = new AbstractDeploymentContext(vf, true, parent);
84                   parent.addChild(ctx);
85                }
86                else
87                   ctx = new AbstractDeploymentContext(vf);
88             }
89             processContext(ctx, vf, info, contextMap);
90          }
91       }
92       catch(Exception JavaDoc e)
93       {
94          throw new DeploymentException("Failed to process context: "+context.getName(), e);
95       }
96       contextMap.clear();
97     }
98
99    protected void processContext(DeploymentContext context, VirtualFile virtualFile,
100          ContextInfo info, HashMap JavaDoc<String JavaDoc, DeploymentContext> contextMap)
101    {
102       boolean trace = log.isTraceEnabled();
103       if( trace )
104          log.trace("Processing context: "+context+", info: "+info);
105       String JavaDoc metaDataPath = info.getMetaDataPath();
106       if( metaDataPath != null && metaDataPath.length() > 0 )
107          context.setMetaDataPath(metaDataPath);
108       ArrayList JavaDoc<VirtualFile> paths = new ArrayList JavaDoc<VirtualFile>();
109       List JavaDoc<ClassPathInfo> classPath = info.getClassPath();
110       boolean classPathHadVF = false;
111       if( classPath != null )
112       {
113          for(ClassPathInfo cp : classPath)
114          {
115             try
116             {
117                VirtualFile child = virtualFile.findChild(cp.getPath());
118                String JavaDoc suffixesOpt = (String JavaDoc) cp.getOption("suffixes");
119                String JavaDoc[] suffixes = null;
120                if( suffixesOpt != null )
121                   suffixes = suffixesOpt.split(",");
122                // Add the path if there is no suffix
123
if( suffixes == null || suffixes.length == 0 )
124                {
125                   paths.add(child);
126                   if( classPathHadVF == false )
127                      classPathHadVF = child.equals(virtualFile);
128                   if( trace )
129                      log.trace("Added simple classpath entry: "+child);
130                   // Process any Manifest Class-Path refs
131
VFSUtils.addManifestLocations(child, paths);
132                }
133                // Filter the immeadiate children against the suffixes
134
else
135                {
136                   SuffixMatchFilter filter = new SuffixMatchFilter(Arrays.asList(suffixes), VisitorAttributes.DEFAULT);
137                   List JavaDoc<VirtualFile> matches = child.getChildren(filter);
138                   if( matches != null )
139                   {
140                      paths.addAll(matches);
141                      if( trace )
142                         log.trace("Added classpath matches: "+matches);
143                      // Process any Manifest Class-Path refs
144
for(VirtualFile file : matches)
145                      {
146                         VFSUtils.addManifestLocations(file, paths);
147                         if( classPathHadVF == false )
148                            classPathHadVF = child.equals(virtualFile);
149                      }
150                   }
151                }
152             }
153             catch(IOException JavaDoc e)
154             {
155                log.debug("Failed to find cp element: "+cp+", "+e.getMessage());
156             }
157          }
158       }
159
160       // If virtualFile was not already processed as part of the classpath
161
if( classPathHadVF == false )
162       {
163          try
164          {
165             // Process any Manifest Class-Path refs on the context itself
166
if( virtualFile.isLeaf() == false )
167                VFSUtils.addManifestLocations(virtualFile, paths);
168          }
169          catch(IOException JavaDoc ignore)
170          {
171          }
172       }
173       // Set the classpath
174
if( paths.size() > 0 )
175          context.setClassPath(paths);
176       // Add the context to the vfs path to context map
177
contextMap.put(virtualFile.getPathName(), context);
178    }
179
180 }
181
Popular Tags