Dépôt officiel du code source de l'ERP OpenConcerto
Blame | Last modification | View Log | RSS feed
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
*
* The contents of this file are subject to the terms of the GNU General Public License Version 3
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each file.
*/
package org.openconcerto.utils.tools;
import org.openconcerto.utils.tools.SimpleURLClassLoader.URLCollector;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
import java.security.ProtectionDomain;
import java.util.PropertyPermission;
public final class Outer {
public static void main(String[] args) throws Exception {
final String className = args[0];
final String fieldName = args[1];
org.openconcerto.utils.protocol.Helper.register();
// Install SecurityManager to test SecureClassLoader.getPermissions().
final ClassLoader mainLoader = Outer.class.getClassLoader();
Policy.setPolicy(new Policy() {
@Override
public boolean implies(ProtectionDomain domain, Permission permission) {
// our class loader gets all permission
if (domain != null && domain.getClassLoader() == mainLoader) {
return true;
}
return super.implies(domain, permission);
}
});
System.setSecurityManager(new SecurityManager());
final PermissionCollection innerPerms = new Permissions();
innerPerms.add(new RuntimePermission("accessDeclaredMembers"));
if (Boolean.getBoolean("property.read"))
innerPerms.add(new PropertyPermission("*", "read"));
innerPerms.setReadOnly();
System.out.print(new Outer(className).getFieldValue(innerPerms, fieldName));
}
private final String className;
public Outer(String className) {
super();
this.className = className;
}
public Object getFieldValue(final PermissionCollection perms, final String fieldName) throws ClassNotFoundException, ReflectiveOperationException {
// needed to test loading from jar inside a jar
if (!this.getClass().getResource(this.getClass().getSimpleName() + ".class").toExternalForm().startsWith("jar:file:"))
throw new IllegalStateException("Class not loaded from a jar");
final ClassLoader loader = new SimpleURLClassLoader(new URLCollector().addJar(this.getClass().getResource("/inner.jar"))) {
@Override
protected PermissionCollection getPermissions(CodeSource codesource) {
return perms;
}
};
final Class<?> loadedClass = loader.loadClass(this.className);
return loadedClass.getDeclaredField(fieldName).get(null);
}
}