Author: [email protected]
Date: Fri Oct 14 15:45:06 2011
New Revision: 1511

Log:
[AMDATUCASSANDRA-109] Fixed writing the cassandra.yaml in all cases upon 
startup (this file is immutable)

Modified:
   
trunk/amdatu-cassandra/cassandra-application/src/main/java/org/amdatu/cassandra/application/service/CassandraConfigurationServiceImpl.java

Modified: 
trunk/amdatu-cassandra/cassandra-application/src/main/java/org/amdatu/cassandra/application/service/CassandraConfigurationServiceImpl.java
==============================================================================
--- 
trunk/amdatu-cassandra/cassandra-application/src/main/java/org/amdatu/cassandra/application/service/CassandraConfigurationServiceImpl.java
  (original)
+++ 
trunk/amdatu-cassandra/cassandra-application/src/main/java/org/amdatu/cassandra/application/service/CassandraConfigurationServiceImpl.java
  Fri Oct 14 15:45:06 2011
@@ -13,8 +13,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.cassandra.application.service;
-
+package org.amdatu.cassandra.application.service;
+
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
@@ -30,181 +30,180 @@
 import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.cm.ManagedService;
 import org.osgi.service.log.LogService;
-
-/**
- * This class provides utility methods to prepare Cassandra configuration 
before starting it.
- * 
- * @author ivol
- */
-public class CassandraConfigurationServiceImpl implements 
CassandraConfigurationService, ManagedService {
-    // Statics
-    private static final String STORAGE_CONF_SOURCE = "conf/cassandra.yaml";
-    private static final String LOG4J_CONF_SOURCE = "conf/log4j.properties";
-
-    // Reference to the LogService
-    private volatile LogService m_logService;
-    private volatile ConfigTemplateManager m_configTemplateManager;
-    private volatile BundleContext m_bundleContext;
-
-    // Private members
-    private File m_workDir;
-    private int m_defaultReplicationFactor;
-    private ConsistencyLevel m_readConsistencyLevel;
-    private ConsistencyLevel m_writeConsistencyLevel;
-    private boolean m_bootstrapMode;
-    private String m_rpcAddress;
-    private int m_rpcPort;
-    private int m_storagePort;
-    private String m_clusterName;
-
-    /**
-     * The init() method is invoked by the Felix dependency manager. It allows 
us to initialize our service. In this
-     * case we will prepare some configuration files needed by Cassandra.
-     */
-    public void init() {
-        m_logService.log(LogService.LOG_DEBUG, "Preparing Cassandra 
configuration");
-        m_logService.log(LogService.LOG_INFO, "Cassandra work directory=" + 
m_workDir.getAbsolutePath());
-
-        // Initialize storage configuration
-        // Load the URL of the storage-conf.xml and write it file using the 
config template
-        // manager, which automatically replaces configuration entries in that 
file
-        File storageConfigFile = new File(m_workDir, 
CassandraConfigurationService.STORAGE_CONF_XML);
-        Bundle bundle = m_bundleContext.getBundle();
-        URL url = bundle.getResource(STORAGE_CONF_SOURCE);
-        try {
-            if (!storageConfigFile.exists()) {
-                // Only write this file if it does not yet exist
-                m_configTemplateManager.writeConfiguration(url, 
storageConfigFile, new CassandraCallbackHandler());
-            }
-            // Cassandra uses this system property to find its storage 
location.
-            System.setProperty("cassandra.config", 
storageConfigFile.toURI().toString());
-        }
-        catch (IOException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not replace 
configuration entries in storage-conf.xml", e);
-        }
-
-        // Initialize log4j
-        File log4jPropertiesFile = new File(m_workDir, 
CassandraConfigurationService.LOG4J_CONF_TARGET);
+
+/**
+ * This class provides utility methods to prepare Cassandra configuration 
before starting it.
+ * 
+ * @author ivol
+ */
+public class CassandraConfigurationServiceImpl implements 
CassandraConfigurationService, ManagedService {
+    // Statics
+    private static final String STORAGE_CONF_SOURCE = "conf/cassandra.yaml";
+    private static final String LOG4J_CONF_SOURCE = "conf/log4j.properties";
+
+    // Reference to the LogService
+    private volatile LogService m_logService;
+    private volatile ConfigTemplateManager m_configTemplateManager;
+    private volatile BundleContext m_bundleContext;
+
+    // Private members
+    private File m_workDir;
+    private int m_defaultReplicationFactor;
+    private ConsistencyLevel m_readConsistencyLevel;
+    private ConsistencyLevel m_writeConsistencyLevel;
+    private boolean m_bootstrapMode;
+    private String m_rpcAddress;
+    private int m_rpcPort;
+    private int m_storagePort;
+    private String m_clusterName;
+
+    /**
+     * The init() method is invoked by the Felix dependency manager. It allows 
us to initialize our service. In this
+     * case we will prepare some configuration files needed by Cassandra.
+     */
+    public void init() {
+        m_logService.log(LogService.LOG_DEBUG, "Preparing Cassandra 
configuration");
+        m_logService.log(LogService.LOG_INFO, "Cassandra work directory=" + 
m_workDir.getAbsolutePath());
+
+        // Initialize storage configuration
+        // Load the URL of the storage-conf.xml and write it file using the 
config template
+        // manager, which automatically replaces configuration entries in that 
file
+        File storageConfigFile = new File(m_workDir, 
CassandraConfigurationService.STORAGE_CONF_XML);
+        Bundle bundle = m_bundleContext.getBundle();
+        URL url = bundle.getResource(STORAGE_CONF_SOURCE);
+        try {
+            // Always write this file, overwriting the existing one (see 
AMDATUCASSANDRA-109)
+            m_configTemplateManager.writeConfiguration(url, storageConfigFile, 
new CassandraCallbackHandler());
+
+            // Cassandra uses this system property to find its storage 
location.
+            System.setProperty("cassandra.config", 
storageConfigFile.toURI().toString());
+        }
+        catch (IOException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not replace 
configuration entries in storage-conf.xml", e);
+        }
+
+        // Initialize log4j
+        File log4jPropertiesFile = new File(m_workDir, 
CassandraConfigurationService.LOG4J_CONF_TARGET);
         url = bundle.getResource(LOG4J_CONF_SOURCE);
-        ClassLoader oldClassLoader = 
Thread.currentThread().getContextClassLoader();
+        ClassLoader oldClassLoader = 
Thread.currentThread().getContextClassLoader();
         try {
             // Perform a classloader switch to prevent log4j trying to load 
classes from the system classloader
             // instead of the bundle classloader.
-            
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
-            m_configTemplateManager.writeConfiguration(url, 
log4jPropertiesFile, new CassandraCallbackHandler());
-            
PropertyConfigurator.configure(log4jPropertiesFile.getAbsolutePath());
-            System.setProperty("log4j.configuration", 
log4jPropertiesFile.toURI().toString());
-            log4jPropertiesFile.deleteOnExit();
-        }
-        catch (IOException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not replace 
configuration entries in cassandra.yaml", e);
-        } 
+            
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+            m_configTemplateManager.writeConfiguration(url, 
log4jPropertiesFile, new CassandraCallbackHandler());
+            
PropertyConfigurator.configure(log4jPropertiesFile.getAbsolutePath());
+            System.setProperty("log4j.configuration", 
log4jPropertiesFile.toURI().toString());
+            log4jPropertiesFile.deleteOnExit();
+        }
+        catch (IOException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not replace 
configuration entries in cassandra.yaml", e);
+        }
         finally {
             Thread.currentThread().setContextClassLoader(oldClassLoader);
-        }
-
-        // Cassandra-foreground must be set to true, otherwise Cassandra will 
close System err and out streams such
-        // that entries will not be visible in the console anymore.
-        System.setProperty("cassandra-foreground", "true");
-
-        // Disable Hector statistics gathering
-        System.setProperty("com.prettyprint.cassandra.load_hector_log4j", 
"true");
-
-        m_logService.log(LogService.LOG_DEBUG, "Cassandra configuration 
preparation completed");
-    }
-
-    @SuppressWarnings("rawtypes")
-    public void updated(final Dictionary dictionary) throws 
ConfigurationException {
-        if (dictionary != null) {
-            if (dictionary.get(CONFIG_WORKDIR) == null) {
-                throw new ConfigurationException("Missing configuration key", 
CONFIG_WORKDIR);
-            }
-            m_workDir = relativeToAbsolute((String) 
dictionary.get(CONFIG_WORKDIR));
-            m_defaultReplicationFactor = 
toInt(dictionary.get(DEFAULT_REPLICATION_FACTOR));
-            m_readConsistencyLevel = 
ConsistencyLevel.valueOf(dictionary.get(READ_CONSISTENCY_LEVEL).toString());
-            m_writeConsistencyLevel = 
ConsistencyLevel.valueOf(dictionary.get(WRITE_CONSISTENCY_LEVEL).toString());
-            m_bootstrapMode = 
dictionary.get(AUTOBOOTSTRAP_MODE).toString().equalsIgnoreCase("true");
-            m_rpcAddress = dictionary.get(RPC_ADDRESS).toString();
-            if (m_rpcAddress.isEmpty()) {
-                m_rpcAddress = dictionary.get(LISTEN_ADDRESS).toString();
-            }
-            m_rpcPort = toInt(dictionary.get(RPC_PORT));
-            m_storagePort = toInt(dictionary.get(STORAGE_PORT));
-            m_clusterName = dictionary.get(CLUSTER_NAME).toString();
-        }
-    }
-
-    private int toInt(final Object property) {
-        return Integer.parseInt(property.toString().trim());
-    }
-
-    private File relativeToAbsolute(final String workDir) {
-        File file = new File(workDir);
-        if (!file.isAbsolute()) {
-            // If amdatu.dir is provided as System property, use that as base 
directory. Otherwise use the current
-            // directory as base directory
-            if (System.getProperty("amdatu.dir") != null) {
-                return new File(new File(System.getProperty("amdatu.dir"), 
"work"), workDir);
-            }
-            else {
-                return new File(new File(System.getProperty("user.dir"), 
"work"), workDir);
-            }
-        }
-        else {
-            return file;
-        }
-    }
-
-    class CassandraCallbackHandler implements ConfigTemplateCallbackHandler {
-        private String m_amdatuDir = System.getProperty("amdatu.dir");
-
-        public String getValue(final String pid, final String property, final 
Object configValue) {
-            if (CassandraConfigurationService.PID.equals(pid) && m_amdatuDir 
!= null) {
-                if 
(CassandraConfigurationService.CONFIG_WORKDIR.equals(property)
-                                || 
CassandraConfigurationService.COMMITLOG_DIR.equals(property)
-                                || 
CassandraConfigurationService.DATAFILE_DIR.equals(property)
-                                || 
CassandraConfigurationService.SAVEDCACHES_DIR.equals(property)
-                                || 
CassandraConfigurationService.LOG4JLOG_DIR.equals(property)) {
-                    // If amdatu.dir is provided as a system property, convert 
the directories to absolute directories
-                    String dir = m_amdatuDir + File.separator + 
configValue.toString();
-                    dir = dir.replace("\\", "/");
-                    return dir;
-                }
-            }
-            // Return the value as-is
-            return configValue.toString();
-        }
-    }
-
-    public int getDefaultReplicationFactor() {
-        return m_defaultReplicationFactor;
-    }
-
-    public ConsistencyLevel getReadConsistencyLevel() {
-        return m_readConsistencyLevel;
-    }
-
-    public ConsistencyLevel getWriteConsistencyLevel() {
-        return m_writeConsistencyLevel;
-    }
-
-    public boolean isAutoBootstrapMode() {
-        return m_bootstrapMode;
-    }
-
-    public String getRPCAddress() {
-        return m_rpcAddress;
-    }
-
-    public int getRPCPort() {
-        return m_rpcPort;
-    }
-
-    public int getStoragePort() {
-        return m_storagePort;
-    }
-
-    public String getClustername() {
-        return m_clusterName;
-    }
-}
+        }
+
+        // Cassandra-foreground must be set to true, otherwise Cassandra will 
close System err and out streams such
+        // that entries will not be visible in the console anymore.
+        System.setProperty("cassandra-foreground", "true");
+
+        // Disable Hector statistics gathering
+        System.setProperty("com.prettyprint.cassandra.load_hector_log4j", 
"true");
+
+        m_logService.log(LogService.LOG_DEBUG, "Cassandra configuration 
preparation completed");
+    }
+
+    @SuppressWarnings("rawtypes")
+    public void updated(final Dictionary dictionary) throws 
ConfigurationException {
+        if (dictionary != null) {
+            if (dictionary.get(CONFIG_WORKDIR) == null) {
+                throw new ConfigurationException("Missing configuration key", 
CONFIG_WORKDIR);
+            }
+            m_workDir = relativeToAbsolute((String) 
dictionary.get(CONFIG_WORKDIR));
+            m_defaultReplicationFactor = 
toInt(dictionary.get(DEFAULT_REPLICATION_FACTOR));
+            m_readConsistencyLevel = 
ConsistencyLevel.valueOf(dictionary.get(READ_CONSISTENCY_LEVEL).toString());
+            m_writeConsistencyLevel = 
ConsistencyLevel.valueOf(dictionary.get(WRITE_CONSISTENCY_LEVEL).toString());
+            m_bootstrapMode = 
dictionary.get(AUTOBOOTSTRAP_MODE).toString().equalsIgnoreCase("true");
+            m_rpcAddress = dictionary.get(RPC_ADDRESS).toString();
+            if (m_rpcAddress.isEmpty()) {
+                m_rpcAddress = dictionary.get(LISTEN_ADDRESS).toString();
+            }
+            m_rpcPort = toInt(dictionary.get(RPC_PORT));
+            m_storagePort = toInt(dictionary.get(STORAGE_PORT));
+            m_clusterName = dictionary.get(CLUSTER_NAME).toString();
+        }
+    }
+
+    private int toInt(final Object property) {
+        return Integer.parseInt(property.toString().trim());
+    }
+
+    private File relativeToAbsolute(final String workDir) {
+        File file = new File(workDir);
+        if (!file.isAbsolute()) {
+            // If amdatu.dir is provided as System property, use that as base 
directory. Otherwise use the current
+            // directory as base directory
+            if (System.getProperty("amdatu.dir") != null) {
+                return new File(new File(System.getProperty("amdatu.dir"), 
"work"), workDir);
+            }
+            else {
+                return new File(new File(System.getProperty("user.dir"), 
"work"), workDir);
+            }
+        }
+        else {
+            return file;
+        }
+    }
+
+    class CassandraCallbackHandler implements ConfigTemplateCallbackHandler {
+        private String m_amdatuDir = System.getProperty("amdatu.dir");
+
+        public String getValue(final String pid, final String property, final 
Object configValue) {
+            if (CassandraConfigurationService.PID.equals(pid) && m_amdatuDir 
!= null) {
+                if 
(CassandraConfigurationService.CONFIG_WORKDIR.equals(property)
+                    || 
CassandraConfigurationService.COMMITLOG_DIR.equals(property)
+                    || 
CassandraConfigurationService.DATAFILE_DIR.equals(property)
+                    || 
CassandraConfigurationService.SAVEDCACHES_DIR.equals(property)
+                    || 
CassandraConfigurationService.LOG4JLOG_DIR.equals(property)) {
+                    // If amdatu.dir is provided as a system property, convert 
the directories to absolute directories
+                    String dir = m_amdatuDir + File.separator + 
configValue.toString();
+                    dir = dir.replace("\\", "/");
+                    return dir;
+                }
+            }
+            // Return the value as-is
+            return configValue.toString();
+        }
+    }
+
+    public int getDefaultReplicationFactor() {
+        return m_defaultReplicationFactor;
+    }
+
+    public ConsistencyLevel getReadConsistencyLevel() {
+        return m_readConsistencyLevel;
+    }
+
+    public ConsistencyLevel getWriteConsistencyLevel() {
+        return m_writeConsistencyLevel;
+    }
+
+    public boolean isAutoBootstrapMode() {
+        return m_bootstrapMode;
+    }
+
+    public String getRPCAddress() {
+        return m_rpcAddress;
+    }
+
+    public int getRPCPort() {
+        return m_rpcPort;
+    }
+
+    public int getStoragePort() {
+        return m_storagePort;
+    }
+
+    public String getClustername() {
+        return m_clusterName;
+    }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to