KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > RepositoryServiceImpl


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

5
6 package org.exoplatform.services.jcr.impl;
7
8 import java.util.*;
9
10 import javax.jcr.Repository;
11 import javax.jcr.RepositoryException;
12
13 import org.exoplatform.container.PortalContainer;
14 import org.exoplatform.container.RootContainer;
15 import org.exoplatform.container.configuration.PortalContainerInfo;
16
17 import org.exoplatform.services.jcr.RepositoryService;
18 import org.exoplatform.services.jcr.impl.core.RepositoryImpl;
19 import org.exoplatform.services.jcr.storage.RepositoryManager;
20 import org.exoplatform.services.jcr.storage.WorkspaceContainer;
21 import org.exoplatform.services.jcr.config.*;
22
23 import org.picocontainer.Parameter;
24 import org.picocontainer.defaults.ComponentParameter;
25 import org.picocontainer.defaults.ConstantParameter;
26 import org.picocontainer.defaults.DefaultComponentAdapterFactory;
27 import org.picocontainer.ComponentAdapter ;
28 /**
29  * Created by The eXo Platform SARL.
30  *
31  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
32  * @version $Id: RepositoryServiceImpl.java,v 1.10 2004/09/22 13:54:55 geaz Exp $
33  */

34
35 public class RepositoryServiceImpl implements RepositoryService {
36
37   RepositoryServiceConfig config;
38   private PortalContainerInfo containerInfo;
39
40   public RepositoryServiceImpl(PortalContainerInfo containerInfo,
41                                RepositoryServiceConfig config) {
42     this.config = config;
43     this.containerInfo = containerInfo;
44   }
45
46   public Repository getRepository() throws RepositoryException {
47     return getRepository(config.getDefaultRepositoryName());
48   }
49
50   public Repository getRepository(String JavaDoc name) throws RepositoryException {
51
52     PortalContainer portalContainer =
53       RootContainer.getInstance().getPortalContainer(containerInfo.getContainerName());
54
55     RepositoryImpl repository = (RepositoryImpl) portalContainer.getComponentInstance(name);
56     if (repository == null){
57       init(portalContainer);
58       repository = (RepositoryImpl) portalContainer.getComponentInstance(name);
59
60       if(repository == null)
61         throw new RepositoryException("Repository '"+name+"' not found.");
62
63       HashMap workspaceContainers = new HashMap();
64
65       try {
66            String JavaDoc managerName = config.getRepositoryEntry(name).getManager();
67
68            repository.setRepositoryManager( (RepositoryManager)PortalContainer.getInstance().
69                 getComponentInstance(managerName) );
70
71             WorkspaceEntry[] wes = config.getWorkspaceEntries();
72             for (int i = 0; i < wes.length; i++) {
73                 if (wes[i].getRepositoryName().equals(name)) {
74                     workspaceContainers.put(wes[i].getName(),
75                         (WorkspaceContainer)PortalContainer.getInstance().
76                         getComponentInstance(config.getWorkspaceEntry(name,
77                         wes[i].getName()).getContainerName()));
78
79                     if(wes[i].isBase())
80                         repository.setDefaultWorkspaceName( wes[i].getName() );
81
82                     repository.getRepositoryManager().addWorkspaceContainer(wes[i].getContainerName());
83                 }
84             }
85        } catch (RepositoryConfigurationException e) {
86             throw new RepositoryException("RepositoryServiceImpl.getRepository() failed ", e);
87        }
88
89        repository.setWorkspaceContainers(workspaceContainers);
90
91     }
92     return repository;
93   }
94
95   public RepositoryServiceConfig getConfig() {
96     return config;
97   }
98   
99   private void init(PortalContainer portalContainer) {
100     DefaultComponentAdapterFactory factory = new DefaultComponentAdapterFactory() ;
101     RepositoryManagerEntry[] mEntries = config.getSupportedRepositoryManagerEntries();
102     for (int i = 0; i < mEntries.length; i++) {
103       Parameter[] params = new Parameter[mEntries[i].getParameters().size()+1];
104       params[0] = new ConstantParameter(mEntries[i].getName());
105       int j = 1;
106       for (Enumeration e = mEntries[i].getParameters().propertyNames(); e.hasMoreElements(); j++)
107         params[j] = new ConstantParameter(mEntries[i].getParameters().getProperty((String JavaDoc) e.nextElement()));
108       ComponentAdapter adapter =
109         factory.createComponentAdapter(mEntries[i].getName(), mEntries[i].getType(), params) ;
110       portalContainer.registerComponent(adapter) ;
111     }
112
113     ContainerEntry[] cEntries = config.getSupportedContainerEntries();
114     for (int i = 0; i < cEntries.length; i++) {
115       Parameter[] params = new Parameter[cEntries[i].getParameters().size()+1];
116       params[0] = new ConstantParameter(cEntries[i].getName());
117       int j = 1;
118       for (Enumeration e = cEntries[i].getParameters().propertyNames(); e.hasMoreElements(); j++)
119         params[j] = new ConstantParameter(cEntries[i].getParameters().getProperty((String JavaDoc) e.nextElement()));
120       ComponentAdapter adapter =
121         factory.createComponentAdapter(cEntries[i].getName(), cEntries[i].getType(), params) ;
122       portalContainer.registerComponent(adapter) ;
123     }
124
125     RepositoryEntry[] rEntries = config.getRepositoryEntries();
126     for (int i = 0; i < rEntries.length; i++) {
127       Parameter[] params = new Parameter[2];
128       params[0] = new ConstantParameter(rEntries[i].getName());
129       params[1] = new ComponentParameter(); // SecurityService
130
ComponentAdapter adapter =
131         factory.createComponentAdapter(rEntries[i].getName(), RepositoryImpl.class, params) ;
132       portalContainer.registerComponent(adapter) ;
133     }
134   }
135 }
136
Popular Tags