List of usage examples for java.net URL getFile
public String getFile()
From source file:edu.stanford.epad.common.plugins.impl.ClassFinderTestUtils.java
/** * Methods to find classes and annotations *//*w w w .j a v a2 s .c om*/ public static List<Class<?>> getClasses(String packageName) throws ClassNotFoundException, IOException { List<Class<?>> classes = new ArrayList<Class<?>>(); try { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // ClassLoader classLoader = ClassLoader.getSystemClassLoader(); String path = packageName.replace('.', '/'); Enumeration<URL> resources = classLoader.getResources(path); List<File> dirs = new ArrayList<File>(); while (resources.hasMoreElements()) { URL resource = resources.nextElement(); dirs.add(new File(resource.getFile())); } logger.info("getClasses has: " + dirs.size() + " directories."); for (File directory : dirs) { logger.info("getClasses - dir=" + directory); classes.addAll(ClassFinderTestUtils.findClasses(directory, packageName)); } } catch (Exception e) { logger.warning("getClass had: " + e.getMessage(), e); } return classes; }
From source file:com.centeractive.ws.builder.DefinitionSaveTest.java
public static File getServiceFolder(int serviceId) { URL definitionUrl = ServiceComplianceTest.getDefinitionUrl(serviceId); File definitionFile = new File(definitionUrl.getFile()); File serviceFolder = new File(definitionUrl.getFile()).getParentFile(); if (serviceFolder.exists() == false) { throw new RuntimeException("Cannot get service folder for service " + serviceId); }/*from ww w .j a v a 2s . com*/ return serviceFolder; }
From source file:com.ibm.team.build.internal.hjplugin.RTCFacadeFactory.java
private static URL getHjplugin_rtcJar(ClassLoader originalClassLoader, String fullClassName, PrintStream debugLog) {//from w w w .j a v a 2s .c o m if (originalClassLoader instanceof URLClassLoader) { URLClassLoader urlClassLoader = (URLClassLoader) originalClassLoader; URL[] originalURLs = urlClassLoader.getURLs(); for (URL url : originalURLs) { String file = url.getFile(); if (file.contains("com.ibm.team.build.hjplugin-rtc")) { //$NON-NLS-1$ //$NON-NLS-2$ debug(debugLog, "Found hjplugin-rtc jar " + url.getFile()); //$NON-NLS-1$ return url; } } debug(debugLog, "Did not find hjplugin-rtc jar from URLClassLoader"); //$NON-NLS-1$ } String realClassName = fullClassName.replace('.', '/') + ".class"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ URL url = originalClassLoader.getResource(realClassName); debug(debugLog, "Found " + realClassName + " in " + url.toString()); //$NON-NLS-1$ //$NON-NLS-2$ try { URLConnection connection = url.openConnection(); if (connection instanceof JarURLConnection) { JarURLConnection jarConnection = (JarURLConnection) connection; debug(debugLog, "hjplugin-rtc jar from the connection " + jarConnection.getJarFileURL()); //$NON-NLS-1$ return jarConnection.getJarFileURL(); } } catch (IOException e) { debug(debugLog, "Unable to obtain URLConnection ", e); //$NON-NLS-1$ } debug(debugLog, "Unable to find hjplugin-rtc.jar"); //$NON-NLS-1$ return null; }
From source file:SerialVersionUID.java
/** * Build a TreeMap of the class name to ClassVersionInfo * /*from w w w . j ava 2s. c om*/ * @param jar * @param classVersionMap * TreeMap<String, ClassVersionInfo> for serializable classes * @param cl - * the class loader to use * @throws IOException * thrown if the jar cannot be opened */ static void generateJarSerialVersionUIDs(URL jar, TreeMap classVersionMap, ClassLoader cl, String pkgPrefix) throws IOException { String jarName = jar.getFile(); JarFile jf = new JarFile(jarName); Enumeration entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry entry = (JarEntry) entries.nextElement(); String name = entry.getName(); if (name.endsWith(".class") && name.startsWith(pkgPrefix)) { name = name.substring(0, name.length() - 6); String classname = name.replace('/', '.'); try { log.fine("Creating ClassVersionInfo for: " + classname); ClassVersionInfo cvi = new ClassVersionInfo(classname, cl); log.fine(cvi.toString()); if (cvi.getSerialVersion() != 0) { ClassVersionInfo prevCVI = (ClassVersionInfo) classVersionMap.put(classname, cvi); if (prevCVI != null) { if (prevCVI.getSerialVersion() != cvi.getSerialVersion()) { log.severe("Found inconsistent classes, " + prevCVI + " != " + cvi + ", jar: " + jarName); } } if (cvi.getHasExplicitSerialVersionUID() == false) { log.warning("No explicit serialVersionUID: " + cvi); } } } catch (OutOfMemoryError e) { log.log(Level.SEVERE, "Check the MaxPermSize", e); } catch (Throwable e) { log.log(Level.FINE, "While loading: " + name, e); } } } jf.close(); }
From source file:mzmatch.util.Tool.java
/** * // w ww . ja v a2 s. c o m * * @param pckgname * @return * @throws ClassNotFoundException */ public static Vector<Class<? extends Object>> getAllClasses(String pckgname) throws ClassNotFoundException { Vector<Class<? extends Object>> classes = new Vector<Class<? extends Object>>(); // load the current package ClassLoader cld = Thread.currentThread().getContextClassLoader(); if (cld == null) throw new ClassNotFoundException("Can't get class loader."); String path = pckgname.replace('.', '/'); URL resource = cld.getResource(path); if (resource == null) throw new ClassNotFoundException("No resource for " + path); // parse the directory File directory = new File(resource.getFile()); if (directory.isDirectory() && directory.exists()) { for (File f : directory.listFiles()) { if (f.getName().endsWith(".class")) classes.add(Class.forName(pckgname + '.' + f.getName().substring(0, f.getName().length() - 6))); } for (File f : directory.listFiles()) { if (f.isDirectory()) classes.addAll(getAllClasses(pckgname + "." + f.getName())); } } else throw new ClassNotFoundException(pckgname + " does not appear to be a valid package"); return classes; }
From source file:kr.co.exsoft.eframework.util.LicenseUtil.java
/** * // w w w . j a v a 2s .co m * <pre> * 1. : ?? APPLICATION * 2. : * </pre> * @Method Name : decipherLicenseKey * @param licenseKey * @return String * @throws Exception */ public static String decipherLicenseKey(String licenseKey) throws Exception { String ret = null; if (StringUtils.isNotBlank(licenseKey)) { // ?? ? public key ? URL url = ClassLoader.getSystemResource("kr/co/exsoft/eframework/cert/exsoft.cer"); FileInputStream certfis = new FileInputStream(new File(url.getFile())); CertificateFactory cf = CertificateFactory.getInstance("X.509"); Certificate cert = cf.generateCertificate(certfis); PublicKey key = cert.getPublicKey(); // ?? . ret = unspell(licenseKey, key); } return ret; }
From source file:io.cloudine.rhq.plugins.worker.ResourceUtils.java
/** * URL(Jar ?? ? Jar ? ?) Jar ?? URL? (Jar ? ? ? Jar ? ? ?). * * @param jarUrl ? // w w w .j a v a 2 s . c om * @return ?? {@link java.net.URL} ? * @throws java.net.MalformedURLException URL ?? ? */ public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException { String urlFile = jarUrl.getFile(); int separatorIndex = urlFile.indexOf(JAR_URL_SEPARATOR); if (separatorIndex != -1) { String jarFile = urlFile.substring(0, separatorIndex); try { return new URL(jarFile); } catch (MalformedURLException e) { // ? ? Jar URL ? ? ?(; "jar:C:/mypath/myjar.jar"). // ? ? Jar ?? ? if (!jarFile.startsWith("/")) { jarFile = "/" + jarFile; } return new URL(FILE_URL_PREFIX + jarFile); } } else { return jarUrl; } }
From source file:com.fmguler.ven.support.LiquibaseConverter.java
/** * Scans all classes accessible from the context class loader which belong to the given package and subpackages. * * @param packageName The base package/*from w ww.j av a 2 s.com*/ * @return The classes * @throws ClassNotFoundException * @throws IOException */ private static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); assert classLoader != null; String path = packageName.replace('.', '/'); Enumeration resources = classLoader.getResources(path); List dirs = new ArrayList(); while (resources.hasMoreElements()) { URL resource = (URL) resources.nextElement(); dirs.add(new File(resource.getFile())); } ArrayList classes = new ArrayList(); Iterator it = dirs.iterator(); while (it.hasNext()) { File directory = (File) it.next(); classes.addAll(findClasses(directory, packageName)); } return (Class[]) classes.toArray(new Class[classes.size()]); }
From source file:com.fengduo.bee.commons.core.lang.ClassLoaderUtils.java
public static List<?> load(String classpath, ClassFilter filter) throws Exception { List<Object> objs = new ArrayList<Object>(); URL resource = ClassLoaderUtils.class.getClassLoader().getResource(classpath); logger.debug("Search from {} ...", resource.getPath()); List<String> classnameArray; if ("jar".equalsIgnoreCase(resource.getProtocol())) { String file = resource.getFile(); String jarName = file.substring(file.indexOf("/"), (file.lastIndexOf("jar") + 3)); classnameArray = getClassNamesInPackage(jarName, classpath); } else {//from ww w. j a v a2 s . c o m Collection<File> listFiles = FileUtils.listFiles(new File(resource.getPath()), null, false); String classNamePrefix = classpath.replaceAll("/", "."); classnameArray = new ArrayList<String>(); for (File file : listFiles) { String name = file.getName(); if (name.endsWith(".class") == false) { continue; } if (StringUtils.contains(name, '$')) { logger.warn("NOT SUPPORT INNERT CLASS" + file.getAbsolutePath()); continue; } String classname = classNamePrefix + "." + StringUtils.remove(name, ".class"); classnameArray.add(classname); } } for (String classname : classnameArray) { try { Class<?> loadClass = ClassLoaderUtils.class.getClassLoader().loadClass(classname); if (filter != null && !filter.filter(loadClass)) { logger.error("{} {} ", classname, filter); continue; } // if (ClassLoaderUtil.class.isAssignableFrom(loadClass) == false) { // logger.error("{} ?????", classname); // continue; // } Object newInstance = loadClass.newInstance(); objs.add(newInstance); logger.debug("load {}/{}.class success", resource.getPath(), classname); } catch (Exception e) { e.printStackTrace(); logger.error("load " + resource.getPath() + "/" + classname + ".class failed", e); } } return objs; }
From source file:kr.co.exsoft.eframework.util.LicenseUtil.java
/** * ?? ?./*from w w w. j a v a 2 s. c om*/ * * @param licenseType * @param userCount * @return String */ public static String generateLicenseKey(String licenseType, int userCount) { String ksPass = "loveboat"; String keyPass = "loveboat"; String alias = "ab942e0f-9e4a-44b9-9f82-0a5f5d48ba12"; String ret = null; try { // ?? URL url = ClassLoader.getSystemResource("kr/co/exsoft/eframework/cert/exsoft.pfx"); FileInputStream certfis = new FileInputStream(new File(url.getFile())); // Private Key ?. BufferedInputStream ksbufin = new BufferedInputStream(certfis); KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(ksbufin, ksPass.toCharArray()); PrivateKey key = (PrivateKey) ks.getKey(alias, keyPass.toCharArray()); // ?? ?. ret = spell("EDMsl|" + licenseType + "|" + userCount + "|", key); } catch (Exception e) { e.printStackTrace(); } return ret; }