[lsc-changes] r302 - in lsc/trunk/src/main/java/org/lsc: . utils/security

jclarke at lsc-project.org jclarke at lsc-project.org
Mon Jul 20 19:12:03 CEST 2009


Author: jclarke
Date: 2009-07-20 19:12:03 +0200 (Mon, 20 Jul 2009)
New Revision: 302

Modified:
   lsc/trunk/src/main/java/org/lsc/Configuration.java
   lsc/trunk/src/main/java/org/lsc/Generator.java
   lsc/trunk/src/main/java/org/lsc/Launcher.java
   lsc/trunk/src/main/java/org/lsc/utils/security/SymmetricEncryption.java
Log:
Refs #40. Factorize setting up configuration from a directory.

Modified: lsc/trunk/src/main/java/org/lsc/Configuration.java
===================================================================
--- lsc/trunk/src/main/java/org/lsc/Configuration.java	2009-07-20 15:34:05 UTC (rev 301)
+++ lsc/trunk/src/main/java/org/lsc/Configuration.java	2009-07-20 17:12:03 UTC (rev 302)
@@ -62,7 +62,9 @@
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
 import org.ietf.ldap.LDAPUrl;
 
 /**
@@ -466,4 +468,29 @@
 	public static String getSeparator() {
 		return System.getProperty("file.separator");
 	}
+	
+	/**
+	 * Set up configuration for the given location, including log4j.
+	 * IMPORTANT: don't log ANYTHING before calling this method!
+	 * @param configurationLocation
+	 */
+	public static void setUp(String configurationLocation)
+	{
+		if (configurationLocation != null)
+		{
+			Configuration.setLocation(configurationLocation);
+		}
+
+		// setup LOG4J
+		// first, reset the configuration because LOG4J automatically loads it from properties
+		// while this may be the Java way, it's not our way, we like real text files, not JARs.
+		LogManager.resetConfiguration();
+
+		String log4jPropertiesFile = Configuration.getConfigurationDirectory() + "log4j.properties";
+		PropertyConfigurator.configure(log4jPropertiesFile);
+
+		// WARNING: don't log anything before HERE!
+
+		LOGGER.debug("Reading configuration from " + Configuration.getConfigurationDirectory());
+	}
 }

Modified: lsc/trunk/src/main/java/org/lsc/Generator.java
===================================================================
--- lsc/trunk/src/main/java/org/lsc/Generator.java	2009-07-20 15:34:05 UTC (rev 301)
+++ lsc/trunk/src/main/java/org/lsc/Generator.java	2009-07-20 17:12:03 UTC (rev 302)
@@ -45,6 +45,8 @@
  */
 package org.lsc;
 
+import java.io.File;
+
 import javax.naming.NamingException;
 
 import org.apache.commons.cli.CommandLine;
@@ -208,6 +210,7 @@
         options.addOption("csvf", "csvfilename", true,
                           "Specify the csv filename");
         options.addOption("name", "taskName", true, "Specify the task name");
+		options.addOption("f", "cfg", true, "Specify configuration directory" );
 
         OptionGroup genTypeOption = new OptionGroup();
         genTypeOption.setRequired(true);
@@ -252,6 +255,11 @@
                     taskName = cmdLine.getOptionValue("name");
                 }
 
+                if (cmdLine.hasOption("f")) {
+                	// if a configuration directory was set on command line, use it to set up Configuration
+                	Configuration.setUp(cmdLine.getOptionValue("f"));
+                }
+                
                 if (cmdLine.hasOption("ldap2ldap")) {
                     genType = GEN_TYPE.LDAP2LDAP;
                 } else if (cmdLine.hasOption("db2ldap")) {

Modified: lsc/trunk/src/main/java/org/lsc/Launcher.java
===================================================================
--- lsc/trunk/src/main/java/org/lsc/Launcher.java	2009-07-20 15:34:05 UTC (rev 301)
+++ lsc/trunk/src/main/java/org/lsc/Launcher.java	2009-07-20 17:12:03 UTC (rev 302)
@@ -118,23 +118,9 @@
      */
     public void run() {
         try {
-        	// if a configuration directory was set on command line, save it to Configuration
-        	if(configurationLocation != null) {
-        		Configuration.setLocation(configurationLocation);
-        	}
-        	        	
-        	// setup LOG4J
-        	// first, reset the configuration because LOG4J automatically loads it from properties
-        	// while this may be the Java way, it's not our way, we like real text files, not JARs.
-        	LogManager.resetConfiguration();
-
-        	String log4jPropertiesFile = Configuration.getConfigurationDirectory() + "log4j.properties";
-        	PropertyConfigurator.configure(log4jPropertiesFile);
+        	// if a configuration directory was set on command line, use it to set up Configuration
+        	Configuration.setUp(configurationLocation);
         	
-        	// WARNING: don't log anything before HERE!
-        	
-    		LOGGER.debug("Reading configuration from " + Configuration.getConfigurationDirectory());
-        	
         	// do the work!
     		sync.launch(syncType, cleanType);
         } catch (Exception e) {

Modified: lsc/trunk/src/main/java/org/lsc/utils/security/SymmetricEncryption.java
===================================================================
--- lsc/trunk/src/main/java/org/lsc/utils/security/SymmetricEncryption.java	2009-07-20 15:34:05 UTC (rev 301)
+++ lsc/trunk/src/main/java/org/lsc/utils/security/SymmetricEncryption.java	2009-07-20 17:12:03 UTC (rev 302)
@@ -257,12 +257,8 @@
 
 			if (cmdLine.getOptions().length > 0 && cmdLine.hasOption("f"))
 			{
-				String configurationLocation = cmdLine.getOptionValue("f");
-				if(configurationLocation != null)
-				{
-					Configuration.setLocation(configurationLocation);
-				}
-				PropertyConfigurator.configure(new File(Configuration.getConfigurationDirectory(), "log4j.properties").toURI().toURL());
+				// if a configuration directory was set on command line, use it to set up Configuration
+				Configuration.setUp(cmdLine.getOptionValue("f"));
             }
 			else
 			{
@@ -270,9 +266,6 @@
 				formatter.printHelp("lsc", options);
                 System.exit(1);
             }
-        } catch (MalformedURLException ex) {
-			System.out.println("Unable to load configuration file : " + argv + " (" + ex + ")");
-            System.exit(1);
 		} catch (ParseException e) {
             System.out.println("Unable to parse options : " + argv + " (" + e + ")");
             System.exit(1);



More information about the lsc-changes mailing list