help: OSGi ClassLoader and ressources

Discussion in 'Java' started by CHAFIK Wassime, Apr 24, 2008.

  1. Hi
    well first sorry for the english that you are about to read :)
    i'm actually trying to make OpenJPA as an OSGi bundle
    first step:
    make a big big jar containing all the packages the OpenJPA may need and
    add them naturally to the bundle classpath (this may change later by
    inflating this monolith into more flexible plugable smaller bundles)
    second step:
    write this ServiceFactory implementation
    /**
    * @(#)JPAServiceFactory.java
    *
    *
    * @author
    * @version 1.00 2008/4/10
    */
    package jpa.impl;

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.net.URL;
    import java.net.URLClassLoader;

    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;

    import org.osgi.framework.Bundle;
    import org.osgi.framework.ServiceFactory;
    import org.osgi.framework.ServiceRegistration;

    public class JPAServiceFactory implements ServiceFactory {
    /**
    * Method getService
    *
    * @param bundle
    * @param registration
    *
    * @return
    *
    */
    public Object getService(Bundle bundle, ServiceRegistration registration) {
    URL[] url = new URL[1];
    url[0] = bundle.getResource("META-INF/persistence.xml");
    if (url[0] == null)
    throw new IllegalStateException(
    "bundle "
    + bundle.getBundleId()
    + " called the JPAService, META-INF/persistence.xml not found");
    //make a ClassLoader able to :
    // 1-bootstrap JPA (all jars needed by OpenJPA)
    // 2-find META-INF/persistence.xml
    ClassLoader bundleCL = this.getClass().getClassLoader();
    URLClassLoader newCL = new URLClassLoader(url, bundleCL);
    Thread.currentThread().setContextClassLoader(newCL);
    //this print OK the ressource exist
    System.out.println("conf url from bundle:" + url[0]);
    Object result=null;
    try {
    Class persistence = newCL.loadClass("javax.persistence.Persistence");
    //WHY IS THIS printing null
    System.out.println("conf seen in bootstrap
    "+newCL.getResource("META-INF/persistence.xml"));
    Method createFacory =
    persistence.getDeclaredMethod("createEntityManagerFactory", new
    Class[]{String.class});
    result = createFacory.invoke(null, new String[]{null});
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }catch (SecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (NoSuchMethodException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }catch (IllegalArgumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (InvocationTargetException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return result;
    }

    /**
    * Method ungetService
    *
    *
    * @param bundle
    * @param registration
    * @param service
    *
    */
    public void ungetService(Bundle bundle, ServiceRegistration registration,
    Object serv) {
    if (serv != null && ((EntityManagerFactory) serv).isOpen())
    ((EntityManagerFactory) serv).close();
    }

    }

    here is the Activator

    package jpa.impl;

    import java.util.Hashtable;

    import javax.persistence.EntityManagerFactory;

    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;

    public class Activator implements BundleActivator {

    /* (non-Javadoc)
    * @see
    org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
    */
    public void start(BundleContext context) throws Exception {
    JPAServiceFactory factory = new JPAServiceFactory();
    Hashtable<String, String> properties = new Hashtable<String, String>();
    properties.put("impl", "OpenJPA");
    properties.put("vendor", "apache");
    context.registerService(EntityManagerFactory.class.getName(),
    factory, properties);
    }

    /* (non-Javadoc)
    * @see
    org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
    */
    public void stop(BundleContext context) throws Exception {
    }
    }


    even when i create a URLClassLoader (newCL) above the bundle ClassLoader
    (aka the bootstraping ClassLoader) by adding the URL of
    "META-INF/persistence.xml" (aka JPA configuration file)
    newCL.getResource("META-INF/persistence.xml") still returns null ????
    ..............
    i can't bootstrap OpenJPA in this conditions :-(
    reaaaaaaaaaaaaaaaaaaally need some help here
     
    CHAFIK Wassime, Apr 24, 2008
    #1
    1. Advertising

  2. CHAFIK Wassime wrote:
    > Hi
    > well first sorry for the english that you are about to read :)
    > i'm actually trying to make OpenJPA as an OSGi bundle
    > first step:
    > make a big big jar containing all the packages the OpenJPA may need and
    > add them naturally to the bundle classpath (this may change later by
    > inflating this monolith into more flexible plugable smaller bundles)
    > second step:
    > write this ServiceFactory implementation
    > /**
    > * @(#)JPAServiceFactory.java
    > *
    > *
    > * @author
    > * @version 1.00 2008/4/10
    > */
    > package jpa.impl;
    >
    > import java.lang.reflect.InvocationTargetException;
    > import java.lang.reflect.Method;
    > import java.net.URL;
    > import java.net.URLClassLoader;
    >
    > import javax.persistence.EntityManagerFactory;
    > import javax.persistence.Persistence;
    >
    > import org.osgi.framework.Bundle;
    > import org.osgi.framework.ServiceFactory;
    > import org.osgi.framework.ServiceRegistration;
    >
    > public class JPAServiceFactory implements ServiceFactory {
    > /**
    > * Method getService
    > *
    > * @param bundle
    > * @param registration
    > *
    > * @return
    > *
    > */
    > public Object getService(Bundle bundle, ServiceRegistration
    > registration) {
    > URL[] url = new URL[1];
    > url[0] = bundle.getResource("META-INF/persistence.xml");
    > if (url[0] == null)
    > throw new IllegalStateException(
    > "bundle "
    > + bundle.getBundleId()
    > + " called the JPAService,
    > META-INF/persistence.xml not found");
    > //make a ClassLoader able to :
    > // 1-bootstrap JPA (all jars needed by OpenJPA)
    > // 2-find META-INF/persistence.xml
    > ClassLoader bundleCL = this.getClass().getClassLoader();
    > URLClassLoader newCL = new URLClassLoader(url, bundleCL);
    > Thread.currentThread().setContextClassLoader(newCL);
    > //this print OK the ressource exist
    > System.out.println("conf url from bundle:" + url[0]);
    > Object result=null;
    > try {
    > Class persistence =
    > newCL.loadClass("javax.persistence.Persistence");
    > //WHY IS THIS printing null
    > System.out.println("conf seen in bootstrap
    > "+newCL.getResource("META-INF/persistence.xml"));
    > Method createFacory =
    > persistence.getDeclaredMethod("createEntityManagerFactory", new
    > Class[]{String.class});
    > result = createFacory.invoke(null, new String[]{null});
    > } catch (ClassNotFoundException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > }catch (SecurityException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > } catch (NoSuchMethodException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > }catch (IllegalArgumentException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > } catch (IllegalAccessException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > } catch (InvocationTargetException e) {
    > // TODO Auto-generated catch block
    > e.printStackTrace();
    > }
    > return result;
    > }
    >
    > /**
    > * Method ungetService
    > *
    > *
    > * @param bundle
    > * @param registration
    > * @param service
    > *
    > */
    > public void ungetService(Bundle bundle, ServiceRegistration
    > registration,
    > Object serv) {
    > if (serv != null && ((EntityManagerFactory) serv).isOpen())
    > ((EntityManagerFactory) serv).close();
    > }
    >
    > }
    >
    > here is the Activator
    >
    > package jpa.impl;
    >
    > import java.util.Hashtable;
    >
    > import javax.persistence.EntityManagerFactory;
    >
    > import org.osgi.framework.BundleActivator;
    > import org.osgi.framework.BundleContext;
    >
    > public class Activator implements BundleActivator {
    >
    > /* (non-Javadoc)
    > * @see
    > org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
    > */
    > public void start(BundleContext context) throws Exception {
    > JPAServiceFactory factory = new JPAServiceFactory();
    > Hashtable<String, String> properties = new Hashtable<String,
    > String>();
    > properties.put("impl", "OpenJPA");
    > properties.put("vendor", "apache");
    > context.registerService(EntityManagerFactory.class.getName(),
    > factory, properties);
    > }
    >
    > /* (non-Javadoc)
    > * @see
    > org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
    > */
    > public void stop(BundleContext context) throws Exception {
    > }
    > }
    >
    >
    > even when i create a URLClassLoader (newCL) above the bundle ClassLoader
    > (aka the bootstraping ClassLoader) by adding the URL of
    > "META-INF/persistence.xml" (aka JPA configuration file)
    > newCL.getResource("META-INF/persistence.xml") still returns null ????
    > .............
    > i can't bootstrap OpenJPA in this conditions :-(
    > reaaaaaaaaaaaaaaaaaaally need some help here


    so there is no help on that :)
    i'm really stuck here :p
    guess i'll have to change the design a little bit :p
     
    CHAFIK Wassime, Apr 28, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pascal Peyremorte

    [VHDL Beginner] About ressources used

    Pascal Peyremorte, Jul 20, 2005, in forum: VHDL
    Replies:
    2
    Views:
    796
    Pascal Peyremorte
    Jul 22, 2005
  2. Franck
    Replies:
    5
    Views:
    852
    =?Utf-8?B?dHBhcmtzNjk=?=
    Oct 27, 2005
  3. herc
    Replies:
    0
    Views:
    378
  4. Replies:
    1
    Views:
    409
    Manish Pandit
    Sep 28, 2006
  5. WT

    Getting htc files from ressources in ASP.NET 2

    WT, May 23, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    298
Loading...

Share This Page