View Javadoc

1   /*
2    * Copyright 2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.jboss.vfs;
17  
18  import java.net.URISyntaxException;
19  import java.net.URL;
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.jboss.vfs.VFS;
25  import org.jboss.vfs.VirtualFile;
26  
27  import de.smartics.util.lang.classpath.ClassPathContext;
28  import de.smartics.util.lang.classpath.ClassPathDirectoryListing;
29  import de.smartics.util.lang.classpath.ClassPathListing;
30  
31  
32  /**
33   * Supports listing of class path elements for protocols <code>vfs</code>,
34   * <code>jar</code> and <code>file</code>.
35   * <p>
36   * Based on implementations provided by
37   * <a href="http://www.smartics.eu/smartics-commons/index.html">smartics-commons</a>.
38   * </p>
39   */
40  public final class VfsClassPathListing implements ClassPathListing
41  {
42    // ********************************* Fields *********************************
43  
44    // --- constants ------------------------------------------------------------
45  
46    // --- members --------------------------------------------------------------
47  
48    // ****************************** Initializer *******************************
49  
50    // ****************************** Constructors ******************************
51  
52    /**
53     * Listing implementation covering the <code>vfs</code> protocol.
54     */
55    private static final class VfsClassPathDirectoryListing extends
56        ClassPathDirectoryListing
57    {
58      private VfsClassPathDirectoryListing(final ClassPathContext context)
59        throws NullPointerException
60      {
61        super(context);
62      }
63  
64      @Override
65      public List<String> list(final String resourcePath)
66        throws NullPointerException, IllegalArgumentException
67      {
68        final URL url = getResource(resourcePath);
69        if (url == null)
70        {
71          return Collections.emptyList();
72        }
73  
74        final String protocol = url.getProtocol();
75        if ("vfs".equals(protocol))
76        {
77          try
78          {
79            final VirtualFile vfile = VFS.getChild(url.toURI());
80            final List<VirtualFile> files = vfile.getChildren();
81            final List<String> listing = new ArrayList<String>();
82            for (final VirtualFile file : files)
83            {
84              final String name = file.getName();
85              listing.add(name);
86            }
87            return listing;
88          }
89          catch (final URISyntaxException e)
90          {
91            // return empty list
92          }
93          return Collections.emptyList();
94        }
95        else
96        {
97          return super.list(resourcePath);
98        }
99      }
100 
101     private URL getResource(final String resourcePath)
102     {
103       final ClassPathContext context = getClassPathContext();
104       final URL url = context.getResource(resourcePath);
105       return url;
106     }
107   }
108 
109   // ****************************** Inner Classes *****************************
110 
111   // ********************************* Methods ********************************
112 
113   // --- init -----------------------------------------------------------------
114 
115   // --- get&set --------------------------------------------------------------
116 
117   // --- business -------------------------------------------------------------
118 
119   //@Override
120   public List<String> list(final ClassPathContext context,
121       final String resourcePath) throws NullPointerException,
122     IllegalArgumentException
123   {
124     final VfsClassPathDirectoryListing delegate =
125         new VfsClassPathDirectoryListing(context);
126     return delegate.list(resourcePath);
127   }
128 
129   // --- object basics --------------------------------------------------------
130 
131 }