-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fiddled with jpaexample and jpaprovider to try and make them handle J…
…PA Models and persistence configurations from multiple bundles
- Loading branch information
reciva-at
committed
Apr 2, 2009
1 parent
1ec8ead
commit 836b509
Showing
24 changed files
with
875 additions
and
7 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
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
29 changes: 29 additions & 0 deletions
29
...xample/src/main/java/org/sakaiproject/kernel2/osgi/jpaexample/jpa/model/ExampleModel.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,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; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
slingtests/osgikernel/bundles/jpaexample/src/main/resources/META-INF/orm.xml
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,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> |
24 changes: 24 additions & 0 deletions
24
slingtests/osgikernel/bundles/jpaexample/src/main/resources/META-INF/persistence.xml
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,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> |
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
145 changes: 145 additions & 0 deletions
145
...ider/src/main/java/org/sakaiproject/kernel2/osgi/jpaprovider/AmalgamatingClassloader.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,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; | ||
} | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...rc/main/java/org/sakaiproject/kernel2/osgi/jpaprovider/BundleGatheringResourceFinder.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,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; | ||
} | ||
|
||
} |
Oops, something went wrong.