You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2008/03/02 04:35:12 UTC

svn commit: r632706 - /openejb/trunk/openejb3/container/openejb-loader/src/main/java/org/apache/openejb/loader/JarLocation.java

Author: dblevins
Date: Sat Mar  1 19:35:12 2008
New Revision: 632706

URL: http://svn.apache.org/viewvc?rev=632706&view=rev
Log:
Small utility class for getting the url or file object of the jar or directory containing a specific class
Sort of a linux command "which" of classes

Added:
    openejb/trunk/openejb3/container/openejb-loader/src/main/java/org/apache/openejb/loader/JarLocation.java

Added: openejb/trunk/openejb3/container/openejb-loader/src/main/java/org/apache/openejb/loader/JarLocation.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-loader/src/main/java/org/apache/openejb/loader/JarLocation.java?rev=632706&view=auto
==============================================================================
--- openejb/trunk/openejb3/container/openejb-loader/src/main/java/org/apache/openejb/loader/JarLocation.java (added)
+++ openejb/trunk/openejb3/container/openejb-loader/src/main/java/org/apache/openejb/loader/JarLocation.java Sat Mar  1 19:35:12 2008
@@ -0,0 +1,55 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.openejb.loader;
+
+import java.io.File;
+import java.net.URI;
+import java.net.URL;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JarLocation {
+
+    public static File get() {
+        return jarLocation(JarLocation.class);
+    }
+
+    public static File jarLocation(Class clazz) {
+        try {
+            String classFileName = clazz.getName().replace(".", "/") + ".class";
+
+            URL classURL = clazz.getClassLoader().getResource(classFileName);
+
+            URI uri = classURL.toURI();
+            if (uri.getPath() == null){
+                uri = new URI(uri.getSchemeSpecificPart());
+            }
+
+            String path = uri.getPath();
+            if (path.contains("!")){
+                path = path.substring(0, path.indexOf('!'));
+            } else {
+                path = path.substring(0, path.length() - classFileName.length());
+            }
+
+            return new File(path);
+        } catch (Exception e) {
+            throw new IllegalStateException(e);
+        }
+    }
+}