From bdd0a41aed7edf21ec2a65cfa17a86af2ef8c48a Mon Sep 17 00:00:00 2001 From: dims Date: Tue, 17 Jun 2008 00:23:01 +0000 Subject: Move Tuscany from Incubator to top level. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@668359 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tuscany/service/openjpa/Constants.java | 35 +++++++++ .../EntityManagerFactoryImplementation.java | 38 ++++++++++ ...EntityManagerFactoryNotConfiguredException.java | 31 ++++++++ .../openjpa/EntityManagerObjectFactory.java | 54 +++++++++++++ .../service/openjpa/EntityManagerProcessor.java | 88 ++++++++++++++++++++++ .../service/openjpa/InvalidInjectionSite.java | 35 +++++++++ .../service/openjpa/JPAAtomicComponent.java | 70 +++++++++++++++++ .../service/openjpa/JPAComponentBuilder.java | 55 ++++++++++++++ .../tuscany/service/openjpa/JPAImportLoader.java | 56 ++++++++++++++ 9 files changed, 462 insertions(+) create mode 100644 sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/Constants.java create mode 100644 sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerFactoryImplementation.java create mode 100644 sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerFactoryNotConfiguredException.java create mode 100644 sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerObjectFactory.java create mode 100644 sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerProcessor.java create mode 100644 sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/InvalidInjectionSite.java create mode 100644 sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAAtomicComponent.java create mode 100644 sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAComponentBuilder.java create mode 100644 sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAImportLoader.java (limited to 'sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany') diff --git a/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/Constants.java b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/Constants.java new file mode 100644 index 0000000000..820f57bcad --- /dev/null +++ b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/Constants.java @@ -0,0 +1,35 @@ +/* + * 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.openjpa; + +/** + * Constants used by the JPA extension + * + * @version $Rev$ $Date$ + */ +public final class Constants { + + public static final String PERSISTENCE_UNIT = "persistenceUnit"; + public static final String CONFIG_PROPERTIES = "configProperties"; + + private Constants() { + } + + +} diff --git a/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerFactoryImplementation.java b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerFactoryImplementation.java new file mode 100644 index 0000000000..d818f145bd --- /dev/null +++ b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerFactoryImplementation.java @@ -0,0 +1,38 @@ +/* + * 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.openjpa; + +import org.apache.tuscany.spi.model.AtomicImplementation; +import org.apache.tuscany.spi.model.ComponentType; + +/** + * Represents the implementation type of a system service that manages JPA + * EntityManagerFactoryImplementation implementations + * + * @version $Rev$ $Date$ + */ +public class EntityManagerFactoryImplementation extends AtomicImplementation { + + public EntityManagerFactoryImplementation() { + } + + public EntityManagerFactoryImplementation(ComponentType componentType) { + super(componentType); + } +} diff --git a/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerFactoryNotConfiguredException.java b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerFactoryNotConfiguredException.java new file mode 100644 index 0000000000..34e69b8951 --- /dev/null +++ b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerFactoryNotConfiguredException.java @@ -0,0 +1,31 @@ +/* + * 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.openjpa; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * @version $Rev$ $Date$ + */ +public class EntityManagerFactoryNotConfiguredException extends ProcessingException { + + public EntityManagerFactoryNotConfiguredException() { + super("Entity manager factory not configured"); + } +} diff --git a/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerObjectFactory.java b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerObjectFactory.java new file mode 100644 index 0000000000..45d4e1a077 --- /dev/null +++ b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerObjectFactory.java @@ -0,0 +1,54 @@ +/* + * 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.openjpa; + +import java.util.Map; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.ObjectFactory; + +/** + * Creates a JPA EntityManager to be injected on a component implementation instance. + * + * @version $Rev$ $Date$ + */ +public class EntityManagerObjectFactory implements ObjectFactory { + + private EntityManagerFactory emf; + private Map props; + + public EntityManagerObjectFactory(EntityManagerFactory emf) { + this.emf = emf; + } + + public EntityManagerObjectFactory(EntityManagerFactory emf, Map props) { + this.emf = emf; + this.props = props; + } + + public EntityManager getInstance() throws ObjectCreationException { + if (props == null) { + return emf.createEntityManager(); + } else { + return emf.createEntityManager(props); + } + } +} diff --git a/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerProcessor.java b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerProcessor.java new file mode 100644 index 0000000000..8046c0a7c0 --- /dev/null +++ b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/EntityManagerProcessor.java @@ -0,0 +1,88 @@ +/* + * 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.openjpa; + +import java.lang.reflect.Member; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.PersistenceContext; + +import org.apache.tuscany.spi.ObjectFactory; +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.implementation.java.AbstractPropertyProcessor; +import org.apache.tuscany.spi.implementation.java.ImplementationProcessorService; +import org.apache.tuscany.spi.implementation.java.JavaMappedProperty; +import org.apache.tuscany.spi.implementation.java.ProcessingException; +import org.apache.tuscany.spi.wire.InboundWire; + + +/** + * Evaluates constructors, methods, and fields annotated with {@link PersistenceContext }, creating a mapped property + * that will inject an {@link EntityManager} on an implementation instance. + * + * @version $Rev$ $Date$ + */ +public class EntityManagerProcessor extends AbstractPropertyProcessor { + + public EntityManagerProcessor(@Autowire ImplementationProcessorService service) { + super(PersistenceContext.class, service); + } + + protected String getName(PersistenceContext annotation) { + String name = annotation.unitName(); + if (name == null) { + return "_defaultJPAPersistenceUnit"; + } + return name; + } + + protected JavaMappedProperty createProperty(String name, + Class javaType, + Member member) throws ProcessingException { + if (!EntityManager.class.equals(javaType)) { + throw new InvalidInjectionSite("Injection site must by of type " + EntityManager.class.getName(), name); + } + return super.createProperty(name, javaType, member); + } + + @SuppressWarnings("unchecked") + protected void initProperty(JavaMappedProperty property, + PersistenceContext annotation, + CompositeComponent parent, + DeploymentContext context) throws ProcessingException { + EntityManagerFactory emf; + try { + InboundWire wire = parent.resolveSystemAutowire(EntityManagerFactory.class); + if (wire == null) { + throw new EntityManagerFactoryNotConfiguredException(); + } + Object o = wire.getTargetService(); + assert o instanceof EntityManagerFactory; + emf = (EntityManagerFactory) o; + } catch (TargetException e) { + throw new ProcessingException(e); + } + ObjectFactory factory = new EntityManagerObjectFactory(emf); + property.setDefaultValueFactory(factory); + } + +} diff --git a/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/InvalidInjectionSite.java b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/InvalidInjectionSite.java new file mode 100644 index 0000000000..0ac5e9ffa1 --- /dev/null +++ b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/InvalidInjectionSite.java @@ -0,0 +1,35 @@ +/* + * 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.openjpa; + +import org.apache.tuscany.spi.implementation.java.ProcessingException; + +/** + * @version $Rev$ $Date$ + */ +public class InvalidInjectionSite extends ProcessingException { + + public InvalidInjectionSite(String message) { + super(message); + } + + public InvalidInjectionSite(String message, String identifier) { + super(message, identifier); + } +} diff --git a/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAAtomicComponent.java b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAAtomicComponent.java new file mode 100644 index 0000000000..df74625b49 --- /dev/null +++ b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAAtomicComponent.java @@ -0,0 +1,70 @@ +/* + * 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.openjpa; + +import java.util.Map; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; + +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.extension.SystemAtomicComponentExtension; + +/** + * A system service that manages a JPA EntityManagerFactory. This service is used by {@link + * EntityManagerProcessor} to inject an EntityManager on a constructor parameter, field, or method of an + * implementation instance. Since a JPA persistence context is specific to an application, this system service is scoped + * to the application composite. That is, it is contained as a system child of the application composite. + * + * @version $Rev$ $Date$ + */ +public class JPAAtomicComponent extends SystemAtomicComponentExtension { + private EntityManagerFactory factory; + private String persistenceUnit; + private Map configProps; + + public JPAAtomicComponent(String name, + CompositeComponent parent, + ScopeContainer scopeContainer, + String persistenceUnit, + Map configProps, + int initLevel) { + super(name, parent, initLevel); + assert persistenceUnit != null; + this.persistenceUnit = persistenceUnit; + this.configProps = configProps; + } + + public Object createInstance() throws ObjectCreationException { + if (configProps != null) { + factory = Persistence.createEntityManagerFactory(persistenceUnit, configProps); + } else { + factory = Persistence.createEntityManagerFactory(persistenceUnit); + } + return factory; + } + + public Object getTargetInstance() throws TargetResolutionException { + return factory; + } + + +} diff --git a/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAComponentBuilder.java b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAComponentBuilder.java new file mode 100644 index 0000000000..d979b93ca2 --- /dev/null +++ b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAComponentBuilder.java @@ -0,0 +1,55 @@ +/* + * 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.openjpa; + +import org.apache.tuscany.spi.builder.BuilderConfigException; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.ComponentBuilderExtension; +import org.apache.tuscany.spi.model.ComponentDefinition; +import org.apache.tuscany.spi.model.ComponentType; +import org.apache.tuscany.spi.model.Property; + +import static org.apache.tuscany.service.openjpa.Constants.PERSISTENCE_UNIT; + +/** + * @version $Rev$ $Date$ + */ +public class JPAComponentBuilder extends ComponentBuilderExtension { + protected Class getImplementationType() { + return null; + } + + public Component build(CompositeComponent parent, ComponentDefinition definition, DeploymentContext ctx) + throws BuilderConfigException { +// String name = definition.getName(); +// ScopeContainer moduleScope = ctx.getCompositeScope(); +// Integer initLevel = definition.getInitLevel(); + ComponentType type = definition.getImplementation().getComponentType(); + Property persistenceUnit = type.getProperties().get(PERSISTENCE_UNIT); + if (persistenceUnit == null) { + throw new BuilderConfigException("Persistence Unit not specified in JPA import in composite", + parent.getName()); + } +// Property configProps = type.getProperties().get(PERSISTENCE_UNIT); + return null; +// return new JPAAtomicComponent(name, parent, moduleScope, initLevel); + } +} diff --git a/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAImportLoader.java b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAImportLoader.java new file mode 100644 index 0000000000..94508e5408 --- /dev/null +++ b/sandbox/old/contrib/persistence/openjpa/src/main/java/org/apache/tuscany/service/openjpa/JPAImportLoader.java @@ -0,0 +1,56 @@ +/* + * 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.openjpa; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; + +import static org.osoa.sca.Version.XML_NAMESPACE_1_0; + +import org.apache.tuscany.spi.annotation.Autowire; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.deployer.DeploymentContext; +import org.apache.tuscany.spi.extension.LoaderExtension; +import org.apache.tuscany.spi.loader.LoaderException; +import org.apache.tuscany.spi.loader.LoaderRegistry; +import org.apache.tuscany.spi.model.ModelObject; + +/** + * @version $Rev$ $Date$ + */ +public class JPAImportLoader extends LoaderExtension { + private static final QName IMPORT = new QName(XML_NAMESPACE_1_0, "import.jpa"); + + public JPAImportLoader(@Autowire LoaderRegistry registry) { + super(registry); + } + + public QName getXMLType() { + return IMPORT; + } + + public ModelObject load(CompositeComponent parent, ModelObject object, XMLStreamReader reader, + DeploymentContext deploymentContext) + throws XMLStreamException, LoaderException { + //ComponentDefinition definition = new ComponentDefinition() + // TODO read in persisitence unit setting and properties, and set them as properties on the component + return null; + } +} -- cgit v1.2.3