KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > plugins > structure > vfs > file > FileStructure


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.file;
23
24 import java.util.Set JavaDoc;
25 import java.util.concurrent.CopyOnWriteArraySet 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
33 /**
34  * FileStructure is a simple suffix recognition structure deployer.
35  *
36  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
37  * @version $Revision: 1.1 $
38  */

39 public class FileStructure extends AbstractStructureDeployer
40 {
41    /** The file suffixes */
42    private static Set JavaDoc<String JavaDoc> fileSuffixes = new CopyOnWriteArraySet JavaDoc<String JavaDoc>();
43
44    // Initialise known suffixes
45
static
46    {
47       fileSuffixes.add("-service.xml");
48       fileSuffixes.add("-beans.xml");
49       fileSuffixes.add("-ds.xml");
50       fileSuffixes.add("-aop.xml");
51    }
52
53    public FileStructure()
54    {
55       
56    }
57    
58    public FileStructure(Set JavaDoc<String JavaDoc> suffixes)
59    {
60       fileSuffixes.clear();
61       fileSuffixes.addAll(suffixes);
62    }
63
64    /**
65     * Gets the list of suffixes recognised as files
66     *
67     * @return the list of suffixes
68     */

69    public Set JavaDoc<String JavaDoc> getSuffixes()
70    {
71       return fileSuffixes;
72    }
73    
74    
75    /**
76     * Add a file suffix
77     *
78     * @param suffix the suffix
79     * @return true when added
80     * @throws IllegalArgumentException for a null suffix
81     */

82    public static boolean addFileSuffix(String JavaDoc suffix)
83    {
84       if (suffix == null)
85          throw new IllegalArgumentException JavaDoc("Null suffix");
86       return fileSuffixes.add(suffix);
87    }
88
89    /**
90     * Remove a file suffix
91     *
92     * @param suffix the suffix
93     * @return true when removed
94     * @throws IllegalArgumentException for a null suffix
95     */

96    public static boolean removeFileSuffix(String JavaDoc suffix)
97    {
98       if (suffix == null)
99          throw new IllegalArgumentException JavaDoc("Null suffix");
100       return fileSuffixes.remove(suffix);
101    }
102    
103    /**
104     * Whether this is an archive
105     *
106     * @param name the name
107     * @return true when an archive
108     * @throws IllegalArgumentException for a null name
109     */

110    public static boolean isKnownFile(String JavaDoc name)
111    {
112       if (name == null)
113          throw new IllegalArgumentException JavaDoc("Null name");
114       
115       int index = name.lastIndexOf('-');
116       if (index == -1)
117          return false;
118       String JavaDoc suffix = name.substring(index);
119       return fileSuffixes.contains(suffix);
120    }
121
122    public boolean determineStructure(VirtualFile root, StructureMetaData metaData, StructuredDeployers deployers)
123    {
124       try
125       {
126          if (root.isLeaf())
127          {
128             // See if this is a top-level by checking the parent
129
if (isTopLevel(root, metaData) == false)
130             {
131                if (isKnownFile(root.getName()) == false)
132                {
133                   log.trace("... no - it is not a top level file and not a known name");
134                   return false;
135                }
136                else
137                {
138                   log.trace("... ok - not a top level file but it is a known name");
139                }
140             }
141             else
142             {
143                log.trace("... ok - it is a top level file");
144             }
145
146             // Create a context info for this file
147
ContextInfoImpl context = new ContextInfoImpl(root.getPathName());
148             metaData.addContext(context);
149             // There are no subdeployments for files
150
return true;
151          }
152          else
153          {
154             log.trace("... no - not a file.");
155             return false;
156          }
157       }
158       catch (Exception JavaDoc e)
159       {
160          log.warn("Error determining structure: " + root.getName(), e);
161          return false;
162       }
163    }
164 }
165
Popular Tags