I have been working on demo example where JPA
would be used with OSGi
.
The thing is, I am able to start/stop the service after bundling however I am not able to get the serviceReference
. Due to this my JPA
implementation is not getting executed.
Following is the code:
Activator.java
package manning.osgi.jpa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
public class Activator implements BundleActivator {
public void start(BundleContext context) throws Exception {
LoginEvent loginEvent = new LoginEvent();
// Set login event...
loginEvent.setUserid("alex");
try {
ServiceReference [] serviceReferences =
context.getServiceReferences(
EntityManagerFactory.class.toString(),
"(osgi.unit.name=LoginEvent)");
System.out.println("Service References Created");
if (serviceReferences != null) {
EntityManagerFactory emf =
(EntityManagerFactory) context.getService(serviceReferences[0]);
EntityManager em = emf.createEntityManager();
persistLoginEvent(em, loginEvent);
System.out.println("Transaction started");
loginEvent = retrieveLoginEvent(em, loginEvent.getId());
System.out.println("Transaction completed");
em.close();
emf.close();
}
} catch (Exception e) {
System.out.println("Exception"+e.getMessage());
}
}
private void persistLoginEvent(EntityManager em, LoginEvent loginEvent) {
em.getTransaction().begin();
em.persist(loginEvent);
em.getTransaction().commit();
}
private LoginEvent retrieveLoginEvent(EntityManager em, int id) {
em.getTransaction().begin();
LoginEvent loginEvent = em.find(LoginEvent.class, id);
loginEvent.getUserid();
loginEvent.getId();
loginEvent.getTimestamp();
em.getTransaction().commit();
return loginEvent;
}
public void stop(BundleContext context) throws Exception {
}
}
LoginEvent.java
package manning.osgi.jpa;
import javax.persistence.*;
@Entity
public class LoginEvent {
@Id
@GeneratedValue
private int id;
private String userid;
private long timestamp;
public int getId() {
System.out.println("\nID: "+this.id);
return this.id;
}
public String getUserid() {
System.out.println("\nUser ID: "+this.userid);
return this.userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public long getTimestamp() {
System.out.println("\nTime Stamp: "+this.timestamp);
return this.timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}
persistence.xml
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="LoginEvent">
<class>manning.osgi.jpa.LoginEvent</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:derbyDB;create=true"/>
</properties>
</persistence-unit>
</persistence>
MANIFEST.MF
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: OsgiDemo
Bundle-SymbolicName: manning.osgi.jpa
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: manning.osgi.jpa.Activator
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: javax.persistence,
org.osgi.framework;version="1.3.0"
Bundle-ActivationPolicy: lazy
Require-Bundle: javax.persistence;bundle-version="2.1.0"
The code is compiling but as the serviceReference
is null I can't progress. I rechecked the persistence-unit
name and its correct.
Can anyone plz help me out what I am missing here. Thank you.