List of usage examples for java.security CodeSource getLocation
public final URL getLocation()
From source file:org.terentich.pram.tookit.MetaReader.java
private static Manifest getCurrentJarManifest() { Manifest manifest = null;//w w w . j ava2s . c o m CodeSource code = MetaReader.class.getProtectionDomain().getCodeSource(); try { String currentFile = URLDecoder.decode(code.getLocation().getFile(), CharEncoding.UTF_8); if (currentFile.endsWith(".jar")) { JarFile jar = new JarFile(currentFile); manifest = jar.getManifest(); } } catch (IOException e) { System.out.println("An exception occurs: " + e); } return manifest; }
From source file:ro.nextreports.designer.util.I18NSupport.java
public static List<String> getUserI18NFiles() { ClassLoader loader = Thread.currentThread().getContextClassLoader(); final String dir = "i18n/"; final String bundlename = "next-ui"; URL url = loader.getResource(dir); List<String> result = new ArrayList<String>(); File root = new File(url.getFile()); // run from java ide (i18n folder is found in classpath) if (root.exists()) { File[] files = root.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.matches("^" + bundlename + "(_\\w{2}(_\\w{2})?)?\\.properties$"); }//from w w w. ja va 2 s . co m }); for (File file : files) { result.add(file.getName()); } // run from jar (i18n folder in inside jar) } else { // find the jar where i18n folder is located CodeSource src = I18NSupport.class.getProtectionDomain().getCodeSource(); if (src != null) { URL jar = src.getLocation(); try { // look for all i18n files with both language and country specified // (existing i18n files have only the language) ZipInputStream zip = new ZipInputStream(jar.openStream()); ZipEntry entry = zip.getNextEntry(); while (entry != null) { if (entry.getName().startsWith(dir + bundlename)) { if (entry.getName().split("_").length == 3) { result.add(entry.getName().substring(dir.length())); LOG.info("I18NSupport found a new i18n file: " + entry.getName().substring(dir.length())); } } zip.closeEntry(); entry = zip.getNextEntry(); } zip.close(); } catch (IOException ex) { ex.printStackTrace(); LOG.error(ex.getMessage(), ex); } } } return result; }
From source file:de.mpc.pia.tools.PIATools.java
/** * This method return the full path of specified subPath. *//* w ww.jav a 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:org.atomserver.utils.io.JarUtils.java
/** * Find out where a class on the classpath will be loaded from. * This will be either a Jar URL (e.g. ) * or a File URL (e.g. file:/foo/bar/target/classes) * @param className name of fully qualified class to find, using dots, but no dot class. * e.g. org.atomserver.utils.io.JarUtils * @return The URL associated with this class * @throws ClassNotFoundException/*from ww w . j a v a2s . co m*/ */ static public URL getURLForClass(String className) throws ClassNotFoundException { Class qc = Class.forName(className); CodeSource source = qc.getProtectionDomain().getCodeSource(); URL location = null; if (source != null) { location = source.getLocation(); log.debug(className + " : " + location); } return location; }
From source file:org.apache.drill.yarn.core.DoYUtil.java
public static Object dynamicCall(Object target, String fnName, Object args[], Class<?> types[]) { // First, look for the method using the names and types provided. final String methodLabel = target.getClass().getName() + "." + fnName; Method m;/*w ww .ja v a2s . c o m*/ try { m = target.getClass().getMethod(fnName, types); } catch (NoSuchMethodException e) { // Ignore, but log: the method does not exist in this distribution. StringBuilder buf = new StringBuilder(); if (types != null) { String sep = ""; for (Class<?> type : types) { buf.append(sep); buf.append(type.getName()); sep = ","; } } LOG.trace("Not supported in this YARN distribution: " + methodLabel + "(" + buf.toString() + ")"); CodeSource src = target.getClass().getProtectionDomain().getCodeSource(); if (src != null) { java.net.URL jar = src.getLocation(); LOG.trace("Class found in URL: " + jar.toString()); } return null; } catch (SecurityException e) { LOG.error("Security prevents dynamic method calls", e); return null; } // Next, call the method with the arguments provided. Object ret = null; try { ret = m.invoke(target, args); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { LOG.error("Failed to dynamically call " + methodLabel, e); return null; } StringBuilder buf = new StringBuilder(); if (args != null) { String sep = ""; for (Object arg : args) { buf.append(sep); buf.append(arg == null ? "null" : arg.toString()); sep = ","; } } LOG.trace("Successfully called " + methodLabel + "( " + buf.toString() + ")"); // Return any return value. Will be null if the method is returns void. return ret; }
From source file:mSearch.tool.Functions.java
public static String getPathJar() { // liefert den Pfad der Programmdatei mit File.separator am Schluss String pFilePath = "version.properties"; File propFile = new File(pFilePath); if (!propFile.exists()) { try {//from w w w. j ava2 s . c om CodeSource cS = Main.class.getProtectionDomain().getCodeSource(); File jarFile = new File(cS.getLocation().toURI().getPath()); String jarDir = jarFile.getParentFile().getPath(); propFile = new File(jarDir + File.separator + pFilePath); } catch (Exception ignored) { } } else { DbgMsg.print("getPath"); } String s = propFile.getAbsolutePath().replace(pFilePath, ""); if (!s.endsWith(File.separator)) { s = s + File.separator; } if (s.endsWith("/lib/")) { // dann sind wir in der msearch-lib s = s.replace("/lib/", ""); } return s; }
From source file:org.broadleafcommerce.common.extensibility.InstrumentationRuntimeFactory.java
private static String getAgentJar() { File agentJarFile = null;/* w w w .j a v a 2 s. c o m*/ // Find the name of the File that this class was loaded from. That // jar *should* be the same location as our agent. CodeSource cs = InstrumentationRuntimeFactory.class.getProtectionDomain().getCodeSource(); if (cs != null) { URL loc = cs.getLocation(); if (loc != null) { agentJarFile = new File(loc.getFile()); } } // Determine whether the File that this class was loaded from has this // class defined as the Agent-Class. boolean createJar = false; if (cs == null || agentJarFile == null || agentJarFile.isDirectory()) { createJar = true; } else if (!validateAgentJarManifest(agentJarFile, InstrumentationRuntimeFactory.class.getName())) { // We have an agentJarFile, but this class isn't the Agent-Class. createJar = true; } String agentJar; if (createJar) { try { agentJar = createAgentJar(); } catch (IOException ioe) { agentJar = null; } } else { agentJar = agentJarFile.getAbsolutePath(); } return agentJar; }
From source file:org.glowroot.common2.repo.util.Compilations.java
private static File getJarFile(String className) throws Exception { CodeSource codeSource = Class.forName(className).getProtectionDomain().getCodeSource(); if (codeSource == null) { throw new IllegalStateException("Code source is null for class: " + className); }/*from ww w . j a v a2 s . c o m*/ URL location = codeSource.getLocation(); return new File(location.toURI()); }
From source file:com.tc.util.ProductInfo.java
static InputStream getData(String name) { CodeSource codeSource = ProductInfo.class.getProtectionDomain().getCodeSource(); if (codeSource != null && codeSource.getLocation() != null) { URL source = codeSource.getLocation(); if (source.getProtocol().equals("file") && source.toExternalForm().endsWith(".jar")) { URL res;/* w ww.j a v a 2 s.c om*/ try { res = new URL("jar:" + source.toExternalForm() + "!" + name); InputStream in = res.openStream(); if (in != null) { return in; } } catch (MalformedURLException e) { throw new AssertionError(e); } catch (IOException e) { // must not be embedded in this jar -- resolve via loader path } } else if (source.getProtocol().equals("file") && (new File(source.getPath()).isDirectory())) { File local = new File(source.getPath(), name); if (local.isFile()) { try { return new FileInputStream(local); } catch (FileNotFoundException e) { throw new AssertionError(e); } } } } return ProductInfo.class.getResourceAsStream(name); }
From source file:de.tuberlin.uebb.jbop.access.ClassAccessor.java
/** * To file./*from w w w.j a v a 2s . c o m*/ * * @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); }