Author: van.halbert
Date: 2012-02-10 17:28:36 -0500 (Fri, 10 Feb 2012)
New Revision: 3864
Modified:
branches/7.7.x/connectors/sandbox/connector-coherence/pom.xml
branches/7.7.x/connectors/sandbox/connector-coherence/readme.txt
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceConnection.java
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceConnectionImpl.java
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceManagedConnectionFactory.java
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/rar/META-INF/ra.xml
branches/7.7.x/connectors/sandbox/connector-coherence/src/test/java/org/teiid/resource/adapter/coherence/TestCoherenceConnection.java
branches/7.7.x/connectors/sandbox/connector-coherence/src/test/resources/coherence-ds.xml
Log:
added insert and delete actions to the Coherence cache
Modified: branches/7.7.x/connectors/sandbox/connector-coherence/pom.xml
===================================================================
--- branches/7.7.x/connectors/sandbox/connector-coherence/pom.xml 2012-02-09 20:56:01 UTC
(rev 3863)
+++ branches/7.7.x/connectors/sandbox/connector-coherence/pom.xml 2012-02-10 22:28:36 UTC
(rev 3864)
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
- <!-- parent>
+ <parent>
<artifactId>sandbox</artifactId>
<groupId>org.jboss.teiid.connectors</groupId>
<version>7.7.0.CR1</version>
- </parent-->
+ </parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>connector-coherence</artifactId>
<groupId>org.jboss.teiid.connectors.sandbox</groupId>
@@ -32,6 +32,12 @@
<version>1.5</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>javax.transaction</groupId>
+ <artifactId>jta</artifactId>
+ <version>1.1</version>
+ <scope>provided</scope>
+ </dependency>
<dependency>
<groupId>coherence</groupId>
@@ -53,10 +59,8 @@
<build>
<plugins>
<plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-jar-plugin</artifactId>
- <version>2.2</version>
- <executions>
+ <artifactId>maven-jar-plugin</artifactId>
+ <executions>
<execution>
<id>build_jar</id>
<phase>process-classes</phase>
Modified: branches/7.7.x/connectors/sandbox/connector-coherence/readme.txt
===================================================================
--- branches/7.7.x/connectors/sandbox/connector-coherence/readme.txt 2012-02-09 20:56:01
UTC (rev 3863)
+++ branches/7.7.x/connectors/sandbox/connector-coherence/readme.txt 2012-02-10 22:28:36
UTC (rev 3864)
@@ -32,10 +32,10 @@
deploy the following files to the jboss as server
-- copy target/connector-coherence-<version>.rar to
server/<profile>/deploy/teiid directory
+- copy target/connector-coherence-<version>.rar to
server/<profile>/deploy/teiid/connectors directory
- copy src/main/resources/coherence-ds.xml to server/<profile>/deploy directory
a. Set the CacheName in the coherence-ds.xml
- b. Set the CacheTranslatorClassName in the coherence-ds.xml
+ b. (OPTIONAL - CONFIGURABLE) Set the CacheTranslatorClassName in the coherence-ds.xml
- copy the coherence.jar to the <profile>/lib directory
- see the translator_coherence to deploy the translator deployment
Modified:
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceConnection.java
===================================================================
---
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceConnection.java 2012-02-09
20:56:01 UTC (rev 3863)
+++
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceConnection.java 2012-02-10
22:28:36 UTC (rev 3864)
@@ -38,7 +38,7 @@
/**
- * Returns the objects from the Coherence Cache based on the
<code>criteria</code> filter sepcified.
+ * Returns the objects from the Coherence Cache based on the
<code>criteria</code> filter specified.
* @param criteria
* @return List of objects found in the cache.
* @throws ResourceException
@@ -51,4 +51,20 @@
*/
public String getCacheTranslatorClassName();
+ /**
+ * Call to add a new top level object to the cache.
+ * @param key to the object in the cache
+ * @param object to be added to the cache
+ * @throws ResourceException
+ */
+ public void add(Object key, Object object) throws ResourceException;
+
+
+ /**
+ * Call to remove the object based on its <code>key</code> that was
specified
+ * @param key of object to be removed
+ * @throws ResourceException
+ */
+ public void remove(Object key) throws ResourceException;
+
}
Modified:
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceConnectionImpl.java
===================================================================
---
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceConnectionImpl.java 2012-02-09
20:56:01 UTC (rev 3863)
+++
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceConnectionImpl.java 2012-02-10
22:28:36 UTC (rev 3864)
@@ -28,8 +28,6 @@
import java.util.Map;
import java.util.Set;
-import javax.resource.ResourceException;
-
import org.teiid.core.BundleUtil;
import org.teiid.logging.LogConstants;
import org.teiid.logging.LogManager;
@@ -39,7 +37,10 @@
import com.tangosol.net.NamedCache;
import com.tangosol.util.ConverterCollections;
import com.tangosol.util.Filter;
+import com.tangosol.util.TransactionMap;
+import javax.resource.ResourceException;
+
/**
* Represents an implementation for the connection to a Coherence data source.
*/
@@ -47,14 +48,12 @@
public static final BundleUtil UTIL =
BundleUtil.getBundleUtil(CoherenceConnection.class);
- private NamedCache sourceCache = null;
private String translatorName = null;
private String cacheName = null;
public CoherenceConnectionImpl(CoherenceManagedConnectionFactory config) throws
ResourceException {
-
- sourceCache = getCache(config.getCacheName());
+ this.cacheName = config.getCacheName();
translatorName = config.getCacheTranslatorClassName();
LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Coherence Connection has been
newly created"); //$NON-NLS-1$
}
@@ -71,9 +70,6 @@
*/
@Override
public void close() {
- CacheFactory.releaseCache( sourceCache);
- sourceCache = null;
-
LogManager.logDetail(LogConstants.CTX_CONNECTOR,"Coherence NamedCache " +
cacheName + " has been released."); //$NON-NLS-1$
}
@@ -95,30 +91,86 @@
}
public List<Object> get(Filter criteria) throws ResourceException {
-// System.out.println("CoherenceConnection cacheName: " + cacheName + "
filter: " + (criteria != null ? criteria.toString() : "NULL" ));
List<Object> objects = new ArrayList<Object>();
- Set<ConverterCollections.ConverterEntrySet> mapResult =
(Set<ConverterCollections.ConverterEntrySet>) sourceCache
+ LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Coherence Connection: Filter -
" + (criteria != null ? criteria.toString() : "NULL"));
+
+ Set<ConverterCollections.ConverterEntrySet> mapResult =
(Set<ConverterCollections.ConverterEntrySet>) getCache()
.entrySet(criteria);
for (Iterator it = mapResult.iterator(); it.hasNext();) {
Map.Entry o = (Map.Entry) it.next();
objects.add(o.getValue());
-// System.out.println("CoherenceConnection: loaded result " +
o.getValue().toString() );
+ }
+ return objects;
+ }
+
+ public void add(Object key, Object value) throws ResourceException {
+
+ NamedCache sourceCache = getCache();
+ if (sourceCache.containsKey(key)) {
+ throw new ResourceException("Unable to add object for key: " + key + "
to cache " + this.cacheName + ", because it already exist");
+ }
+
+ TransactionMap tmap = CacheFactory.getLocalTransaction(sourceCache);
+
+ tmap.setTransactionIsolation(TransactionMap.TRANSACTION_REPEATABLE_GET);
+ tmap.setConcurrency(TransactionMap.CONCUR_PESSIMISTIC);
+
+ tmap.begin();
+ try
+ {
+ tmap.put(key, value);
+ tmap.prepare();
+ tmap.commit();
+ }
+ catch (Exception e) {
+ throw new ResourceException(e);
+ }
+
+ sourceCache = getCache();
+ if (!sourceCache.containsKey(key)) {
+ throw new ResourceException("Problem adding object for key: " + key +
" to the cache " + this.cacheName +", object not found after add");
+ }
+
+ }
+
+ public void remove(Object key) throws ResourceException {
+ System.out.println("Remove: " + key);
+ NamedCache sourceCache = getCache();
+ if (!sourceCache.containsKey(key)) {
+ throw new ResourceException("Unable to remove object for key: " + key +
" from cache " + this.cacheName + ", because it doesn't exist");
}
-// System.out.println("CoherenceConnection: loaded " + objects.size() + "
results ");
+ TransactionMap tmap = CacheFactory.getLocalTransaction(sourceCache);
- return objects;
+ tmap.setTransactionIsolation(TransactionMap.TRANSACTION_REPEATABLE_GET);
+ tmap.setConcurrency(TransactionMap.CONCUR_OPTIMISTIC);
+
+ tmap.begin();
+ try
+ {
+ tmap.remove(key);
+ tmap.prepare();
+ tmap.commit();
+ }
+ catch (Exception e) {
+ throw new ResourceException(e);
+ }
+
+ if (getCache().containsKey(key)) {
+ throw new ResourceException("Unable to remove object for key: " + key +
" from the cache " + this.cacheName );
+ }
+
}
- private NamedCache getCache(String name) {
- this.cacheName = name;
+
+ private NamedCache getCache() {
- sourceCache = CacheFactory.getCache(name);
+ NamedCache sourceCache = CacheFactory.getCache(this.cacheName,
this.getClass().getClassLoader());
LogManager.logDetail(LogConstants.CTX_CONNECTOR,"Coherence NamedCache " +
cacheName + " has been obtained."); //$NON-NLS-1$
@@ -126,5 +178,4 @@
}
-
}
Modified:
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceManagedConnectionFactory.java
===================================================================
---
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceManagedConnectionFactory.java 2012-02-09
20:56:01 UTC (rev 3863)
+++
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/java/org/teiid/resource/adapter/coherence/CoherenceManagedConnectionFactory.java 2012-02-10
22:28:36 UTC (rev 3864)
@@ -21,15 +21,17 @@
*/
package org.teiid.resource.adapter.coherence;
-import javax.resource.ResourceException;
-
import org.teiid.core.BundleUtil;
import org.teiid.resource.spi.BasicConnectionFactory;
import org.teiid.resource.spi.BasicManagedConnectionFactory;
+import javax.resource.ResourceException;
+
public class CoherenceManagedConnectionFactory extends BasicManagedConnectionFactory {
private static final long serialVersionUID = -1832915223199053471L;
+ private static final String DEFAULT_CACHE_ADAPTER_CLASS_NAME =
"org.teiid.translator.coherence.SourceCacheAdapter";
+
public static final BundleUtil UTIL =
BundleUtil.getBundleUtil(CoherenceManagedConnectionFactory.class);
private String cacheName;
@@ -44,7 +46,7 @@
}
if (cacheTranslatorClassName == null) {
- throw new
ResourceException(UTIL.getString("CoherenceVisitor.cacheTranslatorClassName_not_set"));
//$NON-NLS-1$
+ cacheTranslatorClassName = DEFAULT_CACHE_ADAPTER_CLASS_NAME;
}
return new BasicConnectionFactory() {
Modified:
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/rar/META-INF/ra.xml
===================================================================
---
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/rar/META-INF/ra.xml 2012-02-09
20:56:01 UTC (rev 3863)
+++
branches/7.7.x/connectors/sandbox/connector-coherence/src/main/rar/META-INF/ra.xml 2012-02-10
22:28:36 UTC (rev 3864)
@@ -47,7 +47,7 @@
<config-property-type>java.lang.String</config-property-type>
</config-property>
<config-property>
- <description>{$display:"Coherence Object Translator Class
Name",$description:"Name of the class to translate objects in Coherence
Cache",$required:"true"}</description>
+ <description>{$display:"Coherence Object Translator Class
Name",$description:"Name of the class to translate objects in Coherence
Cache"}</description>
<config-property-name>CacheTranslatorClassName</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value>org.teiid.translator.coherence.SourceCacheAdapter</config-property-value>
Modified:
branches/7.7.x/connectors/sandbox/connector-coherence/src/test/java/org/teiid/resource/adapter/coherence/TestCoherenceConnection.java
===================================================================
---
branches/7.7.x/connectors/sandbox/connector-coherence/src/test/java/org/teiid/resource/adapter/coherence/TestCoherenceConnection.java 2012-02-09
20:56:01 UTC (rev 3863)
+++
branches/7.7.x/connectors/sandbox/connector-coherence/src/test/java/org/teiid/resource/adapter/coherence/TestCoherenceConnection.java 2012-02-10
22:28:36 UTC (rev 3864)
@@ -226,6 +226,46 @@
assertEquals("Did not get expected number of trades", 1, trades.size());
+ }
+
+ @Test
+ public void testAddAndRemoveTrade() throws Exception {
+
+ CoherenceManagedConnectionFactory f = new CoherenceManagedConnectionFactory();
+ f.setCacheName(CACHE_NAME);
+ f.setCacheTranslatorClassName(OBJECT_TRANSLATOR);
+
+ CoherenceConnection conn = (CoherenceConnection)
f.createConnectionFactory().getConnection();
+
+ Trade trade = new Trade();
+ trade.setId(999);
+ trade.setName("NameIs " + 999);
+ trade.setLegs(null);
+ conn.add(999l, trade);
+
+
+ // NOTE: Coherence, because the datatype of ID is long, wants the "l"
appended to the value
+ Filter criteria = CoherenceFilterUtil.createFilter("Id = 999l");
+
+ List<?> trades = conn.get(criteria);
+ assertNotNull(trades);
+ assertEquals("Did not get expected 999 trade", 1, trades.size());
+
+ long l = 999l;
+ criteria = CoherenceFilterUtil.createCompareFilter("Id", l,
Comparison.Operator.EQ, Long.class);
+
+ trades = conn.get(CoherenceFilterUtil.createFilter("Id = 999l"));
+ assertNotNull(trades);
+ assertEquals("Did not get expected 999 trade", 1, trades.size());
+
+
+ conn.remove(l);
+
+ trades = conn.get(CoherenceFilterUtil.createFilter("Id = 999l"));
+ assertNotNull(trades);
+ assertEquals("Expected no trade", 0, trades.size());
+
+
}
}
Modified:
branches/7.7.x/connectors/sandbox/connector-coherence/src/test/resources/coherence-ds.xml
===================================================================
---
branches/7.7.x/connectors/sandbox/connector-coherence/src/test/resources/coherence-ds.xml 2012-02-09
20:56:01 UTC (rev 3863)
+++
branches/7.7.x/connectors/sandbox/connector-coherence/src/test/resources/coherence-ds.xml 2012-02-10
22:28:36 UTC (rev 3864)
@@ -14,8 +14,8 @@
<!-- Coherence Cache Name (required)-->
<config-property name="CacheName">Trades</config-property>
- <!-- Coherence Object Translator Class Name (required)-->
- <config-property
name="CacheTranslatorClassName">org.teiid.translator.coherence.TradesCacheSource</config-property>
+ <!-- (OPTIONAL) Coherence Object Translator Class Name -->
+ <!-- config-property
name="CacheTranslatorClassName">org.teiid.translator.coherence.SourceCacheAdapter</config-property-->
<max-pool-size>20</max-pool-size>