KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > core > RepositoryImpl


1 /**
2  **************************************************************************
3  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
4  * Please look at license.txt in info directory for more license detail. *
5  */

6
7 package org.exoplatform.services.jcr.impl.core;
8
9 import java.util.HashMap JavaDoc;
10
11 import javax.jcr.Credentials;
12 import javax.jcr.LoginException;
13 import javax.jcr.NoSuchWorkspaceException;
14 import javax.jcr.Repository;
15 import javax.jcr.RepositoryException;
16 import javax.jcr.Ticket;
17
18 import org.apache.commons.logging.Log;
19 import org.exoplatform.services.jcr.storage.RepositoryManager;
20 import org.exoplatform.services.jcr.storage.WorkspaceContainer;
21 import org.exoplatform.services.log.LogUtil;
22 import org.exoplatform.services.organization.OrganizationService;
23
24 /**
25  * Created by The eXo Platform SARL .
26  *
27  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov </a>
28  * @version $Id: RepositoryImpl.java,v 1.12 2004/11/02 18:36:33 geaz Exp $
29  */

30 public class RepositoryImpl implements Repository {
31
32   private HashMap JavaDoc workspaceContainers;
33
34   private RepositoryManager repositoryManager;
35
36   private String JavaDoc defaultWorkspaceName;
37
38   protected Log log;
39
40   private String JavaDoc name;
41
42   private OrganizationService organizationService;
43
44   public RepositoryImpl(String JavaDoc name, OrganizationService organizationService)
45       throws RepositoryException {
46     this.name = name;
47     this.organizationService = organizationService;
48     log = LogUtil.getLog("org.exoplatform.services.jcr");
49   }
50
51   /**
52    * 6.1.1 Login, creating a new ticket for the given credentials and specified
53    * workspace. If login fails, a <code>LoginException</code> is thrown and no
54    * valid ticket is generated. How the repository treats an attempt to connect
55    * with <code>null</code> <code>credentials</code> and or a
56    * <code>null</code> <code>workspaceName</code> is a matter of
57    * implementation. Possibilities include allowing an anonymous connection in
58    * cases of <code>null credentials</code> and connecting to a default
59    * workspace in cases of a <code>null workspaceName</code>.<p/><b>Level
60    * 2: </b> <p/>Returns a <code>{@link javax.jcr.xa.XATicket}</code> object
61    * in order to support transactions. <p/>
62    *
63    * @param credentials
64    * The credentials of the user
65    * @param workspaceName
66    * the name of a workspace.
67    * @return a valid ticket for the user to access the repository.
68    * @throws LoginException
69    * If the login authentication fails.
70    * @throws NoSuchWorkspaceException
71    * If the specified <code>workspaceName</code> is not recognized.
72    */

73   public Ticket login(Credentials credentials, String JavaDoc workspaceName)
74       throws LoginException, NoSuchWorkspaceException {
75     log.debug("login");
76     if (workspaceName == null) {
77       if (defaultWorkspaceName == null)
78         throw new NoSuchWorkspaceException(
79             "Both workspace and default-workspace name are null! ");
80       else
81         workspaceName = defaultWorkspaceName;
82     }
83
84     if (credentials != null) {
85       if (workspaceContainers.get(workspaceName) == null)
86         throw new NoSuchWorkspaceException("Workspace '" + workspaceName
87             + "' not found ");
88       
89       boolean success = false;
90       try {
91         success = organizationService.authenticate(credentials
92             .getUserId(), new String JavaDoc(credentials.getPassword()));
93       } catch (Exception JavaDoc e1) {
94         throw new LoginException("Can not authorize the user : "
95             + credentials.getUserId(), e1);
96       }
97       if (!success)
98         throw new LoginException("Can not authorize the user : "
99             + credentials.getUserId());
100     }
101     try {
102       return new TicketImpl(this, credentials, workspaceName);
103     } catch (RepositoryException e) {
104       throw new LoginException("Unexpected problems with Ticket creation.", e);
105     }
106   }
107
108   public WorkspaceContainer getContainer(String JavaDoc workspaceName)
109       throws RepositoryException {
110     WorkspaceContainer container = (WorkspaceContainer) workspaceContainers
111         .get(workspaceName);
112     if (container == null)
113       throw new RepositoryException("No container found for workspace '"
114           + workspaceName + "'!");
115     return container;
116   }
117
118   public RepositoryManager getRepositoryManager() {
119     return repositoryManager;
120   }
121
122   public void setRepositoryManager(RepositoryManager repositoryManager) {
123     this.repositoryManager = repositoryManager;
124   }
125
126   public void setWorkspaceContainers(HashMap JavaDoc containers) {
127     this.workspaceContainers = containers;
128   }
129
130   public void setDefaultWorkspaceName(String JavaDoc name) {
131     this.defaultWorkspaceName = name;
132   }
133
134   public String JavaDoc getName() {
135     return name;
136   }
137 }
Popular Tags