forked from dpishchukhin/org.knowhowlab.tips.jpa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71f0b60
commit 7d31fa1
Showing
24 changed files
with
1,844 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.knowhowlab.tips.jpa</groupId> | ||
<artifactId>client</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<packaging>bundle</packaging> | ||
<name>KnowHowLab OSGi JPA tips - client</name> | ||
<description>KnowHowLab OSGi JPA tips - client</description> | ||
<url>http://knowhowlab.org</url> | ||
|
||
<developers> | ||
<developer> | ||
<name>Dmytro Pishchukhin</name> | ||
<timezone>GMT+1</timezone> | ||
</developer> | ||
</developers> | ||
|
||
<organization> | ||
<name>Know-How Lab</name> | ||
<url>http://knowhowlab.org</url> | ||
</organization> | ||
|
||
<licenses> | ||
<license> | ||
<name>Apache License 2.0</name> | ||
<url>http://www.apache.org/licenses/LICENSE-2.0</url> | ||
</license> | ||
</licenses> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.osgi</groupId> | ||
<artifactId>org.osgi.core</artifactId> | ||
<version>4.2.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.osgi</groupId> | ||
<artifactId>org.osgi.enterprise</artifactId> | ||
<version>4.2.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.geronimo.specs</groupId> | ||
<artifactId>geronimo-jpa_2.0_spec</artifactId> | ||
<version>1.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.knowhowlab.tips.jpa</groupId> | ||
<artifactId>model</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.5</source> | ||
<target>1.5</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.felix</groupId> | ||
<artifactId>maven-bundle-plugin</artifactId> | ||
<version>2.3.4</version> | ||
<extensions>true</extensions> | ||
<configuration> | ||
<instructions> | ||
<Export-Package/> | ||
<Import-Package>org.osgi*,org.knowhowlab.tips.jpa.model,javax.persistence;version="1.1.0"</Import-Package> | ||
<Bundle-Activator>org.knowhowlab.tips.jpa.client.Activator</Bundle-Activator> | ||
<Bundle-License>http://code.google.com/p/osgilab/wiki/License</Bundle-License> | ||
<Bundle-RequiredExecutionEnvironment>JavaSE-1.6</Bundle-RequiredExecutionEnvironment> | ||
<_removeheaders>Bnd-LastModified, Built-By, Private-Package, Tool, | ||
Build-Jdk, Include-Resource, Embed-Dependency | ||
</_removeheaders> | ||
</instructions> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
33 changes: 33 additions & 0 deletions
33
client/src/main/java/org/knowhowlab/tips/jpa/client/Activator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.knowhowlab.tips.jpa.client; | ||
|
||
import org.osgi.framework.BundleActivator; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.ServiceRegistration; | ||
|
||
import java.util.Dictionary; | ||
import java.util.Hashtable; | ||
|
||
/** | ||
* @author dpishchukhin | ||
*/ | ||
public class Activator implements BundleActivator { | ||
private ServiceRegistration serviceRegistration; | ||
|
||
public void start(BundleContext context) throws Exception { | ||
Dictionary<String, Object> props = new Hashtable<String, Object>(); | ||
props.put("org.knowhowlab.osgi.shell.group.id", "jpa"); | ||
props.put("org.knowhowlab.osgi.shell.group.name", "JPA tips commands"); | ||
props.put("org.knowhowlab.osgi.shell.commands", new String[][]{ | ||
{"lsstuds", "lsstuds - list students"}, | ||
{"lsgrps", "lsgrps - list groups"}, | ||
{"delgrp", "delgrp <id> - delete group"}, | ||
{"delstud", "delstud <id> - delete student"}, | ||
{"addgrp", "addgrp <name> - add group"}, | ||
{"addstud", "addstud <first_name> <last_name> <group_id> - add student"}}); | ||
serviceRegistration = context.registerService(CommandLineService.class.getName(), new CommandLineService(context), props); | ||
} | ||
|
||
public void stop(BundleContext context) throws Exception { | ||
serviceRegistration.unregister(); | ||
} | ||
} |
236 changes: 236 additions & 0 deletions
236
client/src/main/java/org/knowhowlab/tips/jpa/client/CommandLineService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
package org.knowhowlab.tips.jpa.client; | ||
|
||
import org.knowhowlab.tips.jpa.model.Group; | ||
import org.knowhowlab.tips.jpa.model.Student; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.ServiceReference; | ||
import org.osgi.service.jpa.EntityManagerFactoryBuilder; | ||
|
||
import javax.persistence.*; | ||
import java.io.PrintWriter; | ||
import java.util.List; | ||
|
||
/** | ||
* @author dpishchukhin | ||
*/ | ||
public class CommandLineService { | ||
private BundleContext bc; | ||
private static final String STUDENTS_UNIT = "jpa.students"; | ||
|
||
public CommandLineService(BundleContext bc) { | ||
this.bc = bc; | ||
} | ||
|
||
public void lsstuds(PrintWriter out, String... args) { | ||
try { | ||
ServiceReference reference = getEntityManagerFactoryServiceReference(); | ||
try { | ||
EntityManagerFactory emf = (EntityManagerFactory) bc.getService(reference); | ||
EntityManager em = emf.createEntityManager(); | ||
Query query = em.createNamedQuery(Student.GET_STUDENTS); | ||
List<Student> result = query.getResultList(); | ||
if (result != null) { | ||
out.println(String.format("Students: %d", result.size())); | ||
for (Student student : result) { | ||
out.println(String.format("%d %s %s %d", student.getId(), student.getFirstName(), student.getLastName(), student.getGroup().getId())); | ||
} | ||
} else { | ||
out.println("Result is null"); | ||
} | ||
em.close(); | ||
} finally { | ||
bc.ungetService(reference); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(out); | ||
} | ||
} | ||
|
||
public void lsgrps(PrintWriter out, String... args) { | ||
try { | ||
ServiceReference reference = getEntityManagerFactoryServiceReference(); | ||
try { | ||
EntityManagerFactory emf = (EntityManagerFactory) bc.getService(reference); | ||
EntityManager em = emf.createEntityManager(); | ||
Query query = em.createNamedQuery(Group.GET_GROUPS); | ||
List<Group> result = query.getResultList(); | ||
if (result != null) { | ||
out.println(String.format("Groups: %d", result.size())); | ||
for (Group group : result) { | ||
out.println(String.format("%d %s", group.getId(), group.getName())); | ||
} | ||
} else { | ||
out.println("Result is null"); | ||
} | ||
em.close(); | ||
} finally { | ||
bc.ungetService(reference); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(out); | ||
} | ||
} | ||
|
||
public void addgrp(PrintWriter out, String... args) { | ||
if (args == null || args.length != 1) { | ||
out.println("Group name param is missed"); | ||
return; | ||
} | ||
try { | ||
ServiceReference reference = getEntityManagerFactoryServiceReference(); | ||
try { | ||
EntityManagerFactory emf = (EntityManagerFactory) bc.getService(reference); | ||
EntityManager em = emf.createEntityManager(); | ||
EntityTransaction transaction = em.getTransaction(); | ||
try { | ||
transaction.begin(); | ||
Group group = new Group(args[0]); | ||
em.persist(group); | ||
transaction.commit(); | ||
out.println(String.format("Group is persisted with ID: %d", group.getId())); | ||
} catch (Exception e) { | ||
transaction.rollback(); | ||
e.printStackTrace(out); | ||
} | ||
em.close(); | ||
} finally { | ||
bc.ungetService(reference); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(out); | ||
} | ||
} | ||
|
||
public void delgrp(PrintWriter out, String... args) { | ||
if (args == null || args.length != 1) { | ||
out.println("Group ID param is missed"); | ||
return; | ||
} | ||
try { | ||
ServiceReference reference = getEntityManagerFactoryServiceReference(); | ||
try { | ||
EntityManagerFactory emf = (EntityManagerFactory) bc.getService(reference); | ||
EntityManager em = emf.createEntityManager(); | ||
EntityTransaction transaction = em.getTransaction(); | ||
try { | ||
transaction.begin(); | ||
int groupId = Integer.valueOf(args[0]); | ||
Group group = getGroup(em, groupId); | ||
if (group == null) { | ||
throw new Exception(String.format("Unknown Group ID: %d", groupId)); | ||
} | ||
em.remove(group); | ||
transaction.commit(); | ||
out.println(String.format("Group with ID: %d is removed", groupId)); | ||
} catch (Exception e) { | ||
transaction.rollback(); | ||
e.printStackTrace(out); | ||
} | ||
em.close(); | ||
} finally { | ||
bc.ungetService(reference); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(out); | ||
} | ||
} | ||
|
||
public void delstud(PrintWriter out, String... args) { | ||
if (args == null || args.length != 1) { | ||
out.println("Student ID param is missed"); | ||
return; | ||
} | ||
try { | ||
ServiceReference reference = getEntityManagerFactoryServiceReference(); | ||
try { | ||
EntityManagerFactory emf = (EntityManagerFactory) bc.getService(reference); | ||
EntityManager em = emf.createEntityManager(); | ||
EntityTransaction transaction = em.getTransaction(); | ||
try { | ||
transaction.begin(); | ||
int studentId = Integer.valueOf(args[0]); | ||
Student student = getStudent(em, studentId); | ||
if (student == null) { | ||
throw new Exception(String.format("Unknown Student ID: %d", studentId)); | ||
} | ||
em.remove(student); | ||
transaction.commit(); | ||
out.println(String.format("Student with ID: %d is removed", studentId)); | ||
} catch (Exception e) { | ||
transaction.rollback(); | ||
e.printStackTrace(out); | ||
} | ||
em.close(); | ||
} finally { | ||
bc.ungetService(reference); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(out); | ||
} | ||
} | ||
|
||
public void addstud(PrintWriter out, String... args) { | ||
if (args == null || args.length != 3) { | ||
out.println("Wrong params"); | ||
return; | ||
} | ||
try { | ||
ServiceReference reference = getEntityManagerFactoryServiceReference(); | ||
try { | ||
EntityManagerFactory emf = (EntityManagerFactory) bc.getService(reference); | ||
EntityManager em = emf.createEntityManager(); | ||
EntityTransaction transaction = em.getTransaction(); | ||
try { | ||
transaction.begin(); | ||
int groupId = Integer.valueOf(args[2]); | ||
Group group = getGroup(em, groupId); | ||
if (group == null) { | ||
throw new Exception(String.format("Unknown Group ID: %d", groupId)); | ||
} | ||
Student student = new Student(args[0], args[1], group); | ||
em.persist(student); | ||
transaction.commit(); | ||
out.println(String.format("Student is persisted with ID: %d", student.getId())); | ||
} catch (Exception e) { | ||
transaction.rollback(); | ||
e.printStackTrace(out); | ||
} | ||
em.close(); | ||
} finally { | ||
bc.ungetService(reference); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(out); | ||
} | ||
} | ||
|
||
private Group getGroup(EntityManager em, int groupId) { | ||
Query query = em.createNamedQuery(Group.GET_GROUP_BY_ID); | ||
query.setParameter("groupId", groupId); | ||
try { | ||
return (Group) query.getSingleResult(); | ||
} catch (NoResultException e) { | ||
return null; | ||
} | ||
} | ||
|
||
private Student getStudent(EntityManager em, int studentId) { | ||
Query query = em.createNamedQuery(Student.GET_STUDENT_BY_ID); | ||
query.setParameter("studentId", studentId); | ||
try { | ||
return (Student) query.getSingleResult(); | ||
} catch (NoResultException e) { | ||
return null; | ||
} | ||
} | ||
|
||
private ServiceReference getEntityManagerFactoryServiceReference() throws Exception { | ||
ServiceReference[] serviceReferences = bc.getServiceReferences(EntityManagerFactory.class.getName(), | ||
String.format("(%s=%s)", EntityManagerFactoryBuilder.JPA_UNIT_NAME, STUDENTS_UNIT)); | ||
if (serviceReferences != null && serviceReferences.length > 0) { | ||
return serviceReferences[0]; | ||
} else { | ||
throw new Exception("EntityManagerFactory is not available"); | ||
} | ||
} | ||
} |
Oops, something went wrong.