diff --git a/search-services/alfresco-solrclient-lib/source/java/org/alfresco/solr/client/SOLRAPIClientFactory.java b/search-services/alfresco-solrclient-lib/source/java/org/alfresco/solr/client/SOLRAPIClientFactory.java index 1d4269215..482783ce3 100644 --- a/search-services/alfresco-solrclient-lib/source/java/org/alfresco/solr/client/SOLRAPIClientFactory.java +++ b/search-services/alfresco-solrclient-lib/source/java/org/alfresco/solr/client/SOLRAPIClientFactory.java @@ -18,6 +18,8 @@ */ package org.alfresco.solr.client; +import java.util.HashMap; +import java.util.Map; import java.util.Properties; import org.alfresco.encryption.KeyResourceLoader; @@ -29,9 +31,17 @@ import org.alfresco.httpclient.HttpClientFactory.SecureCommsType; import org.alfresco.repo.dictionary.NamespaceDAO; import org.alfresco.service.cmr.dictionary.DictionaryService; +/** + * This factory encapsulates the creation of a SOLRAPIClient and the management of that resource. + * @author Ahmed Owian + */ public class SOLRAPIClientFactory { - + /* + * Pool of cached client resources keyed on alfresco instances + */ + private static Map clientsPerAlfresco = new HashMap<>(); + // encryption related parameters private String secureCommsType; // "none", "https" private String keyStoreType; @@ -58,31 +68,84 @@ public class SOLRAPIClientFactory private int maxHostConnections = 40; private int socketTimeout = 120000; - - public SOLRAPIClient getSOLRAPIClient(Properties p, KeyResourceLoader keyResourceLoader, DictionaryService dictionaryService, NamespaceDAO namespaceDAO) + /** + * Gets the client resource from the pool. + * @param alfrescoHost + * @param alfrescoPort + * @param alfrescoPortSSL + * @return + */ + private SOLRAPIClient getCachedClient(String alfrescoHost, int alfrescoPort, int alfrescoPortSSL) { - alfrescoHost = p.getProperty("alfresco.host", "localhost"); - alfrescoPort = Integer.parseInt(p.getProperty("alfresco.port", "8080")); - alfrescoPortSSL = Integer.parseInt(p.getProperty("alfresco.port.ssl", "8443")); - baseUrl = p.getProperty("alfresco.baseUrl", "/alfresco"); - keyStoreType = p.getProperty("alfresco.encryption.keystore.type", "JCEKS"); - keyStoreProvider = p.getProperty("alfresco.encryption.keystore.provider"); - passwordFileLocation = p.getProperty("alfresco.encryption.keystore.passwordFileLocation"); - keyStoreLocation = p.getProperty("alfresco.encryption.keystore.location"); - sslKeyStoreType = p.getProperty("alfresco.encryption.ssl.keystore.type"); - sslKeyStoreProvider = p.getProperty("alfresco.encryption.ssl.keystore.provider", ""); - sslKeyStoreLocation = p.getProperty("alfresco.encryption.ssl.keystore.location", "ssl.repo.client.keystore"); - sslKeyStorePasswordFileLocation = p.getProperty("alfresco.encryption.ssl.keystore.passwordFileLocation", "ssl-keystore-passwords.properties"); - sslTrustStoreType = p.getProperty("alfresco.encryption.ssl.truststore.type", "JCEKS"); - sslTrustStoreProvider = p.getProperty("alfresco.encryption.ssl.truststore.provider", ""); - sslTrustStoreLocation = p.getProperty("alfresco.encryption.ssl.truststore.location", "ssl.repo.client.truststore"); - sslTrustStorePasswordFileLocation = p.getProperty("alfresco.encryption.ssl.truststore.passwordFileLocation", "ssl-truststore-passwords.properties"); - secureCommsType = p.getProperty("alfresco.secureComms", "https"); - maxTotalConnections = Integer.parseInt(p.getProperty("alfresco.maxTotalConnections", "40")); - maxHostConnections = Integer.parseInt(p.getProperty("alfresco.maxHostConnections", "40")); - socketTimeout = Integer.parseInt(p.getProperty("alfresco.socketTimeout", "0")); + String key = constructKey(alfrescoHost, alfrescoPort, alfrescoPortSSL); + return clientsPerAlfresco.get(key); + } + + /** + * Constructs a key to identify a unique alfresco instance to which the client will connect. + * @param alfrescoHost + * @param alfrescoPort + * @param alfrescoPortSSL + * @return the key to get a client + */ + private String constructKey(String alfrescoHost, int alfrescoPort, int alfrescoPortSSL) + { + return alfrescoHost + alfrescoPort + alfrescoPortSSL; + } + + /** + * Sets the client in the resource pool. + * @param alfrescoHost + * @param alfrescoPort + * @param alfrescoPortSSL + * @param client + */ + private void setCachedClient(String alfrescoHost, int alfrescoPort, int alfrescoPortSSL, SOLRAPIClient client) + { + String key = constructKey(alfrescoHost, alfrescoPort, alfrescoPortSSL); + clientsPerAlfresco.put(key, client); + } + + /** + * Creates the SOLRAPIClient or gets it from a pool + * @param props solrcore.properties in the /conf directory + * @param keyResourceLoader reads encryption key resources + * @param dictionaryService represents the Repository Data Dictionary + * @param namespaceDAO allows retrieving and creating Namespace definitions + * @return an instance of SOLRAPIClient + */ + public SOLRAPIClient getSOLRAPIClient(Properties props, KeyResourceLoader keyResourceLoader, DictionaryService dictionaryService, NamespaceDAO namespaceDAO) + { + alfrescoHost = props.getProperty("alfresco.host", "localhost"); + alfrescoPort = Integer.parseInt(props.getProperty("alfresco.port", "8080")); + alfrescoPortSSL = Integer.parseInt(props.getProperty("alfresco.port.ssl", "8443")); - return new SOLRAPIClient(getRepoClient(keyResourceLoader), dictionaryService, namespaceDAO); + SOLRAPIClient client = getCachedClient(alfrescoHost, alfrescoPort, alfrescoPortSSL); + if (client == null) + { + baseUrl = props.getProperty("alfresco.baseUrl", "/alfresco"); + keyStoreType = props.getProperty("alfresco.encryption.keystore.type", "JCEKS"); + keyStoreProvider = props.getProperty("alfresco.encryption.keystore.provider"); + passwordFileLocation = props.getProperty("alfresco.encryption.keystore.passwordFileLocation"); + keyStoreLocation = props.getProperty("alfresco.encryption.keystore.location"); + sslKeyStoreType = props.getProperty("alfresco.encryption.ssl.keystore.type"); + sslKeyStoreProvider = props.getProperty("alfresco.encryption.ssl.keystore.provider", ""); + sslKeyStoreLocation = props.getProperty("alfresco.encryption.ssl.keystore.location", "ssl.repo.client.keystore"); + sslKeyStorePasswordFileLocation = props.getProperty("alfresco.encryption.ssl.keystore.passwordFileLocation", "ssl-keystore-passwords.properties"); + sslTrustStoreType = props.getProperty("alfresco.encryption.ssl.truststore.type", "JCEKS"); + sslTrustStoreProvider = props.getProperty("alfresco.encryption.ssl.truststore.provider", ""); + sslTrustStoreLocation = props.getProperty("alfresco.encryption.ssl.truststore.location", "ssl.repo.client.truststore"); + sslTrustStorePasswordFileLocation = props.getProperty("alfresco.encryption.ssl.truststore.passwordFileLocation", "ssl-truststore-passwords.properties"); + secureCommsType = props.getProperty("alfresco.secureComms", "https"); + maxTotalConnections = Integer.parseInt(props.getProperty("alfresco.maxTotalConnections", "40")); + maxHostConnections = Integer.parseInt(props.getProperty("alfresco.maxHostConnections", "40")); + socketTimeout = Integer.parseInt(props.getProperty("alfresco.socketTimeout", "0")); + + client = new SOLRAPIClient(getRepoClient(keyResourceLoader), dictionaryService, namespaceDAO); + setCachedClient(alfrescoHost, alfrescoPort, alfrescoPortSSL, client); + } + + return client; } protected AlfrescoHttpClient getRepoClient(KeyResourceLoader keyResourceLoader)