KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > jcr > repository > RepositoryImpl


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.repository;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.jcr.Credentials;
23 import javax.jcr.LoginException;
24 import javax.jcr.NoSuchWorkspaceException;
25 import javax.jcr.Repository;
26 import javax.jcr.RepositoryException;
27 import javax.jcr.Session;
28 import javax.jcr.SimpleCredentials;
29
30 import org.alfresco.error.AlfrescoRuntimeException;
31 import org.alfresco.jcr.dictionary.NamespaceRegistryImpl;
32 import org.alfresco.jcr.session.SessionImpl;
33 import org.alfresco.repo.importer.ImporterComponent;
34 import org.alfresco.repo.security.authentication.AuthenticationException;
35 import org.alfresco.service.ServiceRegistry;
36 import org.alfresco.service.cmr.security.AuthenticationService;
37 import org.alfresco.service.descriptor.Descriptor;
38 import org.alfresco.service.descriptor.DescriptorService;
39
40
41 /**
42  * Alfresco implementation of a JCR Repository
43  *
44  * @author David Caruana
45  */

46 public class RepositoryImpl implements Repository
47 {
48     /** Empty Password, if not supplied */
49     private final static char[] EMPTY_PASSWORD = "".toCharArray();
50     
51     /** Repository Descriptors */
52     private static final Map JavaDoc<String JavaDoc, String JavaDoc> descriptors = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
53
54     /** Thread Local Session */
55     // Note: For now, we're only allowing one active (i.e. logged in) Session per-thread
56
private static ThreadLocal JavaDoc<SessionImpl> sessions = new ThreadLocal JavaDoc<SessionImpl>();
57     
58     // Service dependencies
59
private ServiceRegistry serviceRegistry;
60     private ImporterComponent importerComponent;
61     private String JavaDoc defaultWorkspace = null;
62
63     // Services
64
private NamespaceRegistryImpl namespaceRegistry = null;
65     
66
67     //
68
// Dependency Injection
69
//
70

71     /**
72      * Set the service registry
73      *
74      * @param serviceRegistry
75      */

76     public void setServiceRegistry(ServiceRegistry serviceRegistry)
77     {
78         this.serviceRegistry = serviceRegistry;
79     }
80     
81     /**
82      * Set the Importer Component
83      *
84      * @param importerComponent
85      */

86     public void setImporterComponent(ImporterComponent importerComponent)
87     {
88         this.importerComponent = importerComponent;
89     }
90     
91     /**
92      * Sets the Default Workspace
93      *
94      * @param defaultWorkspace default workspace
95      */

96     public void setDefaultWorkspace(String JavaDoc defaultWorkspace)
97     {
98         this.defaultWorkspace = defaultWorkspace;
99     }
100
101     /**
102      * Initialisation
103      */

104     public void init()
105     {
106         if (serviceRegistry == null)
107         {
108             throw new IllegalStateException JavaDoc("Service Registry has not been specified.");
109         }
110         
111         // initialise namespace registry
112
namespaceRegistry = new NamespaceRegistryImpl(false, serviceRegistry.getNamespaceService());
113
114         // initialise descriptors
115
DescriptorService descriptorService = serviceRegistry.getDescriptorService();
116         Descriptor descriptor = descriptorService.getServerDescriptor();
117
118         String JavaDoc repNameDesc = "Alfresco Content Repository";
119         String JavaDoc edition = descriptor.getEdition();
120         if (edition != null && edition.length() > 0)
121         {
122             repNameDesc += " (" + edition + ")";
123         }
124         String JavaDoc repVersion = descriptor.getVersion();
125         
126         descriptors.put(Repository.REP_NAME_DESC, repNameDesc);
127         descriptors.put(Repository.REP_VENDOR_DESC, "Alfresco");
128         descriptors.put(Repository.REP_VENDOR_URL_DESC, "http://www.alfresco.org");
129         descriptors.put(Repository.REP_VERSION_DESC, repVersion);
130         descriptors.put(Repository.SPEC_NAME_DESC, "Content Repository API for Java(TM) Technology Specification");
131         descriptors.put(Repository.SPEC_VERSION_DESC, "1.0");
132         descriptors.put(Repository.LEVEL_1_SUPPORTED, "true");
133         descriptors.put(Repository.LEVEL_2_SUPPORTED, "true");
134         descriptors.put(Repository.OPTION_TRANSACTIONS_SUPPORTED, "true");
135         descriptors.put(Repository.QUERY_XPATH_DOC_ORDER, "true");
136         descriptors.put(Repository.QUERY_XPATH_POS_INDEX, "true");
137     }
138     
139     /**
140      * Get the service registry
141      *
142      * @return the service registry
143      */

144     public ServiceRegistry getServiceRegistry()
145     {
146         return serviceRegistry;
147     }
148
149     /**
150      * Get the importer component
151      *
152      * @return the importer component
153      */

154     public ImporterComponent getImporterComponent()
155     {
156         return importerComponent;
157     }
158     
159     /**
160      * Get the Namespace Registry
161      */

162     public NamespaceRegistryImpl getNamespaceRegistry()
163     {
164         return namespaceRegistry;
165     }
166     
167     /* (non-Javadoc)
168      * @see javax.jcr.Repository#getDescriptorKeys()
169      */

170     public String JavaDoc[] getDescriptorKeys()
171     {
172         String JavaDoc[] keys = (String JavaDoc[]) descriptors.keySet().toArray(new String JavaDoc[descriptors.keySet().size()]);
173         return keys;
174     }
175
176     /* (non-Javadoc)
177      * @see javax.jcr.Repository#getDescriptor(java.lang.String)
178      */

179     public String JavaDoc getDescriptor(String JavaDoc key)
180     {
181         return descriptors.get(key);
182     }
183
184     /* (non-Javadoc)
185      * @see javax.jcr.Repository#login(javax.jcr.Credentials, java.lang.String)
186      */

187     public Session login(Credentials credentials, String JavaDoc workspaceName)
188         throws LoginException, NoSuchWorkspaceException, RepositoryException
189     {
190         // extract username and password
191
// TODO: determine support for general Credentials
192
String JavaDoc username = null;
193         char[] password = EMPTY_PASSWORD;
194         if (credentials != null && credentials instanceof SimpleCredentials)
195         {
196             username = ((SimpleCredentials)credentials).getUserID();
197             password = ((SimpleCredentials)credentials).getPassword();
198         }
199
200         try
201         {
202             // construct the session
203
SessionImpl sessionImpl = new SessionImpl(this);
204             registerSession(sessionImpl);
205             
206             // authenticate user
207
AuthenticationService authenticationService = getServiceRegistry().getAuthenticationService();
208             try
209             {
210                 authenticationService.authenticate(username, password);
211             }
212             catch(AuthenticationException e)
213             {
214                 deregisterSession();
215                 throw new LoginException("Alfresco Repository failed to authenticate credentials", e);
216             }
217             
218             // initialise the session
219
String JavaDoc ticket = authenticationService.getCurrentTicket();
220             String JavaDoc sessionWorkspace = (workspaceName == null) ? defaultWorkspace : workspaceName;
221             sessionImpl.init(ticket, sessionWorkspace, getAttributes(credentials));
222
223             // session is now ready
224
Session session = sessionImpl.getProxy();
225             return session;
226         }
227         catch(AlfrescoRuntimeException e)
228         {
229             deregisterSession();
230             throw new RepositoryException(e);
231         }
232     }
233
234     /* (non-Javadoc)
235      * @see javax.jcr.Repository#login(javax.jcr.Credentials)
236      */

237     public Session login(Credentials credentials)
238         throws LoginException, RepositoryException
239     {
240         return login(credentials, null);
241     }
242
243     /* (non-Javadoc)
244      * @see javax.jcr.Repository#login(java.lang.String)
245      */

246     public Session login(String JavaDoc workspaceName)
247         throws LoginException, NoSuchWorkspaceException, RepositoryException
248     {
249         return login(null, workspaceName);
250     }
251
252     /* (non-Javadoc)
253      * @see javax.jcr.Repository#login()
254      */

255     public Session login()
256         throws LoginException, RepositoryException
257     {
258         return login(null, null);
259     }
260
261     /**
262      * Get attributes from passed Credentials
263      *
264      * @param credentials the credentials to extract attribute from
265      * @return the attributes
266      */

267     private Map JavaDoc<String JavaDoc, Object JavaDoc> getAttributes(Credentials credentials)
268     {
269         Map JavaDoc<String JavaDoc, Object JavaDoc> attributes = null;
270         if (credentials != null && credentials instanceof SimpleCredentials)
271         {
272             SimpleCredentials simpleCredentials = (SimpleCredentials)credentials;
273             String JavaDoc[] names = simpleCredentials.getAttributeNames();
274             attributes = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>(names.length);
275             for (String JavaDoc name : names)
276             {
277                 attributes.put(name, simpleCredentials.getAttribute(name));
278             }
279         }
280         return attributes;
281     }
282     
283     /**
284      * Register active session
285      *
286      * @param session
287      */

288     private void registerSession(SessionImpl session)
289         throws RepositoryException
290     {
291         // only allow one active session
292
if (sessions.get() != null)
293         {
294             throw new RepositoryException("Only one active session is allowed per thread.");
295         }
296         
297         // record session in current thread
298
sessions.set(session);
299     }
300
301     /**
302      * De-register current active session
303      */

304     public void deregisterSession()
305     {
306         // remove session from current thread
307
sessions.set(null);
308     }
309     
310 }
311
Popular Tags