You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2007/12/08 04:42:55 UTC
svn commit: r602311 - in /openejb/trunk/openejb3/examples: ./
injection-of-datasource/ injection-of-datasource/src/
injection-of-datasource/src/main/ injection-of-datasource/src/main/java/
injection-of-datasource/src/main/java/org/ injection-of-datasou...
Author: dblevins
Date: Fri Dec 7 19:42:54 2007
New Revision: 602311
URL: http://svn.apache.org/viewvc?rev=602311&view=rev
Log:
OPENEJB-358: Example: Using JDBC
Added:
openejb/trunk/openejb3/examples/injection-of-datasource/
openejb/trunk/openejb3/examples/injection-of-datasource/build.xml
openejb/trunk/openejb3/examples/injection-of-datasource/pom.xml
openejb/trunk/openejb3/examples/injection-of-datasource/src/
openejb/trunk/openejb3/examples/injection-of-datasource/src/main/
openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/
openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/
openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/
openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/
openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movie.java
openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movies.java
openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/MoviesImpl.java
openejb/trunk/openejb3/examples/injection-of-datasource/src/main/resources/
openejb/trunk/openejb3/examples/injection-of-datasource/src/main/resources/META-INF/
openejb/trunk/openejb3/examples/injection-of-datasource/src/main/resources/META-INF/ejb-jar.xml
openejb/trunk/openejb3/examples/injection-of-datasource/src/test/
openejb/trunk/openejb3/examples/injection-of-datasource/src/test/java/
openejb/trunk/openejb3/examples/injection-of-datasource/src/test/java/org/
openejb/trunk/openejb3/examples/injection-of-datasource/src/test/java/org/superbiz/
openejb/trunk/openejb3/examples/injection-of-datasource/src/test/java/org/superbiz/injection/
openejb/trunk/openejb3/examples/injection-of-datasource/src/test/java/org/superbiz/injection/MoviesTest.java
Modified:
openejb/trunk/openejb3/examples/pom.xml
Added: openejb/trunk/openejb3/examples/injection-of-datasource/build.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/injection-of-datasource/build.xml?rev=602311&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/injection-of-datasource/build.xml (added)
+++ openejb/trunk/openejb3/examples/injection-of-datasource/build.xml Fri Dec 7 19:42:54 2007
@@ -0,0 +1,120 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+-->
+
+<!-- $Rev: 580640 $ $Date: 2007-09-29 13:42:19 -0700 (Sat, 29 Sep 2007) $ -->
+
+<project name="MyProject" default="dist" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant">
+
+ <!-- ===============================================================
+
+ HOW TO RUN
+
+ Download http://www.apache.org/dist/maven/binaries/maven-ant-tasks-2.0.7.jar
+ Then execute ant as follows:
+
+ ant -lib maven-ant-tasks-2.0.7.jar
+
+ NOTE
+
+ You do NOT need maven-ant-tasks-2.0.7.jar to use OpenEJB for embedded EJB
+ testing with Ant. It is simply used in this example to make the build.xml
+ a bit simpler. As long as OpenEJB and it's required libraries are in the
+ <junit> classpath, the tests will run with OpenEJB embedded.
+
+ ================================================================= -->
+
+ <artifact:remoteRepository id="apache.snapshot.repository" url="http://people.apache.org/repo/m2-snapshot-repository/" />
+ <artifact:remoteRepository id="m2.repository" url="http://repo1.maven.org/maven2/" />
+
+ <!-- Build Classpath -->
+ <artifact:dependencies pathId="classpath.main">
+ <dependency groupId="org.apache.geronimo.specs" artifactId="geronimo-ejb_3.0_spec" version="1.0"/>
+ <dependency groupId="org.apache.geronimo.specs" artifactId="geronimo-annotation_1.0_spec" version="1.0"/>
+ </artifact:dependencies>
+
+ <!-- Test Build Classpath -->
+ <artifact:dependencies pathId="classpath.test.build">
+ <dependency groupId="junit" artifactId="junit" version="4.3.1"/>
+ </artifact:dependencies>
+
+ <!-- Test Run Classpath -->
+ <artifact:dependencies pathId="classpath.test.run">
+ <remoteRepository refid="apache.snapshot.repository" />
+ <remoteRepository refid="m2.repository" />
+
+ <dependency groupId="org.apache.openejb" artifactId="openejb-core" version="3.0.0-SNAPSHOT"/>
+ <dependency groupId="junit" artifactId="junit" version="4.3.1"/>
+ </artifact:dependencies>
+
+ <!-- Properties -->
+
+ <property name="src.main.java" location="src/main/java"/>
+ <property name="src.main.resources" location="src/main/resources"/>
+ <property name="src.test.java" location="src/test/java"/>
+ <property name="build.main" location="target/classes"/>
+ <property name="build.test" location="target/test-classes"/>
+ <property name="test.reports" location="target/test-reports"/>
+ <property name="dist" location="target"/>
+
+
+ <target name="init">
+ <mkdir dir="${build.main}"/>
+ <mkdir dir="${build.test}"/>
+ <mkdir dir="${test.reports}"/>
+ </target>
+
+ <target name="compile" depends="init">
+
+ <javac srcdir="${src.main.java}" destdir="${build.main}">
+ <classpath refid="classpath.main" />
+ </javac>
+ <copy todir="${build.main}">
+ <fileset dir="${src.main.resources}"/>
+ </copy>
+
+ <javac srcdir="${src.test.java}" destdir="${build.test}">
+ <classpath location="${build.main}"/>
+ <classpath refid="classpath.main"/>
+ <classpath refid="classpath.test.build"/>
+ </javac>
+ </target>
+
+ <target name="test" depends="compile">
+ <junit fork="yes" printsummary="yes">
+ <classpath location="${build.main}"/>
+ <classpath location="${build.test}"/>
+ <classpath refid="classpath.main"/>
+ <classpath refid="classpath.test.build"/>
+ <classpath refid="classpath.test.run"/>
+
+ <formatter type="plain"/>
+
+ <batchtest fork="yes" todir="${test.reports}">
+ <fileset dir="${src.test.java}">
+ <include name="**/*Test.java"/>
+ </fileset>
+ </batchtest>
+ </junit>
+ </target>
+
+ <target name="dist" depends="test">
+ <jar jarfile="${dist}/myproject-1.0.jar" basedir="${build.main}"/>
+ </target>
+
+</project>
Added: openejb/trunk/openejb3/examples/injection-of-datasource/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/injection-of-datasource/pom.xml?rev=602311&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/injection-of-datasource/pom.xml (added)
+++ openejb/trunk/openejb3/examples/injection-of-datasource/pom.xml Fri Dec 7 19:42:54 2007
@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+-->
+
+<!-- $Rev: 601959 $ $Date: 2007-12-06 18:19:09 -0800 (Thu, 06 Dec 2007) $ -->
+
+<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>
+ <groupId>org.superbiz</groupId>
+ <artifactId>injection-of-datasource</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>OpenEJB :: Examples :: @Resource DataSource Injection</name>
+ <build>
+ <defaultGoal>install</defaultGoal>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <repositories>
+ <repository>
+ <id>apache-m2-snapshot</id>
+ <name>Apache Snapshot Repository</name>
+ <url>http://people.apache.org/repo/m2-snapshot-repository/</url>
+ </repository>
+ </repositories>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-ejb_3.0_spec</artifactId>
+ <version>1.0</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-annotation_1.0_spec</artifactId>
+ <version>1.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.1</version>
+ <scope>test</scope>
+ </dependency>
+
+ <!--
+ The <scope>test</scope> guarantees that non of your runtime
+ code is dependent on any OpenEJB classes.
+ -->
+ <dependency>
+ <groupId>org.apache.openejb</groupId>
+ <artifactId>openejb-core</artifactId>
+ <version>3.0.0-SNAPSHOT</version>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+</project>
Added: openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movie.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movie.java?rev=602311&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movie.java (added)
+++ openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movie.java Fri Dec 7 19:42:54 2007
@@ -0,0 +1,61 @@
+/**
+ * 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.superbiz.injection;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public class Movie {
+ private String director;
+ private String title;
+ private int year;
+
+ public Movie() {
+ }
+
+ public Movie(String director, String title, int year) {
+ this.director = director;
+ this.title = title;
+ this.year = year;
+ }
+
+ public String getDirector() {
+ return director;
+ }
+
+ public void setDirector(String director) {
+ this.director = director;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public int getYear() {
+ return year;
+ }
+
+ public void setYear(int year) {
+ this.year = year;
+ }
+
+
+}
Added: openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movies.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movies.java?rev=602311&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movies.java (added)
+++ openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/Movies.java Fri Dec 7 19:42:54 2007
@@ -0,0 +1,30 @@
+/**
+ * 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.superbiz.injection;
+
+import java.util.List;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public interface Movies {
+ void addMovie(Movie movie) throws Exception ;
+
+ void deleteMovie(Movie movie) throws Exception ;
+
+ List<Movie> getMovies() throws Exception ;
+}
Added: openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/MoviesImpl.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/MoviesImpl.java?rev=602311&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/MoviesImpl.java (added)
+++ openejb/trunk/openejb3/examples/injection-of-datasource/src/main/java/org/superbiz/injection/MoviesImpl.java Fri Dec 7 19:42:54 2007
@@ -0,0 +1,104 @@
+/**
+ * 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.superbiz.injection;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
+import javax.ejb.Stateful;
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.util.List;
+import java.util.ArrayList;
+
+@Stateful(name = "Movies")
+public class MoviesImpl implements Movies {
+
+ /**
+ * The field name "movieDatabase" matches the DataSource we
+ * configure in the TestCase via :
+ * p.put("movieDatabase", "new://Resource?type=DataSource");
+ *
+ * This would also match an equivalent delcaration in an openejb.xml:
+ * <Resource id="movieDatabase" type="DataSource"/>
+ *
+ * If you'd like the freedom to change the field name without
+ * impact on your configuration you can set the "name" attribute
+ * of the @Resource annotation to "movieDatabase" instead.
+ */
+ @Resource
+ private DataSource movieDatabase;
+
+ @PostConstruct
+ private void construct() throws Exception {
+ Connection connection = movieDatabase.getConnection();
+ try {
+ PreparedStatement stmt = connection.prepareStatement("CREATE TABLE movie ( director VARCHAR(255), title VARCHAR(255), year integer)");
+ stmt.execute();
+ } finally {
+ connection.close();
+ }
+ }
+
+ public void addMovie(Movie movie) throws Exception {
+ Connection conn = movieDatabase.getConnection();
+ try {
+ PreparedStatement sql = conn.prepareStatement("INSERT into movie (director, title, year) values (?, ?, ?)");
+ sql.setString(1, movie.getDirector());
+ sql.setString(2, movie.getTitle());
+ sql.setInt(3, movie.getYear());
+ sql.execute();
+ } finally {
+ conn.close();
+ }
+ }
+
+ public void deleteMovie(Movie movie) throws Exception {
+ Connection conn = movieDatabase.getConnection();
+ try {
+ PreparedStatement sql = conn.prepareStatement("DELETE from movie where director = ? AND title = ? AND year = ?");
+ sql.setString(1, movie.getDirector());
+ sql.setString(2, movie.getTitle());
+ sql.setInt(3, movie.getYear());
+ sql.execute();
+ } finally {
+ conn.close();
+ }
+ }
+
+ public List<Movie> getMovies() throws Exception {
+ ArrayList<Movie> movies = new ArrayList<Movie>();
+ Connection conn = movieDatabase.getConnection();
+ try {
+ PreparedStatement sql = conn.prepareStatement("SELECT director, title, year from movie");
+ ResultSet set = sql.executeQuery();
+ while ( set.next() ) {
+ Movie movie = new Movie();
+ movie.setDirector(set.getString("director"));
+ movie.setTitle(set.getString("title"));
+ movie.setYear(set.getInt("year"));
+ movies.add( movie );
+ }
+
+ } finally {
+ conn.close();
+ }
+ return movies;
+ }
+
+}
Added: openejb/trunk/openejb3/examples/injection-of-datasource/src/main/resources/META-INF/ejb-jar.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/injection-of-datasource/src/main/resources/META-INF/ejb-jar.xml?rev=602311&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/injection-of-datasource/src/main/resources/META-INF/ejb-jar.xml (added)
+++ openejb/trunk/openejb3/examples/injection-of-datasource/src/main/resources/META-INF/ejb-jar.xml Fri Dec 7 19:42:54 2007
@@ -0,0 +1 @@
+<ejb-jar/>
Added: openejb/trunk/openejb3/examples/injection-of-datasource/src/test/java/org/superbiz/injection/MoviesTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/injection-of-datasource/src/test/java/org/superbiz/injection/MoviesTest.java?rev=602311&view=auto
==============================================================================
--- openejb/trunk/openejb3/examples/injection-of-datasource/src/test/java/org/superbiz/injection/MoviesTest.java (added)
+++ openejb/trunk/openejb3/examples/injection-of-datasource/src/test/java/org/superbiz/injection/MoviesTest.java Fri Dec 7 19:42:54 2007
@@ -0,0 +1,55 @@
+/**
+ * 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.superbiz.injection;
+
+import junit.framework.TestCase;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.util.Properties;
+import java.util.List;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public class MoviesTest extends TestCase {
+
+ public void test() throws Exception {
+ Properties p = new Properties();
+ p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
+ p.put("movieDatabase", "new://Resource?type=DataSource");
+ p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
+ p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+
+ Context context = new InitialContext(p);
+
+ Movies movies = (Movies) context.lookup("MoviesLocal");
+
+ movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
+ movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
+ movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));
+
+ List<Movie> list = movies.getMovies();
+ assertEquals("List.size()", 3, list.size());
+
+ for (Movie movie : list) {
+ movies.deleteMovie(movie);
+ }
+
+ assertEquals("Movies.getMovies()", 0, movies.getMovies().size());
+ }
+}
Modified: openejb/trunk/openejb3/examples/pom.xml
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/pom.xml?rev=602311&r1=602310&r2=602311&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/pom.xml (original)
+++ openejb/trunk/openejb3/examples/pom.xml Fri Dec 7 19:42:54 2007
@@ -36,6 +36,7 @@
<module>component-interfaces</module>
<module>injection-of-ejbs</module>
<module>injection-of-env-entry</module>
+ <module>injection-of-datasource</module>
<module>interceptors</module>
<module>expanded-env-entries</module>
<module>helloworld-weblogic</module>