1 25 26 package org.objectweb.easybeans.security.propagation.context; 27 28 import org.objectweb.easybeans.security.api.EZBSecurityContext; 29 import org.objectweb.easybeans.security.api.EZBSecurityCurrent; 30 31 35 public class SecurityCurrent implements EZBSecurityCurrent { 36 37 40 private static InheritableThreadLocal <EZBSecurityContext> threadLocal; 41 42 46 private static EZBSecurityContext globalContext = null; 47 48 51 private static final EZBSecurityContext DEFAULT_CTX = new SecurityContext(); 52 53 56 static { 57 threadLocal = new InheritableThreadLocal <EZBSecurityContext>(); 58 threadLocal.set(new SecurityContext()); 59 } 60 61 64 private static EZBSecurityCurrent unique = initCurrent(); 65 66 71 @SuppressWarnings ("unchecked") 72 private static EZBSecurityCurrent initCurrent() { 73 String externalPropertyClass = System.getProperty(SECURITY_CURRENT_PROPERTY); 74 if (externalPropertyClass != null) { 75 EZBSecurityCurrent current = null; 76 Class <EZBSecurityCurrent> currentClass = null; 78 try { 79 currentClass = (Class <EZBSecurityCurrent>) Thread.currentThread().getContextClassLoader().loadClass( 80 externalPropertyClass); 81 } catch (ClassNotFoundException e) { 82 throw new IllegalStateException ("Cannot find the class '" + externalPropertyClass + "'", e); 83 } catch (ClassCastException e) { 84 throw new IllegalStateException ("'" + externalPropertyClass 85 + "' class is not an instance of EZBSecurityCurrent interface.", e); 86 } 87 88 try { 89 current = currentClass.newInstance(); 90 } catch (InstantiationException e) { 91 throw new IllegalStateException ("Cannot create an instance of the class '" + externalPropertyClass + "'", e); 92 } catch (IllegalAccessException e) { 93 throw new IllegalStateException ("Cannot create an instance of the class '" + externalPropertyClass + "'", e); 94 } 95 96 return current; 97 98 } 99 100 return new SecurityCurrent(); 102 } 103 104 108 public static EZBSecurityCurrent getCurrent() { 109 return unique; 110 } 111 112 117 public void setSecurityContext(final EZBSecurityContext securityContext) { 118 threadLocal.set(securityContext); 119 } 120 121 125 public void setGlobalSecurityContext(final EZBSecurityContext securityContext) { 126 globalContext = securityContext; 127 } 128 129 134 public EZBSecurityContext getSecurityContext() { 135 if (globalContext != null) { 136 return globalContext; 137 } 138 if (threadLocal.get() != null) { 139 return threadLocal.get(); 140 } 141 142 return DEFAULT_CTX; 144 } 145 146 } 147 | Popular Tags |