Return-Path: X-Original-To: apmail-onami-commits-archive@minotaur.apache.org Delivered-To: apmail-onami-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8E04A10ABD for ; Mon, 7 Apr 2014 14:13:07 +0000 (UTC) Received: (qmail 96539 invoked by uid 500); 7 Apr 2014 14:13:07 -0000 Delivered-To: apmail-onami-commits-archive@onami.apache.org Received: (qmail 95758 invoked by uid 500); 7 Apr 2014 14:12:53 -0000 Mailing-List: contact commits-help@onami.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@onami.apache.org Delivered-To: mailing list commits@onami.apache.org Received: (qmail 95726 invoked by uid 99); 7 Apr 2014 14:12:48 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Apr 2014 14:12:48 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 07 Apr 2014 14:12:44 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 224942388860; Mon, 7 Apr 2014 14:12:24 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1585487 [1/5] - in /onami/sandbox/persist: ./ src/main/java/org/apache/onami/persist/ src/sandbox/ src/test/java/org/apache/onami/persist/ src/test/java/org/apache/onami/persist/test/ src/test/java/org/apache/onami/persist/test/transaction... Date: Mon, 07 Apr 2014 14:12:21 -0000 To: commits@onami.apache.org From: sclassen@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140407141224.224942388860@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: sclassen Date: Mon Apr 7 14:12:18 2014 New Revision: 1585487 URL: http://svn.apache.org/r1585487 Log: onami-persist: implemented the new API and added some test Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AggregatedException.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllPersistenceServices.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllUnitsOfWork.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotatedPersistenceUnitBuilder.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotationHolder.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryFactory.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySource.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceByJndiLookup.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceViaProvider.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForApplicationManaged.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForContainerManaged.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/JndiLookupHelper.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/JtaTransactionFacadeFactory.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/Nullable.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceAnnotation.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceUnitModule.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceUnitModuleConfigurator.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/ResourceLocalTransactionFacadeFactory.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/TransactionFacadeFactory.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/TransactionalAnnotationHelper.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/TxnInterceptor.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/UnannotatedPersistenceUnitBuilder.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/UnconfiguredPersistenceUnitBuilder.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/UserTransactionJndiName.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/UserTransactionProviderByJndiLookup.java (with props) onami/sandbox/persist/src/main/java/org/apache/onami/persist/VisibleForTesting.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/AggregatedExceptionTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/ApplicationManagedEntityManagerFactoryProviderTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/ContainerManagedEntityManagerFactoryProviderTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/EntityManagerFactoryFactoryTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/EntityManagerFactorySourceByJndiLookupTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/EntityManagerFactorySourceViaProviderTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/EntityManagerProviderImplTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/EntityManagerProviderImplThreadingTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/InitialContextFactoryStub.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/JndiLookupHelperTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/JtaTransactionFacadeProviderTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/OtherPersistenceUnit.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/PersistenceFilterTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/PersistenceUnitContainerTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/PreconditionsTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/ResourceLocalTransactionFacadeProviderTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/TestPersistenceUnit.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/TransactionalAnnotationHelperTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/TransactionalAnnotationMatcher.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/TransactionalAnnotationReaderTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/TxnInterceptorTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/UserTransactionFacadeTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/ onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/ onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/NestedTransactionTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/SingleTransactionTest.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/ onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/TransactionTestEntity.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/TransactionalTask.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/TransactionalWorker.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/exceptions/ onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/exceptions/RuntimeTestException.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/exceptions/TestException.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/ onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnAnyThrowingNone.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnAnyThrowingRuntimeTestException.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnAnyThrowingTestException.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnNoneThrowingNone.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnNoneThrowingRuntimeTestException.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnNoneThrowingTestException.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnRuntimeTestExceptionThrowingNone.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnRuntimeTestExceptionThrowingRuntimeTestException.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnRuntimeTestExceptionThrowingTestException.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnTestExceptionThrowingNone.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnTestExceptionThrowingRuntimeTestException.java (with props) onami/sandbox/persist/src/test/java/org/apache/onami/persist/test/transaction/testframework/tasks/TaskRollingBackOnTestExceptionThrowingTestException.java (with props) onami/sandbox/persist/src/test/resources/logback-test.xml (with props) Removed: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AbstractPersistenceUnitModule.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/AbstractTxnInterceptor.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllPersistenceUnits.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/ApplicationManagedPersistenceUnitModule.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/ContainerManagedPersistenceUnitModule.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/JtaTxnInterceptor.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceUnitBuilder.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/ResourceLocalTxnInterceptor.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/TransactionType.java onami/sandbox/persist/src/sandbox/ onami/sandbox/persist/src/test/java/org/apache/onami/persist/NestedTransactionTest.java onami/sandbox/persist/src/test/java/org/apache/onami/persist/SingleTransactionTest.java onami/sandbox/persist/src/test/java/org/apache/onami/persist/testframework/ onami/sandbox/persist/src/test/resources/log4j.properties Modified: onami/sandbox/persist/pom.xml onami/sandbox/persist/src/main/java/org/apache/onami/persist/ApplicationManagedEntityManagerFactoryProvider.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/ContainerManagedEntityManagerFactoryProvider.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryProvider.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerProvider.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerProviderImpl.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceFilter.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceModule.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceService.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceUnitContainer.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/Preconditions.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/TransactionFacade.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/Transactional.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/TransactionalAnnotationReader.java onami/sandbox/persist/src/main/java/org/apache/onami/persist/UserTransactionFacade.java onami/sandbox/persist/src/test/resources/META-INF/persistence.xml Modified: onami/sandbox/persist/pom.xml URL: http://svn.apache.org/viewvc/onami/sandbox/persist/pom.xml?rev=1585487&r1=1585486&r2=1585487&view=diff ============================================================================== --- onami/sandbox/persist/pom.xml (original) +++ onami/sandbox/persist/pom.xml Mon Apr 7 14:12:18 2014 @@ -73,13 +73,12 @@ com.google.inject guice - 3.0 org.hibernate.javax.persistence - hibernate-jpa-2.0-api - 1.0.1.Final + hibernate-jpa-2.1-api + 1.0.0.Final provided @@ -98,31 +97,62 @@ junit junit - 4.10 test - org.slf4j - slf4j-simple - 1.7.1 + de.bechte.junit + junit-hierarchicalcontextrunner + 4.11.2 + test + + + junit + junit + + + mockito-all + org.mockito + + + hamcrest-all + org.hamcrest + + + + + org.mockito + mockito-core + 1.9.5 + test + + + hamcrest-core + org.hamcrest + + + + + ch.qos.logback + logback-classic + 1.1.2 test org.hibernate hibernate-entitymanager - 4.1.7.Final + 4.3.5.Final test org.hsqldb - hsqldb-j5 - 2.0.0 + hsqldb + 2.3.2 test com.google.guava guava - 13.0.1 + 16.0.1 test @@ -132,7 +162,6 @@ org.apache.maven.plugins maven-site-plugin - 3.3 en true Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AggregatedException.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/AggregatedException.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/AggregatedException.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/AggregatedException.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,212 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + +import java.io.PrintStream; +import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; + +import static org.apache.onami.persist.Preconditions.checkNotNull; + + +/** + * Exception holding an aggregation of multiple exceptions which were collected. + */ +class AggregatedException + extends RuntimeException +{ + + private static final Throwable[] EMPTY = new Throwable[0]; + + private static final long serialVersionUID = 1L; + + /** + * Builder for AggregatedException + */ + static class Builder + { + + /** + * list of causes for the aggregated exception + */ + private List causes = new ArrayList(); + + /** + * Add an exception to the list of aggregated exceptions + * + * @param cause the exception to add + */ + void add( Throwable cause ) + { + causes.add( checkNotNull( cause, "cause is mandatory!" ) ); + } + + /** + * Throws a runtime exception if the builder has causes. + * + * @param msg the message to of the aggregated exception. + */ + void throwRuntimeExceptionIfHasCauses( String msg ) + { + try + { + if ( !causes.isEmpty() ) + { + throw getRuntimeException( msg ); + } + } + finally + { + causes = null; + } + } + + /** + * Converts the collected causes into a runtime exception + * + * @param msg the message to of the aggregated exception. + * @return the exception to throw + */ + RuntimeException getRuntimeException( String msg ) + { + if ( causes.size() == 1 ) + { + final Throwable cause = causes.get( 0 ); + if ( cause instanceof RuntimeException ) + { + return (RuntimeException) cause; + } + } + return new AggregatedException( msg, causes.toArray( EMPTY ) ); + } + } + + /** + * all the underlying causes for this aggregated exception. + */ + private final Throwable[] causes; + + /** + * number of causes for this aggregated exceptions. + */ + private final int numCauses; + + /** + * @return the number of causes collected into this exception + */ + public int getNumCauses() + { + return numCauses; + } + + /** + * @return the causes which lead to this exception + */ + public Throwable[] getCauses() + { + return causes.clone(); + } + + /** + * Constructor. + * + * @param message the message + * @param causes all the causes + */ + private AggregatedException( String message, Throwable[] causes ) + { + super( message ); + this.causes = causes; + this.numCauses = this.causes.length; + } + + /** + * {@inheritDoc} + */ + @Override + public void printStackTrace( PrintStream s ) + { + synchronized ( s ) + { + + s.println( this ); + StackTraceElement[] trace = getStackTrace(); + for ( final StackTraceElement aTrace : trace ) + { + s.println( "\tat " + aTrace ); + } + + for ( int i = 0; i < numCauses; i++ ) + { + s.println( "Cause " + ( i + 1 ) + ":" ); + if ( causes[i] == null ) + { + s.println( "" ); + } + else + { + causes[i].printStackTrace( s ); + } + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public void printStackTrace( PrintWriter s ) + { + synchronized ( s ) + { + + s.println( this ); + StackTraceElement[] trace = getStackTrace(); + for ( final StackTraceElement aTrace : trace ) + { + s.println( "\tat " + aTrace ); + } + + for ( int i = 0; i < numCauses; i++ ) + { + s.println( "Cause " + ( i + 1 ) + ":" ); + if ( causes[i] == null ) + { + s.println( "" ); + } + else + { + causes[i].printStackTrace( s ); + } + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() + { + return super.toString() + " (caused by " + numCauses + " causes)"; + } + +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AggregatedException.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AggregatedException.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AggregatedException.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllPersistenceServices.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllPersistenceServices.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllPersistenceServices.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllPersistenceServices.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,37 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + +/** + * Interface for aggregation of multiple {@link PersistenceService PersistenceServices}. + */ +public interface AllPersistenceServices +{ + + /** + * Calls {@link PersistenceService#start()} on all stopped persistence services. + */ + void startAllStoppedPersistenceServices(); + + /** + * Calls {@link PersistenceService#stop()} on all persistence services. + */ + void stopAllRunningPersistenceServices(); +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllPersistenceServices.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllPersistenceServices.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllPersistenceServices.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllUnitsOfWork.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllUnitsOfWork.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllUnitsOfWork.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllUnitsOfWork.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,37 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + +/** + * Interface for aggregation of multiple {@link UnitOfWork UnitsOfWork}. + */ +public interface AllUnitsOfWork +{ + + /** + * Calls {@link UnitOfWork#begin()} on all units of work which are not active + */ + void beginAllInactiveUnitsOfWork(); + + /** + * Calls {@link UnitOfWork#end()} on all units of work + */ + void endAllUnitsOfWork(); +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllUnitsOfWork.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllUnitsOfWork.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AllUnitsOfWork.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotatedPersistenceUnitBuilder.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotatedPersistenceUnitBuilder.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotatedPersistenceUnitBuilder.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotatedPersistenceUnitBuilder.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,47 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + +import com.google.inject.Key; +import com.google.inject.Provider; +import com.google.inject.TypeLiteral; + +import javax.transaction.UserTransaction; + +public interface AnnotatedPersistenceUnitBuilder + extends UnconfiguredPersistenceUnitBuilder +{ + UnconfiguredPersistenceUnitBuilder useLocalTransaction(); + + UnconfiguredPersistenceUnitBuilder useGlobalTransaction( UserTransaction userTransaction ); + + UnconfiguredPersistenceUnitBuilder useGlobalTransactionWithJndiName( String utJndiName ); + + UnconfiguredPersistenceUnitBuilder useGlobalTransactionProvidedBy( Provider utProvider ); + + UnconfiguredPersistenceUnitBuilder useGlobalTransactionProvidedBy( + Class> utProviderClass ); + + UnconfiguredPersistenceUnitBuilder useGlobalTransactionProvidedBy( + TypeLiteral> utProviderType ); + + UnconfiguredPersistenceUnitBuilder useGlobalTransactionProvidedBy( + Key> utProviderKey ); +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotatedPersistenceUnitBuilder.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotatedPersistenceUnitBuilder.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotatedPersistenceUnitBuilder.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotationHolder.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotationHolder.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotationHolder.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotationHolder.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,18 @@ +package org.apache.onami.persist; + +import java.lang.annotation.Annotation; + +public class AnnotationHolder +{ + + private final Class annotation; + + AnnotationHolder( Class annotation ) + { + this.annotation = annotation; + } + + Class getAnnotation() { + return annotation; + } +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotationHolder.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotationHolder.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/AnnotationHolder.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: onami/sandbox/persist/src/main/java/org/apache/onami/persist/ApplicationManagedEntityManagerFactoryProvider.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/ApplicationManagedEntityManagerFactoryProvider.java?rev=1585487&r1=1585486&r2=1585487&view=diff ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/ApplicationManagedEntityManagerFactoryProvider.java (original) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/ApplicationManagedEntityManagerFactoryProvider.java Mon Apr 7 14:12:18 2014 @@ -19,56 +19,47 @@ package org.apache.onami.persist; * under the License. */ +import com.google.inject.Inject; +import com.google.inject.Singleton; + import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; -import java.util.Properties; import static org.apache.onami.persist.Preconditions.checkNotNull; + /** * Implementation of {@link PersistenceService} and {@link EntityManagerFactoryProvider} for * application managed persistence units. + *

+ * This class is a singleton and all methods of the {@link PersistenceService} interface are synchronized. */ -final class ApplicationManagedEntityManagerFactoryProvider +@Singleton +class ApplicationManagedEntityManagerFactoryProvider implements EntityManagerFactoryProvider, PersistenceService { - // ---- Members - /** - * Name of the persistence unit as defined in the persistence.xml. + * Factory for creating the {@link EntityManagerFactory}. */ - private final String puName; + private final EntityManagerFactoryFactory emfFactory; /** - * Additional properties. Theses override the ones defined in the persistence.xml. - */ - private final Properties properties; - - /** - * EntityManagerFactory. + * Currently active entity manager factory. + * Is {@code null} when the persistence service is not running. */ private EntityManagerFactory emf; - // ---- Constructor - /** * Constructor. * - * @param puName the name of the persistence unit as defined in the persistence.xml. Must not be {@code null}. - * @param properties the additional properties. Theses override the ones defined in the persistence.xml. Must not be {@code null}. + * @param emfFactory the factory for the {@link EntityManagerFactory}. Must not be {@code null}. */ - public ApplicationManagedEntityManagerFactoryProvider( String puName, Properties properties ) + @Inject + ApplicationManagedEntityManagerFactoryProvider( EntityManagerFactoryFactory emfFactory ) { - checkNotNull( puName ); - checkNotNull( properties ); - - this.puName = puName; - this.properties = properties; + this.emfFactory = checkNotNull( emfFactory, "emfFactory is mandatory!" ); } - // ---- Methods - /** * {@inheritDoc} */ @@ -87,20 +78,20 @@ final class ApplicationManagedEntityMana * {@inheritDoc} */ // @Override - public void start() + public synchronized void start() { if ( isRunning() ) { throw new IllegalStateException( "PersistenceService is already running." ); } - emf = Persistence.createEntityManagerFactory( puName, properties ); + emf = emfFactory.createApplicationManagedEntityManagerFactory(); } /** * {@inheritDoc} */ // @Override - public boolean isRunning() + public synchronized boolean isRunning() { return null != emf; } @@ -109,12 +100,18 @@ final class ApplicationManagedEntityMana * {@inheritDoc} */ // @Override - public void stop() + public synchronized void stop() { if ( isRunning() ) { - emf.close(); - emf = null; + try + { + emf.close(); + } + finally + { + emf = null; + } } } Modified: onami/sandbox/persist/src/main/java/org/apache/onami/persist/ContainerManagedEntityManagerFactoryProvider.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/ContainerManagedEntityManagerFactoryProvider.java?rev=1585487&r1=1585486&r2=1585487&view=diff ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/ContainerManagedEntityManagerFactoryProvider.java (original) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/ContainerManagedEntityManagerFactoryProvider.java Mon Apr 7 14:12:18 2014 @@ -19,47 +19,47 @@ package org.apache.onami.persist; * under the License. */ -import javax.naming.InitialContext; -import javax.naming.NamingException; +import com.google.inject.Inject; +import com.google.inject.Singleton; + import javax.persistence.EntityManagerFactory; import static org.apache.onami.persist.Preconditions.checkNotNull; + /** * Implementation of {@link PersistenceService} and {@link EntityManagerFactoryProvider} for * container managed entity manager factories. + *

+ * This class is a singleton and all methods of the {@link PersistenceService} interface are synchronized. */ -final class ContainerManagedEntityManagerFactoryProvider +@Singleton +class ContainerManagedEntityManagerFactoryProvider implements EntityManagerFactoryProvider, PersistenceService { - // ---- Members - /** - * The JNDI name of the {@link EntityManagerFactory}. + * The source for retrieving the entity manager factory instance. */ - private final String emfJndiName; + private final EntityManagerFactorySource emfSource; /** - * The {@link EntityManagerFactory}. + * Currently active entity manager factory. + * Is {@code null} when the persistence service is not running. */ private EntityManagerFactory emf; - // ---- Constructor - /** * Constructor. * - * @param emfJndiName the JNDI name of the {@link EntityManagerFactory}. Must not be {@code null}. + * @param emfSource the source for the {@link EntityManagerFactory}. Must not be {@code null}. */ - public ContainerManagedEntityManagerFactoryProvider( String emfJndiName ) + @Inject + ContainerManagedEntityManagerFactoryProvider( EntityManagerFactorySource emfSource ) { - checkNotNull( emfJndiName ); - this.emfJndiName = emfJndiName; + this.emfSource = checkNotNull( emfSource, "emfSource is mandatory!" ); } - // ---- Methods - /** * {@inheritDoc} */ @@ -78,29 +78,21 @@ final class ContainerManagedEntityManage * {@inheritDoc} */ // @Override - public void start() + public synchronized void start() { if ( isRunning() ) { throw new IllegalStateException( "PersistenceService is already running." ); } - try - { - final InitialContext ctx = new InitialContext(); - emf = (EntityManagerFactory) ctx.lookup( emfJndiName ); - } - catch ( NamingException e ) - { - throw new RuntimeException( "lookup for EntityManagerFactory with JNDI name '" + emfJndiName + "' failed", - e ); - } + + emf = emfSource.getEntityManagerFactory(); } /** * {@inheritDoc} */ // @Override - public boolean isRunning() + public synchronized boolean isRunning() { return null != emf; } @@ -109,9 +101,12 @@ final class ContainerManagedEntityManage * {@inheritDoc} */ // @Override - public void stop() + public synchronized void stop() { emf = null; + // the entity manager factory must NOT be closed: + // - because it was created by the container and it is therefore the responsibility of the container to close it + // - because we cannot know if another part of the application has obtained the same instance over JNDI } } Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryFactory.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryFactory.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryFactory.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryFactory.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,71 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + +import com.google.inject.Inject; +import com.google.inject.Singleton; + +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import java.util.Properties; + +import static org.apache.onami.persist.Preconditions.checkNotNull; + +/** + * Factory for {@link EntityManagerFactory}. + */ +@Singleton +class EntityManagerFactoryFactory +{ + + /** + * Name of the persistence unit as defined in the persistence.xml. + */ + private final String puName; + + /** + * Additional properties. Theses override the ones defined in the persistence.xml. + */ + private final Properties properties; + + /** + * Constructor. + * + * @param puName the name of the persistence unit as defined in the persistence.xml. Must not be {@code null}. + * @param properties the additional properties. Theses override the ones defined in the persistence.xml. Must not be {@code null}. + */ + @Inject + EntityManagerFactoryFactory( @ForApplicationManaged String puName, + @Nullable @ForApplicationManaged Properties properties ) + { + this.puName = checkNotNull( puName, "puName is mandatory!" ); + this.properties = properties; + } + + /** + * Creates a new {@link EntityManagerFactory}. + * + * @return the newly created entity manager factory. + */ + EntityManagerFactory createApplicationManagedEntityManagerFactory() + { + return Persistence.createEntityManagerFactory( puName, properties ); + } +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryFactory.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryProvider.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryProvider.java?rev=1585487&r1=1585486&r2=1585487&view=diff ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryProvider.java (original) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactoryProvider.java Mon Apr 7 14:12:18 2014 @@ -22,13 +22,12 @@ package org.apache.onami.persist; import javax.persistence.EntityManagerFactory; /** - * Provider for {@link EntityManagerFactoryProvider}. + * Provider for {@link EntityManagerFactory}. */ interface EntityManagerFactoryProvider { /** - * @return the provider for {@link EntityManagerFactory}. * @throws IllegalStateException if {@link PersistenceService#isRunning()} returns {@code false}. */ EntityManagerFactory get() Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySource.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySource.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySource.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySource.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,8 @@ +package org.apache.onami.persist; + +import javax.persistence.EntityManagerFactory; + +public interface EntityManagerFactorySource +{ + EntityManagerFactory getEntityManagerFactory(); +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySource.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySource.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySource.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceByJndiLookup.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceByJndiLookup.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceByJndiLookup.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceByJndiLookup.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,67 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + +import com.google.inject.Inject; +import com.google.inject.Singleton; + +import javax.persistence.EntityManagerFactory; + +import static org.apache.onami.persist.Preconditions.checkNotNull; + +/** + * Source for {@link javax.persistence.EntityManagerFactory}. + * The sourced instance is looked up via a JNDI call. + */ +@Singleton +class EntityManagerFactorySourceByJndiLookup + implements EntityManagerFactorySource +{ + + private final String jndiName; + + private final JndiLookupHelper jndiLookupHelper; + + /** + * Constructor. + * + * @param jndiName jndi name of the entity manager factory. Must not be {@code null}. + * @param jndiLookupHelper the lookup helper. Must not be {@code null}. + */ + @Inject + EntityManagerFactorySourceByJndiLookup( @ForContainerManaged String jndiName, JndiLookupHelper jndiLookupHelper ) + { + this.jndiName = checkNotNull( jndiName, "jndiName is mandatory!" ); + this.jndiLookupHelper = checkNotNull( jndiLookupHelper, "jndiLookupHelper is mandatory!" ); + } + + /** + * Gets a {@link javax.persistence.EntityManagerFactory} by looking it up in the JNDI context. + * + * @return the found entity manager factory + * @throws RuntimeException when no entity manager factory was found. + */ + //@Override + public EntityManagerFactory getEntityManagerFactory() + { + return jndiLookupHelper.doJndiLookup( EntityManagerFactory.class, jndiName ); + } + +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceByJndiLookup.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceByJndiLookup.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceByJndiLookup.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceViaProvider.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceViaProvider.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceViaProvider.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceViaProvider.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,56 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; + +import javax.persistence.EntityManagerFactory; + +/** + * Source for {@link javax.persistence.EntityManagerFactory}. + * The sourced instance is provided by guice. + */ +@Singleton +public class EntityManagerFactorySourceViaProvider implements EntityManagerFactorySource +{ + private final Provider emfProvider; + + /** + * Constructor. + * + * @param emfProvider the provider which gives access to the instance coming from the container. + */ + @Inject + public EntityManagerFactorySourceViaProvider( @ForContainerManaged Provider emfProvider ) + { + this.emfProvider = emfProvider; + } + + /** + * {@inheritDoc} + */ + //@Override + public EntityManagerFactory getEntityManagerFactory() + { + return emfProvider.get(); + } +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceViaProvider.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceViaProvider.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerFactorySourceViaProvider.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerProvider.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerProvider.java?rev=1585487&r1=1585486&r2=1585487&view=diff ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerProvider.java (original) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerProvider.java Mon Apr 7 14:12:18 2014 @@ -35,7 +35,7 @@ import javax.persistence.EntityManager; * be used. *

* It is a good practice to store this provider in a instance/member variable and only obtain an - * {@link EntityManager} instance in a Method where it is used. This ensures that the method always + * {@link EntityManager} instance in a method where it is used. This ensures that the method always * has access to a valid {@link EntityManager}. *

* The {@link EntityManagerProvider} is thread save. Modified: onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerProviderImpl.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerProviderImpl.java?rev=1585487&r1=1585486&r2=1585487&view=diff ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerProviderImpl.java (original) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/EntityManagerProviderImpl.java Mon Apr 7 14:12:18 2014 @@ -19,6 +19,10 @@ package org.apache.onami.persist; * under the License. */ + +import com.google.inject.Inject; +import com.google.inject.Singleton; + import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import java.util.Properties; @@ -28,14 +32,13 @@ import static org.apache.onami.persist.P /** * Implementation of {@link EntityManagerProvider} and {@link UnitOfWork}. */ -final class EntityManagerProviderImpl +@Singleton +class EntityManagerProviderImpl implements EntityManagerProvider, UnitOfWork { - // ---- Members - /** - * Provider for {@link EntityManagerFactory}. + * Provider for {@link javax.persistence.EntityManagerFactory}. */ private final EntityManagerFactoryProvider emfProvider; @@ -49,47 +52,36 @@ final class EntityManagerProviderImpl */ private final ThreadLocal entityManagers = new ThreadLocal(); - // ---- Constructor - - /** - * Constructor. - * - * @param emfProvider the provider for {@link EntityManagerFactory}. - */ - public EntityManagerProviderImpl( EntityManagerFactoryProvider emfProvider ) - { - this( emfProvider, null ); - } - /** * Constructor. * - * @param emfProvider the provider for {@link EntityManagerFactory}. Must not be {@code null}. + * @param emfProvider the provider for {@link javax.persistence.EntityManagerFactory}. Must not be {@code null}. * @param properties additional properties to be set on every {@link EntityManager} which is created. */ - public EntityManagerProviderImpl( EntityManagerFactoryProvider emfProvider, Properties properties ) + @Inject + public EntityManagerProviderImpl( EntityManagerFactoryProvider emfProvider, + @Nullable @ForContainerManaged Properties properties ) { - checkNotNull( emfProvider ); - - this.emfProvider = emfProvider; + this.emfProvider = checkNotNull( emfProvider, "emfProvider is mandatory!" ); this.properties = properties; } - // ---- Methods - /** * {@inheritDoc} */ // @Override public EntityManager get() + throws IllegalStateException { final EntityManager entityManager = entityManagers.get(); - if ( null != entityManager ) + if ( entityManager != null ) { return entityManager; } - - throw new IllegalStateException( "UnitOfWork is not running." ); + else + { + throw new IllegalStateException( "UnitOfWork is not running." ); + } } /** @@ -102,19 +94,27 @@ final class EntityManagerProviderImpl { throw new IllegalStateException( "Unit of work has already been started." ); } + else + { + final EntityManager em = createEntityManager(); + entityManagers.set( em ); + } + } + /** + * @return a new entity manager instance. + */ + private EntityManager createEntityManager() + { final EntityManagerFactory emf = emfProvider.get(); - final EntityManager em; if ( null == properties ) { - em = emf.createEntityManager(); + return emf.createEntityManager(); } else { - em = emf.createEntityManager( properties ); + return emf.createEntityManager( properties ); } - - entityManagers.set( em ); } /** @@ -123,7 +123,7 @@ final class EntityManagerProviderImpl // @Override public boolean isActive() { - return null != entityManagers.get(); + return entityManagers.get() != null; } /** @@ -133,9 +133,25 @@ final class EntityManagerProviderImpl public void end() { final EntityManager em = entityManagers.get(); - if ( null != em ) + if ( em != null ) + { + closeAndRemoveEntityManager( em ); + } + } + + /** + * closes the entity manager and removes it from the internal storage. + * + * @param em the entity manager to close + */ + private void closeAndRemoveEntityManager( EntityManager em ) + { + try { em.close(); + } + finally + { entityManagers.remove(); } } Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForApplicationManaged.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForApplicationManaged.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForApplicationManaged.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForApplicationManaged.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,38 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * Annotation to mark bindings which are specific for application managed persistence units. + */ +@Target( { ElementType.PARAMETER } ) +@Retention( RetentionPolicy.RUNTIME ) +@BindingAnnotation +@interface ForApplicationManaged +{ +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForApplicationManaged.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForApplicationManaged.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForApplicationManaged.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForContainerManaged.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForContainerManaged.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForContainerManaged.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForContainerManaged.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,38 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * Annotation to mark bindings which are specific for container managed persistence units. + */ +@Target( { ElementType.PARAMETER } ) +@Retention( RetentionPolicy.RUNTIME ) +@BindingAnnotation +@interface ForContainerManaged +{ +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForContainerManaged.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForContainerManaged.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/ForContainerManaged.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/JndiLookupHelper.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/JndiLookupHelper.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/JndiLookupHelper.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/JndiLookupHelper.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,42 @@ +package org.apache.onami.persist; + +import com.google.inject.Singleton; + +import javax.naming.InitialContext; +import javax.naming.NamingException; + +/** + * Helper class which does a JNDI lookup and handles exceptions. + */ +@Singleton +public class JndiLookupHelper +{ + + /** + * Does the actual JNDI lookup. + * + * @param type type of the object to lookup + * @param jndiName name of the object to lookup + * @param type of the object to lookup + * @return the object provided by the JNDI context. + */ + @SuppressWarnings( "unchecked" ) + T doJndiLookup( Class type, String jndiName ) + { + try + { + final InitialContext ctx = new InitialContext(); + final T emf = (T) ctx.lookup( jndiName ); + + Preconditions.checkNotNull( emf, "lookup for " + type.getSimpleName() + " with JNDI name '" + jndiName + + "' returned null" ); + + return emf; + } + catch ( NamingException e ) + { + throw new RuntimeException( + "lookup for " + type.getSimpleName() + " with JNDI name '" + jndiName + "' failed", e ); + } + } +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/JndiLookupHelper.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/JndiLookupHelper.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/JndiLookupHelper.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/JtaTransactionFacadeFactory.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/JtaTransactionFacadeFactory.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/JtaTransactionFacadeFactory.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/JtaTransactionFacadeFactory.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,178 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + + +import com.google.inject.Inject; +import com.google.inject.Singleton; + +import javax.persistence.EntityManager; + +import static org.apache.onami.persist.Preconditions.checkNotNull; + +/** + * Factory for transaction facades in case of JTA transactions. + */ +@Singleton +class JtaTransactionFacadeFactory + implements TransactionFacadeFactory +{ + + /** + * The facade to the user transaction. + */ + private final UserTransactionFacade utFacade; + + /** + * Provider for the entity manager. + * The entity manager will be joined to the the transaction. + */ + private final EntityManagerProvider emProvider; + + /** + * Constructor. + * + * @param utFacade the user transaction facade. + * @param emProvider the entity manager provider. + */ + @Inject + public JtaTransactionFacadeFactory( UserTransactionFacade utFacade, EntityManagerProvider emProvider ) + { + this.utFacade = checkNotNull( utFacade, "utFacade is mandatory!" ); + this.emProvider = checkNotNull( emProvider, "emProvider is mandatory!" ); + } + + /** + * {@inheritDoc} + */ + // @Override + public TransactionFacade createTransactionFacade() + { + if ( utFacade.isActive() ) + { + return new Inner( utFacade, emProvider.get() ); + } + else + { + return new Outer( utFacade, emProvider.get() ); + } + } + + /** + * TransactionFacade representing an inner (nested) transaction. Starting and + * committing a transaction has no effect. This Facade will set the + * rollbackOnly flag on the underlying transaction in case of a rollback. + */ + private static class Inner + implements TransactionFacade + { + private final UserTransactionFacade txn; + + private final EntityManager em; + + Inner( UserTransactionFacade txn, EntityManager em ) + { + this.txn = checkNotNull( txn, "txn is mandatory!" ); + this.em = checkNotNull( em, "em is mandatory!" ); + } + + /** + * {@inheritDoc} + */ + // @Override + public void begin() + { + em.joinTransaction(); + } + + /** + * {@inheritDoc} + */ + // @Override + public void commit() + { + // Do nothing + } + + /** + * {@inheritDoc} + */ + // @Override + public void rollback() + { + txn.setRollbackOnly(); + } + } + + /** + * TransactionFacade representing an outer transaction. This Facade starts + * and ends the transaction. If an inner transaction has set the rollbackOnly + * flag the transaction will be rolled back in any case. + */ + private static class Outer + implements TransactionFacade + { + private final UserTransactionFacade txn; + + private final EntityManager em; + + Outer( UserTransactionFacade txn, EntityManager em ) + { + this.txn = checkNotNull( txn, "txn is mandatory!" ); + this.em = checkNotNull( em, "em is mandatory!" ); + } + + /** + * {@inheritDoc} + */ + // @Override + public void begin() + { + txn.begin(); + em.joinTransaction(); + } + + /** + * {@inheritDoc} + */ + // @Override + public void commit() + { + if ( txn.getRollbackOnly() ) + { + txn.rollback(); + } + else + { + txn.commit(); + } + } + + /** + * {@inheritDoc} + */ + // @Override + public void rollback() + { + txn.rollback(); + } + } + +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/JtaTransactionFacadeFactory.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/JtaTransactionFacadeFactory.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/JtaTransactionFacadeFactory.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/Nullable.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/Nullable.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/Nullable.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/Nullable.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,36 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation to mark injection points which are optional. + * Guice may inject {@code null} if there is no suitable binding. + */ +@Retention( RetentionPolicy.RUNTIME ) +@Target( ElementType.PARAMETER ) +@interface Nullable +{ +} + Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/Nullable.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/Nullable.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/Nullable.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceAnnotation.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceAnnotation.java?rev=1585487&view=auto ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceAnnotation.java (added) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceAnnotation.java Mon Apr 7 14:12:18 2014 @@ -0,0 +1,38 @@ +package org.apache.onami.persist; + +/* + * 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. + */ + +import com.google.inject.BindingAnnotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * Annotation to mark bindings which are specific for application managed persistence units. + */ +@Target( ElementType.PARAMETER ) +@Retention( RetentionPolicy.RUNTIME ) +@BindingAnnotation +@interface PersistenceAnnotation +{ +} Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceAnnotation.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceAnnotation.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceAnnotation.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Modified: onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceFilter.java URL: http://svn.apache.org/viewvc/onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceFilter.java?rev=1585487&r1=1585486&r2=1585487&view=diff ============================================================================== --- onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceFilter.java (original) +++ onami/sandbox/persist/src/main/java/org/apache/onami/persist/PersistenceFilter.java Mon Apr 7 14:12:18 2014 @@ -19,6 +19,8 @@ package org.apache.onami.persist; * under the License. */ +import com.google.inject.Inject; + import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; @@ -49,20 +51,23 @@ public class PersistenceFilter implements Filter { - // ---- Members - + /** + * Container of all known persistence unit and units of work. + */ private final PersistenceUnitContainer persistenceUnitsContainer; - // ---- Constructor - + /** + * Constructor. + * + * @param persistenceUnitsContainer container of all known persistence unit and units of work. + */ + @Inject PersistenceFilter( PersistenceUnitContainer persistenceUnitsContainer ) { checkNotNull( persistenceUnitsContainer ); this.persistenceUnitsContainer = persistenceUnitsContainer; } - // ---- Methods - /** * {@inheritDoc} */ @@ -72,12 +77,12 @@ public class PersistenceFilter { try { - persistenceUnitsContainer.begin(); + persistenceUnitsContainer.beginAllInactiveUnitsOfWork(); chain.doFilter( request, response ); } finally { - persistenceUnitsContainer.end(); + persistenceUnitsContainer.endAllUnitsOfWork(); } } @@ -88,7 +93,7 @@ public class PersistenceFilter public void init( FilterConfig filterConfig ) throws ServletException { - persistenceUnitsContainer.start(); + persistenceUnitsContainer.startAllStoppedPersistenceServices(); } /** @@ -97,6 +102,6 @@ public class PersistenceFilter // @Override public void destroy() { - persistenceUnitsContainer.stop(); + persistenceUnitsContainer.stopAllRunningPersistenceServices(); } }