1 28 package net.sf.jguard.core.authorization.policy; 29 30 import java.io.File ; 31 import java.security.Policy ; 32 import java.util.Enumeration ; 33 import java.util.Properties ; 34 import java.util.logging.Level ; 35 import java.util.logging.Logger ; 36 37 41 public class PolicyHelper { 42 43 public static final String GNU_JAVA_SECURITY_POLICY_FILE = "gnu.java.security.PolicyFile"; 45 private static final String COM_SUN_SECURITY_AUTH_POLICY_FILE = "com.sun.security.auth.PolicyFile"; 46 public static final String SUN_SECURITY_PROVIDER_POLICY_FILE = "sun.security.provider.PolicyFile"; 47 48 private static Logger logger = Logger.getLogger(PolicyHelper.class.getName()); 49 50 54 public static void installPolicyOnJVM() { 55 56 Policy runtimePolicy = Policy.getPolicy(); 57 58 if(!(runtimePolicy.getClass().getName().equals(MultipleAppPolicy.class.getName()))){ 60 61 logger.log(Level.INFO,"init() - JGuardPolicy is not set as the policy provider . the actual policy provider is '"+ runtimePolicy.getClass().getName()+"' which is different of '"+MultipleAppPolicy.class.getName()+"' "); 62 logger.log(Level.INFO,"init() - if you want the jGuard policy 'governs' all java applications (one choice among others described in the jGuard documentation),"); 63 logger.log(Level.INFO,"init() - please correct the 'policy.provider' property (policy.provider=net.sf.jguard.core.JGuardPolicy) in your 'java.security' file,"); 64 logger.log(Level.INFO,"init() - located in this directory: "+ System.getProperty("java.home") + File.separator+ "lib"+ File.separator + "security"+ File.separator); 65 66 try { 67 try { 69 Class clazz = Class.forName(PolicyHelper.COM_SUN_SECURITY_AUTH_POLICY_FILE); 70 javax.security.auth.Policy.setPolicy((javax.security.auth.Policy )clazz.newInstance()); 72 } catch (ClassNotFoundException e) { 73 logger.log(Level.WARNING,"com.sun.security.auth.PolicyFile is not reachable.\n we cannot set the old javax.security.auth.Policy implementation to it\n "+e.getMessage()); 74 } 75 76 Policy.setPolicy(new MultipleAppPolicy(Policy.getPolicy())); 78 79 } catch (InstantiationException e) { 80 logger.log(Level.SEVERE,"init() - Policy Implementation cannot be instantiated : InstantiationException"+e.getMessage()); 81 } catch (IllegalAccessException e) { 82 logger.log(Level.SEVERE,"init() - Policy Implementation cannot be accessed : IllegalAccessException"+e.getMessage()); 83 }catch(SecurityException e){ 84 logger.log(Level.SEVERE,"init() - Policy Implementation cannot be defined : SecurityException . you haven't got the right to set the java policy"+e.getMessage()); 85 } 86 } 87 88 try{ 89 90 logger.log(Level.FINE,"System properties : \n"); 91 Properties props = System.getProperties(); 92 Enumeration enumeration = props.keys(); 93 while(enumeration.hasMoreElements()){ 94 String key = (String )enumeration.nextElement(); 95 String value = (String )props.get(key); 96 logger.log(Level.FINE,key+"="+value); 97 } 98 99 }catch(SecurityException sex){ 100 logger.log(Level.WARNING,"you have not the permission to grab system properties "); 101 } 102 103 } 104 105 109 public static Class findDefaultPolicy(){ 110 String [] policies = {PolicyHelper.SUN_SECURITY_PROVIDER_POLICY_FILE,PolicyHelper.GNU_JAVA_SECURITY_POLICY_FILE}; 112 Class defaultPolicyClass = null; 113 for(int i = 0; i< policies.length;i++){ 114 try { 115 defaultPolicyClass = Class.forName(policies[i]); 116 } catch (ClassNotFoundException e) { 117 logger.log(Level.FINE,"findDefaultPolicy() - " + policies[i]+ " is not the defaultPolicy class "); 118 i++; 119 continue; 120 } 121 logger.log(Level.FINE,"findDefaultPolicy() - " + policies[i]+ " is the defaultPolicy class "); 122 break; 123 } 124 if(null == defaultPolicyClass){ 125 logger.log(Level.FINE,"findDefaultPolicy() - no defaultPolicy class has been found "); 126 } 127 return defaultPolicyClass; 128 } 129 130 131 } 132 | Popular Tags |