KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > repository > clientimpl > RemoteRepositoryManager


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.repository.clientimpl;
17
18 import org.outerj.daisy.repository.*;
19 import org.outerj.daisy.repository.user.Role;
20 import org.outerj.daisy.repository.clientimpl.schema.RemoteSchemaStrategy;
21 import org.outerj.daisy.repository.clientimpl.user.RemoteUserManagementStrategy;
22 import org.outerj.daisy.repository.clientimpl.acl.RemoteAclStrategy;
23 import org.outerj.daisy.repository.clientimpl.variant.RemoteVariantStrategy;
24 import org.outerj.daisy.repository.clientimpl.comment.RemoteCommentStrategy;
25 import org.outerj.daisy.repository.commonimpl.*;
26 import org.outerj.daisy.repository.commonimpl.schema.CommonRepositorySchema;
27 import org.outerj.daisy.util.VersionHelper;
28 import org.outerj.daisy.jms.JmsClient;
29 import org.apache.avalon.framework.configuration.Configurable;
30 import org.apache.avalon.framework.configuration.Configuration;
31 import org.apache.avalon.framework.configuration.ConfigurationException;
32 import org.apache.avalon.framework.activity.Initializable;
33 import org.apache.avalon.framework.thread.ThreadSafe;
34 import org.apache.avalon.framework.service.Serviceable;
35 import org.apache.avalon.framework.service.ServiceManager;
36 import org.apache.avalon.framework.service.ServiceException;
37 import org.apache.avalon.framework.logger.LogEnabled;
38 import org.apache.avalon.framework.logger.Logger;
39 import org.apache.commons.httpclient.HttpClient;
40 import org.apache.commons.httpclient.HostConfiguration;
41 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
42
43 import java.util.Iterator JavaDoc;
44 import java.util.Map JavaDoc;
45 import java.util.Collections JavaDoc;
46 import java.util.HashMap JavaDoc;
47 import java.util.Properties JavaDoc;
48 import java.io.IOException JavaDoc;
49 import java.net.URI JavaDoc;
50
51 /**
52  * @avalon.component version="1.0" name="repository-manager" lifestyle="singleton"
53  * @avalon.service type="org.outerj.daisy.repository.RepositoryManager"
54  */

55 public class RemoteRepositoryManager implements RepositoryManager, Configurable, Initializable, ThreadSafe, ExtensionRegistrar, Serviceable, LogEnabled {
56     private String JavaDoc baseURL;
57     private String JavaDoc jmsTopic;
58     private RemoteDocumentStrategy documentStrategy;
59     private CommonRepository commonRepository;
60     private Credentials cacheUserCredentials;
61     private AuthenticatedUser cacheUser;
62     private Context context = new Context();
63     private Map JavaDoc registeredExtensions = Collections.synchronizedMap(new HashMap JavaDoc());
64     private Logger logger;
65     private JmsClient jmsClient;
66     private HttpClient httpClient;
67     private HostConfiguration hostConfiguration;
68     private static final int DEFAULT_MAX_HTTP_CONNECTIONS = 60;
69     private int maxHttpConnections = DEFAULT_MAX_HTTP_CONNECTIONS;
70
71     /**
72      * Default constructor, only to be used when respecting the Avalon lifecycle
73      * interfaces.
74      */

75     public RemoteRepositoryManager() {
76         // default constructor
77
}
78
79     public RemoteRepositoryManager(String JavaDoc url, Credentials cacheUserCredentials) throws Exception JavaDoc {
80         this(url, cacheUserCredentials, DEFAULT_MAX_HTTP_CONNECTIONS);
81     }
82
83     public RemoteRepositoryManager(String JavaDoc url, Credentials cacheUserCredentials, int maxHttpConnections) throws Exception JavaDoc {
84         this(url, cacheUserCredentials, null, null, null, maxHttpConnections);
85     }
86
87     public RemoteRepositoryManager(String JavaDoc url, Credentials cacheUserCredentials, JmsClient jmsClient, String JavaDoc jmsTopic, Logger logger) throws Exception JavaDoc {
88         this(url, cacheUserCredentials, jmsClient, jmsTopic, logger, DEFAULT_MAX_HTTP_CONNECTIONS);
89     }
90
91     public RemoteRepositoryManager(String JavaDoc url, Credentials cacheUserCredentials, JmsClient jmsClient, String JavaDoc jmsTopic, Logger logger, int maxHttpConnections) throws Exception JavaDoc {
92         if (url == null)
93             throw new NullPointerException JavaDoc("baseURL parameter missing.");
94         if (cacheUserCredentials == null)
95             throw new NullPointerException JavaDoc("cacheUserCredentials parameter missing.");
96         if (jmsClient != null && logger == null) {
97             throw new IllegalStateException JavaDoc("If jmsClient is provided, a logger should also be provided.");
98         }
99
100         this.jmsClient = jmsClient;
101         this.jmsTopic = jmsTopic;
102         this.baseURL = url;
103         this.cacheUserCredentials = cacheUserCredentials;
104         this.logger = logger;
105         this.maxHttpConnections = maxHttpConnections;
106
107         initialize();
108     }
109
110     public void enableLogging(Logger logger) {
111         this.logger = logger;
112     }
113
114     public void service(ServiceManager serviceManager) throws ServiceException {
115         if (!serviceManager.hasService("org.outerj.daisy.jms.JmsClient")) {
116             logger.warn("Failed to lookup jms client component, will do without.");
117             return;
118         }
119         jmsClient = (JmsClient)serviceManager.lookup("org.outerj.daisy.jms.JmsClient");
120     }
121
122     public void configure(Configuration configuration) throws ConfigurationException {
123         this.baseURL = configuration.getChild("repository-server-base-url").getValue();
124
125         Configuration cacheUserConf = configuration.getChild("cacheUser");
126         final String JavaDoc cacheUserLogin = cacheUserConf.getAttribute("login");
127         final String JavaDoc cacheUserPassword = cacheUserConf.getAttribute("password");
128         cacheUserCredentials = new Credentials(cacheUserLogin, cacheUserPassword);
129
130         Configuration[] extensionConfs = configuration.getChild("extensions").getChildren("extension");
131         for (int i = 0; i < extensionConfs.length; i++) {
132             String JavaDoc name = extensionConfs[i].getAttribute("name");
133             String JavaDoc className = extensionConfs[i].getAttribute("class");
134             ExtensionProvider extensionProvider;
135             try {
136                 Class JavaDoc clazz = getClass().getClassLoader().loadClass(className);
137                 extensionProvider = (ExtensionProvider)clazz.newInstance();
138             } catch (Exception JavaDoc e) {
139                 throw new ConfigurationException("Problem loading repository extension " + name, e);
140             }
141             registerExtension(name, extensionProvider);
142         }
143
144         jmsTopic = configuration.getChild("jmsTopic").getValue("daisy");
145         maxHttpConnections = configuration.getChild("maxHttpConnections").getValueAsInteger(DEFAULT_MAX_HTTP_CONNECTIONS);
146     }
147
148     public void initialize() throws Exception JavaDoc {
149         MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
150         httpConnectionManager.setMaxConnectionsPerHost(maxHttpConnections);
151         httpConnectionManager.setMaxTotalConnections(maxHttpConnections);
152         httpClient = new HttpClient(httpConnectionManager);
153         URI JavaDoc parsedBaseURL = new URI JavaDoc(baseURL);
154         hostConfiguration = new HostConfiguration();
155         hostConfiguration.setHost(parsedBaseURL.getHost(), parsedBaseURL.getPort(), parsedBaseURL.getScheme());
156
157         documentStrategy = new RemoteDocumentStrategy(context);
158         this.cacheUser = documentStrategy.getUser(cacheUserCredentials);
159         this.cacheUser.setActiveRoleIds(new long[] {Role.ADMINISTRATOR});
160
161         RemoteSchemaStrategy schemaStrategy = new RemoteSchemaStrategy(context);
162         RemoteAclStrategy aclStrategy = new RemoteAclStrategy(context);
163         RemoteUserManagementStrategy userManagementStrategy = new RemoteUserManagementStrategy(context);
164         RemoteVariantStrategy variantStrategy = new RemoteVariantStrategy(context);
165         RemoteCollectionStrategy collectionStrategy = new RemoteCollectionStrategy(context);
166         RemoteCommentStrategy commentStrategy = new RemoteCommentStrategy(context);
167         this.commonRepository = new RemoteCommonRepository(documentStrategy, schemaStrategy, aclStrategy,
168                 userManagementStrategy, variantStrategy, collectionStrategy, commentStrategy,
169                 context, registeredExtensions, cacheUser);
170
171         if (jmsClient != null) {
172             RemoteEventDispatcher remoteEventDispatcher = new RemoteEventDispatcher(jmsClient, jmsTopic, logger);
173             remoteEventDispatcher.addRepositoryListener(commonRepository.getUserManager().getCacheListener());
174             remoteEventDispatcher.addRepositoryListener(commonRepository.getCollectionManager().getCacheListener());
175             remoteEventDispatcher.addRepositoryListener(commonRepository.getVariantManager().getCacheListener());
176             remoteEventDispatcher.addRepositorySchemaListener(commonRepository.getRepositorySchema().getCacheListener());
177         }
178     }
179
180     public Repository getRepository(final Credentials credentials) throws RepositoryException {
181         AuthenticatedUser user = documentStrategy.getUser(credentials);
182         return new RemoteRepositoryImpl(commonRepository, user, context);
183     }
184
185     public synchronized void registerExtension(String JavaDoc name, ExtensionProvider extensionProvider) {
186         if (registeredExtensions.containsKey(name)) {
187             throw new RuntimeException JavaDoc("There is already and extension registered using the name " + name);
188         }
189
190         if (registeredExtensions.containsValue(extensionProvider)) {
191             throw new RuntimeException JavaDoc("The given extension provider is already registered.");
192         }
193
194         registeredExtensions.put(name, extensionProvider);
195     }
196
197     public synchronized void unregisterExtension(ExtensionProvider extensionProvider) {
198         String JavaDoc key = null;
199         Iterator JavaDoc entryIt = registeredExtensions.entrySet().iterator();
200         while (entryIt.hasNext()) {
201             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entryIt.next();
202             if (entry.getValue() == extensionProvider) {
203                 key = (String JavaDoc)entry.getKey();
204                 break;
205             }
206         }
207         if (key != null)
208             registeredExtensions.remove(key);
209     }
210
211     public class Context {
212         private Context() {
213             // private constructor to make sure no-one else can create this
214
}
215
216         public CommonRepositorySchema getCommonRepositorySchema() {
217             return commonRepository.getRepositorySchema();
218         }
219
220         public CommonRepository getCommonRepository() {
221             return commonRepository;
222         }
223
224         public String JavaDoc getBaseURL() {
225             return baseURL;
226         }
227
228         public HttpClient getSharedHttpClient() {
229             return httpClient;
230         }
231
232         public HostConfiguration getSharedHostConfiguration() {
233             return hostConfiguration;
234         }
235     }
236
237     public String JavaDoc getRepositoryServerVersion() {
238         return commonRepository.getServerVersion(cacheUser);
239     }
240 }
241
Popular Tags