Back to index

wims  3.65+svn20090927
ClassUtilities.java
Go to the documentation of this file.
00001 /* Utilities used to manipulate classes
00002 
00003  Copyright (c) 2003-2005 The Regents of the University of California.
00004  All rights reserved.
00005  Permission is hereby granted, without written agreement and without
00006  license or royalty fees, to use, copy, modify, and distribute this
00007  software and its documentation for any purpose, provided that the above
00008  copyright notice and the following two paragraphs appear in all copies
00009  of this software.
00010 
00011  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
00012  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
00013  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
00014  THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
00015  SUCH DAMAGE.
00016 
00017  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00018  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
00020  PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
00021  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
00022  ENHANCEMENTS, OR MODIFICATIONS.
00023 
00024  PT_COPYRIGHT_VERSION_2
00025  COPYRIGHTENDKEY
00026 
00027  */
00028 package ptolemy.util;
00029 
00030 import java.io.File;
00031 import java.io.IOException;
00032 import java.net.URL;
00033 
00036 
00048 public class ClassUtilities {
00051     private ClassUtilities() {
00052     }
00053 
00056 
00086     public static URL jarURLEntryResource(String jarURLString)
00087             throws IOException {
00088         // At first glance, it would appear that this method could appear
00089         // in specToURL(), but the problem is that specToURL() creates
00090         // a new URL with the spec, so it only does further checks if
00091         // the URL is malformed.  Unfortunately, in Web Start applications
00092         // the URL will often refer to a resource in another jar file,
00093         // which means that the jar url is not malformed, but there is
00094         // no resource by that name.  Probably specToURL() should return
00095         // the resource after calling new URL().
00096         int jarEntry = jarURLString.indexOf("!/");
00097 
00098         if (jarEntry == -1) {
00099             jarEntry = jarURLString.indexOf("!\\");
00100 
00101             if (jarEntry == -1) {
00102                 return null;
00103             }
00104         }
00105 
00106         try {
00107             // !/ means that this could be in a jar file.
00108             String entry = jarURLString.substring(jarEntry + 2);
00109 
00110             // We might be in the Swing Event thread, so
00111             // Thread.currentThread().getContextClassLoader()
00112             // .getResource(entry) probably will not work.
00113             Class refClass = Class.forName("ptolemy.util.ClassUtilities");
00114             URL entryURL = refClass.getClassLoader().getResource(entry);
00115             return entryURL;
00116         } catch (Exception ex) {
00117             // IOException constructor does not take a cause, so we add it.
00118             IOException ioException = new IOException("Cannot find \""
00119                     + jarURLString + "\".");
00120             ioException.initCause(ex);
00121             throw ioException;
00122         }
00123     }
00124 
00135     public static String lookupClassAsResource(String necessaryClass) {
00136         // This method is called from copernicus.kernel.GeneratorAttribute
00137         // and actor.lib.python.PythonScript.  We moved it here
00138         // to avoid dependencies.
00139         String necessaryResource = StringUtilities.substitute(necessaryClass,
00140                 ".", "/")
00141                 + ".class";
00142 
00143         URL necessaryURL = Thread.currentThread().getContextClassLoader()
00144                 .getResource(necessaryResource);
00145 
00146         if (necessaryURL != null) {
00147             String resourceResults = necessaryURL.getFile();
00148 
00149             // Strip off the file:/ and the necessaryResource.
00150             if (resourceResults.startsWith("file:/")) {
00151                 resourceResults = resourceResults.substring(6);
00152             }
00153 
00154             // Strip off the name of the resource we were looking for
00155             // so that we are left with the directory or jar file
00156             // it is in
00157             resourceResults = resourceResults.substring(0, resourceResults
00158                     .length()
00159                     - necessaryResource.length());
00160 
00161             // Strip off the trailing !/
00162             if (resourceResults.endsWith("!/")) {
00163                 resourceResults = resourceResults.substring(0, resourceResults
00164                         .length() - 2);
00165             }
00166 
00167             // Unfortunately, under Windows, URL.getFile() may
00168             // return things like /c:/ptII, so we create a new
00169             // File and get its path, which will return c:\ptII
00170             File resourceFile = new File(resourceResults);
00171 
00172             // Convert backslashes
00173             String sanitizedResourceName = StringUtilities.substitute(
00174                     resourceFile.getPath(), "\\", "/");
00175             return sanitizedResourceName;
00176         }
00177 
00178         return null;
00179     }
00180 }