Permalink
Browse files

Test and commands for jpa

  • Loading branch information...
1 parent 8aa27b7 commit 239e21573d0a7aaa1392f9d6c9fc13a4b3e2c08f Christian Schneider committed Jan 13, 2012
@@ -42,7 +42,11 @@ public void setDbAccess(DbAccess dbAccess) {
public ServiceReference[] getDataSources() {
try {
- return context.getServiceReferences(DataSource.class.getName(), null);
+ ServiceReference[] dsRefs = context.getServiceReferences(DataSource.class.getName(), null);
+ if (dsRefs == null) {
+ dsRefs = new ServiceReference[]{};
+ }
+ return dsRefs;
} catch (InvalidSyntaxException e) {
throw new RuntimeException(e);
}
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
First install the MySQL driver using:
+> install -s mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.stax-api-1.0/1.9.0
> install -s mvn:mysql/mysql-connector-java/5.1.18
Then copy this file to the deploy folder
View
@@ -1,6 +1,6 @@
----------------------------------------------------------------
-Thu Jan 12 15:52:01 CET 2012:
-Booting Derby version The Apache Software Foundation - Apache Derby - 10.8.2.2 - (1181258): instance a816c00e-0134-d267-6e5a-000006faaef8
+Fri Jan 13 16:55:45 CET 2012:
+Booting Derby version The Apache Software Foundation - Apache Derby - 10.8.2.2 - (1181258): instance a816c00e-0134-d7c8-265c-000006d75cf8
on database directory C:\checkout\Karaf-Tutorial\db\examplejdbc\target\test with class loader sun.misc.Launcher$AppClassLoader@6d6f0472
Loaded from file:/C:/Users/cschneider/.m2/repository/org/apache/derby/derby/10.8.2.2/derby-10.8.2.2.jar
java.vendor=Sun Microsystems Inc.
@@ -60,6 +60,9 @@ public void test() throws Exception {
if (stmt != null) {
stmt.close();
}
+ if (con != null) {
+ con.close();
+ }
}
}
View
@@ -3,6 +3,11 @@ dbexample
Example how to access a DataSource using OSGi services and jdbc.
+Build
+-----
+
+mvn clean install
+
Installation
------------
@@ -11,24 +16,37 @@ First see datasources project REDAME and install the derby db and datasource int
install -s mvn:org.apache.geronimo.specs/geronimo-annotation_1.0_spec/1.1.1
install -s mvn:commons-lang/commons-lang/2.6
install -s mvn:commons-pool/commons-pool/1.5.4
-install -s mvn:commons-dbcp/commons-dbcp/1.4
install -s mvn:commons-collections/commons-collections/3.2.1
install -s mvn:org.apache.geronimo.specs/geronimo-jpa_2.0_spec/1.1
install -s wrap:mvn:net.sourceforge.serp/serp/1.13.1
-install -s mvn:org.apache.openjpa/openjpa/2.1.1
-install -s mvn:org.apache.aries.transaction/org.apache.aries.transaction.blueprint/0.3
install -s mvn:org.apache.aries.transaction/org.apache.aries.transaction.manager/0.3
+install -s mvn:org.apache.aries.transaction/org.apache.aries.transaction.blueprint/0.3
+install -s mvn:commons-dbcp/commons-dbcp/1.4
+install -s mvn:org.apache.openjpa/openjpa/2.1.1
+install -s mvn:org.apache.aries/org.apache.aries.util/0.4
install -s mvn:org.apache.aries.jpa/org.apache.aries.jpa.api/0.3
install -s mvn:org.apache.aries.proxy/org.apache.aries.proxy/0.3
install -s mvn:org.apache.aries.jpa/org.apache.aries.jpa.container/0.3
+install -s mvn:org.apache.aries.jpa/org.apache.aries.jpa.container.context/0.3
install -s mvn:org.apache.aries.jpa/org.apache.aries.jpa.blueprint.aries/0.3
+install -s mvn:org.apache.aries.proxy/org.apache.aries.proxy/0.4
+install -s mvn:org.apache.aries.jndi/org.apache.aries.jndi.api/0.3
+install -s mvn:org.apache.aries.jndi/org.apache.aries.jndi.core/0.3.1
+install -s mvn:org.apache.aries.jndi/org.apache.aries.jndi.url/0.3.1
install -s mvn:net.lr.tutorial.karaf.db/db-examplejpa/1.0-SNAPSHOT
Test
----
-The example creates and populates a table on startup, queries it and outputs the result
+The bundle installs two Karaf shell commands:
+
+person:add 'Christian Schneider' @schneider_chris
+-> Adds a person ( this currently does not work)
+
+Instead we use the db commands to populate the DB by manually:
+karaf@root> db:select jdbc/derbyds
+karaf@root> db:exec "insert into person (id, name, twittername) values (1, 'Christian Schneider', '@schneider_chris')"
-The output should look like this:
-Using datasource H2 ver jdbc:h2:~/test
-Christian Schneider, @schneider_chris,
+karaf@root> person:list
+Christian Schneider, @schneider_chris
+-> Lists all persisted Persons
View
@@ -0,0 +1,10 @@
+----------------------------------------------------------------
+Fri Jan 13 16:55:52 CET 2012:
+Booting Derby version The Apache Software Foundation - Apache Derby - 10.8.2.2 - (1181258): instance a816c00e-0134-d7c8-3ee5-000007529e80
+on database directory C:\checkout\Karaf-Tutorial\db\examplejpa\target\test with class loader sun.misc.Launcher$AppClassLoader@6d6f0472
+Loaded from file:/C:/Users/cschneider/.m2/repository/org/apache/derby/derby/10.8.2.2/derby-10.8.2.2.jar
+java.vendor=Sun Microsystems Inc.
+java.runtime.version=1.6.0_25-b06
+user.dir=C:\checkout\Karaf-Tutorial\db\examplejpa
+derby.system.home=null
+Database Class Loader started - derby.database.classpath=''
View
@@ -27,13 +27,18 @@
<artifactId>geronimo-jpa_2.0_spec</artifactId>
<version>1.1</version>
</dependency>
-
+
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
+ <groupId>org.apache.karaf.shell</groupId>
+ <artifactId>org.apache.karaf.shell.console</artifactId>
+ <version>2.2.5</version>
+ </dependency>
+ <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-version}</version>
@@ -84,34 +89,34 @@
</configuration>
</plugin>
<plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>openjpa-maven-plugin</artifactId>
- <configuration>
- <addDefaultConstructor>true</addDefaultConstructor>
- <enforcePropertyRestriction>true</enforcePropertyRestriction>
- </configuration>
- <executions>
- <execution>
- <id>enhancer</id>
- <phase>process-classes</phase>
- <goals>
- <goal>enhance</goal>
- </goals>
- </execution>
- </executions>
- <dependencies>
- <dependency>
- <groupId>org.apache.openjpa</groupId>
- <artifactId>openjpa</artifactId>
- <version>2.1.1</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>${slf4j-version}</version>
- </dependency>
- </dependencies>
- </plugin>
+ <groupId>org.codehaus.mojo</groupId>
+ <artifactId>openjpa-maven-plugin</artifactId>
+ <configuration>
+ <addDefaultConstructor>true</addDefaultConstructor>
+ <enforcePropertyRestriction>true</enforcePropertyRestriction>
+ </configuration>
+ <executions>
+ <execution>
+ <id>enhancer</id>
+ <phase>process-classes</phase>
+ <goals>
+ <goal>enhance</goal>
+ </goals>
+ </execution>
+ </executions>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.openjpa</groupId>
+ <artifactId>openjpa</artifactId>
+ <version>2.1.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>${slf4j-version}</version>
+ </dependency>
+ </dependencies>
+ </plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
@@ -3,6 +3,7 @@
import java.util.List;
public interface PersonService {
- void addPersion(Person person);
- List<Person> getAllPersons();
+ void add(Person person);
+ void deleteAll();
+ List<Person> getAll();
}
@@ -0,0 +1,46 @@
+/*
+ * 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 net.lr.tutorial.karaf.db.examplejpa.command;
+
+import net.lr.tutorial.karaf.db.examplejpa.Person;
+import net.lr.tutorial.karaf.db.examplejpa.PersonService;
+
+import org.apache.felix.gogo.commands.Action;
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.service.command.CommandSession;
+
+@Command(scope = "person", name = "add", description = "Adds a person")
+public class AddPersonCommand implements Action {
+ @Argument(index=0, name="Name", required=true, description="Name", multiValued=false)
+ String name;
+ @Argument(index=1, name="Twitter Name", required=true, description="Twitter Name", multiValued=false)
+ String twitterName;
+ private PersonService personService;
+
+ public void setPersonService(PersonService personService) {
+ this.personService = personService;
+ }
+
+ @Override
+ public Object execute(CommandSession session) throws Exception {
+ Person person = new Person(name, twitterName);
+ personService.add(person);
+ return null;
+ }
+
+}
@@ -0,0 +1,45 @@
+/*
+ * 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 net.lr.tutorial.karaf.db.examplejpa.command;
+
+import java.util.List;
+
+import net.lr.tutorial.karaf.db.examplejpa.Person;
+import net.lr.tutorial.karaf.db.examplejpa.PersonService;
+
+import org.apache.felix.gogo.commands.Action;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.felix.service.command.CommandSession;
+
+@Command(scope = "person", name = "list", description = "Lists all persons")
+public class ListPersonsCommand implements Action {
+ private PersonService personService;
+
+ public void setPersonService(PersonService personService) {
+ this.personService = personService;
+ }
+
+ @Override
+ public Object execute(CommandSession session) throws Exception {
+ List<Person> persons = personService.getAll();
+ for (Person person : persons) {
+ System.out.println(person.getName() + ", " + person.getTwitterName());
+ }
+ return null;
+ }
+
+}
@@ -3,7 +3,6 @@
import java.util.List;
import javax.persistence.EntityManager;
-import javax.persistence.criteria.CriteriaQuery;
import net.lr.tutorial.karaf.db.examplejpa.Person;
import net.lr.tutorial.karaf.db.examplejpa.PersonService;
@@ -16,14 +15,17 @@ public void setEntityManager (EntityManager em) {
}
@Override
- public void addPersion(Person person) {
+ public void add(Person person) {
em.persist(person);
}
+
+ public void deleteAll() {
+ em.createQuery("delete from Person").executeUpdate();
+ }
@Override
- public List<Person> getAllPersons() {
- CriteriaQuery<Person> query = em.getCriteriaBuilder().createQuery(Person.class);
- return em.createQuery(query).getResultList();
+ public List<Person> getAll() {
+ return em.createQuery("select p from Person p", Person.class).getResultList();
}
}
@@ -27,9 +27,9 @@
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
- <property name="openjpa.BrokerImpl" value="non-finalizing" />
- <property name="openjpa.Sequence" value="table(Table=OPENJPASEQ, Increment=100)"/>
- <property name="openjpa.jdbc.UpdateManager" value="org.apache.openjpa.jdbc.kernel.BatchingConstraintUpdateManager"/>
+<!-- <property name="openjpa.BrokerImpl" value="non-finalizing" /> -->
+<!-- <property name="openjpa.Sequence" value="table(Table=OPENJPASEQ, Increment=100)"/> -->
+<!-- <property name="openjpa.jdbc.UpdateManager" value="org.apache.openjpa.jdbc.kernel.BatchingConstraintUpdateManager"/> -->
<!-- These properties are creating the database on the fly. We are using them to avoid users having
to create a database to run the sample. This is not something that should be used in production.
See also the create=true line in the ariestrader-derby-ds blueprint meta data -->
@@ -17,7 +17,7 @@
limitations under the License.
-->
-<blueprint default-activation="lazy"
+<blueprint default-activation="eager"
xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0"
@@ -29,5 +29,17 @@
</bean>
<service ref="personService" interface="net.lr.tutorial.karaf.db.examplejpa.PersonService" />
-
+
+ <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
+ <command name="person/add">
+ <action class="net.lr.tutorial.karaf.db.examplejpa.command.AddPersonCommand">
+ <property name="personService" ref="personService"/>
+ </action>
+ </command>
+ <command name="person/list">
+ <action class="net.lr.tutorial.karaf.db.examplejpa.command.ListPersonsCommand">
+ <property name="personService" ref="personService"/>
+ </action>
+ </command>
+ </command-bundle>
</blueprint>
Oops, something went wrong.

0 comments on commit 239e215

Please sign in to comment.