View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.geronimo.kernel.classloader;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.IOException;
22  import java.net.MalformedURLException;
23  import java.util.jar.Manifest;
24  
25  /**
26   * @version $Rev: 410741 $ $Date: 2006-05-31 21:35:48 -0700 (Wed, 31 May 2006) $
27   */
28  public class DirectoryResourceLocation extends AbstractUrlResourceLocation {
29      private final File baseDir;
30      private boolean manifestLoaded = false;
31      private Manifest manifest;
32  
33      public DirectoryResourceLocation(File baseDir) throws MalformedURLException {
34          super(baseDir.toURL());
35          this.baseDir = baseDir;
36      }
37  
38      public ResourceHandle getResourceHandle(String resourceName) {
39          File file = new File(baseDir, resourceName);
40          if (!file.exists()) {
41              return null;
42          }
43  
44          try {
45              ResourceHandle resourceHandle = new DirectoryResourceHandle(resourceName, file, baseDir, getManifestSafe());
46              return resourceHandle;
47          } catch (MalformedURLException e) {
48              return null;
49          }
50      }
51  
52      public Manifest getManifest() throws IOException {
53          if (!manifestLoaded) {
54              File manifestFile = new File(baseDir, "META-INF/MANIFEST.MF");
55  
56              if (manifestFile.isFile() && manifestFile.canRead()) {
57                  FileInputStream in = null;
58                  try {
59                      in = new FileInputStream(manifestFile);
60                      manifest = new Manifest(in);
61                  } finally {
62                      IoUtil.close(in);
63                  }
64              }
65              manifestLoaded = true;
66          }
67          return manifest;
68      }
69  
70      private Manifest getManifestSafe() {
71          Manifest manifest = null;
72          try {
73              manifest = getManifest();
74          } catch (IOException e) {
75              // ignore
76          }
77          return manifest;
78      }
79  }