added jruby example

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2589 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2010-01-04 00:29:48 +00:00
parent 4af21bc259
commit e14135e535
10 changed files with 178 additions and 153 deletions

View File

@ -20,6 +20,7 @@ package org.jclouds.blobstore;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import java.net.URI;
import java.util.Properties;
@ -28,6 +29,7 @@ import javax.inject.Inject;
import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpPropertiesBuilder;
import com.google.common.io.Resources;
import com.google.inject.Module;
/**
@ -37,6 +39,17 @@ import com.google.inject.Module;
public class BlobStoreContextFactory {
private final Properties properties;
public BlobStoreContextFactory() throws IOException {
this(init());
}
static Properties init() throws IOException {
Properties properties = new Properties();
properties.load(Resources.newInputStreamSupplier(
Resources.getResource("blobstore.properties")).getInput());
return properties;
}
@Inject
public BlobStoreContextFactory(Properties properties) {
this.properties = properties;

View File

@ -16,29 +16,6 @@
# limitations under the License.
# ====================================================================
#
#
#
# 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.
# ====================================================================
#
atmos.contextbuilder=org.jclouds.atmosonline.saas.blobstore.AtmosBlobStoreContextBuilder
atmos.propertiesbuilder=org.jclouds.atmosonline.saas.AtmosStoragePropertiesBuilder
azureblob.contextbuilder=org.jclouds.azure.storage.blob.blobstore.AzureBlobStoreContextBuilder

143
demos/antjruby/build.xml Normal file
View File

@ -0,0 +1,143 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
====================================================================
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.
====================================================================
-->
<project name="shrinkblob" default="shrinkblob" basedir="." xmlns:artifact="urn:maven-artifact-ant">
<!-- maven must be available before we use it -->
<get src="http://apache.imghat.com/maven/binaries/maven-ant-tasks-2.1.0.jar" dest="maven-ant-tasks-2.1.0.jar"/>
<!-- initialize maven tasks -->
<path id="maven-ant-tasks.classpath" path="maven-ant-tasks-2.1.0.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="urn:maven-artifact-ant"
classpathref="maven-ant-tasks.classpath" />
<artifact:localRepository id="local.repository" path="${user.home}/.m2/repository" />
<artifact:remoteRepository id="jclouds-snapshot.repository" url="http://jclouds.rimuhosting.com/maven2/snapshots" />
<!-- Setup maven so that we can get latest version of jclouds, shrinkwrap, and jruby -->
<artifact:dependencies pathId="shrinkwrap.classpath">
<dependency groupId="org.jboss.shrinkwrap" artifactId="shrinkwrap-impl-base" version="1.0.0-alpha-3" />
<dependency groupId="org.jruby" artifactId="jruby" version="1.4.0"/>
<dependency groupId="org.jclouds" artifactId="jclouds-aws" version="1.0-SNAPSHOT" />
<dependency groupId="org.jclouds" artifactId="jclouds-atmos" version="1.0-SNAPSHOT" />
<dependency groupId="org.jclouds" artifactId="jclouds-azure" version="1.0-SNAPSHOT" />
<dependency groupId="org.jclouds" artifactId="jclouds-rackspace" version="1.0-SNAPSHOT" />
<remoteRepository refid="jclouds-snapshot.repository" />
<localRepository refid="local.repository" />
</artifact:dependencies>
<input
message="What is the directory you'd like to upload?"
addproperty="dir"
/>
<input
message="What is the name of the zip you'd like ${dir} stored to?"
addproperty="zip"
/>
<input
message="What is the container you wish to store ${zip} in?"
addproperty="container"
/>
<input
message="Which service would you like to use (atmos,azureblob,cloudfiles,s3)?"
validargs="atmos,azureblob,cloudfiles,s3"
addproperty="service"
/>
<input
message="What is your account on ${service}?"
addproperty="account"
/>
<input
message="What is the key for ${account}?"
addproperty="key"
/>
<property name="urltoparse" value="blobstore://${account}:${key}@${service}/${container}" />
<target name="export">
<script language="jruby" classpathref="shrinkwrap.classpath"> <![CDATA[
require 'java'
require 'jruby/core_ext'
include_class 'org.apache.tools.ant.Task'
include_class 'org.jclouds.http.HttpUtils'
include_class 'org.jclouds.blobstore.BlobStoreContextFactory'
include_class 'org.jboss.shrinkwrap.api.Archives'
include_class 'org.jboss.shrinkwrap.api.exporter.ZipExporter'
include_class 'org.jboss.shrinkwrap.api.importer.ExplodedImporter'
include_class 'org.jboss.shrinkwrap.impl.base.ServiceExtensionLoader'
# define a new ant task that uses ShrinkWrap to zip up things to a BlobStore
class ShrinkBlob < Task
def setBlobstore(blobstore)
@blobstore = blobstore
end
def setContainer(container)
@container = container
end
def setZip(zip)
@zip = zip
end
def setDir(dir)
@dir = dir
end
def execute
# correct the classloader so that extensions can be found
java.lang.Thread.currentThread().setContextClassLoader(ServiceExtensionLoader.new().getClass().getClassLoader())
print "creating the archive from ",@dir,"\n"
zipStream = Archives.create(@zip, ZipExporter.java_class).as(ExplodedImporter.java_class).importDirectory(@dir).as(ZipExporter.java_class).exportZip()
destination = HttpUtils.createUri(@blobstore)
print "connecting to service ",destination.getHost(),"/",@container,"\n"
context = BlobStoreContextFactory.new().createContext(destination, java.lang.reflect.Array.newInstance(com.google.inject.Module.java_class,0))
context.getBlobStore().createContainer(@container)
print "uploading to ",destination.getHost(),"/",@container,"/",@zip,"\n"
context.createInputStreamMap(@container).put(@zip,zipStream);
context.close();
end
end
# make a real java class so that we can register it in ant
ShrinkBlob.add_method_signature("execute", [java.lang.Void::TYPE])
ShrinkBlob.add_method_signature("setBlobstore", [java.lang.Void::TYPE, java.lang.String.java_class])
ShrinkBlob.add_method_signature("setContainer", [java.lang.Void::TYPE, java.lang.String.java_class])
ShrinkBlob.add_method_signature("setZip", [java.lang.Void::TYPE, java.lang.String.java_class])
ShrinkBlob.add_method_signature("setDir", [java.lang.Void::TYPE, java.io.File.java_class])
clazz = ShrinkBlob.become_java!;
# register the ant task
$project.addTaskDefinition("shrinkblob", clazz)
]]></script>
</target>
<target name="shrinkblob" depends="export" >
<shrinkblob blobstore="${urltoparse}" container="${container}" dir="${dir}" zip="${zip}" />
</target>
</project>

View File

@ -16,7 +16,7 @@
* limitations under the License.
* ====================================================================
*/
package org.jclouds.tools.ant;
package org.jclouds.tools.ant.taskdefs.sshjava;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.scriptbuilder.domain.Statements.exec;
@ -51,7 +51,7 @@ import com.google.common.collect.Lists;
*
* @author Adrian Cole
*/
public class JavaOverSsh extends Java {
public class SSHJava extends Java {
private final SSHExec exec;
private final Scp scp;
private final SSHUserInfo userInfo;
@ -68,7 +68,7 @@ public class JavaOverSsh extends Java {
private String outputProperty;
private boolean append;
public JavaOverSsh() {
public SSHJava() {
super();
setFork(true);
exec = new SSHExec();
@ -76,7 +76,7 @@ public class JavaOverSsh extends Java {
userInfo = new SSHUserInfo();
}
public JavaOverSsh(Task owner) {
public SSHJava(Task owner) {
this();
bindToOwner(owner);
}
@ -97,6 +97,7 @@ public class JavaOverSsh extends Java {
copyPathTo(getCommandLine().getClasspath(), remoteDirectory.getAbsolutePath()
+ "/classpath");
}
if (getCommandLine().getBootclasspath() != null) {
copyPathTo(getCommandLine().getBootclasspath(), remoteDirectory.getAbsolutePath()
+ "/bootclasspath");

View File

@ -16,7 +16,7 @@
* limitations under the License.
* ====================================================================
*/
package org.jclouds.tools.ant;
package org.jclouds.tools.ant.taskdefs.sshjava;
import static org.testng.Assert.assertEquals;
@ -32,6 +32,7 @@ import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Environment.Variable;
import org.jclouds.tools.ant.TestClass;
import org.testng.annotations.Test;
import com.google.common.collect.Iterables;
@ -39,14 +40,14 @@ import com.google.common.collect.Iterables;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "jclouds.JavaOverSshTest")
public class JavaOverSshTest {
@Test(groups = "unit", testName = "jclouds.SSHJavaTest")
public class SSHJavaTest {
public static final Entry<String, String> LAST_ENV = Iterables.getLast(System.getenv()
.entrySet());
// TODO, this test will break in windows
public void testFull() throws SecurityException, NoSuchMethodException {
JavaOverSsh task = makeJavaOverSsh();
SSHJava task = makeSSHJava();
String expected = String
.format(
"export %s=\"%s\"%ncd /tmp/foo\n%s -Xms16m -Xmx32m -cp classpath -Dfooble=baz -Dfoo=bar org.jclouds.tools.ant.TestClass %s hello world\n",
@ -92,7 +93,7 @@ public class JavaOverSshTest {
Java java = makeJava();
java.execute();
JavaOverSsh javaOverSsh = makeJavaOverSsh();
SSHJava javaOverSsh = makeSSHJava();
addDestinationTo(javaOverSsh);
javaOverSsh.execute();
@ -104,7 +105,7 @@ public class JavaOverSshTest {
.getProperty("result"));
}
private void addDestinationTo(JavaOverSsh javaOverSsh) throws UnknownHostException {
private void addDestinationTo(SSHJava javaOverSsh) throws UnknownHostException {
String sshHost = System.getProperty("jclouds.test.ssh.host");
String sshPort = System.getProperty("jclouds.test.ssh.port");
String sshUser = System.getProperty("jclouds.test.ssh.username");
@ -124,8 +125,8 @@ public class JavaOverSshTest {
}
}
private JavaOverSsh makeJavaOverSsh() {
JavaOverSsh task = new JavaOverSsh();
private SSHJava makeSSHJava() {
SSHJava task = new SSHJava();
populateTask(task);
task.setRemotedir(new File("/tmp/foo"));
task.setVerbose(true);

View File

@ -28,7 +28,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Properties;
import java.util.Map.Entry;
import org.jclouds.blobstore.util.BlobStoreUtils;
@ -36,7 +35,6 @@ import org.jclouds.http.HttpUtils;
import com.google.common.io.ByteStreams;
import com.google.common.io.Closeables;
import com.google.common.io.Resources;
/**
*
@ -51,10 +49,10 @@ public class GetPath {
public static void main(String... args) throws IOException {
if (args.length < 2)
throw new IllegalArgumentException(INVALID_SYNTAX);
URI locationAndCredentials;
URI uri;
try {
locationAndCredentials = HttpUtils.createUri(args[0]);
checkArgument(locationAndCredentials.getScheme().equals("blobstore"), "wrong scheme");
uri = HttpUtils.createUri(args[0]);
checkArgument(uri.getScheme().equals("blobstore"), "wrong scheme");
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("%s%n%s", e.getMessage(), INVALID_SYNTAX));
}
@ -64,8 +62,9 @@ public class GetPath {
File destinationDir = new File(args[1]);
destinationDir.mkdirs();
BlobStoreContext<?, ?> context = init(locationAndCredentials);
String path = locationAndCredentials.getPath();
BlobStoreContext<?, ?> context = new BlobStoreContextFactory()
.createContext(uri);
String path = uri.getPath();
if (path.startsWith("/"))
path = path.substring(1);
String container = BlobStoreUtils.parseContainerFromPath(path);
@ -103,10 +102,4 @@ public class GetPath {
}
}
private static BlobStoreContext<?, ?> init(URI locationAndCredentials) throws IOException {
Properties properties = new Properties();
properties.load(Resources.newInputStreamSupplier(Resources.getResource("jclouds.properties"))
.getInput());
return new BlobStoreContextFactory(properties).createContext(locationAndCredentials);
}
}

View File

@ -1,47 +0,0 @@
#
#
# Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
#
# ====================================================================
# 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.
# ====================================================================
#
#
#
# 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.
# ====================================================================
#
azureblob.contextbuilder=org.jclouds.azure.storage.blob.blobstore.AzureBlobStoreContextBuilder
azureblob.propertiesbuilder=org.jclouds.azure.storage.blob.AzureBlobPropertiesBuilder
cloudfiles.contextbuilder=org.jclouds.rackspace.cloudfiles.blobstore.CloudFilesBlobStoreContextBuilder
cloudfiles.propertiesbuilder=org.jclouds.rackspace.cloudfiles.CloudFilesPropertiesBuilder
s3.contextbuilder=org.jclouds.aws.s3.blobstore.S3BlobStoreContextBuilder
s3.propertiesbuilder=org.jclouds.aws.s3.S3PropertiesBuilder

View File

@ -44,7 +44,7 @@ public class PropertiesTest {
@BeforeTest
public void setUp() throws IOException {
properties = new Properties();
properties.load(Resources.newInputStreamSupplier(Resources.getResource("jclouds.properties"))
properties.load(Resources.newInputStreamSupplier(Resources.getResource("blobstore.properties"))
.getInput());
}

View File

@ -23,7 +23,6 @@ import java.net.URI;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import org.apache.commons.vfs.Capability;
import org.apache.commons.vfs.FileName;
@ -43,7 +42,6 @@ import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.io.Resources;
import com.google.inject.Module;
/**
@ -80,12 +78,9 @@ public class BlobStoreFileProvider extends AbstractOriginatingFileProvider {
try {
String uriToParse = rootName.getFriendlyURI();
authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
Properties properties = new Properties();
properties.load(Resources.newInputStreamSupplier(
Resources.getResource("jclouds.properties")).getInput());
URI location = HttpUtils.createUri(uriToParse);
blobStore = new BlobStoreContextFactory(properties).createContext(
blobStore = new BlobStoreContextFactory().createContext(
location,
new Credentials(UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(
authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils

View File

@ -1,51 +0,0 @@
#
#
# Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
#
# ====================================================================
# 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.
# ====================================================================
#
#
#
# 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.
# ====================================================================
#
atmos.contextbuilder=org.jclouds.atmosonline.saas.blobstore.AtmosBlobStoreContextBuilder
atmos.propertiesbuilder=org.jclouds.atmosonline.saas.AtmosStoragePropertiesBuilder
azureblob.contextbuilder=org.jclouds.azure.storage.blob.blobstore.AzureBlobStoreContextBuilder
azureblob.propertiesbuilder=org.jclouds.azure.storage.blob.AzureBlobPropertiesBuilder
cloudfiles.contextbuilder=org.jclouds.rackspace.cloudfiles.blobstore.CloudFilesBlobStoreContextBuilder
cloudfiles.propertiesbuilder=org.jclouds.rackspace.cloudfiles.CloudFilesPropertiesBuilder
s3.contextbuilder=org.jclouds.aws.s3.blobstore.S3BlobStoreContextBuilder
s3.propertiesbuilder=org.jclouds.aws.s3.S3PropertiesBuilder
stub.contextbuilder=org.jclouds.blobstore.integration.StubBlobStoreContextBuilder
stub.propertiesbuilder=org.jclouds.blobstore.integration.StubPropertiesBuilder