Author: meerajk
Date: Mon Nov 6 15:32:25 2006
New Revision: 471927
URL: http://svn.apache.org/viewvc?view=rev&rev=471927
Log:
Extracted constants.
Added:
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/JpaConstants.java
(with props)
Modified:
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScanner.java
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyPersistenceUnitInfo.java
incubator/tuscany/java/sca/services/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilderTestCase.java
Added:
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/JpaConstants.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/JpaConstants.java?view=auto&rev=471927
==============================================================================
---
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/JpaConstants.java
(added)
+++
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/JpaConstants.java
Mon Nov 6 15:32:25 2006
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.service.persistence.common;
+
+public class JpaConstants {
+
+ public static final String PROPERTY_VALUE = "value";
+
+ public static final String PROPERTY_NAME = "name";
+
+ public static final String PROPERTY =
"//persistence-unit/properties/property";
+
+ public static final String TRANSACTION_TYPE =
"//persistence-unit/@transaction-type";
+
+ public static final String NAME = "//persistence-unit/@name";
+
+ public static final String PROVIDER = "//persistence-unit/provider";
+
+ public static final String NON_JTA_DATA_SOURCE =
"//persistence-unit/non-jta-data-source";
+
+ public static final String MAPPING_FILE =
"//persistence-unit/mapping-file";
+
+ public static final String CLASS = "//persistence-unit/class";
+
+ public static final String JTA_DATA_SOURCE =
"//persistence-unit/jta-data-source";
+
+ public static final String JAR_FILE = "//persistence-unit/jar-file";
+
+ public static final String EXCLUDE_UNLISTED_CLASSES =
"//persistence-unit/exclude-unlisted-classes";
+
+}
Propchange:
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/JpaConstants.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/JpaConstants.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified:
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScanner.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScanner.java?view=diff&rev=471927&r1=471926&r2=471927
==============================================================================
---
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScanner.java
(original)
+++
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/PersistenceUnitScanner.java
Mon Nov 6 15:32:25 2006
@@ -22,24 +22,14 @@
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
import java.util.Map;
-import java.util.Properties;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.xpath.XPath;
-import javax.xml.xpath.XPathConstants;
-import javax.xml.xpath.XPathExpressionException;
-import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
@@ -47,10 +37,10 @@
*
*/
class PersistenceUnitScanner {
-
+
/** Cache of persistence unit info */
private Map<String, PersistenceUnitInfo> persistenceUnitInfos = new
HashMap<String, PersistenceUnitInfo>();
-
+
/**
* Scans the lassloader for the specified persistence unit and creates
* an immutable representation of the information present in the matching
@@ -61,48 +51,31 @@
* @return Persistence unit information.
*/
PersistenceUnitInfo getPersistenceUnitInfo(String unitName, ClassLoader
classLoader) {
-
-
+
synchronized (persistenceUnitInfos) {
-
+
try {
-
+
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
-
- XPathFactory xpathFactory = XPathFactory.newInstance();
- XPath xpath = xpathFactory.newXPath();
-
+
Enumeration<URL> persistenceUnitUrls =
classLoader.getResources("META-INF/persistence.xml");
- while(persistenceUnitUrls.hasMoreElements()) {
-
+ while (persistenceUnitUrls.hasMoreElements()) {
+
URL persistenceUnitUrl = persistenceUnitUrls.nextElement();
- Document root = db.parse(persistenceUnitUrl.openStream());
-
- String name = getSingleValue(xpath, root,
"//persistence-unit/@name");
- if(!unitName.equals(name)) {
- continue;
- }
-
+ Document persistenceDom =
db.parse(persistenceUnitUrl.openStream());
+
String rootJarUrl = persistenceUnitUrl.toString();
rootJarUrl = rootJarUrl.substring(0,
rootJarUrl.lastIndexOf("META-INF"));
-
- String transactionType = getSingleValue(xpath, root,
"//persistence-unit/@transaction-type");
- String provider = getSingleValue(xpath, root,
"//persistence-unit/provider");
- String jtaDsName = getSingleValue(xpath, root,
"//persistence-unit/jta-data-source");
- String nonJtaDsName = getSingleValue(xpath, root,
"//persistence-unit/non-jta-data-source");
- List<String> mappingFiles = getMultipleValues(xpath, root,
"//persistence-unit/mapping-file");
- List<String> jarFiles = getMultipleValues(xpath, root,
"//persistence-unit/jar-file");
- boolean exludeUnlistedClasses = getBooleanValue(xpath,
root, "//persistence-unit/exclude-unlisted-classes");
- List<String> managedClasses = getMultipleValues(xpath,
root, "//persistence-unit/class");
-
- // TODO load properties
- Properties prop = getProperties(xpath, root);
-
- PersistenceUnitInfo info = new
TuscanyPersistenceUnitInfo(transactionType, prop, rootJarUrl, unitName,
provider, nonJtaDsName, null, mappingFiles, managedClasses, jtaDsName,
jarFiles, classLoader, exludeUnlistedClasses);
+
+ PersistenceUnitInfo info = new
TuscanyPersistenceUnitInfo(persistenceDom, classLoader, rootJarUrl);
+ if (!unitName.equals(info.getPersistenceUnitName())) {
+ continue;
+ }
+
persistenceUnitInfos.put(unitName, info);
return info;
-
+
}
} catch (IOException ex) {
throw new TuscanyJpaException(ex);
@@ -110,59 +83,12 @@
throw new TuscanyJpaException(ex);
} catch (SAXException ex) {
throw new TuscanyJpaException(ex);
- } catch (XPathExpressionException ex) {
- throw new TuscanyJpaException(ex);
}
+
}
-
+
throw new TuscanyJpaException("Unabel to find persistence unit: " +
unitName);
-
- }
-
- /*
- * Extracts additional properties.
- */
- private Properties getProperties(XPath xpath, Document root) throws
XPathExpressionException {
-
- NodeList nodeList = (NodeList)
xpath.evaluate("//persistence-unit/properties/property", root,
XPathConstants.NODESET);
- Properties data = new Properties();
-
- for(int i = 0;i < nodeList.getLength();i++) {
- Element property = (Element) nodeList.item(i);
- data.put(property.getAttribute("name"),
property.getAttribute("value"));
- }
-
- return data;
- }
- /*
- * Gets multiple values for the specified expression.
- */
- private List<String> getMultipleValues(XPath xpath, Node context, String
expression) throws XPathExpressionException {
-
- NodeList nodeList = (NodeList)xpath.evaluate(expression, context,
XPathConstants.NODESET);
- List<String> data = new LinkedList<String>();
-
- for(int i = 0;i < nodeList.getLength();i++) {
- data.add(nodeList.item(i).getTextContent());
- }
-
- return data;
-
- }
-
- /*
- * Gets single value for the specified expression.
- */
- private String getSingleValue(XPath xpath, Node context, String
expression) throws XPathExpressionException {
- return xpath.evaluate(expression, context);
- }
-
- /*
- * Gets single value for the specified expression.
- */
- private boolean getBooleanValue(XPath xpath, Node context, String
expression) throws XPathExpressionException {
- return Boolean.valueOf(xpath.evaluate(expression, context));
}
}
Modified:
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyPersistenceUnitInfo.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyPersistenceUnitInfo.java?view=diff&rev=471927&r1=471926&r2=471927
==============================================================================
---
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyPersistenceUnitInfo.java
(original)
+++
incubator/tuscany/java/sca/services/persistence/common/src/main/java/org/apache/tuscany/service/persistence/common/TuscanyPersistenceUnitInfo.java
Mon Nov 6 15:32:25 2006
@@ -31,120 +31,49 @@
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.sql.DataSource;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
/**
* Encpasulates the information in the persistence.xml file.
+ *
+ * This class is expected to be interogated by the provider only
+ * during the creation of the entity manager factory. Hence none
+ * of the values are cached, rather every time a property is queried
+ * the underlying DOM is interogated.
*
*/
-class TuscanyPersistenceUnitInfo implements PersistenceUnitInfo {
+class TuscanyPersistenceUnitInfo implements PersistenceUnitInfo {
- /**
- * Transaction type.
- */
- private String transactionType;
-
- /**
- * Configuration properties.
- */
- private Properties properties;
-
- /**
- * Root JAT URL.
- */
- private String rootUrl;
-
- /**
- * Persistence unit name.
- */
- private String unitName;
-
- /**
- * Persistence provider class.
- */
- private String providerClass;
-
- /**
- * Non JTA Datasource.
- */
- private DataSource nonJtaDataSource;
-
- /**
- * Non JTA Datasource name.
- */
- private String nonJtaDsName;
-
- /**
- * Temporary classloader.
- */
- private ClassLoader tempClassLoader;
-
- /**
- * Mapped file names.
- */
- private List<String> mappingFileNames;
-
- /**
- * Mapped persistent classes.
- */
- private List<String> managedClassNames;
-
- /**
- * JTA datasource name.
- */
- private String jtaDsName;
-
- /**
- * JTA datasource.
- */
- private DataSource jtaDataSource;
-
- /**
- * JAR file URLs.
- */
- private List<String> jarFileUrls;
-
- /**
- * Classloader.
- */
+ /** Persistence DOM */
+ private Node persistenceDom;
+
+ /** Classloader */
private ClassLoader classLoader;
-
- /**
- * Whether unlisted classes in the DD are exluded.
- */
- private boolean unlistedClassesExcluded;
-
+ /** Root Url */
+ private String rootUrl;
+
+ /** XPath API */
+ XPath xpath = XPathFactory.newInstance().newXPath();
+
/**
* Initializes the properties.
*
- * @param transactionType Transaction type.
- * @param properties Configuration properties.
- * @param rootUrl Root JAT URL.
- * @param unitName Persistence unit name.
- * @param providerClass Persistence provider class.
- * @param nonJtaDataSource Non JTA Datasource.
- * @param tempClassLoader Temporary classloader.
- * @param mappingFileNames Mapped file names.
- * @param managedClassNames Mapped persistent classes.
- * @param jtaDataSource JTA datasource.
- * @param jarFileUrls JAR file URLs.
- * @param classLoader Classloader.
- * @param unlistedClassesExcluded Whether unlisted classes in the DD are
exluded.
- */
- public TuscanyPersistenceUnitInfo(String transactionType, Properties
properties, String rootUrl, String unitName, String providerClass, String
nonJtaDsName, ClassLoader tempClassLoader, List<String> mappingFileNames,
List<String> managedClassNames, String jtaDsName, List<String> jarFileUrls,
ClassLoader classLoader, boolean unlistedClassesExcluded) {
- this.transactionType = transactionType;
- this.properties = properties;
- this.rootUrl = rootUrl;
- this.unitName = unitName;
- this.providerClass = providerClass;
- this.nonJtaDsName = nonJtaDsName;
- this.tempClassLoader = tempClassLoader;
- this.mappingFileNames = mappingFileNames;
- this.managedClassNames = managedClassNames;
- this.jtaDsName = jtaDsName;
- this.jarFileUrls = jarFileUrls;
+ * @param persistenceDom
+ * @param classLoader
+ * @param rootUrl
+ */
+ public TuscanyPersistenceUnitInfo(Node persistenceDom, ClassLoader
classLoader, String rootUrl) {
+ this.persistenceDom = persistenceDom;
this.classLoader = classLoader;
- this.unlistedClassesExcluded = unlistedClassesExcluded;
+ this.rootUrl = rootUrl;
}
/* (non-Javadoc)
@@ -157,7 +86,7 @@
* @see javax.persistence.spi.PersistenceUnitInfo#excludeUnlistedClasses()
*/
public boolean excludeUnlistedClasses() {
- return unlistedClassesExcluded;
+ return getBooleanValue(persistenceDom,
JpaConstants.EXCLUDE_UNLISTED_CLASSES);
}
/* (non-Javadoc)
@@ -171,80 +100,79 @@
* @see javax.persistence.spi.PersistenceUnitInfo#getJarFileUrls()
*/
public List<URL> getJarFileUrls() {
+
+ List<String> jarFiles = getMultipleValues(persistenceDom,
JpaConstants.JAR_FILE);
try {
- List<URL> jarFiles = new LinkedList<URL>();
- for(String jarFileUrl : jarFileUrls) {
- jarFiles.add(new URL(jarFileUrl));
+ List<URL> jarUrls = new LinkedList<URL>();
+ for (String jarFile : jarFiles) {
+ jarUrls.add(new URL(jarFile));
}
- return jarFiles;
- } catch(MalformedURLException ex) {
+ return jarUrls;
+ } catch (MalformedURLException ex) {
throw new TuscanyJpaException(ex);
}
+
}
/* (non-Javadoc)
* @see javax.persistence.spi.PersistenceUnitInfo#getJtaDataSource()
*/
public DataSource getJtaDataSource() {
-
- if(jtaDataSource != null) {
- return jtaDataSource;
- }
- if(jtaDsName == null || "".equals(jtaDsName)) {
+
+ String jtaDsName = getSingleValue(persistenceDom,
JpaConstants.JTA_DATA_SOURCE);
+ if (jtaDsName == null || "".equals(jtaDsName)) {
return null;
- }
+ }
return lookupDataSource(jtaDsName);
-
+
}
/* (non-Javadoc)
* @see javax.persistence.spi.PersistenceUnitInfo#getManagedClassNames()
*/
public List<String> getManagedClassNames() {
- return managedClassNames;
+ return getMultipleValues(persistenceDom, JpaConstants.CLASS);
}
/* (non-Javadoc)
* @see javax.persistence.spi.PersistenceUnitInfo#getMappingFileNames()
*/
public List<String> getMappingFileNames() {
- return mappingFileNames;
+ return getMultipleValues(persistenceDom, JpaConstants.MAPPING_FILE);
}
/* (non-Javadoc)
* @see javax.persistence.spi.PersistenceUnitInfo#getNewTempClassLoader()
*/
public ClassLoader getNewTempClassLoader() {
- return tempClassLoader;
+ return null;
}
/* (non-Javadoc)
* @see javax.persistence.spi.PersistenceUnitInfo#getNonJtaDataSource()
*/
public DataSource getNonJtaDataSource() {
-
- if(nonJtaDataSource != null) {
- return nonJtaDataSource;
- }
- if(nonJtaDsName == null || "".equals(nonJtaDsName)) {
+
+ String nonJtaDsName = getSingleValue(persistenceDom,
JpaConstants.NON_JTA_DATA_SOURCE);
+ if (nonJtaDsName == null || "".equals(nonJtaDsName)) {
return null;
- }
+ }
return lookupDataSource(nonJtaDsName);
-
+
}
/* (non-Javadoc)
* @see
javax.persistence.spi.PersistenceUnitInfo#getPersistenceProviderClassName()
*/
public String getPersistenceProviderClassName() {
- return providerClass;
+ return getSingleValue(persistenceDom, JpaConstants.PROVIDER);
}
/* (non-Javadoc)
* @see javax.persistence.spi.PersistenceUnitInfo#getPersistenceUnitName()
*/
public String getPersistenceUnitName() {
- return unitName;
+ return getSingleValue(persistenceDom, JpaConstants.NAME);
}
/* (non-Javadoc)
@@ -253,7 +181,7 @@
public URL getPersistenceUnitRootUrl() {
try {
return new URL(rootUrl);
- } catch(MalformedURLException ex) {
+ } catch (MalformedURLException ex) {
throw new TuscanyJpaException(ex);
}
}
@@ -262,17 +190,79 @@
* @see javax.persistence.spi.PersistenceUnitInfo#getProperties()
*/
public Properties getProperties() {
- return properties;
+ return getProperties(persistenceDom);
}
/* (non-Javadoc)
* @see javax.persistence.spi.PersistenceUnitInfo#getTransactionType()
*/
public PersistenceUnitTransactionType getTransactionType() {
+ String transactionType = getSingleValue(persistenceDom,
JpaConstants.TRANSACTION_TYPE);
return "JTA".equals(transactionType) ?
PersistenceUnitTransactionType.JTA :
PersistenceUnitTransactionType.RESOURCE_LOCAL;
}
/*
+ * Extracts additional properties.
+ */
+ private Properties getProperties(Node root) {
+
+ try {
+ NodeList nodeList = (NodeList)
xpath.evaluate(JpaConstants.PROPERTY, root, XPathConstants.NODESET);
+ Properties data = new Properties();
+
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ Element property = (Element) nodeList.item(i);
+ data.put(property.getAttribute(JpaConstants.PROPERTY_NAME),
property.getAttribute(JpaConstants.PROPERTY_VALUE));
+ }
+
+ return data;
+ } catch (XPathExpressionException ex) {
+ throw new TuscanyJpaException(ex);
+ }
+
+ }
+
+ /*
+ * Gets multiple values for the specified expression.
+ */
+ private List<String> getMultipleValues(Node context, String expression) {
+
+ try {
+ NodeList nodeList = (NodeList) xpath.evaluate(expression, context,
XPathConstants.NODESET);
+ List<String> data = new LinkedList<String>();
+
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ data.add(nodeList.item(i).getTextContent());
+ }
+
+ return data;
+ } catch (XPathExpressionException ex) {
+ throw new TuscanyJpaException(ex);
+ }
+
+ }
+
+ /*
+ * Gets single value for the specified expression.
+ */
+ private String getSingleValue(Node context, String expression) {
+
+ try {
+ return xpath.evaluate(expression, context);
+ } catch (XPathExpressionException ex) {
+ throw new TuscanyJpaException(ex);
+ }
+
+ }
+
+ /*
+ * Gets single value for the specified expression.
+ */
+ private boolean getBooleanValue(Node context, String expression) {
+ return Boolean.valueOf(getSingleValue(context, expression));
+ }
+
+ /*
* Looks up datasource.
*/
private DataSource lookupDataSource(String dsName) {
@@ -280,13 +270,13 @@
try {
ctx = new InitialContext();
return (DataSource) ctx.lookup(dsName);
- } catch(NamingException ex) {
+ } catch (NamingException ex) {
throw new TuscanyJpaException(ex);
} finally {
- if(ctx != null) {
+ if (ctx != null) {
try {
ctx.close();
- } catch(NamingException ex) {
+ } catch (NamingException ex) {
throw new TuscanyJpaException(ex);
}
}
Modified:
incubator/tuscany/java/sca/services/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilderTestCase.java
URL:
http://svn.apache.org/viewvc/incubator/tuscany/java/sca/services/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilderTestCase.java?view=diff&rev=471927&r1=471926&r2=471927
==============================================================================
---
incubator/tuscany/java/sca/services/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilderTestCase.java
(original)
+++
incubator/tuscany/java/sca/services/persistence/common/src/test/java/org/apache/tuscany/service/persistence/common/DefaultPersistenceUnitBuilderTestCase.java
Mon Nov 6 15:32:25 2006
@@ -1,3 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
package org.apache.tuscany.service.persistence.common;
import junit.framework.TestCase;
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]