Issue 126: added domain objects for java instances and also varible export writers

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2337 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-11-28 02:42:22 +00:00
parent a6e43612fd
commit a52209bfc8
8 changed files with 525 additions and 0 deletions

51
initbuilder/pom.xml Normal file
View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
$HeadURL$ $Revision$ $Date$ Copyright (C) 2009 Adrian Cole
<adrian@jclouds.org>
====================================================================
Licensed to the Apache Software Foundation (ASF) under one or
more contributor license agreements. See the NOTICE file
distributed with this work for additional information regarding
copyright ownership. The ASF licenses this file to you 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.html 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.
====================================================================
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>jclouds-project</artifactId>
<groupId>org.jclouds</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../project/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jclouds-initbuilder</artifactId>
<name>jclouds init builder</name>
<packaging>jar</packaging>
<description>creates init scripts that can be used to manage services</description>
<properties>
<jclouds.test.listener></jclouds.test.listener>
</properties>
<dependencies>
<dependency>
<groupId>com.google.common</groupId>
<artifactId>google-guava</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0-rc4</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,131 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.jclouds.initbuilder.domain;
import java.net.URI;
/**
* Defines the environment of a process that can be started in the background on an operating
* system.
*
* @author Adrian Cole
*/
public class InitMetadata {
private final String name;
private final String platformHome;
private final URI endPoint;
private final String startDir;
private final String stopDir;
private final String configDir;
private final String dataDir;
private final String logDir;
private final String goldDir;
public InitMetadata(String name, String platformHome, URI endPoint, String startDir,
String stopDir, String configDir, String dataDir, String logDir, String goldDir) {
this.name = name;
this.platformHome = platformHome;
this.endPoint = endPoint;
this.startDir = startDir;
this.stopDir = stopDir;
this.configDir = configDir;
this.dataDir = dataDir;
this.logDir = logDir;
this.goldDir = goldDir;
}
/**
* working directory when starting the server.
*/
public String getStartDir() {
return startDir;
}
/**
* working directory when stopping the server.
*/
public String getStopDir() {
return stopDir;
}
/**
* Where the platform that this process is an instance of is located. This is analogous to the
* CATALINA_HOME on the tomcat platform.
*/
public String getPlatformHome() {
return platformHome;
}
/**
* what uniquely identifies your process in a listing. Note that this will become a part of the
* process args.
*/
public String getName() {
return name;
}
/**
* holds configuration files of the process. These are generated or copied from data in the
* {@link #getGoldDir gold copy directory}.
*/
public String getConfigDir() {
return configDir;
}
/**
* holds files that are generated at runtime, but are not temporary. Ex. customer data, state,
* etc. These files survive recreation of the instance.
*/
public String getDataDir() {
return dataDir;
}
/**
* where all logs are written. The following files are created here:
* <ul>
* <li>stdout.log - where stdout is piped to upon start.</li>
* <li>stderr.log - where stderr is piped to upon start.</li>
* <li>pid.log - holds the process id, if the process is running.</li>
* </ul>
*/
public String getLogDir() {
return logDir;
}
/**
* on-disk, read-only location of the artifacts needed to recreate this process.
*/
public String getGoldDir() {
return goldDir;
}
/**
* the named ip and port that this process will bind server sockets to, as well the protocol used
* to test it.
*/
public URI getEndPoint() {
return endPoint;
}
}

View File

@ -0,0 +1,75 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.jclouds.initbuilder.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI;
/**
* Defines the environment of a java process that can be started in the background on an operating
* system.
*
* @see InitMetadata
* @author Adrian Cole
*/
public class JavaInitMetadata extends InitMetadata {
private final String javaHome;
private final String[] classpath;
private final String mainClass;
private final String[] opts;
private final String[] args;
public JavaInitMetadata(String name, String platformHome, URI endPoint, String startDir,
String stopDir, String configDir, String dataDir, String logDir, String goldDir,
String javaHome, String[] classpath, String mainClass, String[] opts, String[] args) {
super(name, platformHome, endPoint, startDir, stopDir, configDir, dataDir, logDir, goldDir);
this.javaHome = checkNotNull(javaHome, "javaHome");
this.classpath = checkNotNull(classpath, "classpath");
this.mainClass = checkNotNull(mainClass, "mainClass");
this.opts = checkNotNull(opts, "opts");
this.args = checkNotNull(args, "args");
}
public String getJavaHome() {
return javaHome;
}
public String[] getClasspath() {
return classpath;
}
public String getMainClass() {
return mainClass;
}
public String[] getOpts() {
return opts;
}
public String[] getArgs() {
return args;
}
}

View File

@ -0,0 +1,41 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.jclouds.initbuilder.domain;
/**
* Type of an Operating System.
*
* @author Adrian Cole
*/
public enum OsFamily {
/**
* All versions of Microsoft Windows
*/
WINDOWS,
/**
* All Unix and Unix-like operating systems
*/
UNIX
}

View File

@ -0,0 +1,71 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.jclouds.initbuilder.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI;
/**
* A java process that has a graceful shutdown mechanism.
*
* @see JavaInitMetadata
* @author Adrian Cole
*/
public class StoppableJavaInitMetadata extends JavaInitMetadata {
private final String[] stopClasspath;
private final String stopClass;
private final String[] stopOpts;
private final String[] stopArgs;
public StoppableJavaInitMetadata(String name, String platformHome, URI endPoint,
String startDir, String stopDir, String configDir, String dataDir, String logDir,
String goldDir, String javaHome, String[] classpath, String mainClass, String[] opts,
String[] args, String[] stopClasspath, String stopClass, String[] stopOpts,
String[] stopArgs) {
super(name, platformHome, endPoint, startDir, stopDir, configDir, dataDir, logDir, goldDir,
javaHome, classpath, mainClass, opts, args);
this.stopClasspath = checkNotNull(stopClasspath, "stopClasspath");
this.stopClass = checkNotNull(stopClass, "stopClass");
this.stopOpts = checkNotNull(stopOpts, "stopOpts");
this.stopArgs = checkNotNull(stopArgs, "stopArgs");
}
public String[] getStopClasspath() {
return stopClasspath;
}
public String getStopClass() {
return stopClass;
}
public String[] getStopOpts() {
return stopOpts;
}
public String[] getStopArgs() {
return stopArgs;
}
}

View File

@ -0,0 +1,100 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.jclouds.initbuilder.util;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jclouds.initbuilder.domain.OsFamily;
import com.google.common.base.CaseFormat;
import com.google.common.collect.ImmutableMap;
/**
* Utilities used to build init scripts.
*
* @author Adrian Cole
*/
public class Utils {
public static final Pattern pattern = Pattern.compile("\\{(.+?)\\}");
/**
* replaces tokens that are expressed as <code>{token}</code>
*
* <p/>
* ex. if input is "hello {where}"<br/>
* and replacements is "where" -> "world" <br/>
* then replaceTokens returns "hello world"
*
* @param input
* source to replace
* @param replacements
* token/value pairs
*/
public static String replaceTokens(String input, Map<String, String> replacements) {
Matcher matcher = pattern.matcher(input);
StringBuilder builder = new StringBuilder();
int i = 0;
while (matcher.find()) {
String replacement = replacements.get(matcher.group(1));
builder.append(input.substring(i, matcher.start()));
if (replacement == null)
builder.append(matcher.group(0));
else
builder.append(replacement);
i = matcher.end();
}
builder.append(input.substring(i, input.length()));
return builder.toString();
}
public static final Map<OsFamily, String> OS_TO_EXPORTER_PATTERN = ImmutableMap.of(
OsFamily.UNIX, "export {key}=\"{value}\"\n", OsFamily.WINDOWS, "set {key}={value}\r\n");
/**
* converts a map into variable exports relevant to the specified platform.
* <p/>
* ex. if variablesInLowerCamelCase is "mavenOpts" -> "-Xms64m -Xmx256m" <br/>
* and family is UNIX<br/>
* then writeVariableExporters returns literally {@code export MAVEN_OPTS="-Xms64m -Xmx256m"\n}
*
* @param variablesInLowerCamelCase
* lower camel keys to values
* @param family
* operating system for formatting
*/
public static String writeVariableExporters(Map<String, String> variablesInLowerCamelCase,
OsFamily family) {
StringBuilder initializers = new StringBuilder();
for (Entry<String, String> entry : variablesInLowerCamelCase.entrySet()) {
String key = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, entry.getKey());
initializers.append(replaceTokens(OS_TO_EXPORTER_PATTERN.get(family), ImmutableMap.of(
"key", key, "value", entry.getValue())));
}
return initializers.toString();
}
}

View File

@ -0,0 +1,55 @@
/**
*
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.jclouds.initbuilder.util;
import static org.testng.Assert.assertEquals;
import java.io.UnsupportedEncodingException;
import org.jclouds.initbuilder.domain.OsFamily;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "jclouds.UtilsTest")
public class UtilsTest {
public void testReplaceTokens() throws UnsupportedEncodingException {
assertEquals(Utils.replaceTokens("hello {where}", ImmutableMap.of("where", "world")),
"hello world");
}
public void testWriteVariableExportersUNIX() {
assertEquals(Utils.writeVariableExporters(ImmutableMap.of("mavenOpts",
"-Xms128m -Xmx256m -XX:+HeapDumpOnOutOfMemoryError"), OsFamily.UNIX),
"export MAVEN_OPTS=\"-Xms128m -Xmx256m -XX:+HeapDumpOnOutOfMemoryError\"\n");
}
public void testWriteVariableExportersWindows() {
assertEquals(Utils.writeVariableExporters(ImmutableMap.of("mavenOpts",
"-Xms128m -Xmx256m -XX:+HeapDumpOnOutOfMemoryError"), OsFamily.WINDOWS),
"set MAVEN_OPTS=-Xms128m -Xmx256m -XX:+HeapDumpOnOutOfMemoryError\r\n");
}
}

View File

@ -49,6 +49,7 @@
<module>vcloud</module>
<module>twitter</module>
<module>rimuhosting</module>
<module>initbuilder</module>
</modules>
<build>
<plugins>