Author: gnodet
Date: Fri Sep 26 11:32:37 2014
New Revision: 1627754

URL: http://svn.apache.org/r1627754
Log:
[ARIES-1250] Ability to manage non XA DataSource

Modified:
    
aries/trunk/transaction/transaction-jdbc/src/main/java/org/apache/aries/transaction/jdbc/internal/Activator.java
    
aries/trunk/transaction/transaction-jdbc/src/main/java/org/apache/aries/transaction/jdbc/internal/ManagedDataSourceFactory.java

Modified: 
aries/trunk/transaction/transaction-jdbc/src/main/java/org/apache/aries/transaction/jdbc/internal/Activator.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-jdbc/src/main/java/org/apache/aries/transaction/jdbc/internal/Activator.java?rev=1627754&r1=1627753&r2=1627754&view=diff
==============================================================================
--- 
aries/trunk/transaction/transaction-jdbc/src/main/java/org/apache/aries/transaction/jdbc/internal/Activator.java
 (original)
+++ 
aries/trunk/transaction/transaction-jdbc/src/main/java/org/apache/aries/transaction/jdbc/internal/Activator.java
 Fri Sep 26 11:32:37 2014
@@ -23,6 +23,7 @@ import org.apache.aries.transaction.Arie
 import org.apache.xbean.blueprint.context.impl.XBeanNamespaceHandler;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Filter;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceEvent;
 import org.osgi.framework.ServiceListener;
@@ -33,10 +34,13 @@ import org.osgi.util.tracker.ServiceTrac
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.sql.CommonDataSource;
 import javax.transaction.TransactionManager;
 import java.util.Hashtable;
 
-public class Activator implements BundleActivator, ServiceTrackerCustomizer, 
ServiceListener {
+public class Activator implements BundleActivator,
+                                  ServiceTrackerCustomizer<CommonDataSource, 
ManagedDataSourceFactory>,
+                                  ServiceListener {
 
     private static final Logger LOGGER = 
LoggerFactory.getLogger(Activator.class);
 
@@ -58,7 +62,14 @@ public class Activator implements Bundle
             LOGGER.error("Unable to register JDBC blueprint namespace 
handler", e);
         }
 
-        t = new ServiceTracker(ctx, javax.sql.XADataSource.class.getName(), 
this);
+        Filter filter;
+        String flt = 
"(&(|(objectClass=javax.sql.XADataSource)(objectClass=javax.sql.DataSource))(!(aries.managed=true)))";
+        try {
+            filter = context.createFilter(flt);
+        } catch (InvalidSyntaxException e) {
+            throw new IllegalStateException(e);
+        }
+        t = new ServiceTracker<CommonDataSource, 
ManagedDataSourceFactory>(ctx, filter, this);
 
         try {
             ctx.addServiceListener(this, "(objectClass=" + 
AriesTransactionManager.class.getName() + ")");
@@ -88,31 +99,37 @@ public class Activator implements Bundle
         }
     }
 
-    public Object addingService(ServiceReference ref) {
+    public ManagedDataSourceFactory 
addingService(ServiceReference<CommonDataSource> ref) {
         try {
-            LOGGER.info("Wrapping XADataSource " + ref);
+            LOGGER.info("Wrapping DataSource " + ref);
             ManagedDataSourceFactory mdsf = new ManagedDataSourceFactory(ref, 
tm);
-            return mdsf.register();
+            mdsf.register();
+            return mdsf;
         } catch (Exception e) {
-            LOGGER.warn("Error wrapping XADataSource " + ref, e);
+            LOGGER.warn("Error wrapping DataSource " + ref, e);
             return null;
         }
     }
 
-    public void modifiedService(ServiceReference ref, Object service) {
-        ServiceRegistration reg = (ServiceRegistration) service;
-
-        Hashtable<String, Object> map = new Hashtable<String, Object>();
-        for (String key : ref.getPropertyKeys()) {
-            map.put(key, ref.getProperty(key));
+    public void modifiedService(ServiceReference<CommonDataSource> ref, 
ManagedDataSourceFactory service) {
+        try {
+            service.unregister();
+        } catch (Exception e) {
+            LOGGER.warn("Error closing DataSource " + ref, e);
+        }
+        try {
+            service.register();
+        } catch (Exception e) {
+            LOGGER.warn("Error wrapping DataSource " + ref, e);
         }
-        map.put("aries.xa.aware", "true");
-
-        reg.setProperties(map);
     }
 
-    public void removedService(ServiceReference ref, Object service) {
-        safeUnregisterService((ServiceRegistration) service);
+    public void removedService(ServiceReference<CommonDataSource> ref, 
ManagedDataSourceFactory service) {
+        try {
+            service.unregister();
+        } catch (Exception e) {
+            LOGGER.warn("Error closing DataSource " + ref, e);
+        }
     }
 
     public void serviceChanged(ServiceEvent event) {

Modified: 
aries/trunk/transaction/transaction-jdbc/src/main/java/org/apache/aries/transaction/jdbc/internal/ManagedDataSourceFactory.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/transaction/transaction-jdbc/src/main/java/org/apache/aries/transaction/jdbc/internal/ManagedDataSourceFactory.java?rev=1627754&r1=1627753&r2=1627754&view=diff
==============================================================================
--- 
aries/trunk/transaction/transaction-jdbc/src/main/java/org/apache/aries/transaction/jdbc/internal/ManagedDataSourceFactory.java
 (original)
+++ 
aries/trunk/transaction/transaction-jdbc/src/main/java/org/apache/aries/transaction/jdbc/internal/ManagedDataSourceFactory.java
 Fri Sep 26 11:32:37 2014
@@ -24,6 +24,7 @@ import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 
+import javax.sql.CommonDataSource;
 import javax.sql.DataSource;
 import javax.sql.XADataSource;
 import java.util.Hashtable;
@@ -33,9 +34,12 @@ public class ManagedDataSourceFactory {
 
     private final ServiceReference reference;
     private final AriesTransactionManager transactionManager;
-    private final XADataSource dataSource;
+    private final CommonDataSource dataSource;
     private final Map<String, Object> properties;
 
+    private ServiceRegistration<DataSource> registration;
+    private ConnectionManagerFactory cm;
+
     public ManagedDataSourceFactory(ServiceReference reference,
                                     AriesTransactionManager 
transactionManager) {
         this.reference = reference;
@@ -44,14 +48,14 @@ public class ManagedDataSourceFactory {
         for (String key : reference.getPropertyKeys()) {
             this.properties.put(key, reference.getProperty(key));
         }
-        this.dataSource = (XADataSource) 
reference.getBundle().getBundleContext().getService(reference);
+        this.dataSource = (CommonDataSource) 
reference.getBundle().getBundleContext().getService(reference);
     }
 
     public AriesTransactionManager getTransactionManager() {
         return transactionManager;
     }
 
-    public XADataSource getDataSource() {
+    public CommonDataSource getDataSource() {
         return dataSource;
     }
 
@@ -90,19 +94,22 @@ public class ManagedDataSourceFactory {
         }
     }
 
-    public ServiceRegistration register() throws Exception {
+    public void register() throws Exception {
         Hashtable<String, Object> props = new Hashtable<String, 
Object>(this.properties);
+        props.put("aries.managed", "true");
         props.put("aries.xa.aware", "true");
         props.put(Constants.SERVICE_RANKING, getInt(Constants.SERVICE_RANKING, 
0) + 1000);
 
-        XADataSourceMCFFactory mcf = new XADataSourceMCFFactory();
+        boolean isXaDataSource = (dataSource instanceof XADataSource);
+
+        AbstractMCFFactory mcf = isXaDataSource ? new XADataSourceMCFFactory() 
: new DataSourceMCFFactory();
         mcf.setDataSource(dataSource);
         mcf.setExceptionSorterAsString(getString("aries.xa.exceptionSorter", 
"all"));
         mcf.setUserName(getString("aries.xa.username", null));
         mcf.setPassword(getString("aries.xa.password", null));
         mcf.init();
 
-        ConnectionManagerFactory cm = new ConnectionManagerFactory();
+        cm = new ConnectionManagerFactory();
         cm.setManagedConnectionFactory(mcf.getConnectionFactory());
         cm.setTransactionManager(transactionManager);
         cm.setAllConnectionsEqual(getBool("aries.xa.allConnectionsEquals", 
true));
@@ -115,16 +122,27 @@ public class ManagedDataSourceFactory {
         cm.setValidateOnMatch(getBool("aries.xa.validateOnMatch", true));
         cm.setBackgroundValidation(getBool("aries.xa.backgroundValidation", 
false));
         
cm.setBackgroundValidationMilliseconds(getInt("aries.xa.backgroundValidationMilliseconds",
 600000));
-        cm.setTransaction(getString("aries.xa.transaction", "xa"));
+        cm.setTransaction(getString("aries.xa.transaction", isXaDataSource ? 
"xa" : "local"));
         cm.setName(getResourceName());
         cm.init();
 
         BundleContext context = reference.getBundle().getBundleContext();
         DataSource ds = (DataSource) 
mcf.getConnectionFactory().createConnectionFactory(cm.getConnectionManager());
-        ServiceRegistration registration = 
context.registerService(DataSource.class.getName(), ds, props);
-        Recovery.recover(getResourceName(), dataSource, transactionManager);
-        return registration;
+        registration = context.registerService(DataSource.class, ds, props);
 
+        if (isXaDataSource) {
+            Recovery.recover(getResourceName(), (XADataSource) dataSource, 
transactionManager);
+        }
+    }
+
+    public void unregister() throws Exception {
+        if (registration != null) {
+            registration.unregister();
+            registration = null;
+        }
+        if (cm != null) {
+            cm.destroy();
+        }
     }
 
 }


Reply via email to