DELTASPIKE-1095 Move persistence/orm.xml mapping + parsing from DATA impl to JPA api/spi
Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/6480a0e5 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/6480a0e5 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/6480a0e5 Branch: refs/heads/master Commit: 6480a0e5466567f487f36d73de697c6310f69149 Parents: f30b389 Author: Thomas Andraschko <[email protected]> Authored: Sat Mar 19 21:20:52 2016 +0100 Committer: Thomas Andraschko <[email protected]> Committed: Sat Mar 19 21:20:52 2016 +0100 ---------------------------------------------------------------------- .../core/util/AggregatedClassLoader.java | 95 ++++++++ .../data/impl/RepositoryExtension.java | 4 +- .../meta/extractor/TypeMetadataExtractor.java | 8 +- .../data/impl/meta/unit/Descriptor.java | 47 ---- .../meta/unit/DescriptorHierarchyBuilder.java | 88 ------- .../data/impl/meta/unit/DescriptorReader.java | 115 --------- .../data/impl/meta/unit/EntityDescriptor.java | 81 ------- .../impl/meta/unit/EntityDescriptorReader.java | 223 ------------------ .../meta/unit/MappedSuperclassDescriptor.java | 67 ------ .../data/impl/meta/unit/PersistenceUnit.java | 72 ------ .../impl/meta/unit/PersistenceUnitReader.java | 130 ---------- .../data/impl/meta/unit/PersistenceUnits.java | 150 ------------ .../meta/unit/PersistentClassDescriptor.java | 140 ----------- .../data/impl/meta/verifier/EntityVerifier.java | 6 +- .../deltaspike/data/impl/util/EntityUtils.java | 21 +- .../impl/util/cl/AggregatedClassLoader.java | 93 -------- .../unit/DescriptorHierarchyBuilderTest.java | 26 +- .../impl/meta/unit/PersistenceUnitsTest.java | 44 ++-- .../xml/AbstractEntityDescriptor.java | 108 +++++++++ .../xml/AbstractEntityHierarchyBuilder.java | 81 +++++++ .../jpa/spi/descriptor/xml/Descriptor.java | 46 ++++ .../spi/descriptor/xml/DescriptorReader.java | 114 +++++++++ .../spi/descriptor/xml/EntityDescriptor.java | 63 +++++ .../xml/EntityMappingsDescriptor.java | 66 ++++++ .../xml/EntityMappingsDescriptorParser.java | 235 +++++++++++++++++++ .../xml/MappedSuperclassDescriptor.java | 48 ++++ .../xml/PersistenceUnitDescriptor.java | 80 +++++++ .../xml/PersistenceUnitDescriptorParser.java | 136 +++++++++++ .../xml/PersistenceUnitDescriptorProvider.java | 191 +++++++++++++++ .../xml/EntityMappingsDescriptorParserTest.java | 114 +++++++++ .../test/jpa/spi/descriptor/xml/MappedId.java | 36 +++ .../test/jpa/spi/descriptor/xml/MappedOne.java | 56 +++++ .../spi/descriptor/xml/MappedSuperclass.java | 36 +++ .../jpa/spi/descriptor/xml/MappedThree.java | 36 +++ .../test/jpa/spi/descriptor/xml/MappedTwo.java | 59 +++++ .../PersistenceUnitDescriptorParserTest.java | 71 ++++++ .../PersistenceUnitDescriptorProviderTest.java | 43 ++++ .../test/jpa/spi/descriptor/xml/TeeId.java | 103 ++++++++ .../src/test/resources/META-INF/persistence.xml | 38 +++ .../src/test/resources/META-INF/test-orm.xml | 58 +++++ 40 files changed, 1978 insertions(+), 1250 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AggregatedClassLoader.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AggregatedClassLoader.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AggregatedClassLoader.java new file mode 100644 index 0000000..84f9106 --- /dev/null +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/AggregatedClassLoader.java @@ -0,0 +1,95 @@ +/* + * 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.deltaspike.core.util; + +import java.io.IOException; +import java.net.URL; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + +public class AggregatedClassLoader extends ClassLoader +{ + + private final List<ClassLoader> classLoaders; + + public AggregatedClassLoader(List<ClassLoader> classLoaders) + { + super(); + this.classLoaders = classLoaders; + } + + public static AggregatedClassLoader newInstance() + { + return new AggregatedClassLoader(Arrays.asList( + AggregatedClassLoader.class.getClassLoader(), + Thread.currentThread().getContextClassLoader(), + ClassLoader.getSystemClassLoader())); + } + + @Override + public URL getResource(String name) + { + for (ClassLoader loader : classLoaders) + { + URL url = loader.getResource(name); + if (url != null) + { + return url; + } + } + return super.getResource(name); + } + + @Override + public Enumeration<URL> getResources(String name) throws IOException + { + final Set<URL> result = new LinkedHashSet<URL>(); + + for (ClassLoader loader : classLoaders) + { + Enumeration<URL> urls = loader.getResources(name); + while (urls.hasMoreElements()) + { + result.add(urls.nextElement()); + } + } + + return new Enumeration<URL>() + { + private final Iterator<URL> iterator = result.iterator(); + + @Override + public URL nextElement() + { + return iterator.next(); + } + + @Override + public boolean hasMoreElements() + { + return iterator.hasNext(); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/RepositoryExtension.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/RepositoryExtension.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/RepositoryExtension.java index f33545d..28714a7 100755 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/RepositoryExtension.java +++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/RepositoryExtension.java @@ -39,7 +39,7 @@ import org.apache.deltaspike.data.api.AbstractEntityRepository; import org.apache.deltaspike.data.api.AbstractFullEntityRepository; import org.apache.deltaspike.data.api.Repository; import org.apache.deltaspike.data.impl.meta.RepositoryComponents; -import org.apache.deltaspike.data.impl.meta.unit.PersistenceUnits; +import org.apache.deltaspike.jpa.spi.descriptor.xml.PersistenceUnitDescriptorProvider; /** * The main extension class for Repositories, based on PartialBeans. Handles following events:<br/> @@ -77,7 +77,7 @@ public class RepositoryExtension implements Extension, Deactivatable { return; } - PersistenceUnits.instance().init(); + PersistenceUnitDescriptorProvider.getInstance().init(); } @SuppressWarnings("unchecked") http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/extractor/TypeMetadataExtractor.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/extractor/TypeMetadataExtractor.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/extractor/TypeMetadataExtractor.java index f02a621..043ab88 100644 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/extractor/TypeMetadataExtractor.java +++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/extractor/TypeMetadataExtractor.java @@ -71,11 +71,17 @@ public class TypeMetadataExtractor implements MetadataExtractor { return null; } + ParameterizedType parametrizedType = (ParameterizedType) type; Type[] genericTypes = parametrizedType.getActualTypeArguments(); + RepositoryEntity result = null; - for (Type genericType : genericTypes) + + // don't use a foreach here, we must be sure that the we first get the entity type + for (int i = 0; i < genericTypes.length; i++) { + Type genericType = genericTypes[i]; + if (genericType instanceof Class && EntityUtils.isEntityClass((Class<?>) genericType)) { result = new RepositoryEntity((Class<?>) genericType); http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/Descriptor.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/Descriptor.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/Descriptor.java deleted file mode 100644 index 8350f36..0000000 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/Descriptor.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.deltaspike.data.impl.meta.unit; - -import java.net.URL; - -import org.w3c.dom.Document; - -class Descriptor -{ - - private final Document document; - private final URL url; - - public Descriptor(Document document, URL url) - { - this.document = document; - this.url = url; - } - - public Document getDocument() - { - return document; - } - - public URL getUrl() - { - return url; - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilder.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilder.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilder.java deleted file mode 100644 index e57099c..0000000 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilder.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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.deltaspike.data.impl.meta.unit; - -import java.util.List; - -public final class DescriptorHierarchyBuilder -{ - - private final List<EntityDescriptor> entities; - private final List<MappedSuperclassDescriptor> superClasses; - - private DescriptorHierarchyBuilder(List<EntityDescriptor> entities, - List<MappedSuperclassDescriptor> superClasses) - { - this.entities = entities; - this.superClasses = superClasses; - } - - public static DescriptorHierarchyBuilder newInstance(List<EntityDescriptor> entities, - List<MappedSuperclassDescriptor> superClasses) - { - return new DescriptorHierarchyBuilder(entities, superClasses); - } - - public void buildHierarchy() - { - for (EntityDescriptor descriptor : entities) - { - buildHierarchy(descriptor); - } - } - - private void buildHierarchy(PersistentClassDescriptor descriptor) - { - Class<?> superClass = descriptor.getEntityClass().getSuperclass(); - while (superClass != null) - { - PersistentClassDescriptor superDescriptor = findPersistentClassDescriptor(superClass); - if (superDescriptor != null) - { - if (descriptor.getParent() == null) - { - buildHierarchy(superDescriptor); - } - descriptor.setParent(superDescriptor); - return; - } - superClass = superClass.getSuperclass(); - } - } - - private PersistentClassDescriptor findPersistentClassDescriptor(Class<?> superClass) - { - for (MappedSuperclassDescriptor descriptor : superClasses) - { - if (descriptor.getEntityClass().equals(superClass)) - { - return descriptor; - } - } - for (EntityDescriptor descriptor : entities) - { - if (descriptor.getEntityClass().equals(superClass)) - { - return descriptor; - } - } - return null; - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorReader.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorReader.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorReader.java deleted file mode 100644 index 300b904..0000000 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorReader.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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.deltaspike.data.impl.meta.unit; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Collections; -import java.util.Enumeration; -import java.util.LinkedList; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; - -import org.apache.deltaspike.data.impl.util.cl.AggregatedClassLoader; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; - -abstract class DescriptorReader -{ - private static final Logger log = Logger.getLogger(DescriptorReader.class.getName()); - - private final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - - List<Descriptor> readAllFromClassPath(String resource) throws IOException - { - List<Descriptor> result = new LinkedList<Descriptor>(); - Enumeration<URL> urls = classLoader().getResources(resource); - while (urls.hasMoreElements()) - { - URL u = urls.nextElement(); - try - { - result.add(readFromUrl(u)); - } - catch (Exception e) - { - log.log(Level.WARNING, "Could not load " + resource + " from " + u, e); - } - } - return Collections.unmodifiableList(result); - } - - Descriptor readFromClassPath(String resource) throws IOException - { - return readFromUrl(classLoader().getResource(resource)); - } - - Descriptor readFromUrl(URL url) throws IOException - { - InputStream stream = url.openStream(); - try - { - DocumentBuilder builder = factory.newDocumentBuilder(); - return new Descriptor(builder.parse(new InputSource(stream)), url); - } - catch (SAXException e) - { - throw new RuntimeException("Failed reading XML document", e); - } - catch (ParserConfigurationException e) - { - throw new RuntimeException("Failed reading XML document", e); - } - finally - { - stream.close(); - } - } - - Descriptor read(String baseUrl, String resource) throws IOException - { - try - { - URL url = new URL(baseUrl + resource); - return readFromUrl(url); - } - catch (Exception e) - { - return readFromClassPath(resource); - } - } - - String extractBaseUrl(URL fileUrl, String resource) - { - String file = fileUrl.toString(); - return file.substring(0, file.length() - resource.length()); - } - - ClassLoader classLoader() - { - return AggregatedClassLoader.newInstance(); - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/EntityDescriptor.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/EntityDescriptor.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/EntityDescriptor.java deleted file mode 100644 index 6469baa..0000000 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/EntityDescriptor.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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.deltaspike.data.impl.meta.unit; - -import java.io.Serializable; - -import static org.apache.deltaspike.core.util.StringUtils.isEmpty; - -class EntityDescriptor extends PersistentClassDescriptor -{ - - protected final String tableName; - - EntityDescriptor(String name, String packageName, String className, String idClass, String id, - String version, String tableName) - { - super(name, packageName, className, idClass, id, version); - this.tableName = tableName; - } - - public boolean is(Class<?> entityClass) - { - return this.entityClass.equals(entityClass); - } - - @Override - public Class<? extends Serializable> getIdClass() - { - if (idClass == null && getParent() != null) - { - return getParent().getIdClass(); - } - return super.getIdClass(); - } - - @Override - public String getId() - { - if (isEmpty(id) && getParent() != null) - { - return getParent().getId(); - } - return super.getId(); - } - - public String getTableName() - { - return tableName; - } - - @Override - public String toString() - { - StringBuilder builder = new StringBuilder(); - builder.append("EntityDescriptor ") - .append("[entityClass=").append(className(entityClass)) - .append(", name=").append(name) - .append(", idClass=").append(className(idClass)) - .append(", id=").append(id) - .append(", superClass=").append(getParent()) - .append(", tableName=").append(tableName) - .append("]"); - return builder.toString(); - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/EntityDescriptorReader.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/EntityDescriptorReader.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/EntityDescriptorReader.java deleted file mode 100644 index 0b85e70..0000000 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/EntityDescriptorReader.java +++ /dev/null @@ -1,223 +0,0 @@ -/* - * 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.deltaspike.data.impl.meta.unit; - -import java.io.IOException; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; - -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -public class EntityDescriptorReader extends DescriptorReader -{ - - public MappingFile readAll(String baseUrl, String resource) throws IOException - { - return readFromDocument(read(baseUrl, resource).getDocument()); - } - - public MappingFile readDefaultOrm(String baseUrl) throws IOException - { - try - { - Descriptor desc = read(baseUrl, PersistenceUnit.DEFAULT_ORM_PATH); - return readFromDocument(desc.getDocument()); - } - catch (Exception e) - { - return new MappingFile(Collections.<EntityDescriptor>emptyList(), - Collections.<MappedSuperclassDescriptor>emptyList()); - } - } - - public MappingFile readFromDocument(Document doc) - { - EntityBuilder<EntityDescriptor> entityDescriptorBuilder = new EntityBuilder<EntityDescriptor>() - { - @Override - protected EntityDescriptor instance(String name, String packageName, String className, - String idClass, String id, String version, String tableName) - { - return new EntityDescriptor(name, packageName, className, idClass, id, version, tableName); - } - - @Override - protected String tagName() - { - return "entity"; - } - }; - - MappedSuperClassBuilder<MappedSuperclassDescriptor> superClassBuilder = - new MappedSuperClassBuilder<MappedSuperclassDescriptor>() - { - @Override - protected MappedSuperclassDescriptor instance(String name, String packageName, String className, - String idClass, String id, String version) - { - return new MappedSuperclassDescriptor(name, packageName, className, idClass, id, version); - } - - @Override - protected String tagName() - { - return "mapped-superclass"; - } - }; - return new MappingFile(entityDescriptorBuilder.build(doc), superClassBuilder.build(doc)); - } - - private String extractNodeAttribute(Element element, String childName, String attribute) - { - NodeList list = element.getElementsByTagName(childName); - if (list.getLength() == 0) - { - return null; - } - return extractAttribute(list.item(0), attribute); - } - - private String extractAttribute(Node item, String name) - { - Node node = item.getAttributes().getNamedItem(name); - if (node != null) - { - return node.getTextContent(); - } - return null; - } - - private String extractNodeContent(Element element, String name) - { - NodeList list = element.getElementsByTagName(name); - if (list.getLength() == 0) - { - return null; - } - return list.item(0).getTextContent(); - } - - public static class MappingFile - { - private final List<EntityDescriptor> entities; - private final List<MappedSuperclassDescriptor> superClasses; - - public MappingFile(List<EntityDescriptor> entities, List<MappedSuperclassDescriptor> superClasses) - { - this.entities = entities; - this.superClasses = superClasses; - } - - public List<EntityDescriptor> getEntities() - { - return entities; - } - - public List<MappedSuperclassDescriptor> getSuperClasses() - { - return superClasses; - } - } - - - private abstract class PersistenceBuilder<T extends PersistentClassDescriptor> - { - protected List<T> result; - protected String packageName; - protected String name; - protected String className; - protected String idClass; - protected String id; - protected String version; - protected String embeddedId; - - public List<T> build(Document doc) - { - this.result = new LinkedList<T>(); - this.packageName = extractNodeContent(doc.getDocumentElement(), "package"); - NodeList mappings = doc.getElementsByTagName(tagName()); - for (int i = 0; i < mappings.getLength(); i++) - { - this.name = extractAttribute(mappings.item(i), "name"); - this.className = extractAttribute(mappings.item(i), "class"); - this.idClass = extractNodeAttribute((Element) mappings.item(i), "id-class", "class"); - this.id = extractNodeAttribute((Element) mappings.item(i), "id", "name"); - this.version = extractNodeAttribute((Element) mappings.item(i), "version", "name"); - this.embeddedId = extractNodeAttribute((Element) mappings.item(i), "embedded-id", "name"); - addFields((Element) mappings.item(i)); - addInResult(); - } - return this.result; - } - - protected abstract String tagName(); - - protected abstract void addInResult(); - - protected abstract void addFields(Element element); - } - - private abstract class MappedSuperClassBuilder<T extends PersistentClassDescriptor> extends PersistenceBuilder<T> - { - protected abstract T instance(String name, String packageName, String className, String idClass, String id, - String version); - - protected abstract String tagName(); - - @Override - protected void addInResult() - { - result.add(instance(name, packageName, className, idClass, id != null ? id : embeddedId, version)); - } - - @Override - protected void addFields(Element element) - { - // do nothing; - } - } - - private abstract class EntityBuilder<T extends PersistentClassDescriptor> extends PersistenceBuilder<T> - { - - protected String tableName; - - protected abstract T instance(String name, String packageName, String className, String idClass, String id, - String version, String tableName); - - protected abstract String tagName(); - - @Override - protected void addInResult() - { - result.add(instance(name, packageName, className, idClass, id != null ? id : embeddedId, - version, tableName)); - } - - @Override - protected void addFields(Element element) - { - this.tableName = extractNodeAttribute(element, "table", "name"); - } - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/MappedSuperclassDescriptor.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/MappedSuperclassDescriptor.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/MappedSuperclassDescriptor.java deleted file mode 100644 index 58d752b..0000000 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/MappedSuperclassDescriptor.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.deltaspike.data.impl.meta.unit; - -import java.io.Serializable; -import org.apache.deltaspike.core.util.StringUtils; - -class MappedSuperclassDescriptor extends PersistentClassDescriptor -{ - - MappedSuperclassDescriptor(String name, String packageName, String className, String idClass, String id, - String version) - { - super(name, packageName, className, idClass, id, version); - } - - @Override - public Class<? extends Serializable> getIdClass() - { - if (idClass == null && getParent() != null) - { - return getParent().getIdClass(); - } - return super.getIdClass(); - } - - @Override - public String getId() - { - if (StringUtils.isEmpty(id) && getParent() != null) - { - return getParent().getId(); - } - return super.getId(); - } - - @Override - public String toString() - { - StringBuilder builder = new StringBuilder(); - builder.append("MappedSuperclassDescriptor ") - .append("[entityClass=").append(className(entityClass)) - .append(", name=").append(name) - .append(", idClass=").append(className(idClass)) - .append(", id=").append(id) - .append(", parent=").append(getParent()) - .append("]"); - return builder.toString(); - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnit.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnit.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnit.java deleted file mode 100644 index e238385..0000000 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnit.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * 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.deltaspike.data.impl.meta.unit; - -import java.util.List; -import java.util.Map; - -public class PersistenceUnit -{ - - public static final String RESOURCE_PATH = "META-INF/persistence.xml"; - public static final String DEFAULT_ORM_PATH = "META-INF/orm.xml"; - - private final String unitName; - private final List<EntityDescriptor> entities; - private final Map<String, String> properties; - - PersistenceUnit(String unitName, List<EntityDescriptor> entities, Map<String, String> properties) - { - this.unitName = unitName; - this.entities = entities; - this.properties = properties; - } - - public EntityDescriptor find(Class<?> entityClass) - { - for (EntityDescriptor entity : entities) - { - if (entity.is(entityClass)) - { - return entity; - } - } - return null; - } - - public String getUnitName() - { - return unitName; - } - - public Map<String, String> getProperties() - { - return properties; - } - - @Override - public String toString() - { - StringBuilder builder = new StringBuilder(); - builder.append("PersistenceUnit [unitName=").append(unitName) - .append(", entities=").append(entities).append("]"); - return builder.toString(); - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitReader.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitReader.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitReader.java deleted file mode 100644 index ed30e32..0000000 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitReader.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * 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.deltaspike.data.impl.meta.unit; - -import org.apache.deltaspike.data.impl.meta.unit.EntityDescriptorReader.MappingFile; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -import java.io.IOException; -import java.util.Collections; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public class PersistenceUnitReader extends DescriptorReader -{ - - public List<PersistenceUnit> readAll() throws IOException - { - List<PersistenceUnit> result = new LinkedList<PersistenceUnit>(); - List<Descriptor> persistenceXmls = readAllFromClassPath(PersistenceUnit.RESOURCE_PATH); - for (Descriptor desc : persistenceXmls) - { - result.addAll(lookupUnits(desc)); - } - return Collections.unmodifiableList(result); - } - - private List<PersistenceUnit> lookupUnits(Descriptor descriptor) - { - List<PersistenceUnit> result = new LinkedList<PersistenceUnit>(); - NodeList list = descriptor.getDocument().getDocumentElement().getElementsByTagName("persistence-unit"); - for (int i = 0; i < list.getLength(); i++) - { - Node node = list.item(i); - String unitName = extractUnitName(node); - String baseUrl = extractBaseUrl(descriptor.getUrl(), PersistenceUnit.RESOURCE_PATH); - List<EntityDescriptor> entities = extractMappings((Element) node, baseUrl, unitName); - Map<String, String> properties = extractProperties((Element) node); - result.add(new PersistenceUnit(unitName, entities, properties)); - } - return result; - } - - private List<EntityDescriptor> extractMappings(Element element, String baseUrl, String unitName) - { - try - { - EntityDescriptorReader reader = new EntityDescriptorReader(); - List<EntityDescriptor> entities = new LinkedList<EntityDescriptor>(); - List<MappedSuperclassDescriptor> superClasses = new LinkedList<MappedSuperclassDescriptor>(); - NodeList list = element.getElementsByTagName("mapping-file"); - readMappingFiles(baseUrl, unitName, reader, entities, superClasses, list); - MappingFile mappings = reader.readDefaultOrm(baseUrl); - entities.addAll(mappings.getEntities()); - superClasses.addAll(mappings.getSuperClasses()); - DescriptorHierarchyBuilder.newInstance(entities, superClasses).buildHierarchy(); - return entities; - } - catch (Exception e) - { - throw new RuntimeException("Failed initializing mapping files", e); - } - } - - private void readMappingFiles(String baseUrl, String unitName, EntityDescriptorReader reader, - List<EntityDescriptor> entities, List<MappedSuperclassDescriptor> superClasses, - NodeList list) - { - for (int i = 0; i < list.getLength(); i++) - { - String resource = list.item(i).getTextContent(); - try - { - MappingFile mappings = reader.readAll(baseUrl, resource); - entities.addAll(mappings.getEntities()); - superClasses.addAll(mappings.getSuperClasses()); - } - catch (Exception e) - { - throw new RuntimeException("[PersistenceUnit: " + unitName + "] " + - "Unable to resolve named mapping-file [" + resource + "]"); - } - } - } - - private String extractUnitName(Node node) - { - return node.getAttributes().getNamedItem("name").getTextContent(); - } - - private Map<String, String> extractProperties(Element element) - { - Map<String, String> propertiesMap = new HashMap<String, String>(); - - Node propertiesNode = element.getElementsByTagName("properties").item(0); - if (propertiesNode != null) - { - NodeList propertyNodes = propertiesNode.getChildNodes(); - for (int i = 0; i < propertyNodes.getLength(); i++) - { - if ("property".equals(propertyNodes.item(i).getNodeName())) - { - Element propertyNode = (Element) propertyNodes.item(i); - propertiesMap.put(propertyNode.getAttribute("name"), propertyNode.getAttribute("value")); - } - } - } - - return Collections.unmodifiableMap(propertiesMap); - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnits.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnits.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnits.java deleted file mode 100644 index 9bbb0e7..0000000 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnits.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * 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.deltaspike.data.impl.meta.unit; - -import java.io.IOException; -import java.util.Collections; -import java.util.List; - -import org.apache.deltaspike.data.impl.meta.RepositoryEntity; - -public final class PersistenceUnits -{ - - private static PersistenceUnits instance = new PersistenceUnits(); - - private List<PersistenceUnit> persistenceUnits = Collections.emptyList(); - - private PersistenceUnits() - { - } - - public static PersistenceUnits instance() - { - return instance; - } - - public void init() - { - persistenceUnits = readPersistenceXmls(); - } - - public PersistenceUnit get(String name) - { - for (PersistenceUnit unit : persistenceUnits) - { - if (name.equalsIgnoreCase(unit.getUnitName())) - { - return unit; - } - } - return null; - } - - public boolean isEntity(Class<?> entityClass) - { - return find(entityClass) != null; - } - - public String primaryKeyField(Class<?> entityClass) - { - EntityDescriptor entity = find(entityClass); - if (entity != null) - { - return entity.getId(); - } - return null; - } - - public String versionField(Class<?> entityClass) - { - EntityDescriptor entity = find(entityClass); - if (entity != null) - { - return entity.getVersion(); - } - return null; - } - - public Class<?> primaryKeyIdClass(Class<?> entityClass) - { - EntityDescriptor entity = find(entityClass); - if (entity != null && entity.getIdClass() != null) - { - return entity.getIdClass(); - } - return null; - } - - public String entityName(Class<?> entityClass) - { - EntityDescriptor entity = find(entityClass); - if (entity != null) - { - return entity.getName(); - } - return null; - } - - public RepositoryEntity lookupMetadata(Class<?> entityClass) - { - EntityDescriptor entity = find(entityClass); - if (entity != null) - { - return new RepositoryEntity(entityClass, entity.getIdClass()); - } - return null; - } - - private List<PersistenceUnit> readPersistenceXmls() - { - try - { - PersistenceUnitReader reader = new PersistenceUnitReader(); - return reader.readAll(); - } - catch (IOException e) - { - throw new RuntimeException("Failed to read persistence unit info", e); - } - } - - private EntityDescriptor find(Class<?> entityClass) - { - for (PersistenceUnit unit : persistenceUnits) - { - EntityDescriptor entity = unit.find(entityClass); - if (entity != null) - { - return entity; - } - } - return null; - } - - public String entityTableName(Class<?> entityClass) - { - EntityDescriptor entity = find(entityClass); - if (entity != null) - { - return entity.getTableName(); - } - return null; - } -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistentClassDescriptor.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistentClassDescriptor.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistentClassDescriptor.java deleted file mode 100644 index 6c45b4f..0000000 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/unit/PersistentClassDescriptor.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * 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.deltaspike.data.impl.meta.unit; - -import java.io.Serializable; - -import org.apache.deltaspike.data.impl.property.query.NamedPropertyCriteria; -import org.apache.deltaspike.data.impl.property.query.PropertyQueries; -import org.apache.deltaspike.data.impl.property.query.PropertyQuery; - -abstract class PersistentClassDescriptor -{ - - protected final String name; - protected final Class<?> entityClass; - protected final Class<? extends Serializable> idClass; - protected final String id; - protected final String version; - private PersistentClassDescriptor parent; - - PersistentClassDescriptor(String name, String packageName, String className, String idClass, String id, - String version) - { - Class<?> clazz = entityClass(className, packageName); - this.name = name; - this.entityClass = clazz; - this.idClass = idClass(clazz, idClass, packageName, id); - this.id = id; - this.version = version; - } - - public Class<? extends Serializable> getIdClass() - { - return idClass; - } - - public String getId() - { - return id; - } - - public String getVersion() - { - return version; - } - - public String getName() - { - return name; - } - - public Class<?> getEntityClass() - { - return entityClass; - } - - String className(Class<?> clazz) - { - return clazz == null ? null : clazz.getSimpleName(); - } - - private Class<?> entityClass(String entityClass, String packageName) - { - try - { - String clazzName = buildClassName(entityClass, packageName); - return Class.forName(clazzName); - } - catch (ClassNotFoundException e) - { - throw new IllegalArgumentException("Can't create class " + buildClassName(entityClass, packageName), e); - } - } - - @SuppressWarnings("unchecked") - private Class<? extends Serializable> idClass(Class<?> entity, String idClass, String packageName, String id) - { - try - { - return (Class<? extends Serializable>) (idClass != null ? Class - .forName(buildClassName(idClass, packageName)) : lookupIdClass(entity, id)); - } - catch (ClassNotFoundException e) - { - throw new IllegalArgumentException("Failed to get ID class", e); - } - } - - private Class<?> lookupIdClass(Class<?> entity, String id) - { - if (entity == null || id == null) - { - return null; - } - PropertyQuery<Serializable> query = PropertyQueries.<Serializable> createQuery(entity) - .addCriteria(new NamedPropertyCriteria(id)); - return query.getFirstResult().getJavaClass(); - } - - private String buildClassName(String clazzName, String packageName) - { - if (clazzName == null && packageName == null) - { - return null; - } - return (packageName != null && !isClassNameQualified(clazzName)) ? packageName + "." + clazzName : clazzName; - } - - private boolean isClassNameQualified(String name) - { - return name.contains("."); - } - - public PersistentClassDescriptor getParent() - { - return parent; - } - - public void setParent(PersistentClassDescriptor parent) - { - this.parent = parent; - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/verifier/EntityVerifier.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/verifier/EntityVerifier.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/verifier/EntityVerifier.java index e37d1a8..ada5776 100644 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/verifier/EntityVerifier.java +++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/meta/verifier/EntityVerifier.java @@ -19,8 +19,7 @@ package org.apache.deltaspike.data.impl.meta.verifier; import javax.persistence.Entity; - -import org.apache.deltaspike.data.impl.meta.unit.PersistenceUnits; +import org.apache.deltaspike.jpa.spi.descriptor.xml.PersistenceUnitDescriptorProvider; public class EntityVerifier implements Verifier<Class<?>> { @@ -28,7 +27,8 @@ public class EntityVerifier implements Verifier<Class<?>> @Override public boolean verify(Class<?> entity) { - return entity.isAnnotationPresent(Entity.class) || PersistenceUnits.instance().isEntity(entity); + return entity.isAnnotationPresent(Entity.class) + || PersistenceUnitDescriptorProvider.getInstance().isEntity(entity); } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/EntityUtils.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/EntityUtils.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/EntityUtils.java index ec38e34..736452f 100755 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/EntityUtils.java +++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/EntityUtils.java @@ -32,7 +32,6 @@ import javax.persistence.Version; import javax.persistence.metamodel.EntityType; import org.apache.deltaspike.core.util.StringUtils; -import org.apache.deltaspike.data.impl.meta.unit.PersistenceUnits; import org.apache.deltaspike.data.impl.meta.verifier.EntityVerifier; import org.apache.deltaspike.data.impl.property.Property; import org.apache.deltaspike.data.impl.property.query.AnnotatedPropertyCriteria; @@ -40,6 +39,7 @@ import org.apache.deltaspike.data.impl.property.query.NamedPropertyCriteria; import org.apache.deltaspike.data.impl.property.query.PropertyCriteria; import org.apache.deltaspike.data.impl.property.query.PropertyQueries; import org.apache.deltaspike.data.impl.property.query.PropertyQuery; +import org.apache.deltaspike.jpa.spi.descriptor.xml.PersistenceUnitDescriptorProvider; public final class EntityUtils { @@ -56,7 +56,7 @@ public final class EntityUtils return entityClass.getAnnotation(IdClass.class).value(); // Serializablity isn't required, could cause // problems } - Class clazz = PersistenceUnits.instance().primaryKeyIdClass(entityClass); + Class clazz = PersistenceUnitDescriptorProvider.getInstance().primaryKeyIdClass(entityClass); if (clazz != null) { return clazz; @@ -85,14 +85,14 @@ public final class EntityUtils } else { - result = PersistenceUnits.instance().entityName(entityClass); + result = PersistenceUnitDescriptorProvider.getInstance().entityName(entityClass); } return (result != null && !"".equals(result)) ? result : entityClass.getSimpleName(); } public static String tableName(Class<?> entityClass, EntityManager entityManager) { - String tableName = PersistenceUnits.instance().entityTableName(entityClass); + String tableName = PersistenceUnitDescriptorProvider.getInstance().entityTableName(entityClass); if (StringUtils.isEmpty(tableName)) { EntityType<?> entityType = entityManager.getMetamodel().entity(entityClass); @@ -109,7 +109,7 @@ public final class EntityUtils public static Property<Serializable> primaryKeyProperty(Class<?> entityClass) { - for (PropertyCriteria c : criteriaList(entityClass)) + for (PropertyCriteria c : primaryKeyPropertyCriteriaList(entityClass)) { PropertyQuery<Serializable> query = PropertyQueries.<Serializable> createQuery(entityClass) .addCriteria(c); @@ -121,15 +121,18 @@ public final class EntityUtils throw new IllegalStateException("Class " + entityClass + " has no id defined"); } - private static List<PropertyCriteria> criteriaList(Class<?> entityClass) + private static List<PropertyCriteria> primaryKeyPropertyCriteriaList(Class<?> entityClass) { List<PropertyCriteria> criteria = new LinkedList<PropertyCriteria>(); criteria.add(new AnnotatedPropertyCriteria(Id.class)); criteria.add(new AnnotatedPropertyCriteria(EmbeddedId.class)); - String fromMappingFiles = PersistenceUnits.instance().primaryKeyField(entityClass); + String[] fromMappingFiles = PersistenceUnitDescriptorProvider.getInstance().primaryKeyFields(entityClass); if (fromMappingFiles != null) { - criteria.add(new NamedPropertyCriteria(fromMappingFiles)); + for (String id : fromMappingFiles) + { + criteria.add(new NamedPropertyCriteria(id)); + } } return criteria; } @@ -139,7 +142,7 @@ public final class EntityUtils List<PropertyCriteria> criteriaList = new LinkedList<PropertyCriteria>(); criteriaList.add(new AnnotatedPropertyCriteria(Version.class)); - String fromMappingFiles = PersistenceUnits.instance().versionField(entityClass); + String fromMappingFiles = PersistenceUnitDescriptorProvider.getInstance().versionField(entityClass); if (fromMappingFiles != null) { criteriaList.add(new NamedPropertyCriteria(fromMappingFiles)); http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/cl/AggregatedClassLoader.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/cl/AggregatedClassLoader.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/cl/AggregatedClassLoader.java deleted file mode 100644 index 65864c9..0000000 --- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/cl/AggregatedClassLoader.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * 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.deltaspike.data.impl.util.cl; - -import java.io.IOException; -import java.net.URL; -import java.util.Arrays; -import java.util.Enumeration; -import java.util.Iterator; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; - -public class AggregatedClassLoader extends ClassLoader -{ - - private final List<ClassLoader> classLoaders; - - public AggregatedClassLoader(List<ClassLoader> classLoaders) - { - super(); - this.classLoaders = classLoaders; - } - - public static AggregatedClassLoader newInstance() - { - return new AggregatedClassLoader(Arrays.asList( - AggregatedClassLoader.class.getClassLoader(), - Thread.currentThread().getContextClassLoader(), - ClassLoader.getSystemClassLoader())); - } - - @Override - public URL getResource(String name) - { - for (ClassLoader loader : classLoaders) - { - URL url = loader.getResource(name); - if (url != null) - { - return url; - } - } - return super.getResource(name); - } - - @Override - public Enumeration<URL> getResources(String name) throws IOException - { - final Set<URL> result = new LinkedHashSet<URL>(); - for (ClassLoader loader : classLoaders) - { - Enumeration<URL> urls = loader.getResources(name); - while (urls.hasMoreElements()) - { - result.add(urls.nextElement()); - } - } - return new Enumeration<URL>() - { - private final Iterator<URL> iterator = result.iterator(); - - @Override - public URL nextElement() - { - return iterator.next(); - } - - @Override - public boolean hasMoreElements() - { - return iterator.hasNext(); - } - }; - } - -} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilderTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilderTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilderTest.java index 4cf9a43..d050589 100644 --- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilderTest.java +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/DescriptorHierarchyBuilderTest.java @@ -23,10 +23,10 @@ import static org.junit.Assert.assertNull; import java.util.LinkedList; import java.util.List; +import org.apache.deltaspike.jpa.spi.descriptor.xml.AbstractEntityHierarchyBuilder; +import org.apache.deltaspike.jpa.spi.descriptor.xml.EntityDescriptor; +import org.apache.deltaspike.jpa.spi.descriptor.xml.MappedSuperclassDescriptor; -import org.apache.deltaspike.data.impl.meta.unit.DescriptorHierarchyBuilder; -import org.apache.deltaspike.data.impl.meta.unit.EntityDescriptor; -import org.apache.deltaspike.data.impl.meta.unit.MappedSuperclassDescriptor; import org.junit.Before; import org.junit.Test; @@ -39,23 +39,21 @@ public class DescriptorHierarchyBuilderTest @Before public void before() { - entities.add(new EntityDescriptor("test", null, EntityLevel3.class.getName(), null, null, null, null)); - entities.add(new EntityDescriptor("test", null, EntityLevel5.class.getName(), null, null, null, null)); - - superClasses.add(new MappedSuperclassDescriptor("test", null, MappedLevel1.class.getName(), null, "id", null)); - superClasses.add(new MappedSuperclassDescriptor("test", null, MappedLevel4.class.getName(), null, null, null)); - superClasses.add(new MappedSuperclassDescriptor("test", null, MappedUnrelated.class.getName(), null, null, null)); - superClasses.add(new MappedSuperclassDescriptor("test", null, MappedLevel2.class.getName(), null, null, null)); + entities.add(new EntityDescriptor(null, null, "test", EntityLevel3.class, null, null, null)); + entities.add(new EntityDescriptor(null, null, "test", EntityLevel5.class, null, null, null)); + + superClasses.add(new MappedSuperclassDescriptor(new String[] { "id" }, null, "test", MappedLevel1.class, + null, null)); + superClasses.add(new MappedSuperclassDescriptor(null, null, "test", MappedLevel4.class, null, null)); + superClasses.add(new MappedSuperclassDescriptor(null, null, "test", MappedUnrelated.class, null, null)); + superClasses.add(new MappedSuperclassDescriptor(null, null, "test", MappedLevel2.class, null, null)); } @Test public void should_build_hierarchy() { - // given - DescriptorHierarchyBuilder builder = DescriptorHierarchyBuilder.newInstance(entities, superClasses); - // when - builder.buildHierarchy(); + AbstractEntityHierarchyBuilder.buildHierarchy(entities, superClasses); // then assertEquals(entities.size(), 2); http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitsTest.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitsTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitsTest.java index b9bfb88..33fe4bd 100644 --- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitsTest.java +++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/meta/unit/PersistenceUnitsTest.java @@ -20,10 +20,12 @@ package org.apache.deltaspike.data.impl.meta.unit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import org.apache.deltaspike.data.impl.meta.RepositoryEntity; +import org.apache.deltaspike.data.impl.util.EntityUtils; import org.apache.deltaspike.data.test.domain.Parent; import org.apache.deltaspike.data.test.domain.TeeId; import org.apache.deltaspike.data.test.domain.mapped.MappedOne; @@ -31,6 +33,8 @@ import org.apache.deltaspike.data.test.domain.mapped.MappedThree; import org.apache.deltaspike.data.test.domain.mapped.MappedTwo; import org.apache.deltaspike.data.test.service.MappedOneRepository; import org.apache.deltaspike.data.test.util.TestDeployments; +import org.apache.deltaspike.jpa.spi.descriptor.xml.EntityDescriptor; +import org.apache.deltaspike.jpa.spi.descriptor.xml.PersistenceUnitDescriptorProvider; import org.apache.deltaspike.test.category.WebProfileCategory; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; @@ -43,8 +47,7 @@ import org.junit.runner.RunWith; @RunWith(Arquillian.class) @Category(WebProfileCategory.class) public class PersistenceUnitsTest -{ - +{ @Deployment public static Archive<?> deployment() { @@ -66,10 +69,10 @@ public class PersistenceUnitsTest // given // when - boolean positive1 = PersistenceUnits.instance().isEntity(MappedOne.class); - boolean positive2 = PersistenceUnits.instance().isEntity(MappedTwo.class); - boolean positive3 = PersistenceUnits.instance().isEntity(MappedThree.class); - boolean negative = PersistenceUnits.instance().isEntity(Long.class); + boolean positive1 = PersistenceUnitDescriptorProvider.getInstance().isEntity(MappedOne.class); + boolean positive2 = PersistenceUnitDescriptorProvider.getInstance().isEntity(MappedTwo.class); + boolean positive3 = PersistenceUnitDescriptorProvider.getInstance().isEntity(MappedThree.class); + boolean negative = PersistenceUnitDescriptorProvider.getInstance().isEntity(Long.class); // then assertTrue(positive1); @@ -84,12 +87,14 @@ public class PersistenceUnitsTest // given // when - String idField1 = PersistenceUnits.instance().primaryKeyField(MappedOne.class); - String idField2 = PersistenceUnits.instance().primaryKeyField(MappedThree.class); + String[] idField1 = PersistenceUnitDescriptorProvider.getInstance().primaryKeyFields(MappedOne.class); + String[] idField2 = PersistenceUnitDescriptorProvider.getInstance().primaryKeyFields(MappedThree.class); // then - assertEquals("id", idField1); - assertEquals("id", idField2); + assertEquals(1, idField1.length); + assertEquals("id", idField1[0]); + assertEquals(1, idField2.length); + assertEquals("id", idField2[0]); } @Test @@ -98,7 +103,7 @@ public class PersistenceUnitsTest // given // when - String name = PersistenceUnits.instance().entityName(MappedOne.class); + String name = PersistenceUnitDescriptorProvider.getInstance().entityName(MappedOne.class); // then assertEquals("Mapped_One", name); @@ -110,7 +115,7 @@ public class PersistenceUnitsTest // given // when - Class<?> idClass = PersistenceUnits.instance().primaryKeyIdClass(MappedTwo.class); + Class<?> idClass = PersistenceUnitDescriptorProvider.getInstance().primaryKeyIdClass(MappedTwo.class); // then assertEquals(TeeId.class, idClass); @@ -122,9 +127,9 @@ public class PersistenceUnitsTest // given // when - RepositoryEntity entity1 = PersistenceUnits.instance().lookupMetadata(MappedOne.class); - RepositoryEntity entity2 = PersistenceUnits.instance().lookupMetadata(MappedTwo.class); - RepositoryEntity entity3 = PersistenceUnits.instance().lookupMetadata(MappedThree.class); + RepositoryEntity entity1 = lookupMetadata(MappedOne.class); + RepositoryEntity entity2 = lookupMetadata(MappedTwo.class); + RepositoryEntity entity3 = lookupMetadata(MappedThree.class); // then assertNotNull(entity1); @@ -134,4 +139,13 @@ public class PersistenceUnitsTest assertEquals(Long.class, entity3.getPrimaryKeyClass()); } + protected RepositoryEntity lookupMetadata(Class<?> entityClass) + { + EntityDescriptor entity = PersistenceUnitDescriptorProvider.getInstance().find(entityClass); + if (entity != null) + { + return new RepositoryEntity(entityClass, EntityUtils.primaryKeyClass(entityClass)); + } + return null; + } } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/AbstractEntityDescriptor.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/AbstractEntityDescriptor.java b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/AbstractEntityDescriptor.java new file mode 100644 index 0000000..776943d --- /dev/null +++ b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/AbstractEntityDescriptor.java @@ -0,0 +1,108 @@ +/* + * 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.deltaspike.jpa.spi.descriptor.xml; + +import java.io.Serializable; + +public abstract class AbstractEntityDescriptor +{ + private String id[]; + private String version; + private String name; + private Class<?> entityClass; + private Class<? extends Serializable> idClass; + + private AbstractEntityDescriptor parent; + + public AbstractEntityDescriptor() + { + + } + + public AbstractEntityDescriptor(String[] id, String version, String name, Class<?> entityClass, + Class<? extends Serializable> idClass, AbstractEntityDescriptor parent) + { + this.id = id; + this.version = version; + this.name = name; + this.entityClass = entityClass; + this.idClass = idClass; + this.parent = parent; + } + + public String[] getId() + { + return id; + } + + public void setId(String[] id) + { + this.id = id; + } + + public String getVersion() + { + return version; + } + + public void setVersion(String version) + { + this.version = version; + } + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + public Class<?> getEntityClass() + { + return entityClass; + } + + public void setEntityClass(Class<?> entityClass) + { + this.entityClass = entityClass; + } + + public Class<? extends Serializable> getIdClass() + { + return idClass; + } + + public void setIdClass(Class<? extends Serializable> idClass) + { + this.idClass = idClass; + } + + public AbstractEntityDescriptor getParent() + { + return parent; + } + + public void setParent(AbstractEntityDescriptor parent) + { + this.parent = parent; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/AbstractEntityHierarchyBuilder.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/AbstractEntityHierarchyBuilder.java b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/AbstractEntityHierarchyBuilder.java new file mode 100644 index 0000000..3ad255e --- /dev/null +++ b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/AbstractEntityHierarchyBuilder.java @@ -0,0 +1,81 @@ +/* + * 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.deltaspike.jpa.spi.descriptor.xml; + +import java.util.List; + +public final class AbstractEntityHierarchyBuilder +{ + private AbstractEntityHierarchyBuilder() + { + } + + public static void buildHierarchy(List<EntityDescriptor> entities, List<MappedSuperclassDescriptor> superClasses) + { + for (EntityDescriptor descriptor : entities) + { + buildHierarchy(descriptor, entities, superClasses); + } + } + + protected static void buildHierarchy(AbstractEntityDescriptor descriptor, + List<EntityDescriptor> entities, List<MappedSuperclassDescriptor> superClasses) + { + Class<?> superClass = descriptor.getEntityClass().getSuperclass(); + while (superClass != null) + { + AbstractEntityDescriptor superDescriptor = + findPersistentClassDescriptor(superClass, entities, superClasses); + if (superDescriptor != null) + { + if (descriptor.getParent() == null) + { + buildHierarchy(superDescriptor, entities, superClasses); + } + + descriptor.setParent(superDescriptor); + return; + } + + superClass = superClass.getSuperclass(); + } + } + + protected static AbstractEntityDescriptor findPersistentClassDescriptor(Class<?> superClass, + List<EntityDescriptor> entities, List<MappedSuperclassDescriptor> superClasses) + { + for (MappedSuperclassDescriptor descriptor : superClasses) + { + if (descriptor.getEntityClass().equals(superClass)) + { + return descriptor; + } + } + + for (EntityDescriptor descriptor : entities) + { + if (descriptor.getEntityClass().equals(superClass)) + { + return descriptor; + } + } + + return null; + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/Descriptor.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/Descriptor.java b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/Descriptor.java new file mode 100644 index 0000000..177adfe --- /dev/null +++ b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/Descriptor.java @@ -0,0 +1,46 @@ +/* + * 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.deltaspike.jpa.spi.descriptor.xml; + +import java.net.URL; + +import org.w3c.dom.Document; + +public class Descriptor +{ + private final Document document; + private final URL url; + + public Descriptor(Document document, URL url) + { + this.document = document; + this.url = url; + } + + public Document getDocument() + { + return document; + } + + public URL getUrl() + { + return url; + } + +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/DescriptorReader.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/DescriptorReader.java b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/DescriptorReader.java new file mode 100644 index 0000000..68d89ac --- /dev/null +++ b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/DescriptorReader.java @@ -0,0 +1,114 @@ +/* + * 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.deltaspike.jpa.spi.descriptor.xml; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Collections; +import java.util.Enumeration; +import java.util.LinkedList; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.apache.deltaspike.core.util.AggregatedClassLoader; + +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +abstract class DescriptorReader +{ + private static final Logger LOG = Logger.getLogger(DescriptorReader.class.getName()); + + private final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + + protected List<Descriptor> readAllFromClassPath(String resource) throws IOException + { + List<Descriptor> result = new LinkedList<Descriptor>(); + Enumeration<URL> urls = classLoader().getResources(resource); + while (urls.hasMoreElements()) + { + URL u = urls.nextElement(); + try + { + result.add(readFromUrl(u)); + } + catch (Exception e) + { + LOG.log(Level.WARNING, "Could not load " + resource + " from " + u, e); + } + } + return Collections.unmodifiableList(result); + } + + protected Descriptor readFromClassPath(String resource) throws IOException + { + return readFromUrl(classLoader().getResource(resource)); + } + + protected Descriptor readFromUrl(URL url) throws IOException + { + InputStream stream = url.openStream(); + try + { + DocumentBuilder builder = factory.newDocumentBuilder(); + return new Descriptor(builder.parse(new InputSource(stream)), url); + } + catch (SAXException e) + { + throw new RuntimeException("Failed reading XML document", e); + } + catch (ParserConfigurationException e) + { + throw new RuntimeException("Failed reading XML document", e); + } + finally + { + stream.close(); + } + } + + protected Descriptor read(String baseUrl, String resource) throws IOException + { + try + { + URL url = new URL(baseUrl + resource); + return readFromUrl(url); + } + catch (Exception e) + { + return readFromClassPath(resource); + } + } + + protected String extractBaseUrl(URL fileUrl, String resource) + { + String file = fileUrl.toString(); + return file.substring(0, file.length() - resource.length()); + } + + protected ClassLoader classLoader() + { + return AggregatedClassLoader.newInstance(); + } +} http://git-wip-us.apache.org/repos/asf/deltaspike/blob/6480a0e5/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/EntityDescriptor.java ---------------------------------------------------------------------- diff --git a/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/EntityDescriptor.java b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/EntityDescriptor.java new file mode 100644 index 0000000..f8bf884 --- /dev/null +++ b/deltaspike/modules/jpa/api/src/main/java/org/apache/deltaspike/jpa/spi/descriptor/xml/EntityDescriptor.java @@ -0,0 +1,63 @@ +/* + * 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.deltaspike.jpa.spi.descriptor.xml; + +import java.io.Serializable; + +public class EntityDescriptor extends AbstractEntityDescriptor +{ + private String tableName; + + public EntityDescriptor() + { + } + + public EntityDescriptor(String[] id, String version, String name, Class<?> entityClass, + Class<? extends Serializable> idClass, AbstractEntityDescriptor parent, + String tableName) + { + super(id, version, name, entityClass, idClass, parent); + this.tableName = tableName; + } + + public String getTableName() + { + return tableName; + } + + public void setTableName(String tableName) + { + this.tableName = tableName; + } + + @Override + public String toString() + { + StringBuilder builder = new StringBuilder(); + builder.append("EntityDescriptor ") + .append("[entityClass=").append(getEntityClass().getName()) + .append(", name=").append(getName()) + .append(", idClass=").append(getIdClass().getName()) + .append(", id=").append(getId()) + .append(", superClass=").append(getParent()) + .append(", tableName=").append(tableName) + .append("]"); + return builder.toString(); + } +}
