Author: [email protected]
Date: Wed Jun 6 14:38:56 2012
New Revision: 2448
Log:
[AMDATUCASSANDRA-191] Some refactoring
Added:
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CassandraInstaller.java
Modified:
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/LauncherConfigurationService.java
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CompressionUtil.java
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/LauncherConfigurationServiceImpl.java
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/LauncherServiceImpl.java
Modified:
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/LauncherConfigurationService.java
==============================================================================
---
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/LauncherConfigurationService.java
(original)
+++
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/LauncherConfigurationService.java
Wed Jun 6 14:38:56 2012
@@ -38,11 +38,6 @@
String STORAGE_CONF_XML = "cassandra.yaml";
/**
- * Configuration key for the working directory to use for Cassandra.
- */
- String CONFIG_WORKDIR = "workdir";
-
- /**
* Configuration key for the commit log directory to use for Cassandra.
*/
String COMMITLOG_DIR = "commitlogdir";
@@ -239,11 +234,20 @@
* Config Admin.
*
* @param targetFile Location of the target cassandra.yaml file
- * @throws IOException
- * @throws URISyntaxException
- * @throws TemplateException
+ * @throws IOException In case an IO exception occurred
+ * @throws URISyntaxException In case an URL syntax exception occurred
+ * @throws TemplateException In case an template exception occurred
*/
void writeCassandraYaml(File targetFile) throws IOException,
URISyntaxException, TemplateException;
-
+
+ /**
+ * Writes a custom log4jserver.properties to the Cassandra installation,
filled with properties from
+ * Config Admin.
+ *
+ * @param targetFile Location of the target log4jserver.properties file
+ * @throws IOException In case an IO exception occurred
+ * @throws URISyntaxException In case an URL syntax exception occurred
+ * @throws TemplateException In case an template exception occurred
+ */
void writeLog4jServerProperties(File targetFile) throws IOException,
URISyntaxException, TemplateException;
}
Added:
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CassandraInstaller.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CassandraInstaller.java
Wed Jun 6 14:38:56 2012
@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2010-2012 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.amdatu.cassandra.launcher.service;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+import org.amdatu.cassandra.launcher.LauncherConfigurationService;
+import org.amdatu.template.processor.TemplateException;
+import org.apache.commons.io.FileUtils;
+import org.osgi.service.log.LogService;
+
+/**
+ * This class facilitates downloading a Cassandra release from an URL and
unzipping it.
+ *
+ * @author <a href="mailto:[email protected]">Amdatu Project
Team</a>
+ */
+public class CassandraInstaller {
+ // Cassandra HOME dir becomes [prefix][version]
+ private static final String CASSANDRA_HOME_PREFIX = "cassandra-";
+
+ // Instance variables
+ private LogService m_logService;
+ private LauncherConfigurationService m_configService;
+ private File m_rootDir;
+
+ public CassandraInstaller(LogService logService,
LauncherConfigurationService configService, File dir) {
+ m_logService = logService;
+ m_configService = configService;
+ m_rootDir = dir;
+ }
+
+ /**
+ * Verifies if Cassandra is installed or needs to be updated by comparing
the installed
+ * version with the specified target version in config admin. If required,
it will install or
+ * update Cassandra. After a successful installation or update, or no
update is required
+ * and installation finished successfully before, the home directory of
this Cassandra
+ * installation is returned (the cassandra home directory).
+ * If a Cassandra could not
+ *
+ * @return The cassandra home directory of a successfully installed
Cassandra version.
+ */
+ public File installOrUpdate() {
+ // Step 1. Verify if Cassandra needs to be installed or updated.
+ if (m_configService.isUpdateRequired()) {
+ // Step 1a. Trigger the update
+ if (installUpdate()) {
+ // Update installed successfully
+ // Update the memorized installed version
+
m_configService.updateInstalledVersion(m_configService.getDownloadversion());
+ }
+ else {
+ // Update failed
+ // Verify if a previous version is available, if so, use that
as fallback.
+ if (m_configService.getInstalledVersion() != null &&
!m_configService.getInstalledVersion().isEmpty()) {
+ m_logService.log(
+ LogService.LOG_ERROR,
+ "Installation of Cassandra version '" +
m_configService.getDownloadversion()
+ + "' failed. Falling back to previous installed
version '"
+ + m_configService.getInstalledVersion() + "'.");
+ }
+ else {
+ // Log an error and return null
+ m_logService.log(LogService.LOG_ERROR,
+ "Installation of Cassandra version '" +
m_configService.getDownloadversion()
+ + "' failed. Cassandra could not be started.");
+ return null;
+ }
+ }
+ }
+ else {
+ m_logService.log(LogService.LOG_INFO, "Existing Cassandra
installation found. Installed version: "
+ + m_configService.getInstalledVersion());
+ }
+
+ // The target directory is '[prefix][installed_version]'
+ String installedVersion = m_configService.getInstalledVersion();
+ File cassandraHome = new File(m_rootDir, CASSANDRA_HOME_PREFIX +
installedVersion);
+ return cassandraHome;
+ }
+
+ private boolean installUpdate() {
+ // Step 1a. Log info about the update
+ String downloadUrl = m_configService.getDownloadUrl();
+ String downloadVersion = m_configService.getDownloadversion();
+ String installedVersion = m_configService.getInstalledVersion();
+ String msg = "Cassandra update required. Installed version: ";
+ if (installedVersion == null) {
+ msg += "[none], ";
+ }
+ else {
+ msg += installedVersion + ", ";
+ }
+ msg += "target version: " + downloadVersion + ".";
+ m_logService.log(LogService.LOG_INFO, msg);
+
+ m_logService.log(LogService.LOG_INFO, "Downloading Cassandra from '" +
downloadUrl + "'...");
+
+ // Step 1b. Download and unzip the Cassandra distribution
+ try {
+ File downloadedFile = download(downloadUrl, m_rootDir);
+ m_logService.log(LogService.LOG_INFO, "Download finished
successfully.");
+ m_logService.log(LogService.LOG_INFO, "Deflating file...");
+
+ // The tmp directory is '[prefix][installed_version]-tmp'
+ File tmpDir = new File(m_rootDir, CASSANDRA_HOME_PREFIX +
downloadVersion + "-tmp");
+ tmpDir.mkdirs();
+ File deflatedDir = CompressionUtil.decompress(downloadedFile,
tmpDir);
+
+ // Now move the deflated stuff to the proper directory
+ File srcDir = findFile(deflatedDir,
"cassandra.bat").getParentFile().getParentFile();
+
+ // The target directory is '[prefix][installed_version]'
+ File targetDir = new File(m_rootDir, CASSANDRA_HOME_PREFIX +
downloadVersion);
+ srcDir.renameTo(targetDir);
+ FileUtils.deleteDirectory(tmpDir);
+ m_logService.log(LogService.LOG_INFO, "Deflating finished
successfully.");
+ }
+ catch (IOException e) {
+ m_logService.log(LogService.LOG_ERROR, "Unable to download and
decompress file from '" + downloadUrl
+ + "', Cassandra could not be started.", e);
+ return false;
+ }
+
+ // Step 1c. Find the cassandra.yaml and log4jserver.properties files
and update them
+ try {
+ File yaml = findFile(m_rootDir, "cassandra.yaml");
+ m_logService.log(LogService.LOG_INFO, "Updating
cassandra.yaml...");
+ m_configService.writeCassandraYaml(yaml);
+ m_logService.log(LogService.LOG_INFO, "Cassandra.yaml updated
successfully.");
+
+ File log4j = findFile(m_rootDir, "log4j-server.properties");
+ m_logService.log(LogService.LOG_INFO, "Updating
log4j-server.properties...");
+ m_configService.writeLog4jServerProperties(log4j);
+ m_logService.log(LogService.LOG_INFO, "log4j-server.properties
updated successfully.");
+ }
+ catch (IOException e) {
+ m_logService.log(LogService.LOG_ERROR, "Unable to update
configuration file.", e);
+ return false;
+ }
+ catch (URISyntaxException e) {
+ m_logService.log(LogService.LOG_ERROR, "Unable to update
configuration file.", e);
+ return false;
+ }
+ catch (TemplateException e) {
+ m_logService.log(LogService.LOG_ERROR, "Unable to update
configuration file.", e);
+ return false;
+ }
+
+ // Step 1d. Update is a complete success, so return true
+ return true;
+ }
+
+ private File download(String sDownloadUrl, File targetDir) throws
IOException {
+ String fileName =
sDownloadUrl.substring(sDownloadUrl.lastIndexOf("/"));
+ File targetFile = new File(targetDir, fileName);
+ URL downloadUrl = new URL(sDownloadUrl);
+ m_logService.log(LogService.LOG_INFO, "Downloading '" + sDownloadUrl +
"' to '" + targetFile.getAbsolutePath()
+ + "'");
+ FileUtils.copyURLToFile(downloadUrl, targetFile);
+ m_logService.log(LogService.LOG_INFO, "File downloaded successfully");
+ return targetFile;
+ }
+
+ private File findFile(File dir, String fileName) {
+ File[] files = dir.listFiles();
+ if (files != null) {
+ for (File file : files) {
+
+ if (file.isFile() && file.getName().equals(fileName)) {
+ return file;
+ }
+ else {
+ File found = findFile(file, fileName);
+ if (found != null) {
+ return found;
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+}
Modified:
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CompressionUtil.java
==============================================================================
---
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CompressionUtil.java
(original)
+++
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/CompressionUtil.java
Wed Jun 6 14:38:56 2012
@@ -20,6 +20,8 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
@@ -34,6 +36,23 @@
public final class CompressionUtil {
private CompressionUtil() {
}
+
+ public static File decompress(File file, File targetDir) throws
IOException {
+ File currentFile = file;
+ List<File> deleteFiles = new ArrayList<File>();
+ if (currentFile.getName().endsWith(".tar.gz")) {
+ deleteFiles.add(currentFile);
+ currentFile = CompressionUtil.decompressGz(currentFile, targetDir);
+ }
+ if (currentFile.getName().endsWith(".tar")) {
+ deleteFiles.add(currentFile);
+ currentFile = CompressionUtil.decompressTar(currentFile,
targetDir);
+ }
+ for (File deleteFile : deleteFiles) {
+ deleteFile.delete();
+ }
+ return currentFile;
+ }
/**
* Decompress a gz compressed file.
Modified:
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/LauncherConfigurationServiceImpl.java
==============================================================================
---
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/LauncherConfigurationServiceImpl.java
(original)
+++
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/LauncherConfigurationServiceImpl.java
Wed Jun 6 14:38:56 2012
@@ -73,7 +73,7 @@
private static String DEFAULT_SAVEDCACHED_DIR =
"work/cassandra/saved_caches";
private static String DEFAULT_SYSTEMLOG_DIR = "work/cassandra/log";
- // Reference to the LogService
+ // Service dependencies
private volatile LogService m_logService;
private volatile TemplateEngine m_templateEngine;
private volatile BundleContext m_bundleContext;
@@ -123,10 +123,6 @@
public void updated(final Dictionary dictionary) throws
ConfigurationException {
if (dictionary != null) {
m_properties = dictionary;
- // FIXME: workdir can be removed????
- if (dictionary.get(CONFIG_WORKDIR) == null) {
- throw new ConfigurationException("Missing configuration key",
CONFIG_WORKDIR);
- }
m_commitLogDir = getString(dictionary, COMMITLOG_DIR,
DEFAULT_COMMITLOG_DIR);
m_dataFileDir = getString(dictionary, DATAFILE_DIR,
DEFAULT_DATAFILE_DIR);
m_savedCachesDir = getString(dictionary, SAVEDCACHES_DIR,
DEFAULT_SAVEDCACHED_DIR);
@@ -143,8 +139,8 @@
m_jmxSsl = getBoolean(dictionary, JMX_SSL, DEFAULT_JMX_SSL, false);
m_jmxAuthenticate = getBoolean(dictionary, JMX_AUTHENTICATE,
DEFAULT_JMX_AUTHENTICATE, false);
// FIXME: temp fix, config files not picked up using old
fileinstall
- m_downloadVersion = "1.1.0"; // getString(dictionary,
DOWNLOAD_VERSION, null);
- m_downloadUrl =
"http://apache.cs.uu.nl/dist/cassandra/1.1.0/apache-cassandra-1.1.0-bin.tar.gz";
+ m_downloadVersion = "1.1.1"; // getString(dictionary,
DOWNLOAD_VERSION, null);
+ m_downloadUrl =
"http://apache.cs.uu.nl/dist/cassandra/1.1.1/apache-cassandra-1.1.1-bin.tar.gz";
// getString(dictionary, DOWNLOAD_URL, null);
m_installedVersion = getString(dictionary, INSTALLED_VERSION,
null);
m_systemLogDirectory = getString(dictionary, SYSTEMLOG_DIR,
DEFAULT_SYSTEMLOG_DIR);
@@ -232,14 +228,14 @@
return m_jmxSsl;
}
- public boolean isJmxAuthenticate() {
- return m_jmxAuthenticate;
- }
-
public String getDownloadUrl() {
return m_downloadUrl;
}
+ public boolean isJmxAuthenticate() {
+ return m_jmxAuthenticate;
+ }
+
public String getDownloadversion() {
return m_downloadVersion;
}
Modified:
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/LauncherServiceImpl.java
==============================================================================
---
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/LauncherServiceImpl.java
(original)
+++
trunk/amdatu-cassandra/cassandra-launcher/src/main/java/org/amdatu/cassandra/launcher/service/LauncherServiceImpl.java
Wed Jun 6 14:38:56 2012
@@ -15,22 +15,15 @@
*/
package org.amdatu.cassandra.launcher.service;
-import org.amdatu.cassandra.launcher.CassandraProcessStreamListener;
-import org.amdatu.cassandra.launcher.LauncherConfigurationService;
-import org.amdatu.template.processor.TemplateException;
-
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
-import java.util.List;
+import org.amdatu.cassandra.launcher.CassandraProcessStreamListener;
+import org.amdatu.cassandra.launcher.LauncherConfigurationService;
import org.apache.ace.processlauncher.ProcessLauncherService;
-import org.apache.commons.io.FileUtils;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
@@ -53,36 +46,17 @@
@SuppressWarnings("rawtypes")
public void start() {
+ // Step 1. Install or update Cassandra and retrieve the Cassandra home
dir
File dir = m_bundleContext.getDataFile("");
-
- // Step 1. Install and configure Cassandra
- // Step 1a. Find the cassandra.bat. If it is not found, we assume
Cassandra is not yet installed
- // and we install it
- if (m_configService.isUpdateRequired()) {
- if (installUpdate()) {
- // Step 1b. Update installed version
-
m_configService.updateInstalledVersion(m_configService.getDownloadversion());
- }
- else {
- // Update failed, log error and exit
- m_logService.log(LogService.LOG_ERROR,
- "Installation of Cassandra version '" +
m_configService.getDownloadversion()
- + "' failed. Cassandr acould not be started.");
- return;
- }
- }
- else {
- m_logService.log(LogService.LOG_INFO, "Existing Cassandra
installation found. Installed version: "
- + m_configService.getInstalledVersion());
+ CassandraInstaller installer = new CassandraInstaller(m_logService,
m_configService, dir);
+ File cassandraHome = installer.installOrUpdate();
+ if (cassandraHome == null) {
+ // No working Cassandra installation available, so return.
+ return;
}
- // The target directory is 'cassandra-[installed_version]'
- String installedVersion = m_configService.getInstalledVersion();
- File cassandraHome = new File(dir, "cassandra-" + installedVersion);
-
// Step 2. Run Cassandra
// Step 2a. Setup the configuration to deploy to the Ace process
launcher
-
String exe = buildExecutable(cassandraHome.getAbsolutePath());
Dictionary properties = new Hashtable();
@@ -94,7 +68,7 @@
properties.put("executable.processStreamListener", streamFilter);
properties.put("executable.args", "");
- // Deploy the config
+ // Step 2b. Deploy the config to Config Admin
try {
m_logService.log(LogService.LOG_INFO, "Launching Cassandra...");
m_logService.log(LogService.LOG_DEBUG, "Executable command:");
@@ -113,6 +87,8 @@
m_logService.log(LogService.LOG_INFO, "Stopping Cassandra
instance...");
if (m_config != null) {
try {
+ // Upon stopping this bundle, delete the config file. The ACE
process launcher
+ // will notify this deletion and stop the external Cassandra
process.
m_config.delete();
m_logService.log(LogService.LOG_INFO, "Cassandra stopped
successfully.");
}
@@ -122,127 +98,6 @@
}
}
- private boolean installUpdate() {
- // Step 1b. Log info about the update
- File dir = m_bundleContext.getDataFile("");
- String downloadUrl = m_configService.getDownloadUrl();
- String downloadVersion = m_configService.getDownloadversion();
- String installedVersion = m_configService.getInstalledVersion();
- String msg = "Cassandra update required. Installed version: ";
- if (installedVersion == null) {
- msg += "[none], ";
- }
- else {
- msg += installedVersion + ", ";
- }
- msg += "target version: " + downloadVersion + ".";
- m_logService.log(LogService.LOG_INFO, msg);
-
- m_logService.log(LogService.LOG_INFO, "Downloading Cassandra from '" +
downloadUrl + "'...");
-
- // Step 1c. Download and unzip the Cassandra distribution
- try {
- File downloadedFile = download(downloadUrl, dir);
- m_logService.log(LogService.LOG_INFO, "Download finished
successfully.");
- m_logService.log(LogService.LOG_INFO, "Deflating file...");
-
- // The tmp directory is 'cassandra-[installed_version]-tmp'
- File tmpDir = new File(dir, "cassandra-" + downloadVersion +
"-tmp");
- tmpDir.mkdirs();
- File deflatedDir = decompress(downloadedFile, tmpDir);
-
- // Now move the deflated stuff to the proper directory
- File srcDir = findFile(deflatedDir,
"cassandra.bat").getParentFile().getParentFile();
-
- // The target directory is 'cassandra-[installed_version]'
- File targetDir = new File(dir, "cassandra-" + downloadVersion);
- srcDir.renameTo(targetDir);
- FileUtils.deleteDirectory(tmpDir);
- m_logService.log(LogService.LOG_INFO, "Deflating finished
successfully.");
- }
- catch (IOException e) {
- m_logService.log(LogService.LOG_ERROR, "Unable to download and
decompress file from '" + downloadUrl
- + "', Cassandra could not be started.", e);
- return false;
- }
-
- // Step 1d. Find the cassandra.yaml and log4jserver.properties files
and update them
- try {
- File yaml = findFile(dir, "cassandra.yaml");
- m_logService.log(LogService.LOG_INFO, "Updating
cassandra.yaml...");
- m_configService.writeCassandraYaml(yaml);
- m_logService.log(LogService.LOG_INFO, "Cassandra.yaml updated
successfully.");
-
- File log4j = findFile(dir, "log4j-server.properties");
- m_logService.log(LogService.LOG_INFO, "Updating
log4j-server.properties...");
- m_configService.writeLog4jServerProperties(log4j);
- m_logService.log(LogService.LOG_INFO, "log4j-server.properties
updated successfully.");
- }
- catch (IOException e) {
- m_logService.log(LogService.LOG_ERROR, "Unable to update
configuration file.", e);
- return false;
- }
- catch (URISyntaxException e) {
- m_logService.log(LogService.LOG_ERROR, "Unable to update
configuration file.", e);
- return false;
- }
- catch (TemplateException e) {
- m_logService.log(LogService.LOG_ERROR, "Unable to update
configuration file.", e);
- return false;
- }
-
- // If we reach this code, update is a complete success
- return true;
- }
-
- private File download(String sDownloadUrl, File targetDir) throws
IOException {
- String fileName =
sDownloadUrl.substring(sDownloadUrl.lastIndexOf("/"));
- File targetFile = new File(targetDir, fileName);
- URL downloadUrl = new URL(sDownloadUrl);
- m_logService.log(LogService.LOG_INFO, "Downloading '" + sDownloadUrl +
"' to '" + targetFile.getAbsolutePath()
- + "'");
- FileUtils.copyURLToFile(downloadUrl, targetFile);
- m_logService.log(LogService.LOG_INFO, "File downloaded successfully");
- return targetFile;
- }
-
- private File decompress(File file, File targetDir) throws IOException {
- File currentFile = file;
- List<File> deleteFiles = new ArrayList<File>();
- if (currentFile.getName().endsWith(".tar.gz")) {
- deleteFiles.add(currentFile);
- currentFile = CompressionUtil.decompressGz(currentFile, targetDir);
- }
- if (currentFile.getName().endsWith(".tar")) {
- deleteFiles.add(currentFile);
- currentFile = CompressionUtil.decompressTar(currentFile,
targetDir);
- }
- for (File deleteFile : deleteFiles) {
- deleteFile.delete();
- }
- return currentFile;
- }
-
- private File findFile(File dir, String fileName) {
- File[] files = dir.listFiles();
- if (files != null) {
- for (File file : files) {
-
- if (file.isFile() && file.getName().equals(fileName)) {
- return file;
- }
- else {
- File found = findFile(file, fileName);
- if (found != null) {
- return found;
- }
- }
- }
- }
-
- return null;
- }
-
private String buildExecutable(String cassandraHome) {
String javaOpts = m_configService.getJavaOpts();
String systemProperties = m_configService.getSystemProperties();
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits