use of java.security.CodeSource in project jdk8u_jdk by JetBrains.
the class TokenStore method init.
private static void init() throws Exception {
// first write policy files
PolicyParser pp = new PolicyParser();
pp.read(new StringReader(POLICY_NO_STORE));
pp.write(new FileWriter(NO_STORE_FILE, false));
pp = new PolicyParser();
pp.read(new StringReader(POLICY_URL));
pp.write(new FileWriter(URL_FILE, false));
pp = new PolicyParser();
pp.read(new StringReader(POLICY_URL_T));
pp.write(new FileWriter(URL_T_FILE, false));
pp = new PolicyParser();
pp.read(new StringReader(POLICY_URL_T_P));
pp.write(new FileWriter(URL_T_P_FILE, false));
pp = new PolicyParser();
pp.read(new StringReader(POLICY_URL_PWD));
pp.write(new FileWriter(URL_PWD_FILE, false));
pp = new PolicyParser();
pp.read(new StringReader(POLICY_URL_T_P_PWD));
pp.write(new FileWriter(URL_T_P_PWD_FILE, false));
pp = new PolicyParser();
pp.read(new StringReader(POLICY_BADPASS));
pp.write(new FileWriter(BADPASS_FILE, false));
// next load keystore data to build PD's
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(new FileInputStream(System.getProperty("test.src", ".") + File.separatorChar + "TokenStore.keystore"), storePassword);
NO_STORE_DOMAIN = new ProtectionDomain(new CodeSource(new URL("file:/foo"), (java.security.cert.Certificate[]) null), // perms
null, // class loader
null, // principals
null);
Certificate[] chain = (Certificate[]) ks.getCertificateChain("POLICY_URL");
URL_DOMAIN = new ProtectionDomain(new CodeSource(new URL("file:/foo"), chain), // perms
null, // class loader
null, // principals
null);
chain = (Certificate[]) ks.getCertificateChain("POLICY_URL_T");
URL_T_DOMAIN = new ProtectionDomain(new CodeSource(new URL("file:/foo"), chain), // perms
null, // class loader
null, // principals
null);
chain = (Certificate[]) ks.getCertificateChain("POLICY_URL_T_P");
URL_T_P_DOMAIN = new ProtectionDomain(new CodeSource(new URL("file:/foo"), chain), // perms
null, // class loader
null, // principals
null);
}
use of java.security.CodeSource in project tdme by andreasdr.
the class StandardFileSystem method list.
/*
* (non-Javadoc)
* @see net.drewke.tdme.os.FileSystemInterface#listFiles(java.lang.String, java.io.FilenameFilter)
*/
public String[] list(String path, FilenameFilter filter) throws IOException {
ArrayList<String> files = new ArrayList<String>();
// list files in file system
String[] fileSystemFiles = new File(path).list(filter);
if (fileSystemFiles != null) {
for (String fileName : fileSystemFiles) {
files.add(fileName);
}
}
// list file in associated jar from calling class
try {
// we only support unix style path names in jar files
path = path.replace('\\', '/');
//
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
CodeSource src = Class.forName(stackTraceElements[2].getClassName()).getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
ZipInputStream zip = new ZipInputStream(jar.openStream());
while (true) {
ZipEntry e = zip.getNextEntry();
if (e == null)
break;
String name = e.getName();
if (name.startsWith(path)) {
String fileName = name.substring(path.length() + 1);
if (filter.accept(new File(path), fileName))
files.add(fileName);
}
}
}
} catch (ClassNotFoundException cnfe) {
//
}
// remove duplicate entries
ArrayList<String> filesNoDuplicates = new ArrayList<String>();
for (String file : files) {
boolean duplicate = false;
for (String _file : filesNoDuplicates) {
if (file.equals(_file)) {
duplicate = true;
break;
}
}
if (duplicate == false)
filesNoDuplicates.add(file);
}
//
String[] _files = new String[filesNoDuplicates.size()];
filesNoDuplicates.toArray(_files);
return _files;
}
use of java.security.CodeSource in project apex-core by apache.
the class JarHelper method getJar.
public String getJar(Class<?> jarClass) {
String jar = null;
final CodeSource codeSource = jarClass.getProtectionDomain().getCodeSource();
if (codeSource != null) {
URL location = codeSource.getLocation();
jar = sourceToJar.get(location);
if (jar == null) {
// don't create jar file from folders multiple times
if ("jar".equals(location.getProtocol())) {
try {
location = ((JarURLConnection) location.openConnection()).getJarFileURL();
} catch (IOException e) {
throw new AssertionError("Cannot resolve jar file for " + jarClass, e);
}
}
if ("file".equals(location.getProtocol())) {
jar = location.getFile();
final File dir = new File(jar);
if (dir.isDirectory()) {
try {
jar = createJar("apex-", dir, false);
} catch (IOException e) {
throw new AssertionError("Cannot resolve jar file for " + jarClass + ". URL " + location, e);
}
}
} else {
throw new AssertionError("Cannot resolve jar file for " + jarClass + ". URL " + location);
}
sourceToJar.put(location, jar);
logger.debug("added sourceLocation {} as {}", location, jar);
}
if (jar == null) {
throw new AssertionError("Cannot resolve jar file for " + jarClass);
}
}
return jar;
}
use of java.security.CodeSource in project poi by apache.
the class OOXMLLite method getLoadedClasses.
/**
*
* @param ptrn the pattern to filter output
* @return the classes loaded by the system class loader keyed by class name
*/
@SuppressWarnings("unchecked")
private static Map<String, Class<?>> getLoadedClasses(String ptrn) {
// make the field accessible, we defer this from static initialization to here to
// allow JDKs which do not have this field (e.g. IBM JDK) to at least load the class
// without failing, see https://issues.apache.org/bugzilla/show_bug.cgi?id=56550
final Field _classes = AccessController.doPrivileged(new PrivilegedAction<Field>() {
@SuppressForbidden("TODO: Reflection works until Java 8 on Oracle/Sun JDKs, but breaks afterwards (different classloader types, access checks)")
public Field run() {
try {
Field fld = ClassLoader.class.getDeclaredField("classes");
fld.setAccessible(true);
return fld;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
ClassLoader appLoader = ClassLoader.getSystemClassLoader();
try {
Vector<Class<?>> classes = (Vector<Class<?>>) _classes.get(appLoader);
Map<String, Class<?>> map = new HashMap<String, Class<?>>();
for (Class<?> cls : classes) {
// e.g. proxy-classes, ...
ProtectionDomain pd = cls.getProtectionDomain();
if (pd == null)
continue;
CodeSource cs = pd.getCodeSource();
if (cs == null)
continue;
URL loc = cs.getLocation();
if (loc == null)
continue;
String jar = loc.toString();
if (jar.contains(ptrn)) {
map.put(cls.getName(), cls);
}
}
return map;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
use of java.security.CodeSource in project sling by apache.
the class JspRuntimeContext method initSecurity.
// -------------------------------------------------------- Private Methods
/**
* Method used to initialize SecurityManager data.
*/
private void initSecurity() {
// Setup the PermissionCollection for this web app context
// based on the permissions configured for the root of the
// web app context directory, then add a file read permission
// for that directory.
Policy policy = Policy.getPolicy();
if (policy != null) {
try {
// Get the permissions for the web app context
String docBase = context.getRealPath("/");
if (docBase == null) {
docBase = options.getScratchDir().toString();
}
String codeBase = docBase;
if (!codeBase.endsWith(File.separator)) {
codeBase = codeBase + File.separator;
}
File contextDir = new File(codeBase);
URL url = contextDir.getCanonicalFile().toURL();
final CodeSource codeSource = new CodeSource(url, (Certificate[]) null);
permissionCollection = policy.getPermissions(codeSource);
// Create a file read permission for web app context directory
if (!docBase.endsWith(File.separator)) {
permissionCollection.add(new FilePermission(docBase, "read"));
docBase = docBase + File.separator;
} else {
permissionCollection.add(new FilePermission(docBase.substring(0, docBase.length() - 1), "read"));
}
docBase = docBase + "-";
permissionCollection.add(new FilePermission(docBase, "read"));
// Create a file read permission for web app tempdir (work)
// directory
String workDir = options.getScratchDir().toString();
if (!workDir.endsWith(File.separator)) {
permissionCollection.add(new FilePermission(workDir, "read"));
workDir = workDir + File.separator;
}
workDir = workDir + "-";
permissionCollection.add(new FilePermission(workDir, "read"));
// Allow the JSP to access org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase
permissionCollection.add(new RuntimePermission("accessClassInPackage.org.apache.jasper.runtime"));
} catch (final Exception e) {
context.log("Security Init for context failed", e);
}
}
}
Aggregations