View Javadoc

1   /*
2    * Copyright 2011-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.util.lang;
17  
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.net.MalformedURLException;
21  import java.net.URL;
22  import java.util.Enumeration;
23  
24  import org.apache.commons.lang.ObjectUtils;
25  
26  /**
27   * A constraint on resources to be loaded from the class path.
28   *
29   * @deprecated Moved to {@link de.smartics.util.lang.classpath.ClassPathContext}
30   *             .
31   */
32  @Deprecated
33  public final class ClassPathContext
34  {
35    // ********************************* Fields *********************************
36  
37    // --- constants ------------------------------------------------------------
38  
39    // --- members --------------------------------------------------------------
40  
41    /**
42     * The class loader to load the directory listings.
43     */
44    private final ClassLoader classLoader;
45  
46    /**
47     * Selects the archive root to load from.
48     */
49    private final String archiveRoot;
50  
51    // ****************************** Initializer *******************************
52  
53    // ****************************** Constructors ******************************
54  
55    /**
56     * Default constructor.
57     *
58     * @param classLoader the class loader to load the directory listings.
59     * @param archiveRoot the value for archiveRoot.
60     * @throws NullPointerException if {@code classLoader} is <code>null</code>.
61     */
62    public ClassPathContext(final ClassLoader classLoader,
63        final String archiveRoot) throws NullPointerException
64    {
65      Arg.checkNotNull("classLoader", classLoader);
66  
67      this.classLoader = classLoader;
68      this.archiveRoot = archiveRoot;
69    }
70  
71    // ****************************** Inner Classes *****************************
72  
73    // ********************************* Methods ********************************
74  
75    // --- init -----------------------------------------------------------------
76  
77    // --- get&set --------------------------------------------------------------
78  
79    // --- business -------------------------------------------------------------
80  
81    /**
82     * Constructs the URL to the resource.
83     *
84     * @param resource the resource whose URL is requested.
85     * @return the URL to the resource or <code>null</code> if the resource cannot
86     *         be found on the class path.
87     */
88    public URL getResource(final String resource)
89    {
90      if (archiveRoot == null)
91      {
92        return classLoader.getResource(resource);
93      }
94  
95      // TODO: maybe we should simply construct the URL?
96      try
97      {
98        for (final Enumeration<URL> en = classLoader.getResources(resource); en
99            .hasMoreElements();)
100       {
101         final URL current = en.nextElement();
102         final String urlString = current.toExternalForm();
103         if (urlString.startsWith(archiveRoot))
104         {
105           return current;
106         }
107       }
108     }
109     catch (final IOException e)
110     {
111       // return null
112     }
113     return null;
114   }
115 
116   /**
117    * Opens the stream to the resource.
118    *
119    * @param resource the resource whose stream is requested.
120    * @return the stream to the resource or <code>null</code> if the resource
121    *         cannot be found on the class path. If a stream is returned, the
122    *         client is responsible to close that stream.
123    */
124   public InputStream getResourceAsStream(final String resource)
125   {
126     final URL url = getResource(resource);
127     if (url != null)
128     {
129       try
130       {
131         return url.openStream();
132       }
133       catch (final IOException e)
134       {
135         // return null;
136       }
137     }
138     return null;
139   }
140 
141   /**
142    * Constructs the URL to the resource.
143    *
144    * @param resource the resource whose URL is requested.
145    * @return the constructed URL.
146    * @throws IllegalArgumentException if the URL to the resource cannot be
147    *           constructed.
148    */
149   public URL createUrl(final String resource) throws IllegalArgumentException
150   {
151     try
152     {
153       final String urlString =
154           (archiveRoot != null ? archiveRoot : "") + resource;
155       return new URL(urlString);
156     }
157     catch (final MalformedURLException e)
158     {
159       throw new IllegalArgumentException("Cannot construct URL with resource '"
160                                          + resource + "'.", e);
161     }
162   }
163 
164   // --- object basics --------------------------------------------------------
165 
166   /**
167    * Returns the hash code of the object.
168    *
169    * @return the hash code.
170    */
171   @Override
172   public int hashCode()
173   {
174     return ObjectUtils.hashCode(archiveRoot);
175   }
176 
177   /**
178    * Returns <code>true</code> if the given object is semantically equal to the
179    * given object, <code>false</code> otherwise.
180    *
181    * @param object the instance to compare to.
182    * @return <code>true</code> if the given object is semantically equal to the
183    *         given object, <code>false</code> otherwise.
184    */
185   @Override
186   public boolean equals(final Object object)
187   {
188     if (this == object)
189     {
190       return true;
191     }
192     else if (object == null || getClass() != object.getClass())
193     {
194       return false;
195     }
196 
197     final ClassPathContext other = (ClassPathContext) object;
198 
199     return ObjectUtils.equals(archiveRoot, other.archiveRoot);
200   }
201 
202   @Override
203   public String toString()
204   {
205     return archiveRoot != null ? archiveRoot : "<unspecified archive root>";
206   }
207 }