KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > util > JCRProxyFactory


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.jcr.util;
18
19 import java.lang.reflect.InvocationHandler JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.Proxy JavaDoc;
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 /**
39  * Factory for creating a JCR Session Context Proxy
40  *
41  * The Session Context Proxy is responsible for ensuring that the appropriate Alfresco
42  * Repository context is setup when a method is executed within a JCR session. For
43  * example, the appropriate authenticated user is validated.
44  *
45  * @author David Caruana
46  *
47  */

48 public class JCRProxyFactory
49 {
50     
51     /**
52      * Create a Session Context Proxy
53      *
54      * @param target target object to wrap
55      * @param proxyInterface the proxy interface to export
56      * @param context the session context
57      * @return the proxied target
58      */

59     public static Object JavaDoc create(Object JavaDoc target, Class JavaDoc proxyInterface, SessionImpl context)
60     {
61         InvocationHandler JavaDoc handler = new SessionContextInvocationHandler(target, context);
62         return Proxy.newProxyInstance(proxyInterface.getClassLoader(), new Class JavaDoc[]{proxyInterface}, handler);
63     }
64
65
66     /**
67      * Session Context Invocation Handler
68      *
69      * @author David Caruana
70      */

71     private static class SessionContextInvocationHandler implements InvocationHandler JavaDoc
72     {
73         private Object JavaDoc target;
74         private SessionImpl session;
75         private AuthenticationService authenticationService;
76
77         /**
78          * Constuct.
79          *
80          * @param instance the object instance holding the method
81          * @param delegateMethod the method to invoke
82          */

83         private SessionContextInvocationHandler(Object JavaDoc target, SessionImpl context)
84         {
85             this.target = target;
86             this.session = context;
87             this.authenticationService = session.getRepositoryImpl().getServiceRegistry().getAuthenticationService();
88         }
89         
90         /* (non-Javadoc)
91          * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
92          */

93         public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) throws Throwable JavaDoc
94         {
95             // Handle Object level methods
96
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                 // establish authentication context
116
String JavaDoc username = authenticationService.getCurrentUserName();
117
118                 try
119                 {
120                     // setup authentication context, if one does not exist (for example, in remote case)
121
if (!method.getName().equals("logout"))
122                     {
123                         if (username == null)
124                         {
125                             authenticationService.validate(session.getTicket());
126                         }
127                     }
128
129                     // invoke underlying service
130
return method.invoke(target, args);
131                 }
132                 catch (InvocationTargetException JavaDoc e)
133                 {
134                     Throwable JavaDoc cause = e.getCause();
135                     throw cause;
136                 }
137                 finally
138                 {
139                     // cleanup authentication context (only if one didn't exist before)
140
if (username == null)
141                     {
142                         authenticationService.clearCurrentSecurityContext();
143                     }
144                 }
145             }
146             catch(Throwable JavaDoc cause)
147             {
148                 // Map Alfresco exceptions to JCR exceptions
149
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 JavaDoc
174         public boolean equals(Object JavaDoc 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 JavaDoc
189         public int hashCode()
190         {
191             return target.hashCode();
192         }
193     
194         @Override JavaDoc
195         public String JavaDoc toString()
196         {
197             return target.toString();
198         }
199     }
200
201 }
202
Popular Tags