Skip to content
Browse files

First version of Neo4j/Scala project template

  • Loading branch information...
0 parents commit 8e5fad8f5df8ecb30d5ab9893fc8c333ede7532a Martin Kleppmann committed
31 .classpath
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src/main/scala"/>
+ <classpathentry kind="src" output="target/webapp" path="src/main/webapp"/>
+ <classpathentry kind="src" output="target/test-classes" path="src/test/scala"/>
+ <classpathentry kind="src" path="src/main/java"/>
+ <classpathentry kind="var" path="M2_REPO/asm/asm/3.1/asm-3.1.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/glassfish/embedded/glassfish-embedded-all/3.0-Prelude-Embedded-b10/glassfish-embedded-all-3.0-Prelude-Embedded-b10.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/sun/grizzly/grizzly-framework/1.9.8/grizzly-framework-1.9.8.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/sun/grizzly/grizzly-http/1.9.8/grizzly-http-1.9.8.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/sun/grizzly/grizzly-http-servlet/1.9.8/grizzly-http-servlet-1.9.8.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/sun/grizzly/grizzly-portunif/1.9.8/grizzly-portunif-1.9.8.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/sun/grizzly/grizzly-rcm/1.9.8/grizzly-rcm-1.9.8.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/sun/grizzly/grizzly-servlet-webserver/1.9.8/grizzly-servlet-webserver-1.9.8.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/sun/grizzly/grizzly-utils/1.9.8/grizzly-utils-1.9.8.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-client/1.0.3/jersey-client-1.0.3.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-core/1.0.3/jersey-core-1.0.3.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/sun/jersey/jersey-server/1.0.3/jersey-server-1.0.3.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/sun/jersey/test/framework/jersey-test-framework/1.0.3/jersey-test-framework-1.0.3.jar"/>
+ <classpathentry kind="var" path="M2_REPO/javax/ws/rs/jsr311-api/1.0/jsr311-api-1.0.jar"/>
+ <classpathentry kind="var" path="M2_REPO/javax/transaction/jta/1.1/jta-1.1.jar"/>
+ <classpathentry kind="var" path="M2_REPO/junit/junit/4.5/junit-4.5.jar"/>
+ <classpathentry kind="var" path="M2_REPO/com/jteigen/scalatest/junit4runner/1.0-SNAPSHOT/junit4runner-1.0-SNAPSHOT.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/neo4j/neo/1.0-b8/neo-1.0-b8.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/scala-lang/scala-library/2.7.4/scala-library-2.7.4.jar"/>
+ <classpathentry kind="var" path="M2_REPO/org/scala-tools/testing/scalatest/0.9.5/scalatest-0.9.5.jar"/>
+ <classpathentry kind="var" path="M2_REPO/javax/servlet/servlet-api/2.4/servlet-api-2.4.jar"/>
+ <classpathentry kind="con" path="ch.epfl.lamp.sdt.launching.SCALA_CONTAINER"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="output" path="target/classes"/>
+</classpath>
3 .gitignore
@@ -0,0 +1,3 @@
+generated
+target
+.manager
18 .project
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>neo4j-scala-template</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>ch.epfl.lamp.sdt.core.scalabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>ch.epfl.lamp.sdt.core.scalanature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
202 pom.xml
@@ -0,0 +1,202 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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">
+ <modelVersion>4.0.0</modelVersion>
+ <packaging>war</packaging>
+
+ <!--
+ * This is a template for new Scala projects which use the Neo4j graph database
+ * and expose a domain-specific REST API. Please edit the following few lines to
+ * match your project details.
+ -->
+ <groupId>com.example</groupId>
+ <artifactId>neo4j-scala-template</artifactId>
+ <name>neo4j-scala-template</name>
+ <version>1.0-SNAPSHOT</version>
+
+ <dependencies>
+ <!-- Scala standard library -->
+ <dependency>
+ <groupId>org.scala-lang</groupId>
+ <artifactId>scala-library</artifactId>
+ <version>2.7.4</version>
+ </dependency>
+
+ <!-- Testing frameworks -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.5</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.scala-tools.testing</groupId>
+ <artifactId>scalatest</artifactId>
+ <version>0.9.5</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- Install manually: see http://github.com/teigen/scalatest-junit4runner/tree/master -->
+ <dependency>
+ <groupId>com.jteigen.scalatest</groupId>
+ <artifactId>junit4runner</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- Jersey and Servlet (for the REST API) -->
+ <dependency>
+ <groupId>com.sun.jersey</groupId>
+ <artifactId>jersey-server</artifactId>
+ <version>1.0.3</version>
+ </dependency>
+
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.4</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>com.sun.jersey.test.framework</groupId>
+ <artifactId>jersey-test-framework</artifactId>
+ <version>1.0.3</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- Neo4j graph database -->
+ <dependency>
+ <groupId>org.neo4j</groupId>
+ <artifactId>neo</artifactId>
+ <version>1.0-b8</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <!-- Compile to Java 1.6 -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ <executions>
+ <execution>
+ <phase>compile</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <!-- Build Scala sources -->
+ <plugin>
+ <groupId>org.scala-tools</groupId>
+ <artifactId>maven-scala-plugin</artifactId>
+ <version>2.10</version>
+ <executions>
+ <execution>
+ <id>scala-compile-first</id>
+ <phase>process-resources</phase>
+ <goals>
+ <goal>add-source</goal>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ <execution>
+ <id>scala-test-compile</id>
+ <phase>process-test-resources</phase>
+ <goals>
+ <goal>testCompile</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <!-- Run the application using "mvn glassfish:run" -->
+ <plugin>
+ <groupId>org.glassfish</groupId>
+ <artifactId>maven-glassfish-plugin</artifactId>
+ </plugin>
+
+ <!-- Configure which tests should be run as part of a build -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.4.3</version>
+ <configuration>
+ <includes>
+ <include>**/Test*.class</include>
+ <include>**/*Test.class</include>
+ <include>**/*TestCase.class</include>
+ <include>**/*Spec.class</include>
+ </includes>
+ </configuration>
+ </plugin>
+ </plugins>
+
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.scala-tools</groupId>
+ <artifactId>maven-scala-plugin</artifactId>
+ <version>2.10</version>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.0.2</version>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+
+
+ <!-- List of repositories where the various dependencies and plugins can be found -->
+ <repositories>
+ <repository>
+ <id>scala-tools.org</id>
+ <name>Scala-tools Maven2 Repository</name>
+ <url>http://scala-tools.org/repo-releases</url>
+ </repository>
+
+ <repository>
+ <id>maven2-repository.dev.java.net</id>
+ <name>Java.net Repository for Maven</name>
+ <url>http://download.java.net/maven/2/</url>
+ <layout>default</layout>
+ </repository>
+
+ <repository>
+ <id>glassfish-repository</id>
+ <name>Java.net Repository for Glassfish</name>
+ <url>http://download.java.net/maven/glassfish</url>
+ </repository>
+
+ <repository>
+ <id>neo4j-public-repository</id>
+ <url>http://m2.neo4j.org</url>
+ </repository>
+ </repositories>
+
+ <pluginRepositories>
+ <pluginRepository>
+ <id>scala-tools.org</id>
+ <name>Scala-tools Maven2 Repository</name>
+ <url>http://scala-tools.org/repo-releases</url>
+ </pluginRepository>
+
+ <pluginRepository>
+ <id>maven2-repository.dev.java.net</id>
+ <name>Java.net Repository for Maven</name>
+ <url>http://download.java.net/maven/2/</url>
+ <layout>default</layout>
+ </pluginRepository>
+ </pluginRepositories>
+</project>
8 src/main/java/com/example/model/Predicates.java
@@ -0,0 +1,8 @@
+package com.example.model;
+
+import org.neo4j.api.core.RelationshipType;
+
+
+public enum Predicates implements RelationshipType {
+ KNOWS
+}
13 src/main/scala/com/example/InitServlet.scala
@@ -0,0 +1,13 @@
+package com.example
+
+import java.util.logging.Logger
+import javax.servlet.http.HttpServlet
+
+class InitServlet extends HttpServlet {
+ val log = Logger.getLogger(this.getClass.getName)
+
+ override def init {
+ log.info("Initializing Neo4J server.")
+ // Do initialization
+ }
+}
20 src/main/scala/com/example/NeoServer.scala
@@ -0,0 +1,20 @@
+package com.example
+
+import org.neo4j.api.core.EmbeddedNeo
+
+object NeoServer {
+ val neo = new EmbeddedNeo("/tmp/neo4j")
+
+ def exec[T<:AnyRef](operation: EmbeddedNeo => T): T = {
+ val tx = neo.beginTx()
+ try {
+ val ret = operation(neo)
+ tx.success()
+ return ret
+ } finally {
+ tx.finish();
+ }
+ }
+
+ // somehow catch shutdown and run: neo.shutdown
+}
43 src/main/scala/com/example/restapi/ParamType.scala
@@ -0,0 +1,43 @@
+package com.example.restapi
+
+import javax.ws.rs._
+import javax.ws.rs.core._
+
+/**
+ * Encapsulates the value of a parameter passed to the API (e.g. type of a @FormParam or
+ * @QueryParam parameter). Subclass and implement the 'parse' method, throwing an exception
+ * if invalid, and we'll turn it into a proper HTTP 'Bad Request' response.
+ */
+abstract class ParamType[V](param: String) {
+
+ val value = try {
+ parse(param)
+ } catch {
+ case e => throw new WebApplicationException(
+ Response.status(Response.Status.BAD_REQUEST)
+ .entity("Invalid value for " + paramName + ": " + param + " (" + e.getMessage() + ")").build
+ )
+ }
+
+ def parse(param: String): V
+ def paramName: String
+
+ override def toString = value.toString
+}
+
+
+/**
+ * Provides a method which checks whether a parameter is present, and raises a proper HTTP
+ * 'Bad Request' response if not.
+ */
+trait RequiredParam {
+ def requiredParam(paramName: String, paramValue: Object) {
+ if (paramValue == null) {
+ throw new WebApplicationException(
+ Response.status(Response.Status.BAD_REQUEST)
+ .entity("Required parameter " + paramName + " not given.").build
+ )
+ }
+ }
+}
+
27 src/main/scala/com/example/restapi/Root.scala
@@ -0,0 +1,27 @@
+package com.example.restapi
+
+import java.util.logging.Logger
+import javax.ws.rs._
+
+import com.example.NeoServer
+import com.example.model.Predicates
+
+@Path("/")
+class Root extends RequiredParam {
+
+ val log = Logger.getLogger(this.getClass.getName)
+
+ @GET @Produces(Array("text/html"))
+ def helloWorld: String = {
+ NeoServer.exec(neo => {
+ val firstNode = neo.createNode
+ val secondNode = neo.createNode
+ val relationship = firstNode.createRelationshipTo(secondNode, Predicates.KNOWS)
+ firstNode.setProperty("message", "Hello, ")
+ secondNode.setProperty("message", "world!")
+ relationship.setProperty("message", "brave Neo ")
+ firstNode.getProperty("message").toString + relationship.getProperty("message").toString +
+ secondNode.getProperty("message").toString
+ })
+ }
+}
26 src/main/webapp/WEB-INF/web.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
+ <servlet>
+ <servlet-name>InitServlet</servlet-name>
+ <servlet-class>com.example.InitServlet</servlet-class>
+ <load-on-startup>1</load-on-startup>
+ </servlet>
+ <servlet>
+ <servlet-name>REST API</servlet-name>
+ <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
+ <init-param>
+ <param-name>com.sun.jersey.config.property.packages</param-name>
+ <param-value>com.example.restapi</param-value>
+ </init-param>
+ <load-on-startup>2</load-on-startup>
+ </servlet>
+ <servlet-mapping>
+ <servlet-name>REST API</servlet-name>
+ <url-pattern>/*</url-pattern>
+ </servlet-mapping>
+ <session-config>
+ <session-timeout>30</session-timeout>
+ </session-config>
+</web-app>
17 src/test/scala/com/example/Dummy.scala
@@ -0,0 +1,17 @@
+package com.example
+
+import org.scalatest.Spec
+import org.scalatest.matchers.ShouldMatchers
+import org.junit.runner.RunWith
+import com.jteigen.scalatest.JUnit4Runner
+
+@RunWith(classOf[JUnit4Runner])
+class DummySpec extends Spec with ShouldMatchers {
+
+ describe("Dummy") {
+ it("should pass the test") {
+ 1 should equal(1)
+ }
+ }
+
+}

0 comments on commit 8e5fad8

Please sign in to comment.
Something went wrong with that request. Please try again.