1 18 package org.apache.batik.util; 19 20 import java.net.URL ; 21 import java.security.Policy ; 22 23 38 public class ApplicationSecurityEnforcer { 39 44 public static final String EXCEPTION_ALIEN_SECURITY_MANAGER 45 = "ApplicationSecurityEnforcer.message.security.exception.alien.security.manager"; 46 47 51 public static final String EXCEPTION_NO_POLICY_FILE 52 = "ApplicationSecurityEnforcer.message.null.pointer.exception.no.policy.file"; 53 54 57 public static final String PROPERTY_JAVA_SECURITY_POLICY 58 = "java.security.policy"; 59 60 63 public static final String JAR_PROTOCOL 64 = "jar:"; 65 66 70 public static final String JAR_URL_FILE_SEPARATOR 71 = "!/"; 72 73 76 public static final String PROPERTY_APP_DEV_BASE 77 = "app.dev.base"; 78 79 82 public static final String PROPERTY_APP_JAR_BASE 83 = "app.jar.base"; 84 85 89 public static final String APP_MAIN_CLASS_DIR 90 = "classes/"; 91 92 95 protected Class appMainClass; 96 97 100 protected String securityPolicy; 101 102 105 protected String appMainClassRelativeURL; 106 107 110 protected BatikSecurityManager lastSecurityManagerInstalled; 111 112 123 public ApplicationSecurityEnforcer(Class appMainClass, 124 String securityPolicy, 125 String appJarFile){ 126 this(appMainClass, securityPolicy); 127 } 128 129 130 136 public ApplicationSecurityEnforcer(Class appMainClass, 137 String securityPolicy){ 138 this.appMainClass = appMainClass; 139 this.securityPolicy = securityPolicy; 140 this.appMainClassRelativeURL = 141 appMainClass.getName().replace('.', '/') 142 + 143 ".class"; 144 145 } 146 147 155 public void enforceSecurity(boolean enforce){ 156 SecurityManager sm = System.getSecurityManager(); 157 158 if (sm != null && sm != lastSecurityManagerInstalled) { 159 throw new SecurityException 163 (Messages.getString(EXCEPTION_ALIEN_SECURITY_MANAGER)); 164 } 165 166 if (enforce) { 167 System.setSecurityManager(null); 172 installSecurityManager(); 173 } else { 174 if (sm != null) { 175 System.setSecurityManager(null); 176 lastSecurityManagerInstalled = null; 177 } 178 } 179 } 180 181 185 public URL getPolicyURL() { 186 ClassLoader cl = appMainClass.getClassLoader(); 187 URL policyURL = cl.getResource(securityPolicy); 188 189 if (policyURL == null) { 190 throw new NullPointerException 191 (Messages.formatMessage(EXCEPTION_NO_POLICY_FILE, 192 new Object []{securityPolicy})); 193 } 194 195 return policyURL; 196 } 197 198 201 public void installSecurityManager(){ 202 Policy policy = Policy.getPolicy(); 203 BatikSecurityManager securityManager = new BatikSecurityManager(); 204 205 ClassLoader cl = appMainClass.getClassLoader(); 211 String securityPolicyProperty 212 = System.getProperty(PROPERTY_JAVA_SECURITY_POLICY); 213 214 if (securityPolicyProperty == null || securityPolicyProperty.equals("")) { 215 URL policyURL = getPolicyURL(); 218 219 System.setProperty(PROPERTY_JAVA_SECURITY_POLICY, 220 policyURL.toString()); 221 } 222 223 URL mainClassURL = cl.getResource(appMainClassRelativeURL); 235 if (mainClassURL == null){ 236 throw new Error (appMainClassRelativeURL); 239 } 240 241 String expandedMainClassName = mainClassURL.toString(); 242 if (expandedMainClassName.startsWith(JAR_PROTOCOL) ) { 243 setJarBase(expandedMainClassName); 244 } else { 245 setDevBase(expandedMainClassName); 246 } 247 248 System.setSecurityManager(securityManager); 250 lastSecurityManagerInstalled = securityManager; 251 252 policy.refresh(); 254 255 if (securityPolicyProperty == null || securityPolicyProperty.equals("")) { 256 System.setProperty(PROPERTY_JAVA_SECURITY_POLICY, ""); 257 } 258 } 259 260 private void setJarBase(String expandedMainClassName){ 261 String curAppJarBase = System.getProperty(PROPERTY_APP_JAR_BASE); 265 if (curAppJarBase == null) { 266 expandedMainClassName = expandedMainClassName.substring(JAR_PROTOCOL.length()); 267 268 int codeBaseEnd = 269 expandedMainClassName.indexOf(JAR_URL_FILE_SEPARATOR + 270 appMainClassRelativeURL); 271 272 if (codeBaseEnd == -1){ 273 throw new Error (); 277 } 278 279 String appCodeBase = expandedMainClassName.substring(0, codeBaseEnd); 280 281 codeBaseEnd = appCodeBase.lastIndexOf('/'); 284 if (codeBaseEnd == -1) { 285 appCodeBase = ""; 286 } else { 287 appCodeBase = appCodeBase.substring(0, codeBaseEnd); 288 } 289 290 System.setProperty(PROPERTY_APP_JAR_BASE, appCodeBase); 291 } 292 } 293 294 299 private void setDevBase(String expandedMainClassName){ 300 String curAppCodeBase = System.getProperty(PROPERTY_APP_DEV_BASE); 305 if (curAppCodeBase == null) { 306 int codeBaseEnd = 307 expandedMainClassName.indexOf(APP_MAIN_CLASS_DIR 308 + appMainClassRelativeURL); 309 310 if (codeBaseEnd == -1){ 311 throw new Error (); 315 } 316 317 String appCodeBase = expandedMainClassName.substring(0, codeBaseEnd); 318 System.setProperty(PROPERTY_APP_DEV_BASE, appCodeBase); 319 } 320 } 321 322 323 } 324 325 | Popular Tags |