01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.openejb.loader;
17:
18: import java.io.File;
19: import java.net.URI;
20: import java.net.URL;
21: import java.net.URLDecoder;
22:
23: /**
24: * @version $Rev$ $Date$
25: */
26: public class JarLocation {
27:
28: public static File get() {
29: return jarLocation(JarLocation.class);
30: }
31:
32: public static File jarLocation(Class clazz) {
33: try {
34: String classFileName = clazz.getName().replace(".", "/")
35: + ".class";
36:
37: URL classURL = clazz.getClassLoader().getResource(
38: classFileName);
39:
40: URI uri = null;
41: String url = classURL.toExternalForm();
42: if (url.contains(" ")) {
43: url = url.replaceAll(" ", "%20");
44: }
45: uri = new URI(url);
46:
47: if (uri.getPath() == null) {
48: uri = new URI(uri.getRawSchemeSpecificPart());
49: }
50:
51: String path = uri.getPath();
52: if (path.contains("!")) {
53: path = path.substring(0, path.indexOf('!'));
54: } else {
55: path = path.substring(0, path.length()
56: - classFileName.length());
57: }
58:
59: return new File(URLDecoder.decode(path));
60: } catch (Exception e) {
61: throw new IllegalStateException(e);
62: }
63: }
64: }
|