KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > classloader > DirectoryResourceLocation


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.util.jar.Manifest JavaDoc;
24
25 /**
26  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
27  */

28 public class DirectoryResourceLocation extends AbstractUrlResourceLocation {
29     private final File JavaDoc baseDir;
30     private boolean manifestLoaded = false;
31     private Manifest JavaDoc manifest;
32
33     public DirectoryResourceLocation(File JavaDoc baseDir) throws MalformedURLException JavaDoc {
34         super(baseDir.toURL());
35         this.baseDir = baseDir;
36     }
37
38     public ResourceHandle getResourceHandle(String JavaDoc resourceName) {
39         File JavaDoc file = new File JavaDoc(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 JavaDoc e) {
48             return null;
49         }
50     }
51
52     public Manifest JavaDoc getManifest() throws IOException JavaDoc {
53         if (!manifestLoaded) {
54             File JavaDoc manifestFile = new File JavaDoc(baseDir, "META-INF/MANIFEST.MF");
55
56             if (manifestFile.isFile() && manifestFile.canRead()) {
57                 FileInputStream JavaDoc in = null;
58                 try {
59                     in = new FileInputStream JavaDoc(manifestFile);
60                     manifest = new Manifest JavaDoc(in);
61                 } finally {
62                     IoUtil.close(in);
63                 }
64             }
65             manifestLoaded = true;
66         }
67         return manifest;
68     }
69
70     private Manifest JavaDoc getManifestSafe() {
71         Manifest JavaDoc manifest = null;
72         try {
73             manifest = getManifest();
74         } catch (IOException JavaDoc e) {
75             // ignore
76
}
77         return manifest;
78     }
79 }
80
Popular Tags