Skip to content

Commit

Permalink
up and running
Browse files Browse the repository at this point in the history
  • Loading branch information
aarontharris committed Oct 2, 2013
1 parent d9b444b commit 25df78b
Show file tree
Hide file tree
Showing 14 changed files with 298 additions and 172 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
.DS_Store
97 changes: 49 additions & 48 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>Loopd</groupId>
<artifactId>Loopd</artifactId>
<packaging>war</packaging>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Loopd</name>
<description>Loopd</description>
Expand All @@ -16,39 +16,41 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId> </dependency> -->

<dependency>
<groupId>com.google.code.gson</groupId>
Expand All @@ -60,13 +62,12 @@
<artifactId>mongo-java-driver</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.8.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<dependency>
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
Expand All @@ -80,19 +81,19 @@
<url>http://download.oracle.com/maven</url>
<layout>default</layout>
</repository>
<repository>
<id>spy</id>
<name>Spy Repository</name>
<layout>default</layout>
<url>http://files.couchbase.com/maven2/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spy</id>
<name>Spy Repository</name>
<layout>default</layout>
<url>http://files.couchbase.com/maven2/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<properties>
<jersey.version>2.3.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<properties>
<jersey.version>2.3.1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
25 changes: 0 additions & 25 deletions src/com/example/MyResource.java

This file was deleted.

27 changes: 27 additions & 0 deletions src/com/loopd/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.loopd;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import com.google.inject.Guice;
import com.google.inject.Injector;

@WebListener
public class Application implements ServletContextListener {
private static Injector injector;

public void contextInitialized( ServletContextEvent event ) {
injector = Guice.createInjector( new LoopdGuiceModule() );
}

public void contextDestroyed( ServletContextEvent event ) {
// Do your thing during webapp's shutdown.
}

public static void injectMembers( Object o ) {
if ( injector != null ) {
injector.injectMembers( o );
}
}
}
19 changes: 19 additions & 0 deletions src/com/loopd/GuiceResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.loopd;

import com.google.inject.Inject;


public class GuiceResource {

@Inject
private Logger log;

protected Logger log() {
return log;
}

public GuiceResource() {
Application.injectMembers( this );
}

}
22 changes: 1 addition & 21 deletions src/com/loopd/LoopdGuiceModule.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
package com.loopd;

import java.net.InetSocketAddress;

import net.spy.memcached.MemcachedClient;

import com.google.gson.Gson;
import com.google.inject.AbstractModule;
import com.mongodb.DB;
import com.mongodb.MongoClient;

public class LoopdGuiceModule extends AbstractModule {

@Override
protected void configure() {
try {
{ // Guice
{ // Gson
bind( Gson.class ).toInstance( new Gson() );
}

{ // MongoDB
MongoClient mongoClient = new MongoClient( "127.0.0.1", 26017 );
DB db = mongoClient.getDB( "loop" );
if ( !db.authenticate( "loopuser", "looppass".toCharArray() ) ) {
throw new IllegalStateException( "Not authorized to access the DB" );
}
bind( DB.class ).toInstance( db );
}

{ // Memcached
MemcachedClient client = new MemcachedClient( new InetSocketAddress( "127.0.0.1", 11211 ) );
bind( MemcachedClient.class ).toInstance( client );
}
} catch ( Exception e ) {
e.printStackTrace();
throw new IllegalStateException( "Guice Initialization Failed" );
Expand Down
12 changes: 0 additions & 12 deletions src/com/loopd/SampleService.java

This file was deleted.

58 changes: 0 additions & 58 deletions src/com/loopd/Test.java

This file was deleted.

34 changes: 30 additions & 4 deletions src/com/loopd/data/Serializer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.loopd.data;

import java.net.InetSocketAddress;
import java.util.Set;

import net.spy.memcached.MemcachedClient;
Expand All @@ -13,6 +14,7 @@
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;

Expand All @@ -24,15 +26,35 @@ public class Serializer {
private static final int SECONDS_7DAY = SECONDS_HOUR * 7;
private static final int SECONDS_14DAY = SECONDS_DAY * 14;

@Inject
private DB db;
@Inject
private MemcachedClient memc;
@Inject
private Gson gson;
@Inject
private Logger log;

private DB db;
private MemcachedClient memc;

@Inject
private void init() {
try {
{ // MongoDB
MongoClient mongoClient = new MongoClient( "127.0.0.1", 26017 );
db = mongoClient.getDB( "loop" );
if ( !db.authenticate( "loopuser", "looppass".toCharArray() ) ) {
throw new IllegalStateException( "Not authorized to access the DB" );
}
}

{ // Memcached
memc = new MemcachedClient( new InetSocketAddress( "127.0.0.1", 11211 ) );
}
} catch ( Exception e ) {
e.printStackTrace();
throw new IllegalStateException( "Guice Initialization Failed" );
}
}


private String toKey( Class<?> clazz, String value ) {
return clazz.getCanonicalName() + "_" + value;
}
Expand Down Expand Up @@ -60,6 +82,10 @@ private void putToCache( String key, String jsonStr ) {
memc.set( key, SECONDS_14DAY, jsonStr );
}

public String toJsonString( Object o ) {
return gson.toJson( o );
}

public void saveOrUpdate( Object o ) throws Exception {
try {
String itemStr = gson.toJson( o );
Expand Down
Loading

0 comments on commit 25df78b

Please sign in to comment.