Skip to content

Commit

Permalink
TEIID-2266 changed connector to support any Infinispan cache manager …
Browse files Browse the repository at this point in the history
…deployed to JNDI
  • Loading branch information
vhalbert committed Oct 22, 2012
1 parent b11e70e commit 1654cae
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 48 deletions.
Expand Up @@ -24,6 +24,8 @@

import java.util.Map;

import javax.resource.ResourceException;

import org.teiid.logging.LogConstants;
import org.teiid.logging.LogManager;
import org.teiid.resource.spi.BasicConnection;
Expand Down Expand Up @@ -58,7 +60,7 @@ public void close() {
* @return boolean true if CacheContainer has been started
*/
public boolean isAlive() {
boolean alive = (config == null ? false : config.getCacheManager() != null);
boolean alive = (config == null ? false : config.isAlive());
LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Infinispan Cache Connection is alive:", alive); //$NON-NLS-1$
return (alive);
}
Expand All @@ -68,20 +70,18 @@ public boolean isAlive() {
Map<?,?> m = null;
LogManager.logTrace(LogConstants.CTX_CONNECTOR, "=== GetMap:", name, "==="); //$NON-NLS-1$ //$NON-NLS-2$

m = config.getCacheManager().getCache();

if (name == null) {
m = config.getCacheManager().getCache();
} else {
m = config.getCacheManager().getCache(name);
}

if (m == null) {
final String msg = InfinispanPlugin.Util.getString("InfinispanConnection.cacheNotDefined", (name != null ? name : "Default") ); //$NON-NLS-1$ //$NON-NLS-2$
throw new TranslatorException(msg);
}
try {
m = config.getCache(name);
} catch (ResourceException e) {
throw new TranslatorException(e);
}

return m;
if (m == null) {
final String msg = InfinispanPlugin.Util.getString("InfinispanConnection.cacheNotDefined", (name != null ? name : "Default") ); //$NON-NLS-1$ //$NON-NLS-2$
throw new TranslatorException(msg);
}

return m;
}

@Override
Expand Down
Expand Up @@ -23,6 +23,8 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;
Expand All @@ -36,7 +38,6 @@
import javax.resource.spi.InvalidPropertyException;

import org.infinispan.Cache;
import org.infinispan.api.BasicCacheContainer;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.DefaultCacheManager;
Expand All @@ -50,7 +51,20 @@
import org.teiid.resource.spi.BasicConnectionFactory;
import org.teiid.resource.spi.BasicManagedConnectionFactory;


/**
* The InfinispanManagedConnectionFactory exposes the Infinispan CacheManager in
* order to obtain a cache by the {@link InfinispanConnectionImpl connection}.
* <p>
* Because Infinispan has various CacheManagers and they can be implemented
* differently based on clustering, remote access, or just basic default cache
* manager, it was easier to use reflections to get the cache, because they all
* implement the same methods for cache access. And this makes the connector
* logic simpler without having to know (or keep up with) which type of cache manager
* is being used.
*
* @author vhalbert
*
*/
public class InfinispanManagedConnectionFactory extends BasicManagedConnectionFactory {

private static final long serialVersionUID = -9153717006234080627L;
Expand All @@ -60,7 +74,13 @@ public class InfinispanManagedConnectionFactory extends BasicManagedConnectionFa
private String cacheJndiName=null;
private Map<String, Class<?>> typeMap = null;
private String cacheTypes = null;
private BasicCacheContainer cacheContainer = null;

private Object cacheManager;

private Method getCacheMethod = null;
private Method getCacheByNameMethod = null;
private Method isAliveMethod = null;

private String module;

@Override
Expand Down Expand Up @@ -240,24 +260,46 @@ public String getCacheJndiName() {
public void setCacheJndiName( String jndiName ) {
this.cacheJndiName = jndiName;
}



protected BasicCacheContainer getCacheManager() {
return this.cacheContainer;
@SuppressWarnings("unchecked")
protected Map<Object,Object> getCache(String cacheName) throws ResourceException {
if (cacheManager != null) {
if (cacheName == null) {
return (Map<Object, Object>) executeMethod(this.getCacheMethod, cacheManager, new Object[] {});
} else {
return (Map<Object, Object>) executeMethod(this.getCacheByNameMethod, cacheManager, new Object[] {cacheName});
}
}

return null;

}

protected boolean isAlive() {
if (this.cacheManager != null) {
if (this.isAliveMethod != null) {
try {
Object obj = executeMethod(this.isAliveMethod, cacheManager, null);
return Boolean.getBoolean(obj.toString());
} catch (ResourceException e) {
return false;
}
}
}
return false;
}


protected void createCacheContainer() throws ResourceException {
this.cacheContainer=this.getLocalCacheContainer();
if (this.cacheContainer == null) {
this.cacheContainer= createRemoteCacheContainer();
}
createLocalCacheContainer();
if (this.cacheManager == null) {
createRemoteCacheContainer();
}
deriveMethods();

}

private RemoteCacheManager createRemoteCacheContainer() throws ResourceException {
private void createRemoteCacheContainer() throws ResourceException {

RemoteCacheManager container = null;
if (this.getHotRodClientPropertiesFile() != null) {
File f = new File(this.getHotRodClientPropertiesFile());
if (! f.exists()) {
Expand All @@ -266,9 +308,11 @@ private RemoteCacheManager createRemoteCacheContainer() throws ResourceException
}
try {
Properties props = PropertiesUtils.load(f.getAbsolutePath());
container = new RemoteCacheManager(props);
container.start();
container.getCache();
RemoteCacheManager remoteCacheManager = new RemoteCacheManager(props);
remoteCacheManager.start();
remoteCacheManager.getCache();

cacheManager = remoteCacheManager;
} catch (MalformedURLException e) {
throw new ResourceException(e);
} catch (IOException e) {
Expand All @@ -285,32 +329,30 @@ private RemoteCacheManager createRemoteCacheContainer() throws ResourceException

Properties props = new Properties();
props.put("infinispan.client.hotrod.server_list", this.getRemoteServerList()); //$NON-NLS-1$
container = new RemoteCacheManager(props);
container.start();
RemoteCacheManager remoteCacheManager = new RemoteCacheManager(props);
remoteCacheManager.start();

cacheManager = remoteCacheManager;
LogManager
.logInfo(LogConstants.CTX_CONNECTOR,
"=== Using RemoteCacheManager (loaded by serverlist) ==="); //$NON-NLS-1$
}
}

return container;
}
protected BasicCacheContainer getLocalCacheContainer() throws ResourceException {
protected synchronized void createLocalCacheContainer() throws ResourceException {

Object cache = null;
if (this.getConfigurationFileNameForLocalCache() != null) {

DefaultCacheManager container;
if (this.getConfigurationFileNameForLocalCache() != null) {
try {
container = new DefaultCacheManager(
cacheManager = new DefaultCacheManager(
this.getConfigurationFileNameForLocalCache());
} catch (IOException e) {
throw new ResourceException(e);
}
LogManager
.logInfo(LogConstants.CTX_CONNECTOR,
"=== Using DefaultCacheManager (loaded by configuration) ==="); //$NON-NLS-1$
return container;

}

Expand All @@ -326,23 +368,91 @@ protected BasicCacheContainer getLocalCacheContainer() throws ResourceException
cache = context.lookup(jndiName);

if (cache == null) {
throw new ResourceException("Unable to find cache using JNDI: " + jndiName); //$NON-NLS-1$
throw new ResourceException(InfinispanPlugin.Util.getString("InfinispanManagedConnectionFactory.unableToFindCacheUsingJNDI", jndiName)); //$NON-NLS-1$
}

LogManager
.logInfo(LogConstants.CTX_CONNECTOR,
"=== Using CacheContainer (obtained by JNDI: " + jndiName + " ==="); //$NON-NLS-1$ //$NON-NLS-2$

"=== Using CacheContainer (obtained by JNDI: " + jndiName + " ==="); //$NON-NLS-1

return (BasicCacheContainer) cache;
cacheManager = cache;
} catch (Exception err) {
if (err instanceof RuntimeException) throw (RuntimeException)err;
throw new ResourceException(err);
}
}
return null;
}

/**
* DeriveMethods is used to identify the methods to use based on the
* CacheManager being used.
*
* @throws ResourceException
*/
private void deriveMethods() throws ResourceException {

try {
this.getCacheMethod = this.cacheManager.getClass()
.getDeclaredMethod("getCache"); //$NON-NLS-1$
this.getCacheByNameMethod = this.cacheManager.getClass()
.getDeclaredMethod("getCache", String.class); //$NON-NLS-1$
} catch (SecurityException e1) {
throw new ResourceException(e1);
} catch (NoSuchMethodException e1) {
throw new ResourceException(e1);
}

try {
this.isAliveMethod = this.cacheManager.getClass().getMethod(
"isStarted"); //$NON-NLS-1$

} catch (SecurityException e) {
throw new ResourceException(e);
} catch (NoSuchMethodException e) {
try {
this.isAliveMethod = this.cacheManager.getClass()
.getMethod("isDefaultRunning"); //$NON-NLS-1$
} catch (SecurityException e1) {
throw new ResourceException(e1);
} catch (NoSuchMethodException e1) {
// do nothing
}
}
}

/**
* Call to execute the method
*
* @param m
* is the method to execute
* @param api
* is the object to execute the method on
* @param parms
* are the parameters to pass when the method is executed
* @return Object return value
* @throws Exception
*/
private static Object executeMethod(Method m, Object api, Object[] parms)
throws ResourceException {
try {
if (parms != null) {
return m.invoke(api, parms);
}
return m.invoke(api, (Object[]) null);
} catch (InvocationTargetException x) {
x.printStackTrace();
Throwable cause = x.getCause();
System.err.format("invocation of %s failed: %s%n",
"set" + m.getName(), cause.getMessage());
LogManager.logError(LogConstants.CTX_CONNECTOR, "Error calling "
+ m.getName() + ":" + cause.getMessage());
throw new ResourceException(x.getMessage());
} catch (Exception e) {
e.printStackTrace();
throw new ResourceException(e.getMessage());
}
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
@@ -1 +1 @@
Dependencies: org.jboss.teiid.common-core,org.jboss.teiid.api,javax.api,org.jboss.teiid.translator.object,org.infinispan.client.hotrod,org.infinispan.cachestore.remote,org.jboss.modules
Dependencies: org.jboss.teiid.common-core,org.jboss.teiid.api,javax.api,org.jboss.teiid.translator.object,org.infinispan.client.hotrod,org.infinispan.cachestore.remote,org.jboss.as.clustering.infinispan,org.jboss.modules
Expand Up @@ -23,6 +23,7 @@
InfinispanManagedConnectionFactory.cacheTypeMapNotSet=CacheTypeMap has not been set
InfinispanManagedConnectionFactory.invalidServerConfiguration=Must set configuration as to how the cache will be obtained (i.e., configuration, jndi or server list)
InfinispanManagedConnectionFactory.clientPropertiesFileDoesNotExist=HotRod client properties file {0} does not exist
InfinispanManagedConnectionFactory.unableToFindCacheUsingJNDI=Unable to find cache using JNDI {0}

InfinispanConnection.cacheNotDefined={0} cache was not defined, see CacheTypeMap setting
InfinispanConnection.typeNotFound=Unable to determine class type in cache {0}
Expand Up @@ -46,7 +46,7 @@ public static void beforeEachClass() throws Exception {

// initialize container and cache
factory.createCacheContainer();
factory.getCacheManager().getCache(RemoteInfinispanTestHelper.CACHE_NAME).put("1", new Long(12345678));
factory.getCache(RemoteInfinispanTestHelper.CACHE_NAME).put("1", new Long(12345678));

}

Expand Down

0 comments on commit 1654cae

Please sign in to comment.