Skip to content

Commit

Permalink
Merge pull request #7 from vhalbert/teiid-2266
Browse files Browse the repository at this point in the history
TEIID-2266 changed Infinspan connector to support any Infinispan cache manager
  • Loading branch information
shawkins committed Oct 22, 2012
2 parents 726b177 + 5ff7cc1 commit b9c2929
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 44 deletions.
Expand Up @@ -58,30 +58,24 @@ 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);
}

@Override
public Map<?, ?> getMap(String name) throws TranslatorException {
Map<?,?> m = null;
LogManager.logTrace(LogConstants.CTX_CONNECTOR, "=== GetMap:", name, "==="); //$NON-NLS-1$ //$NON-NLS-2$
LogManager.logTrace(LogConstants.CTX_CONNECTOR, "=== GetMap:", (name != null ? name : "Default"), "==="); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

m = config.getCacheManager().getCache();
m = config.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);
}

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);
}

return m;
return m;
}

@Override
Expand Down
Expand Up @@ -40,6 +40,7 @@
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.manager.CacheContainer;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.manager.EmbeddedCacheManager;
import org.jboss.modules.Module;
import org.jboss.modules.ModuleIdentifier;
import org.jboss.modules.ModuleLoadException;
Expand All @@ -61,6 +62,8 @@ public class InfinispanManagedConnectionFactory extends BasicManagedConnectionFa
private Map<String, Class<?>> typeMap = null;
private String cacheTypes = null;
private BasicCacheContainer cacheContainer = null;


private String module;

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



protected BasicCacheContainer getCacheManager() {
return this.cacheContainer;
protected Map<Object,Object> getCache(String cacheName) {
if (cacheContainer != null) {
if (cacheName == null) {
return cacheContainer.getCache();
} else {
return cacheContainer.getCache(cacheName);
}
}
return null;
}

protected boolean isAlive() {
if (this.cacheContainer != null) {
return true;
}
return false;
}


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

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 +279,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();

this.cacheContainer = remoteCacheManager;
} catch (MalformedURLException e) {
throw new ResourceException(e);
} catch (IOException e) {
Expand All @@ -285,32 +300,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();

this.cacheContainer = 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(
cacheContainer = 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,21 +339,20 @@ 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;

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

@Override
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,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 b9c2929

Please sign in to comment.