1 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 ; 44 import java.util.Map ; 45 import java.util.Collections ; 46 import java.util.HashMap ; 47 import java.util.Properties ; 48 import java.io.IOException ; 49 import java.net.URI ; 50 51 55 public class RemoteRepositoryManager implements RepositoryManager, Configurable, Initializable, ThreadSafe, ExtensionRegistrar, Serviceable, LogEnabled { 56 private String baseURL; 57 private String 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 registeredExtensions = Collections.synchronizedMap(new HashMap ()); 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 75 public RemoteRepositoryManager() { 76 } 78 79 public RemoteRepositoryManager(String url, Credentials cacheUserCredentials) throws Exception { 80 this(url, cacheUserCredentials, DEFAULT_MAX_HTTP_CONNECTIONS); 81 } 82 83 public RemoteRepositoryManager(String url, Credentials cacheUserCredentials, int maxHttpConnections) throws Exception { 84 this(url, cacheUserCredentials, null, null, null, maxHttpConnections); 85 } 86 87 public RemoteRepositoryManager(String url, Credentials cacheUserCredentials, JmsClient jmsClient, String jmsTopic, Logger logger) throws Exception { 88 this(url, cacheUserCredentials, jmsClient, jmsTopic, logger, DEFAULT_MAX_HTTP_CONNECTIONS); 89 } 90 91 public RemoteRepositoryManager(String url, Credentials cacheUserCredentials, JmsClient jmsClient, String jmsTopic, Logger logger, int maxHttpConnections) throws Exception { 92 if (url == null) 93 throw new NullPointerException ("baseURL parameter missing."); 94 if (cacheUserCredentials == null) 95 throw new NullPointerException ("cacheUserCredentials parameter missing."); 96 if (jmsClient != null && logger == null) { 97 throw new IllegalStateException ("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 cacheUserLogin = cacheUserConf.getAttribute("login"); 127 final String 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 name = extensionConfs[i].getAttribute("name"); 133 String className = extensionConfs[i].getAttribute("class"); 134 ExtensionProvider extensionProvider; 135 try { 136 Class clazz = getClass().getClassLoader().loadClass(className); 137 extensionProvider = (ExtensionProvider)clazz.newInstance(); 138 } catch (Exception 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 { 149 MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager(); 150 httpConnectionManager.setMaxConnectionsPerHost(maxHttpConnections); 151 httpConnectionManager.setMaxTotalConnections(maxHttpConnections); 152 httpClient = new HttpClient(httpConnectionManager); 153 URI parsedBaseURL = new URI (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 name, ExtensionProvider extensionProvider) { 186 if (registeredExtensions.containsKey(name)) { 187 throw new RuntimeException ("There is already and extension registered using the name " + name); 188 } 189 190 if (registeredExtensions.containsValue(extensionProvider)) { 191 throw new RuntimeException ("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 key = null; 199 Iterator entryIt = registeredExtensions.entrySet().iterator(); 200 while (entryIt.hasNext()) { 201 Map.Entry entry = (Map.Entry )entryIt.next(); 202 if (entry.getValue() == extensionProvider) { 203 key = (String )entry.getKey(); 204 break; 205 } 206 } 207 if (key != null) 208 registeredExtensions.remove(key); 209 } 210 211 public class Context { 212 private Context() { 213 } 215 216 public CommonRepositorySchema getCommonRepositorySchema() { 217 return commonRepository.getRepositorySchema(); 218 } 219 220 public CommonRepository getCommonRepository() { 221 return commonRepository; 222 } 223 224 public String 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 getRepositoryServerVersion() { 238 return commonRepository.getServerVersion(cacheUser); 239 } 240 } 241 | Popular Tags |