From 58a6d0acd3a673541375b64e9d2e5d7cfcac4442 Mon Sep 17 00:00:00 2001 From: Gethin James Date: Wed, 8 Jun 2016 12:19:46 +0200 Subject: [PATCH] SEARCH-56: Added a ConfigUtil class for locating properties --- .../org/alfresco/solr/config/ConfigUtil.java | 112 ++++++++++++++++++ .../alfresco/solr/config/ConfigUtilTest.java | 68 +++++++++++ 2 files changed, 180 insertions(+) create mode 100644 search-services/alfresco-solr/src/main/java/org/alfresco/solr/config/ConfigUtil.java create mode 100644 search-services/alfresco-solr/src/test/java/org/alfresco/solr/config/ConfigUtilTest.java diff --git a/search-services/alfresco-solr/src/main/java/org/alfresco/solr/config/ConfigUtil.java b/search-services/alfresco-solr/src/main/java/org/alfresco/solr/config/ConfigUtil.java new file mode 100644 index 000000000..82d479afb --- /dev/null +++ b/search-services/alfresco-solr/src/main/java/org/alfresco/solr/config/ConfigUtil.java @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2005-2016 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.solr.config; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.naming.NoInitialContextException; + +/** + * Helps with configuring and setup of Alfresco and Solr. + * + * @author Gethin James + */ +public class ConfigUtil { + + protected final static Logger log = LoggerFactory.getLogger(ConfigUtil.class); + + private static final String JNDI_PREFIX = "java:comp/env/"; + /** + * Finds the property based on looking up the value in one of three places (in order of preference): + *
    + *
  1. JNDI: via java:comp/env/{propertyName/converted/to/slash}
  2. + *
  3. A Java system property or a Java system property prefixed with solr.
  4. + *
  5. OS environment variable
  6. + *
+ * + * @return A property + */ + public static String locateProperty(String propertyName, String defaultValue) + { + + String propertyValue = null; + String propertyKey = propertyName.toLowerCase(); + String jndiKey = convertPropertyNameToJNDIPath(propertyKey); + String envVar = convertPropertyNameToEnvironmentParam(propertyKey); + + // Try JNDI + try { + Context c = new InitialContext(); + propertyValue = (String) c.lookup(jndiKey); + log.info("Using JNDI key: "+jndiKey+": "+propertyValue ); + return propertyValue; + } catch (NoInitialContextException e) { + log.info("JNDI not configured (NoInitialContextEx)"); + } catch (NamingException e) { + log.info("No "+jndiKey+" in JNDI"); + } catch( RuntimeException ex ) { + log.warn("Odd RuntimeException while testing for JNDI: " + ex.getMessage()); + } + + // Now try system property + propertyValue = System.getProperty(propertyKey); + if( propertyValue != null ) { + log.info("Using system property "+propertyKey+": " + propertyValue ); + return propertyValue; + } + + //try system property again with a solr. prefix + propertyValue = System.getProperty("solr."+propertyKey); + if( propertyValue != null ) { + log.info("Using system property "+"solr."+propertyKey+": " + propertyValue ); + return propertyValue; + } + + // Now try an environment variable + propertyValue = System.getenv(envVar); + if( propertyValue != null ) { + log.info("Using environment variable "+envVar+": " + propertyValue ); + return propertyValue; + } + + //if all else fails then return the default + return defaultValue; + } + + /** + * Takes a property name and splits it via / instead of . + * @param propertyName + * @return the property name as a jndi path + */ + protected static String convertPropertyNameToJNDIPath(String propertyName) + { + if (propertyName == null) propertyName = ""; + return JNDI_PREFIX+propertyName.replace('.','/'); + } + + protected static String convertPropertyNameToEnvironmentParam(String propertyName) + { + if (propertyName == null) propertyName = ""; + return propertyName.replace('.','_').toUpperCase(); + } +} diff --git a/search-services/alfresco-solr/src/test/java/org/alfresco/solr/config/ConfigUtilTest.java b/search-services/alfresco-solr/src/test/java/org/alfresco/solr/config/ConfigUtilTest.java new file mode 100644 index 000000000..843133292 --- /dev/null +++ b/search-services/alfresco-solr/src/test/java/org/alfresco/solr/config/ConfigUtilTest.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2005-2016 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.solr.config; + +import org.junit.Test; + +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +/** + * Tests configuring and setup of Alfresco and Solr properties + * + * @author Gethin James + */ + +public class ConfigUtilTest { + + @Test + public void locateProperty() throws Exception { + assertEquals("king", ConfigUtil.locateProperty("find.me", "king")); + System.setProperty("solr.find.me", "iamfound"); + assertEquals("iamfound", ConfigUtil.locateProperty("find.me", "king")); + System.clearProperty("solr.find.me"); + assertEquals("king", ConfigUtil.locateProperty("find.me", "king")); + System.setProperty("find.me", "iamfoundagain"); + assertEquals("iamfoundagain", ConfigUtil.locateProperty("find.me", "king")); + System.clearProperty("find.me"); + + //Assumes there is always a PATH environment variable + assertNotEquals("king", ConfigUtil.locateProperty("PATH", "king")); + } + + @Test + public void convertPropertyNameToJNDIPath() throws Exception { + assertEquals("java:comp/env/gethin",ConfigUtil.convertPropertyNameToJNDIPath("gethin")); + assertEquals("java:comp/env/solr/content/dir",ConfigUtil.convertPropertyNameToJNDIPath("solr.content.dir")); + assertEquals("java:comp/env/solr/model/dir",ConfigUtil.convertPropertyNameToJNDIPath("solr.model.dir")); + assertEquals("java:comp/env/",ConfigUtil.convertPropertyNameToJNDIPath("")); + assertEquals("java:comp/env/",ConfigUtil.convertPropertyNameToJNDIPath(null)); + } + + @Test + public void convertPropertyNameToEnvironmentParam() throws Exception { + assertEquals("GETHIN",ConfigUtil.convertPropertyNameToEnvironmentParam("gethin")); + assertEquals("SOLR_CONTENT_DIR",ConfigUtil.convertPropertyNameToEnvironmentParam("solr.content.dir")); + assertEquals("SOLR_MODEL_DIR",ConfigUtil.convertPropertyNameToEnvironmentParam("solr.model.dir")); + assertEquals("SOLR_HOST",ConfigUtil.convertPropertyNameToEnvironmentParam("solr.host")); + } + +} \ No newline at end of file