1 17 package org.alfresco.jcr.repository; 18 19 import java.util.HashMap ; 20 import java.util.Map ; 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 46 public class RepositoryImpl implements Repository 47 { 48 49 private final static char[] EMPTY_PASSWORD = "".toCharArray(); 50 51 52 private static final Map <String , String > descriptors = new HashMap <String , String >(); 53 54 55 private static ThreadLocal <SessionImpl> sessions = new ThreadLocal <SessionImpl>(); 57 58 private ServiceRegistry serviceRegistry; 60 private ImporterComponent importerComponent; 61 private String defaultWorkspace = null; 62 63 private NamespaceRegistryImpl namespaceRegistry = null; 65 66 67 71 76 public void setServiceRegistry(ServiceRegistry serviceRegistry) 77 { 78 this.serviceRegistry = serviceRegistry; 79 } 80 81 86 public void setImporterComponent(ImporterComponent importerComponent) 87 { 88 this.importerComponent = importerComponent; 89 } 90 91 96 public void setDefaultWorkspace(String defaultWorkspace) 97 { 98 this.defaultWorkspace = defaultWorkspace; 99 } 100 101 104 public void init() 105 { 106 if (serviceRegistry == null) 107 { 108 throw new IllegalStateException ("Service Registry has not been specified."); 109 } 110 111 namespaceRegistry = new NamespaceRegistryImpl(false, serviceRegistry.getNamespaceService()); 113 114 DescriptorService descriptorService = serviceRegistry.getDescriptorService(); 116 Descriptor descriptor = descriptorService.getServerDescriptor(); 117 118 String repNameDesc = "Alfresco Content Repository"; 119 String edition = descriptor.getEdition(); 120 if (edition != null && edition.length() > 0) 121 { 122 repNameDesc += " (" + edition + ")"; 123 } 124 String 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 144 public ServiceRegistry getServiceRegistry() 145 { 146 return serviceRegistry; 147 } 148 149 154 public ImporterComponent getImporterComponent() 155 { 156 return importerComponent; 157 } 158 159 162 public NamespaceRegistryImpl getNamespaceRegistry() 163 { 164 return namespaceRegistry; 165 } 166 167 170 public String [] getDescriptorKeys() 171 { 172 String [] keys = (String []) descriptors.keySet().toArray(new String [descriptors.keySet().size()]); 173 return keys; 174 } 175 176 179 public String getDescriptor(String key) 180 { 181 return descriptors.get(key); 182 } 183 184 187 public Session login(Credentials credentials, String workspaceName) 188 throws LoginException, NoSuchWorkspaceException, RepositoryException 189 { 190 String 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 SessionImpl sessionImpl = new SessionImpl(this); 204 registerSession(sessionImpl); 205 206 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 String ticket = authenticationService.getCurrentTicket(); 220 String sessionWorkspace = (workspaceName == null) ? defaultWorkspace : workspaceName; 221 sessionImpl.init(ticket, sessionWorkspace, getAttributes(credentials)); 222 223 Session session = sessionImpl.getProxy(); 225 return session; 226 } 227 catch(AlfrescoRuntimeException e) 228 { 229 deregisterSession(); 230 throw new RepositoryException(e); 231 } 232 } 233 234 237 public Session login(Credentials credentials) 238 throws LoginException, RepositoryException 239 { 240 return login(credentials, null); 241 } 242 243 246 public Session login(String workspaceName) 247 throws LoginException, NoSuchWorkspaceException, RepositoryException 248 { 249 return login(null, workspaceName); 250 } 251 252 255 public Session login() 256 throws LoginException, RepositoryException 257 { 258 return login(null, null); 259 } 260 261 267 private Map <String , Object > getAttributes(Credentials credentials) 268 { 269 Map <String , Object > attributes = null; 270 if (credentials != null && credentials instanceof SimpleCredentials) 271 { 272 SimpleCredentials simpleCredentials = (SimpleCredentials)credentials; 273 String [] names = simpleCredentials.getAttributeNames(); 274 attributes = new HashMap <String , Object >(names.length); 275 for (String name : names) 276 { 277 attributes.put(name, simpleCredentials.getAttribute(name)); 278 } 279 } 280 return attributes; 281 } 282 283 288 private void registerSession(SessionImpl session) 289 throws RepositoryException 290 { 291 if (sessions.get() != null) 293 { 294 throw new RepositoryException("Only one active session is allowed per thread."); 295 } 296 297 sessions.set(session); 299 } 300 301 304 public void deregisterSession() 305 { 306 sessions.set(null); 308 } 309 310 } 311 | Popular Tags |