1

我创建了两个 rcp-bundle。第一个包实现 JPA - 服务,第二个 - 客户端。使用下一个技术:

OSGi、JPA Eclipselink、RCP Eclipse、RAP Eclipse、Eclipse Equinox

服务包:

public class JpaUtil {

    private static EntityManagerFactory emf;
    private static Map<String, Object> properties = new HashMap<String, Object>();

public EntityManagerFactory getEntityManagerFactory() {

        try {
            properties.put(PersistenceUnitProperties.JDBC_DRIVER, "org.postgresql.Driver");
            properties.put(PersistenceUnitProperties.JDBC_URL, "jdbc:postgresql://localhost:5432/db");
            properties.put(PersistenceUnitProperties.JDBC_USER, "db");
            properties.put(PersistenceUnitProperties.JDBC_PASSWORD, "db");
            properties.put(PersistenceUnitProperties.CLASSLOADER, this.getClass().getClassLoader());

            emf = new PersistenceProvider().createEntityManagerFactory("service", properties);
        } catch (Throwable e) {
            System.err.println("Error create initializer SessionFactory " + e);
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }

        return emf;
    }

}

DAO 实现:

public class HotelJPAImpl implements HotelJPA {     

    @Override
    public boolean checkStatusRoom(Integer numbRoom) {

    EntityManager em = new JpaUtil().getEntityManagerFactory().createEntityManager();               

        boolean status;
        try{        
            Long l = Long.valueOf(numbRoom.longValue());
            Hotel hotel = (Hotel) em.find(Hotel.class, numbRoom);
            status = hotel.getIsStatus();           
        }finally{
            em.close();         
        }           
        return status;
    }

}

服务激活器:

public class ServiceActivator implements BundleActivator {
    ServiceRegistration<HotelJPA> ServiceRegistration;

    @Override
    public void start(BundleContext context) throws Exception {


        HotelJPA hotelJPA = new HotelJPAImpl();
        ServiceRegistration = (ServiceRegistration<HotelJPA>) context
                .registerService(HotelJPA.class.getName(), hotelJPA,
                        null);
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        ServiceRegistration.unregister();
    }
}

持久性.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="service">

        <class>service.table.Hotel</class>
        <properties>        
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/db" />
            <property name="javax.persistence.jdbc.user" value="db" />
            <property name="javax.persistence.jdbc.password" value="db" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
        </properties>
    </persistence-unit>
</persistence>

客户端捆绑包:

活化剂:

public class Activator implements BundleActivator {

    ServiceReference<HotelJPA> serviceReference;    

    @Override
    public void start(BundleContext context) throws Exception {

        serviceReference = (ServiceReference<HotelJPA>)context.getServiceReference(HotelJPA.class.getName());       
        HotelJPA hotelJPA = (HotelJPA) context.getService(serviceReference);

        System.out.println(hotelJPA.checkStatusRoom(1));

    }

    @Override
    public void stop(BundleContext context) throws Exception {

        context.ungetService(serviceReference);

    }

}

当我开始捆绑是这样一个例外:

!SESSION 2013-10-05 19:21:27.348 -----------------------------------------------
eclipse.buildId=unknown
java.version=1.7.0_25
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=en_US
Command-line arguments:  -dev file:C:/My_documents/Java/eclipse/workspace/.metadata/.plugins/org.eclipse.pde.core/RAP/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog -console -data C:\My_documents\Java\eclipse\workspace/.metadata/.plugins/org.eclipse.rap.tools.launch/RAP

!ENTRY com.client 4 0 2013-10-05 19:21:27.898
!MESSAGE FrameworkEvent ERROR
!STACK 0
org.osgi.framework.BundleException: Exception in hotel.client.Activator.start() of bundle com.client.
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:734)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:390)
    at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1176)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
Caused by: java.lang.NoClassDefFoundError: org/eclipse/persistence/jpa/PersistenceProvider
    at hotel.service.daoimpl.HotelJPAImpl.checkStatusRoom(HotelJPAImpl.java:70)
    at hotel.client.Activator.start(Activator.java:21)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
    ... 12 more
Caused by: java.lang.ClassNotFoundException: org.eclipse.persistence.jpa.PersistenceProvider cannot be found by com.service_1.0.0.qualifier
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 17 more
Root exception:
java.lang.NoClassDefFoundError: org/eclipse/persistence/jpa/PersistenceProvider
    at hotel.service.daoimpl.HotelJPAImpl.checkStatusRoom(HotelJPAImpl.java:70)
    at hotel.client.Activator.start(Activator.java:21)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:711)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:702)
    at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:683)
    at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:381)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.resume(AbstractBundle.java:390)
    at org.eclipse.osgi.framework.internal.core.Framework.resumeBundle(Framework.java:1176)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:559)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.resumeBundles(StartLevelManager.java:544)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.incFWSL(StartLevelManager.java:457)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.doSetStartLevel(StartLevelManager.java:243)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:438)
    at org.eclipse.osgi.framework.internal.core.StartLevelManager.dispatchEvent(StartLevelManager.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)

原因:java.lang.ClassNotFoundException:com.service_1.0.0.qualifier 找不到 org.eclipse.persistence.jpa.PersistenceProvider

    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 17 more

!ENTRY org.eclipse.osgi 4 0 2013-10-05 19:21:27.901
!MESSAGE Bundle com.client_1.0.0.qualifier [52] is not active.

请帮忙!谢谢。

4

1 回答 1

0

异常表示捆绑“服务”的类加载器没有看到 EclipseLink 的 PersistenceProvider 类。

可能包 org.eclipse.persistence.jpa 未在服务包(Import-Package)中导入。

于 2013-10-06T22:42:08.510 回答