KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > util > URLType


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
18 package org.apache.geronimo.deployment.util;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.JarURLConnection JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLConnection JavaDoc;
26 import java.util.jar.JarFile JavaDoc;
27 import java.util.zip.ZipException JavaDoc;
28
29 /**
30  * The URLType class assigns type to resources, i.e. files or URLs.
31  * <p>
32  * The following types are available:
33  * <ul>
34  * <li><b>UNPACKED_ARCHIVE</b> - directory with META-INF/MANIFEST.MF
35  * <li><b>PACKED_ARCHIVE</b> - file with META-INF/MANIFEST.MF
36  * <li><b>COLLECTION</b> - directory with no META-INF/MANIFEST.MF
37  * <li><b>RESOURCE</b> - none of the above
38  * </ul>
39  *
40  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
41  */

42 public class URLType {
43     public static final String JavaDoc MANIFEST_LOCATION = "META-INF/MANIFEST.MF";
44
45     public static final URLType RESOURCE = new URLType("RESOURCE");
46     public static final URLType COLLECTION = new URLType("COLLECTION");
47     public static final URLType PACKED_ARCHIVE = new URLType("PACKED_ARCHIVE");
48     public static final URLType UNPACKED_ARCHIVE = new URLType("UNPACKED_ARCHIVE");
49
50     public static URLType getType(File JavaDoc file) throws IOException JavaDoc {
51         if (file.isDirectory()) {
52             // file is a directory - see if it has a manifest
53
// we check for an actual manifest file to keep things consistent with a packed archive
54
if (new File JavaDoc(file, MANIFEST_LOCATION).exists()) {
55                 return UNPACKED_ARCHIVE;
56             } else {
57                 return COLLECTION;
58             }
59         } else {
60             // we have a regular file - see if it contains a manifest
61
try {
62                 JarFile JavaDoc jar = null;
63                 try {
64                     jar = new JarFile JavaDoc(file);
65                     jar.getManifest();
66                 } finally {
67                     if (jar != null) {
68                         jar.close();
69                     }
70                 }
71                 return PACKED_ARCHIVE;
72             } catch (ZipException JavaDoc e) {
73                 return RESOURCE;
74             }
75         }
76     }
77
78     /**
79      * Returns the type of url
80      *
81      * @param url
82      * @return type of the url
83      * @throws IOException whenever there're problems with accessing portion of the url
84      */

85     public static URLType getType(URL JavaDoc url) throws IOException JavaDoc {
86         if (url.toString().endsWith("/")) {
87             URL JavaDoc metaInfURL = new URL JavaDoc(url, MANIFEST_LOCATION);
88             URLConnection JavaDoc urlConnection = metaInfURL.openConnection();
89             urlConnection.connect();
90             try {
91                 InputStream JavaDoc is = urlConnection.getInputStream();
92                 is.close();
93                 return UNPACKED_ARCHIVE;
94             } catch (IOException JavaDoc e) {
95                 return COLLECTION;
96             }
97         } else {
98             URL JavaDoc jarURL = new URL JavaDoc("jar:" + url.toString() + "!/");
99             JarURLConnection JavaDoc jarConnection = (JarURLConnection JavaDoc) jarURL.openConnection();
100             try {
101                 jarConnection.getManifest();
102                 return PACKED_ARCHIVE;
103             } catch (ZipException JavaDoc e) {
104                 return RESOURCE;
105             }
106         }
107     }
108
109     private final String JavaDoc desc;
110
111     private URLType(final String JavaDoc desc) {
112         this.desc = desc;
113     }
114
115     public boolean equals(Object JavaDoc obj) {
116         return this == obj;
117     }
118
119     public String JavaDoc toString() {
120         return desc;
121     }
122 }
123
Popular Tags