1 17 package org.alfresco.jcr.util; 18 19 import java.lang.reflect.InvocationHandler ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Proxy ; 23 24 import javax.jcr.AccessDeniedException; 25 import javax.jcr.RepositoryException; 26 import javax.jcr.lock.LockException; 27 import javax.jcr.nodetype.ConstraintViolationException; 28 import javax.jcr.nodetype.NoSuchNodeTypeException; 29 30 import org.alfresco.error.AlfrescoRuntimeException; 31 import org.alfresco.jcr.session.SessionImpl; 32 import org.alfresco.repo.node.integrity.IntegrityException; 33 import org.alfresco.service.cmr.dictionary.InvalidTypeException; 34 import org.alfresco.service.cmr.lock.NodeLockedException; 35 import org.alfresco.service.cmr.security.AuthenticationService; 36 37 38 48 public class JCRProxyFactory 49 { 50 51 59 public static Object create(Object target, Class proxyInterface, SessionImpl context) 60 { 61 InvocationHandler handler = new SessionContextInvocationHandler(target, context); 62 return Proxy.newProxyInstance(proxyInterface.getClassLoader(), new Class []{proxyInterface}, handler); 63 } 64 65 66 71 private static class SessionContextInvocationHandler implements InvocationHandler 72 { 73 private Object target; 74 private SessionImpl session; 75 private AuthenticationService authenticationService; 76 77 83 private SessionContextInvocationHandler(Object target, SessionImpl context) 84 { 85 this.target = target; 86 this.session = context; 87 this.authenticationService = session.getRepositoryImpl().getServiceRegistry().getAuthenticationService(); 88 } 89 90 93 public Object invoke(Object proxy, Method method, Object [] args) throws Throwable 94 { 95 if (method.getName().equals("toString")) 97 { 98 return toString(); 99 } 100 else if (method.getName().equals("hashCode")) 101 { 102 return hashCode(); 103 } 104 else if (method.getName().equals("equals")) 105 { 106 if (Proxy.isProxyClass(args[0].getClass())) 107 { 108 return equals(Proxy.getInvocationHandler(args[0])); 109 } 110 return false; 111 } 112 113 try 114 { 115 String username = authenticationService.getCurrentUserName(); 117 118 try 119 { 120 if (!method.getName().equals("logout")) 122 { 123 if (username == null) 124 { 125 authenticationService.validate(session.getTicket()); 126 } 127 } 128 129 return method.invoke(target, args); 131 } 132 catch (InvocationTargetException e) 133 { 134 Throwable cause = e.getCause(); 135 throw cause; 136 } 137 finally 138 { 139 if (username == null) 141 { 142 authenticationService.clearCurrentSecurityContext(); 143 } 144 } 145 } 146 catch(Throwable cause) 147 { 148 if (cause instanceof IntegrityException) 150 { 151 throw new ConstraintViolationException(cause); 152 } 153 else if (cause instanceof NodeLockedException) 154 { 155 throw new LockException(cause); 156 } 157 else if (cause instanceof InvalidTypeException) 158 { 159 throw new NoSuchNodeTypeException(cause); 160 } 161 else if (cause instanceof org.alfresco.repo.security.permissions.AccessDeniedException) 162 { 163 throw new AccessDeniedException(cause); 164 } 165 else if (cause instanceof AlfrescoRuntimeException) 166 { 167 throw new RepositoryException(cause); 168 } 169 throw cause; 170 } 171 } 172 173 @Override 174 public boolean equals(Object obj) 175 { 176 if (obj == this) 177 { 178 return true; 179 } 180 else if (obj == null || !(obj instanceof SessionContextInvocationHandler)) 181 { 182 return false; 183 } 184 SessionContextInvocationHandler other = (SessionContextInvocationHandler)obj; 185 return target.equals(other.target); 186 } 187 188 @Override 189 public int hashCode() 190 { 191 return target.hashCode(); 192 } 193 194 @Override 195 public String toString() 196 { 197 return target.toString(); 198 } 199 } 200 201 } 202 | Popular Tags |