List of usage examples for java.lang Class getProtectionDomain
public java.security.ProtectionDomain getProtectionDomain()
From source file:gov.nih.nci.caarray.external.v1_0.AbstractCaArrayEntityTest.java
public static <T> List<Class<? extends T>> findLocalSubclasses(Class<T> c) throws ClassNotFoundException, URISyntaxException { List<Class<? extends T>> l = new ArrayList<Class<? extends T>>(); File dir = new File(c.getProtectionDomain().getCodeSource().getLocation().toURI()); Iterator<File> fi = FileUtils.iterateFiles(dir, new String[] { "class" }, true); int prefix = dir.getAbsolutePath().length() + 1; int suffix = ".class".length(); while (fi.hasNext()) { File cf = fi.next();//www . jav a2 s. com String fn = cf.getAbsolutePath(); try { String cn = fn.substring(prefix, fn.length() - suffix); if (cn.endsWith("<error>")) { continue; } cn = cn.replace('/', '.'); System.out.println("cn = " + cn); Class tmp = c.getClassLoader().loadClass(cn); if ((tmp.getModifiers() & Modifier.ABSTRACT) != 0) { continue; } if (c.isAssignableFrom(tmp)) { l.add(tmp); System.out.println("added " + cf.getAbsolutePath() + " as " + cn); } } catch (Exception e) { System.err.println(fn); e.printStackTrace(); } } return l; }
From source file:de.mpc.pia.tools.PIATools.java
/** * This method return the full path of specified subPath. */// w w w .j a va 2 s. c o m public static URL getFullPath(Class cs, String subPath) throws MalformedURLException { if (cs == null) { throw new IllegalArgumentException("Input class cannot be NULL"); } URL fullPath = null; CodeSource src = cs.getProtectionDomain().getCodeSource(); if (src != null) { if (subPath == null) { fullPath = src.getLocation(); } else { fullPath = new URL(src.getLocation(), subPath); } } return fullPath; }
From source file:com.seleniumtests.util.FileUtility.java
public static void extractJar(final String storeLocation, final Class<?> clz) throws IOException { File firefoxProfile = new File(storeLocation); String location = clz.getProtectionDomain().getCodeSource().getLocation().getFile(); try (JarFile jar = new JarFile(location);) { logger.info("Extracting jar file::: " + location); firefoxProfile.mkdir();// w ww . j a v a 2 s . c o m Enumeration<?> jarFiles = jar.entries(); while (jarFiles.hasMoreElements()) { ZipEntry entry = (ZipEntry) jarFiles.nextElement(); String currentEntry = entry.getName(); File destinationFile = new File(storeLocation, currentEntry); File destinationParent = destinationFile.getParentFile(); // create the parent directory structure if required destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(jar.getInputStream(entry)); int currentByte; // buffer for writing file byte[] data = new byte[BUFFER]; // write the current file to disk try (FileOutputStream fos = new FileOutputStream(destinationFile);) { BufferedOutputStream destination = new BufferedOutputStream(fos, BUFFER); // read and write till last byte while ((currentByte = is.read(data, 0, BUFFER)) != -1) { destination.write(data, 0, currentByte); } destination.flush(); destination.close(); is.close(); } } } } FileUtils.deleteDirectory(new File(storeLocation + "\\META-INF")); if (OSUtility.isWindows()) { new File(storeLocation + "\\" + clz.getCanonicalName().replaceAll("\\.", "\\\\") + ".class").delete(); } else { new File(storeLocation + "/" + clz.getCanonicalName().replaceAll("\\.", "/") + ".class").delete(); } }
From source file:jenkins.security.ClassFilterImpl.java
/** * Tries to determine what JAR file a given class was loaded from. * The location is an opaque string suitable only for comparison to others. * Similar to {@link Which#jarFile(Class)} but potentially faster, and more tolerant of unknown URL formats. * @param c some class//from ww w . java 2 s . c o m * @return something typically like {@code file://plugins/structs/WEB-INF/lib/structs-1.10.jar}; * or null for classes in the Java Platform, some generated classes, etc. */ private static @CheckForNull String codeSource(@Nonnull Class<?> c) { CodeSource cs = c.getProtectionDomain().getCodeSource(); if (cs == null) { return null; } URL loc = cs.getLocation(); if (loc == null) { return null; } String r = loc.toString(); if (r.endsWith(".class")) { // JENKINS-49147: Tomcat bug. Now do the more expensive check String suffix = c.getName().replace('.', '/') + ".class"; if (r.endsWith(suffix)) { r = r.substring(0, r.length() - suffix.length()); } } if (r.startsWith("jar:file:/") && r.endsWith(".jar!/")) { // JENKINS-49543: also an old behavior of Tomcat. Legal enough, but unexpected by isLocationWhitelisted. r = r.substring(4, r.length() - 2); } return r; }
From source file:ie.deri.unlp.javaservices.topicextraction.topicextractor.gate.TopicExtractorGate.java
/** * /*from www . j a v a 2s . c o m*/ * Extract a directory in a JAR on the classpath to an output folder. * * Note: User's responsibility to ensure that the files are actually in a JAR. * * @param classInJar A class in the JAR file which is on the classpath * @param resourceDirectory Path to resource directory in JAR * @param outputDirectory Directory to write to * @return String containing the path to the outputDirectory * @throws IOException */ private static String extractDirectoryFromClasspathJAR(Class<?> classInJar, String resourceDirectory, String outputDirectory) throws IOException { resourceDirectory = StringUtils.strip(resourceDirectory, "\\/") + File.separator; URL jar = classInJar.getProtectionDomain().getCodeSource().getLocation(); JarFile jarFile = new JarFile(new File(jar.getFile())); byte[] buf = new byte[1024]; Enumeration<JarEntry> jarEntries = jarFile.entries(); while (jarEntries.hasMoreElements()) { JarEntry jarEntry = jarEntries.nextElement(); if (jarEntry.isDirectory() || !jarEntry.getName().startsWith(resourceDirectory)) { continue; } String outputFileName = FilenameUtils.concat(outputDirectory, jarEntry.getName()); //Create directories if they don't exist new File(FilenameUtils.getFullPath(outputFileName)).mkdirs(); //Write file FileOutputStream fileOutputStream = new FileOutputStream(outputFileName); int n; InputStream is = jarFile.getInputStream(jarEntry); while ((n = is.read(buf, 0, 1024)) > -1) { fileOutputStream.write(buf, 0, n); } is.close(); fileOutputStream.close(); } jarFile.close(); String fullPath = FilenameUtils.concat(outputDirectory, resourceDirectory); return fullPath; }
From source file:de.tuberlin.uebb.jbop.access.ClassAccessor.java
/** * To file.//from w w w. j a va 2s . c om * * @param clazz * the clazz * @return the file * @throws JBOPClassException * the jBOP class exception */ static Path toPath(final Class<?> clazz) throws JBOPClassException { final CodeSource cs = clazz.getProtectionDomain().getCodeSource(); final URL resource = cs.getLocation(); if (resource == null) { throw new JBOPClassException("The Classfile for Class<" + clazz.getName() + "> couldn't be determined.", null); } String filename = FilenameUtils.normalize(FileUtils.toFile(resource).getAbsolutePath()); final String path = toClassPath(clazz); if (filename.endsWith(".jar")) { filename = filename + "!" + path; } else { filename = filename + "/" + path; } return Paths.get(filename); }
From source file:com.eucalyptus.entities.PersistenceContexts.java
private static boolean isDuplicate(Class entity) { PersistenceContext ctx = Ats.from(entity).get(PersistenceContext.class); if (Ats.from(entity).has(MappedSuperclass.class) || Ats.from(entity).has(Embeddable.class)) { return false; } else if (ctx == null || ctx.name() == null) { RuntimeException ex = new RuntimeException( "Failed to register broken entity class: " + entity.getCanonicalName() + ". Ensure that the class has a well-formed @PersistenceContext annotation."); LOG.error(ex, ex);/* ww w .j av a 2s. com*/ return false; } else if (sharedEntities.contains(entity)) { Class old = sharedEntities.get(sharedEntities.indexOf(entity)); LOG.error("Duplicate entity definition detected: " + entity.getCanonicalName()); LOG.error("=> OLD: " + old.getProtectionDomain().getCodeSource().getLocation()); LOG.error("=> NEW: " + entity.getProtectionDomain().getCodeSource().getLocation()); throw BootstrapException.throwFatal("Duplicate entity definition in shared entities: " + entity.getCanonicalName() + ". See error logs for details."); } else if (entities.get(ctx.name()) != null && entities.get(ctx.name()).contains(entity)) { List<Class> context = entities.get(ctx.name()); Class old = context.get(context.indexOf(entity)); LOG.error("Duplicate entity definition detected: " + entity.getCanonicalName()); LOG.error("=> OLD: " + old.getProtectionDomain().getCodeSource().getLocation()); LOG.error("=> NEW: " + entity.getProtectionDomain().getCodeSource().getLocation()); throw BootstrapException.throwFatal("Duplicate entity definition in '" + ctx.name() + "': " + entity.getCanonicalName() + ". See error logs for details."); } else { return false; } }
From source file:azkaban.utils.FileIOUtils.java
public static String getSourcePathFromClass(Class<?> containedClass) { File file = new File(containedClass.getProtectionDomain().getCodeSource().getLocation().getPath()); if (!file.isDirectory() && file.getName().endsWith(".class")) { String name = containedClass.getName(); StringTokenizer tokenizer = new StringTokenizer(name, "."); while (tokenizer.hasMoreTokens()) { tokenizer.nextElement();/* w w w . j av a 2 s . com*/ file = file.getParentFile(); } return file.getPath(); } else { return containedClass.getProtectionDomain().getCodeSource().getLocation().getPath(); } }
From source file:com.alibaba.rocketmq.common.MixAll.java
public static String findClassPath(Class<?> c) { URL url = c.getProtectionDomain().getCodeSource().getLocation(); return url.getPath(); }
From source file:com.elastica.helper.FileUtility.java
public static void extractJar(final String storeLocation, final Class<?> clz) throws IOException { File firefoxProfile = new File(storeLocation); String location = clz.getProtectionDomain().getCodeSource().getLocation().getFile(); JarFile jar = new JarFile(location); System.out.println("Extracting jar file::: " + location); firefoxProfile.mkdir();/*from w w w . j a va 2 s . co m*/ Enumeration<?> jarFiles = jar.entries(); while (jarFiles.hasMoreElements()) { ZipEntry entry = (ZipEntry) jarFiles.nextElement(); String currentEntry = entry.getName(); File destinationFile = new File(storeLocation, currentEntry); File destinationParent = destinationFile.getParentFile(); // create the parent directory structure if required destinationParent.mkdirs(); if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(jar.getInputStream(entry)); int currentByte; // buffer for writing file byte[] data = new byte[BUFFER]; // write the current file to disk FileOutputStream fos = new FileOutputStream(destinationFile); BufferedOutputStream destination = new BufferedOutputStream(fos, BUFFER); // read and write till last byte while ((currentByte = is.read(data, 0, BUFFER)) != -1) { destination.write(data, 0, currentByte); } destination.flush(); destination.close(); is.close(); } } FileUtils.deleteDirectory(new File(storeLocation + "\\META-INF")); if (OSUtility.isWindows()) { new File(storeLocation + "\\" + clz.getCanonicalName().replaceAll("\\.", "\\\\") + ".class").delete(); } else { new File(storeLocation + "/" + clz.getCanonicalName().replaceAll("\\.", "/") + ".class").delete(); } }