1 22 package org.jboss.test.security; 23 24 import java.io.File ; 25 import java.net.URL ; 26 import java.security.CodeSource ; 27 import java.security.PermissionCollection ; 28 import java.security.Permission ; 29 import java.security.ProtectionDomain ; 30 import java.util.HashSet ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Properties ; 34 import java.util.Enumeration ; 35 import java.lang.reflect.Constructor ; 36 37 import org.jboss.test.visitor.TypeHierarchyTraversal; 38 import org.jboss.test.visitor.PropertiesVisitorImpl; 39 40 46 public class TestsPolicyPlugin extends PolicyPlugin 47 { 48 private HashSet <Permission > classPermissions = new HashSet <Permission >(); 49 50 private static final URL codeSourceLocation; 51 52 static 53 { 54 URL temp = null; 55 ProtectionDomain pd = TestsPolicyPlugin.class.getProtectionDomain(); 56 if (pd != null) 57 { 58 CodeSource cs = pd.getCodeSource(); 59 if (cs != null) 60 { 61 temp = cs.getLocation(); 62 } 63 } 64 codeSourceLocation = temp; 65 } 66 67 78 public TestsPolicyPlugin(Class clazz) 79 { 80 PropertiesVisitorImpl visitor = new PropertiesVisitorImpl(); 82 TypeHierarchyTraversal.visit(clazz, visitor); 83 HashMap <Class , Properties > typeProperties = visitor.getTypeProperties(); 84 Iterator <Properties > iter = typeProperties.values().iterator(); 85 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 86 while( iter.hasNext() ) 87 { 88 Properties props = iter.next(); 89 Enumeration names = props.propertyNames(); 90 while( names.hasMoreElements() ) 91 { 92 String name = (String ) names.nextElement(); 93 if( name.matches("test.Permission.[0-9]+") ) 95 { 96 String value = props.getProperty(name); 98 String [] info = value.split(", "); 99 try 100 { 101 Class pc = loader.loadClass(info[0]); 103 Permission p; 104 if( info.length == 1 ) 105 { 106 p = (Permission ) pc.newInstance(); 107 } 108 else if( info.length == 2 ) 109 { 110 Class [] sig = {String .class}; 111 Object [] args = {info[1]}; 112 Constructor ctor = pc.getConstructor(sig); 113 p = (Permission ) ctor.newInstance(args); 114 } 115 else 116 { 117 Class [] sig = {String .class, String .class}; 118 Object [] args = {info[1], info[2]}; 119 Constructor ctor = pc.getConstructor(sig); 120 p = (Permission ) ctor.newInstance(args); 121 } 122 classPermissions.add(p); 123 } 124 catch(ClassNotFoundException e) 125 { 126 } 128 catch(Exception e) 129 { 130 e.printStackTrace(); 131 } 132 } 133 } 134 } 135 } 136 137 public PermissionCollection getPermissions(CodeSource codesource) 138 { 139 URL url = codesource.getLocation(); 140 if (url != null) 141 { 142 if (url.equals(codeSourceLocation)) 144 return allPermissions(); 145 146 File file = new File (url.toString()); 148 String name = file.getName(); 149 if (name.indexOf("tests") != -1 || name.indexOf("-test.jar") != -1) 150 { 151 PermissionCollection pc = fileReadPermissions(); 153 pc.add(new RuntimePermission ("createClassLoader")); 155 pc.add(new RuntimePermission ("getProtectionDomain")); 156 Iterator iter = classPermissions.iterator(); 158 while( iter.hasNext() ) 159 { 160 Permission p = (Permission ) iter.next(); 161 pc.add(p); 162 } 163 return pc; 164 } 165 } 166 return allPermissions(); 167 } 168 } 169 | Popular Tags |