OPENJPA-1593: Add OSGi managed runtime.

Submitted By: Wolfgang Glas

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1068588 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2011-02-08 21:28:30 +00:00
parent af080beed3
commit c415857292
4 changed files with 674 additions and 0 deletions

View File

@ -65,6 +65,14 @@
<artifactId>ant</artifactId>
<scope>provided</scope>
</dependency>
<!-- for osgi ManagedRuntime implementation, -->
<!-- we pull in OSGi core at the same level as Apache Aries -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>4.2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,178 @@
/*
* 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.openjpa.ee;
import javax.transaction.TransactionManager;
import org.apache.openjpa.util.InternalException;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
/**
* <p>Implementation of the {@link ManagedRuntime} interface that listens
* for an OSGi service with interface <code>javax.transaction.TransactionManager</code>
* to create a {@link TransactionManager} for controlling transactions.</p>
*
* <p>The support for a transaction manager is usually activated inside an OSGi
* {@link BundleActivator} implementation using {@link #registerServiceListener(BundleContext)}
* and {@link #deregisterServiceListener(BundleContext)}.
* </p>
*/
public class OSGiManagedRuntime extends AbstractManagedRuntime {
/**
* a static instance, which is managed by the ServiceListener added to the bundle
* context by {@link #registerServiceListener(BundleContext)}.
*/
private static TransactionManager transactionManager;
private static ServiceReference serviceReference;
private static ServiceListener listener;
private static final class Listener implements ServiceListener {
final BundleContext bundleContext;
// avoid the garbage collection of the OSGiManagedRuntime class itself,
// by holding a reference to it.
final Class<OSGiManagedRuntime> clazzRef;
public Listener(BundleContext bundleContext) {
this.bundleContext = bundleContext;
this.clazzRef = OSGiManagedRuntime.class;
}
@Override
public void serviceChanged(ServiceEvent event) {
synchronized (this.clazzRef) {
switch (event.getType()) {
case ServiceEvent.REGISTERED:
OSGiManagedRuntime.serviceReference = event.getServiceReference();
OSGiManagedRuntime.transactionManager =
(TransactionManager) this.bundleContext.getService(OSGiManagedRuntime.serviceReference);
break;
case ServiceEvent.UNREGISTERING:
OSGiManagedRuntime.transactionManager = null;
OSGiManagedRuntime.serviceReference = null;
this.bundleContext.ungetService(event.getServiceReference());
break;
}
}
}
}
/**
* <p>Register a service listener to the given bundle context by
* {@link BundleContext#addServiceListener(ServiceListener,String)} with a filter
* expression of <code>(objectClass=javax.transaction.TransactionManager)</code>.</p>
*
* <p>The caller is responsible for calling
* {@link #deregisterServiceListener(BundleContext)}, when
* the bundle context is being stopped.</p>
*
* @param bundle The bundle, which is currently being started.
* @throws InvalidSyntaxException When the filter expression is invalid.
*/
public static synchronized void registerServiceListener(BundleContext bundleContext)
throws InvalidSyntaxException {
if (listener != null) {
throw new InternalException("Another OSGi service listener has already been registered.");
}
listener = new Listener(bundleContext);
bundleContext.addServiceListener(listener, "(" + Constants.OBJECTCLASS
+ "=javax.transaction.TransactionManager)");
serviceReference = bundleContext.getServiceReference("javax.transaction.TransactionManager");
if (serviceReference != null) {
transactionManager = (TransactionManager)bundleContext.getService(serviceReference);
}
}
/**
* Remove a service listener previously started inside {@link #registerServiceListener(BundleContext)}.
*
* @param bundleContext The bundle context to call
* {@link BundleContext#removeServiceListener(ServiceListener)} on.
*/
public static synchronized void deregisterServiceListener(BundleContext bundleContext) {
try {
if (serviceReference != null) {
bundleContext.ungetService(serviceReference);
transactionManager = null;
serviceReference = null;
}
}
finally {
// assure, that the service listener is removed, whatever happens above.
if (listener != null) {
bundleContext.removeServiceListener(listener);
listener = null;
}
}
}
/* (non-Javadoc)
* @see org.apache.openjpa.ee.ManagedRuntime#getTransactionManager()
*/
@Override
public TransactionManager getTransactionManager() throws Exception {
synchronized (OSGiManagedRuntime.class) {
if (transactionManager == null) {
throw new InternalException("No javax.transaction.TransactionManager " +
"service is currently registered as an OSGi service.");
}
return transactionManager;
}
}
/* (non-Javadoc)
*
* @see org.apache.openjpa.ee.ManagedRuntime#setRollbackOnly(java.lang.Throwable)
*/
@Override
public void setRollbackOnly(Throwable cause) throws Exception {
// there is no generic support for setting the rollback cause
getTransactionManager().getTransaction().setRollbackOnly();
}
/* (non-Javadoc)
* @see org.apache.openjpa.ee.ManagedRuntime#getRollbackCause()
*/
@Override
public Throwable getRollbackCause() throws Exception {
// there is no generic support for setting the rollback cause
return null;
}
}

View File

@ -0,0 +1,482 @@
/*
* 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.openjpa.ee;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.List;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.InvalidTransactionException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.openjpa.util.InternalException;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.BundleListener;
import org.osgi.framework.Filter;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
/**
* Test javax.transaction.TransactionManager OSGi service discovery.
*/
public class TestOSGiManagedRuntime extends TestCase {
private static final String TXN_MANAGER_CLASS_NAME = "javax.transaction.TransactionManager";
/**
* A transaction manager instance an nothing more.
*/
private static final TransactionManager TXN_MANAGER = new TransactionManager() {
public void begin() throws NotSupportedException, SystemException {
}
public void commit() throws HeuristicMixedException,
HeuristicRollbackException, IllegalStateException,
RollbackException, SecurityException, SystemException {
}
public int getStatus() throws SystemException {
return 0;
}
public Transaction getTransaction() throws SystemException {
return null;
}
public void resume(Transaction tobj) throws IllegalStateException,
InvalidTransactionException, SystemException {
}
public void rollback() throws IllegalStateException, SecurityException,
SystemException {
}
public void setRollbackOnly() throws IllegalStateException,
SystemException {
}
public void setTransactionTimeout(int seconds) throws SystemException {
}
public Transaction suspend() throws SystemException {
return null;
}
public String toString() {
return TestOSGiManagedRuntime.class.getName()+"::TXN_MANAGER";
}
};
/**
* A service reference instance an nothing more.
*/
private static final ServiceReference TXN_SVC_REFERENCE = new ServiceReference() {
@Override
public boolean isAssignableTo(Bundle bundle, String className) {
return false;
}
@Override
public Bundle[] getUsingBundles() {
return null;
}
@Override
public String[] getPropertyKeys() {
return null;
}
@Override
public Object getProperty(String key) {
return null;
}
@Override
public Bundle getBundle() {
return null;
}
@Override
public int compareTo(Object reference) {
return reference == TXN_SVC_REFERENCE ? 0 : 1;
}
public String toString() {
return TestOSGiManagedRuntime.class.getName()+"::TXN_SVC_REFERENCE";
}
};
/**
* A fake bundle context with a reference counter for a javax.transaction.TRansactionManager
*/
private static final class TestBundleContext implements BundleContext {
private List<ServiceListener> serviceListeners;
private TransactionManager transactionManager;
private int txnRefCount;
public void setTransactionManager(TransactionManager transactionManager) {
if (transactionManager == null) {
if (this.serviceListeners != null) {
for (ServiceListener listener :this.serviceListeners) {
listener.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING,TXN_SVC_REFERENCE));
}
}
}
// test for properly calling ungetReference().
assertEquals(0,this.txnRefCount);
this.transactionManager = transactionManager;
if (transactionManager != null) {
if (this.serviceListeners != null) {
for (ServiceListener listener :this.serviceListeners) {
listener.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED,TXN_SVC_REFERENCE));
}
}
}
}
public int getServiceListenerCount() {
if (this.serviceListeners == null) {
return 0;
}
else {
return this.serviceListeners.size();
}
}
public int getTxnRefCount() {
return this.txnRefCount;
}
public String getProperty(String key) {
throw new UnsupportedOperationException();
}
public Bundle getBundle() {
throw new UnsupportedOperationException();
}
public Bundle installBundle(String location, InputStream input)
throws BundleException {
throw new UnsupportedOperationException();
}
public Bundle installBundle(String location) throws BundleException {
throw new UnsupportedOperationException();
}
public Bundle getBundle(long id) {
throw new UnsupportedOperationException();
}
public Bundle[] getBundles() {
throw new UnsupportedOperationException();
}
public void addServiceListener(ServiceListener listener, String filter)
throws InvalidSyntaxException {
assertEquals("(objectClass="+TXN_MANAGER_CLASS_NAME+")",filter);
if (this.serviceListeners == null)
this.serviceListeners = new ArrayList<ServiceListener>();
this.serviceListeners.add(listener);
}
public void addServiceListener(ServiceListener listener) {
throw new AssertionFailedError("service listener must be added using an objectClass filter.");
}
public void removeServiceListener(ServiceListener listener) {
if (this.serviceListeners == null || ! this.serviceListeners.remove(listener)) {
throw new AssertionFailedError("Try to remove service listener, which has not been added before.");
}
assertEquals(0,this.txnRefCount);
}
public void addBundleListener(BundleListener listener) {
throw new UnsupportedOperationException();
}
public void removeBundleListener(BundleListener listener) {
throw new UnsupportedOperationException();
}
public void addFrameworkListener(FrameworkListener listener) {
throw new UnsupportedOperationException();
}
public void removeFrameworkListener(FrameworkListener listener) {
throw new UnsupportedOperationException();
}
@SuppressWarnings("rawtypes")
public ServiceRegistration registerService(String[] clazzes,
Object service, Dictionary properties) {
throw new UnsupportedOperationException();
}
@SuppressWarnings("rawtypes")
public ServiceRegistration registerService(String clazz,
Object service, Dictionary properties) {
throw new UnsupportedOperationException();
}
public ServiceReference[] getServiceReferences(String clazz,
String filter) throws InvalidSyntaxException {
throw new UnsupportedOperationException();
}
public ServiceReference[] getAllServiceReferences(String clazz,
String filter) throws InvalidSyntaxException {
throw new UnsupportedOperationException();
}
public ServiceReference getServiceReference(String clazz) {
assertEquals(TXN_MANAGER_CLASS_NAME,clazz);
if (this.transactionManager == null) {
return null;
}
assertEquals(TXN_MANAGER_CLASS_NAME,clazz);
return TXN_SVC_REFERENCE;
}
@Override
public Object getService(ServiceReference reference) {
assertSame(TXN_SVC_REFERENCE,reference);
++this.txnRefCount;
return this.transactionManager;
}
@Override
public boolean ungetService(ServiceReference reference) {
assertSame(TXN_SVC_REFERENCE,reference);
--this.txnRefCount;
return true;
}
public File getDataFile(String filename) {
throw new UnsupportedOperationException();
}
public Filter createFilter(String filter) throws InvalidSyntaxException {
throw new UnsupportedOperationException();
}
}
private static void assertTxnManagerAvailable(TestBundleContext context, ManagedRuntime mr) throws Exception {
assertSame(TXN_MANAGER,mr.getTransactionManager());
assertEquals(1,context.getTxnRefCount());
}
private static void assertTxnManagerUnavailable(TestBundleContext context, ManagedRuntime mr) throws Exception {
InternalException ie = null;
try {
mr.getTransactionManager();
} catch (InternalException e) {
ie = e;
}
assertNotNull(ie);
assertEquals(0,context.getTxnRefCount());
}
/**
* Test the discovery, when transaction manager is available before starting
* OSGiManagedRuntime and disappears after stopping the managed runtime.
*
* @throws Throwable
*/
public void testTxnServiceDiscoveryPreStartPostStop() throws Throwable {
TestBundleContext context = new TestBundleContext();
context.setTransactionManager(TXN_MANAGER);
OSGiManagedRuntime.registerServiceListener(context);
ManagedRuntime mr = new OSGiManagedRuntime();
try {
assertEquals(1,context.getServiceListenerCount());
assertTxnManagerAvailable(context, mr);
context.setTransactionManager(null);
assertTxnManagerUnavailable(context, mr);
OSGiManagedRuntime.deregisterServiceListener(context);
}
catch(Throwable e) {
// this is her in order to make test repeatable in one JVM, because
// OSGiManagerRuntime has static properties.
OSGiManagedRuntime.deregisterServiceListener(context);
throw e;
}
assertEquals(0,context.getServiceListenerCount());
assertTxnManagerUnavailable(context, mr);
}
/**
* Test the discovery, when transaction manager is available before starting
* OSGiManagedRuntime and disappears before stopping the managed runtime.
*
* @throws Throwable
*/
public void testTxnServiceDiscoveryPreStartPreStop() throws Throwable {
TestBundleContext context = new TestBundleContext();
context.setTransactionManager(TXN_MANAGER);
OSGiManagedRuntime.registerServiceListener(context);
ManagedRuntime mr = new OSGiManagedRuntime();
try {
assertEquals(1,context.getServiceListenerCount());
assertTxnManagerAvailable(context, mr);
OSGiManagedRuntime.deregisterServiceListener(context);
}
catch(Throwable e) {
// this is her in order to make test repeatable in one JVM, because
// OSGiManagerRuntime has static properties.
OSGiManagedRuntime.deregisterServiceListener(context);
throw e;
}
assertEquals(0,context.getServiceListenerCount());
assertTxnManagerUnavailable(context, mr);
}
/**
* Test the discovery, when transaction manager becomes available after starting
* OSGiManagedRuntime and disappears after stopping the managed runtime.
*
* @throws Throwable
*/
public void testTxnServiceDiscoveryPostStartPostStop() throws Throwable {
TestBundleContext context = new TestBundleContext();
OSGiManagedRuntime.registerServiceListener(context);
ManagedRuntime mr = new OSGiManagedRuntime();
try{
assertEquals(1,context.getServiceListenerCount());
assertTxnManagerUnavailable(context, mr);
context.setTransactionManager(TXN_MANAGER);
assertTxnManagerAvailable(context, mr);
OSGiManagedRuntime.deregisterServiceListener(context);
}
catch(Throwable e) {
// this is her in order to make test repeatable in one JVM, because
// OSGiManagerRuntime has static proeprties.
OSGiManagedRuntime.deregisterServiceListener(context);
throw e;
}
assertEquals(0,context.getServiceListenerCount());
assertTxnManagerUnavailable(context, mr);
}
/**
* Test the discovery, when transaction manager becomes available after starting
* OSGiManagedRuntime and disappears before stopping the managed runtime.
*
* @throws Throwable
*/
public void testTxnServiceDiscoveryPostStartPreStop() throws Throwable {
TestBundleContext context = new TestBundleContext();
OSGiManagedRuntime.registerServiceListener(context);
ManagedRuntime mr = new OSGiManagedRuntime();
try {
assertEquals(1,context.getServiceListenerCount());
assertTxnManagerUnavailable(context, mr);
context.setTransactionManager(TXN_MANAGER);
assertTxnManagerAvailable(context, mr);
context.setTransactionManager(null);
assertTxnManagerUnavailable(context, mr);
OSGiManagedRuntime.deregisterServiceListener(context);
}
catch(Throwable e) {
// this is her in order to make test repeatable in one JVM, because
// OSGiManagerRuntime has static proeprties.
OSGiManagedRuntime.deregisterServiceListener(context);
throw e;
}
assertEquals(0,context.getServiceListenerCount());
assertTxnManagerUnavailable(context, mr);
}
}

View File

@ -20,6 +20,7 @@ import java.util.Hashtable;
import javax.persistence.spi.PersistenceProvider;
import org.apache.openjpa.ee.OSGiManagedRuntime;
import org.apache.openjpa.persistence.PersistenceProviderImpl;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
@ -50,12 +51,17 @@ public class PersistenceActivator implements BundleActivator {
Hashtable<String, String> props = new Hashtable<String, String>();
props.put(PERSISTENCE_PROVIDER_ARIES, OSGI_PERSISTENCE_PROVIDER);
svcReg = ctx.registerService(PERSISTENCE_PROVIDER, provider, props);
OSGiManagedRuntime.registerServiceListener(ctx);
}
/* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext ctx) throws Exception {
OSGiManagedRuntime.deregisterServiceListener(ctx);
if (svcReg != null) {
svcReg.unregister();
svcReg = null;