Permalink
Browse files

initial beanstalk spring integration

  • Loading branch information...
0 parents commit e35ace3d8c0cb284bc1dcf1dc2c7fe81807340d5 @zhihengz zhihengz committed Dec 18, 2011
@@ -0,0 +1,2 @@
+*~
+target/
4 README
@@ -0,0 +1,4 @@
+Beanstalk Spring Integration
+===========================
+
+This is place we hosts any spring specific code.
86 pom.xml
@@ -0,0 +1,86 @@
+<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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.longhorn.beanstalk</groupId>
+ <artifactId>beanstalk-spring-integration</artifactId>
+ <version>0.0.1${base}</version>
+ <packaging>jar</packaging>
+ <description>beanstalk spring plumber</description>
+ <name>beanstalk-spring-integration</name>
+ <properties>
+ <base>-SNAPSHOT</base>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context</artifactId>
+ <version>3.0.6.RELEASE</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-web</artifactId>
+ <version>3.0.6.RELEASE</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.16</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>servletapi</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.4</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>net.java.dev.jets3t</groupId>
+ <artifactId>jets3t</artifactId>
+ <version>0.8.1</version>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0.2</version>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ <compilerArgument>-Xlint:all</compilerArgument>
+ <showWarnings>true</showWarnings>
+ <showDeprecation>true</showDeprecation>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.11</version>
+ <configuration>
+ <systemProperties>
+ <property>
+ <name>AWS_ACCESS_KEY_ID</name>
+ <value>${aws.credentials.accessKey}</value>
+ </property>
+ <property>
+ <name>AWS_SECRET_KEY</name>
+ <value>${aws.credentials.secretKey}</value>
+ </property>
+ </systemProperties>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
@@ -0,0 +1,77 @@
+package org.longhorn.beanstalk.springintegration.config;
+
+import java.io.IOException;
+import java.util.Properties;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+import org.springframework.core.io.Resource;
+import org.springframework.web.util.Log4jWebConfigurer;
+import org.springframework.util.PropertyPlaceholderHelper;
+import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
+import org.apache.log4j.PropertyConfigurator;
+
+public class Log4jS3ConfigListener implements ServletContextListener {
+
+ private static final String
+ CONFIG_LOCATION_PARAM = "log4jS3ConfigLocation";
+
+ private boolean fallback = true;
+ public void contextInitialized( ServletContextEvent event ) {
+
+ ServletContext servletContext = event.getServletContext();
+ String location = servletContext.getInitParameter( CONFIG_LOCATION_PARAM );
+ if ( location != null ) {
+ try {
+ fallback = false;
+ loadPropertiesFromS3( location );
+ } catch( Exception e ) {
+ fallback = true;
+ servletContext.log( "failed to load log4j properties from s3", e );
+ }
+ }
+ if ( fallback ) {
+ Log4jWebConfigurer.initLogging( event.getServletContext() );
+ }
+ }
+
+ public void contextDestroyed( ServletContextEvent event ) {
+
+ if ( fallback ) {
+ Log4jWebConfigurer.shutdownLogging( event.getServletContext() );
+ }
+ }
+
+ private void loadPropertiesFromS3( String location ) {
+
+ S3ResourceLoader s3ResourceLoader = new S3ResourceLoader();
+ String resolvedLocation = parseStringValue( location );
+ Resource resource = s3ResourceLoader.getResource( resolvedLocation );
+ Properties properties = new Properties();
+ try {
+ properties.load( resource.getInputStream() );
+ } catch( IOException e ) {
+ throw new S3ResourceException( "could not load log4j properties from " + resolvedLocation, e );
+ }
+ PropertyConfigurator.configure( properties );
+
+ }
+ private String parseStringValue( String strVal ) {
+ PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}", ":", false);
+ PlaceholderResolver resolver = new SimpleSystemPropertyPlaceholderResolver();
+ return helper.replacePlaceholders(strVal, resolver);
+ }
+
+ private class SimpleSystemPropertyPlaceholderResolver implements PlaceholderResolver {
+
+ public String resolvePlaceholder( String placeholderName ) {
+ String value = System.getProperty( placeholderName );
+ if ( value == null ) {
+ throw new S3ResourceException( "could not resolve " + placeholderName );
+ }
+ return value;
+ }
+ }
+}
@@ -0,0 +1,59 @@
+package org.longhorn.beanstalk.springintegration.config;
+
+import java.util.List;
+import java.util.Properties;
+import java.util.ArrayList;
+import java.util.HashSet;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.context.ResourceLoaderAware;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+
+public class S3PropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
+
+ private S3ResourceLoader resourceLoader;
+ private String[] s3Locations = new String[ 0 ];
+ private Resource[] conventionalResources = new Resource[ 0 ];
+
+ public S3PropertyPlaceholderConfigurer() {
+ resourceLoader = new S3ResourceLoader();
+ }
+
+ public void setLocations( Resource[] locations ) {
+ this.conventionalResources = locations;
+ }
+ public void setS3Locations( String[] s3Locations ) {
+ this.s3Locations = new String[ s3Locations.length ];
+ for ( int i = 0; i < s3Locations.length; i++ ) {
+ this.s3Locations[i] = parseStringValue( s3Locations[i],
+ new Properties(),
+ new HashSet() );
+ }
+
+ }
+
+ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory ) throws BeansException {
+ injectS3Resources();
+ super.postProcessBeanFactory( beanFactory );
+ }
+
+ private void injectS3Resources( ) {
+
+ int total = conventionalResources.length + s3Locations.length;
+
+ if ( total > 0 ) {
+ List<Resource> allResources = new ArrayList<Resource>();
+ for ( Resource conventionalResource : conventionalResources ) {
+ allResources.add( conventionalResource );
+ }
+ for ( String s3Location : s3Locations ) {
+ allResources.add( resourceLoader.getResource( s3Location ) );
+ }
+ super.setLocations( allResources.toArray( new Resource[0] ) );
+ }
+
+ }
+}
@@ -0,0 +1,11 @@
+package org.longhorn.beanstalk.springintegration.config;
+
+public class S3ResourceException extends RuntimeException {
+
+ public S3ResourceException( String msg ) {
+ super( msg );
+ }
+ public S3ResourceException( String msg, Throwable cause ) {
+ super( msg, cause );
+ }
+}
@@ -0,0 +1,103 @@
+package org.longhorn.beanstalk.springintegration.config;
+
+import java.io.InputStream;
+
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ByteArrayResource;
+import org.jets3t.service.security.AWSCredentials;
+import org.jets3t.service.S3Service;
+import org.jets3t.service.S3ServiceException;
+import org.jets3t.service.ServiceException;
+import org.jets3t.service.impl.rest.httpclient.RestS3Service;
+import org.jets3t.service.model.S3Object;
+
+public class S3ResourceLoader implements ResourceLoader {
+
+ private static final String LOCATION_PREFIX = "s3://";
+
+ public static class S3Path {
+ public String bucket;
+ public String key;
+ };
+
+ private String awsAccessKey;
+ private String awsSecretKey;
+ private AWSCredentials credentials;
+ private S3Service s3Service;
+ public S3ResourceLoader( ) {
+ this.awsAccessKey = getRequiredSystemProperty( "AWS_ACCESS_KEY_ID" );
+ this.awsSecretKey = getRequiredSystemProperty( "AWS_SECRET_KEY" );
+ this.credentials = new AWSCredentials( awsAccessKey, awsSecretKey );
+ try {
+ this.s3Service = new RestS3Service( credentials );
+ } catch( S3ServiceException e ) {
+ throw new S3ResourceException( "could not initialize s3 service", e );
+ }
+ }
+ public ClassLoader getClassLoader() {
+ return this.getClassLoader();
+ }
+
+ public Resource getResource( String location ) {
+ try {
+ S3Path s3Path = parseS3Path( location );
+ S3Object s3Object = s3Service.getObject( s3Path.bucket, s3Path.key );
+ byte[] buf = readS3Object( s3Object );
+ return new ByteArrayResource( buf , location );
+
+ } catch ( Exception e ) {
+ throw new S3ResourceException( "could not load resource from " + location, e );
+ }
+ }
+
+ private String getRequiredSystemProperty( String propertyName ) {
+ String value = System.getProperty( propertyName );
+ if ( value == null || "".equals( value.trim() ) ) {
+ throw new S3ResourceException( "no " + propertyName + " property found in system" );
+ }
+ return value;
+ }
+
+ private S3Path parseS3Path( String location ) {
+
+ String path = getLocationPath( location );
+ int indexOfSlash = path.lastIndexOf( "/" );
+ S3Path s3Path = new S3Path( );
+ s3Path.bucket = path.substring( 0, indexOfSlash );
+ s3Path.key = path.substring( indexOfSlash + 1, path.length() );
+ return s3Path;
+ }
+
+ private String getLocationPath( String location ) {
+
+ if ( location == null || "".equals( location.trim() ) ) {
+ throw new S3ResourceException( "location cannot be empty or null" );
+ }
+
+ String resolvedLocation = location;
+
+ if ( ! resolvedLocation.startsWith( LOCATION_PREFIX ) ) {
+ throw new S3ResourceException( resolvedLocation + " does not begin with " + LOCATION_PREFIX );
+ }
+
+ return resolvedLocation.substring( LOCATION_PREFIX.length(), resolvedLocation.length() );
+
+ }
+ private byte[] readS3Object( S3Object s3Object ) throws Exception {
+ InputStream inputStream = s3Object.getDataInputStream();
+ int size = 1024;
+ byte[] buf = new byte[ size ];
+
+ int readedSize = inputStream.read( buf );
+ while ( readedSize == 1024 ) {
+ byte[] tmpBuf = new byte[ size ];
+ readedSize = inputStream.read( tmpBuf );
+ byte[] newBuf = new byte[ buf.length + readedSize ];
+ System.arraycopy( buf, 0, newBuf, 0, buf.length );
+ System.arraycopy( tmpBuf, 0, newBuf, buf.length, readedSize );
+ buf = newBuf;
+ }
+ return buf;
+ }
+}
Oops, something went wrong.

0 comments on commit e35ace3

Please sign in to comment.