Skip to content

Commit

Permalink
Fiddled with jpaexample and jpaprovider to try and make them handle J…
Browse files Browse the repository at this point in the history
…PA Models

and persistence configurations from multiple bundles
  • Loading branch information
reciva-at committed Apr 2, 2009
1 parent 1ec8ead commit 836b509
Show file tree
Hide file tree
Showing 24 changed files with 875 additions and 7 deletions.
1 change: 1 addition & 0 deletions slingtests/osgikernel/bundles/jpaexample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<instructions>
<Bundle-Category> sakai-kernel </Bundle-Category>
<Bundle-Activator>org.sakaiproject.kernel2.osgi.jpaexample.JpaExample</Bundle-Activator>
<JPA-PersistenceUnits>sakaiPU</JPA-PersistenceUnits>
</instructions>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.sakaiproject.kernel2.osgi.jpaexample.jpa.model.ExampleModel;
import org.sakaiproject.kernel2.osgi.jpaprovider.UserManagerFactory;
import org.sakaiproject.kernel2.osgi.jpaprovider.model.SystemUser;

Expand All @@ -19,11 +20,22 @@ public void start(BundleContext arg0) throws Exception {
em.persist(u);
em.getTransaction().commit();

// g should be written to database now.
// u should be written to database now.
// Read it from db (no transaction context needed for em.find method)
SystemUser u2 = em.find(SystemUser.class, u.getId());
System.out.println("User " + u.getId() + " from db: " + u2);

System.out.println("Creating example model");
ExampleModel model = new ExampleModel();
model.setProperty("Some property");
em.getTransaction().begin();
em.persist(model);
em.getTransaction().commit();

System.out.println("Attempting to read back model from database");
// model should be written to database now.
ExampleModel model2 = em.find(ExampleModel.class, model.getId());
System.out.println("Model " + model.getId() + " from db: " + model2);
}

public void stop(BundleContext arg0) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.sakaiproject.kernel2.osgi.jpaexample.jpa.model;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class ExampleModel {

@Id
private long id;

@Basic
private String property;

public long getId()
{
return id;
}

public void setProperty(String property) {
this.property = property;
}

public String getProperty() {
return property;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
<entity class="org.sakaiproject.kernel2.osgi.jpaexample.jpa.model.ExampleModel">
</entity>
</entity-mappings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8" ?>
<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"
version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="sakaiPU">
<class>org.sakaiproject.kernel2.osgi.jpaexample.jpa.model.ExampleModel</class>
<properties>
<property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="eclipselink.jdbc.url" value="jdbc:derby:testdb;create=true"/>
<property name="eclipselink.jdbc.user" value="sa"/> <property name="eclipselink.jdbc.password" value=""/>
<property name="eclipselink.target-server" value="None"/>
<property name="eclipselink.jdbc.write-connections.min" value="1" />
<property name="eclipselink.jdbc.read-connections.min" value="1" />
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.logging.timestamp" value="false" />
<property name="eclipselink.logging.session" value="false" />
<property name="eclipselink.logging.thread" value="false" />
<property name="eclipselink.logging.exceptions" value="true" />
<property name="eclipselink.ddl-generation.output-mode" value="both" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
38 changes: 36 additions & 2 deletions slingtests/osgikernel/bundles/jpaprovider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,25 @@
<configuration>
<instructions>
<Bundle-Category> sakai-kernel </Bundle-Category>
<JPA-PersistenceUnits>systemuser</JPA-PersistenceUnits>
<Bundle-Activator>org.sakaiproject.kernel2.osgi.jpaprovider.PersistenceBundleMonitor</Bundle-Activator>
<JPA-PersistenceUnits>sakaiPU</JPA-PersistenceUnits>
<Import-Package>sun.io;resolution:=optional,
sun.misc;resolution:=optional,
sun.reflect;resolution:=optional,
javax.xml.parsers,
org.w3c.dom,
org.xml.sax,
org.xml.sax.ext,
org.xml.sax.helpers,
!org.apache.commons.pool.impl,
!org.codehaus.jettison,
!org.codehaus.jettison.*,
!org.dom4j.*,
!nu.xom,
!net.sf.cglib.proxy,
!org.joda.time.*,
!org.jdom.*,
!org.xmlpull.*,
*</Import-Package>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
</instructions>
Expand All @@ -55,7 +67,7 @@ org.xml.sax.helpers,
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeArtifactIds>derby,derbynet,xalan,serializer,xercesImpl,xml-resolver</includeArtifactIds>
<includeArtifactIds>derby,derbynet,xalan,serializer,xercesImpl,xml-resolver,commons-pool,xstream</includeArtifactIds>
<excludeTransitive>false</excludeTransitive>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
</configuration>
Expand All @@ -71,6 +83,11 @@ org.xml.sax.helpers,
<version>10.4.2.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbynet</artifactId>
Expand Down Expand Up @@ -101,6 +118,12 @@ org.xml.sax.helpers,
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
Expand All @@ -125,5 +148,16 @@ org.xml.sax.helpers,
<version>1.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package org.sakaiproject.kernel2.osgi.jpaprovider;

import com.thoughtworks.xstream.XStream;

import org.sakaiproject.kernel2.osgi.jpaprovider.xstream.OrmEntity;
import org.sakaiproject.kernel2.osgi.jpaprovider.xstream.OrmSettings;
import org.sakaiproject.kernel2.osgi.jpaprovider.xstream.PersistenceSettings;
import org.sakaiproject.kernel2.osgi.jpaprovider.xstream.PersistenceUnit;
import org.sakaiproject.kernel2.osgi.jpaprovider.xstream.XStreamWritable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class AmalgamatingClassloader extends ClassLoader {

private static final Logger LOG = LoggerFactory.getLogger(AmalgamatingClassloader.class);

public static final String KERNEL_PERSISTENCE_XML = "META-INF/kernel-persistence.xml";
public static final String PERSISTENCE_XML = "META-INF/persistence.xml";
public static final String ORM_XML = "META-INF/orm.xml";

private Map<String, PersistenceUnit> settingsMap = new HashMap<String, PersistenceUnit>();
private PersistenceSettings baseSettings;
private Set<OrmEntity> ormClasses = new HashSet<OrmEntity>();
private URL persistenceXMLurl;
private URL ormXMLurl;

public AmalgamatingClassloader(ClassLoader classLoader) {
super(classLoader);
baseSettings = PersistenceBundleMonitor.parsePersistenceXml(classLoader
.getResourceAsStream(KERNEL_PERSISTENCE_XML));
for (PersistenceUnit unit : baseSettings.getPersistenceUnits()) {
settingsMap.put(unit.getName(), unit);
}
}

public void importPersistenceXml(URL persistence) throws IOException {
PersistenceSettings newSettings = PersistenceBundleMonitor.parsePersistenceXml(persistence
.openStream());
for (PersistenceUnit unit : newSettings.getPersistenceUnits()) {
PersistenceUnit existingUnit = settingsMap.get(unit.getName());
if (existingUnit == null) {
settingsMap.put(unit.getName(), unit);
} else {
existingUnit.addProperties(unit.getPropertiesList());
}
}
}

public void importOrmXml(URL orm) throws IOException {
OrmSettings settings = PersistenceBundleMonitor.parseOrmXml(orm.openStream());
for (OrmEntity entity : settings.getEntities()) {
ormClasses.add(entity);
}
}

public Enumeration<URL> getResources(final String name) throws IOException {
Enumeration<URL> retEnum = null;
if (PERSISTENCE_XML.equals(name)) {
if (persistenceXMLurl == null) {
persistenceXMLurl = constructUrl(PersistenceBundleMonitor.getPersistenceSettingsXStream(),
baseSettings, PERSISTENCE_XML);
}
retEnum = new UrlEnumeration(persistenceXMLurl);
}
// make sure subsequent lookups for orm.xml get the merged copy of the
// file
else if (ORM_XML.equals(name)) {
if (ormXMLurl == null) {
OrmSettings settings = new OrmSettings();
settings.setEntities(ormClasses);
ormXMLurl = constructUrl(PersistenceBundleMonitor.getOrmSettingsXStream(), settings,
ORM_XML);
}
retEnum = new UrlEnumeration(ormXMLurl);
} else {
retEnum = super.getResources(name);
}
return retEnum;
}

/**
* Constructs a temporary file that merges together the requested filename as
* it is found in different artifacts (jars). The URL to the merged file is
* returned.
*
* @param filename
* The file to look for in the classloader.
* @return The merged result of the found filenames.
* @throws IOException
*/
private URL constructUrl(XStream xstream, XStreamWritable writable, String filename)
throws IOException {
LOG.debug(filename + " " + writable);

// The base directory must be empty since JPA will scan it searching for
// classes.
File file = new File(System.getProperty("java.io.tmpdir") + "/sakai/" + filename);
if (file.getParentFile().mkdirs()) {
LOG.debug("Created " + file);
}

xstream.toXML(writable, new FileOutputStream(file));
URL url = null;
try {
url = file.toURI().toURL();
} catch (MalformedURLException e) {
LOG.error("cannot convert file to URL " + e.toString());
}
LOG.debug("URL: " + url);
return url;
}

/**
* Enumeration for handling the return from getResources of new temp URLs.
*/
private static class UrlEnumeration implements Enumeration<URL> {
private URL url;

UrlEnumeration(URL url) {
this.url = url;
}

public boolean hasMoreElements() {
return url != null;
}

public URL nextElement() {
final URL url2 = url;
url = null;
return url2;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.sakaiproject.kernel2.osgi.jpaprovider;

import org.osgi.framework.Bundle;

import java.net.URL;
import java.util.LinkedList;
import java.util.List;

public class BundleGatheringResourceFinder {

private List<Bundle> bundles;

public BundleGatheringResourceFinder(List<Bundle> bundles) {
this.bundles = bundles;
}

public List<URL> getResources(String string) {
List<URL> result = new LinkedList<URL>();
for (Bundle bundle : bundles) {
URL resource = bundle.getResource(string);
if (resource != null) {
result.add(resource);
}
}
return result;
}

}
Loading

0 comments on commit 836b509

Please sign in to comment.