Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package me.prettyprint.cassandra.jndi;
  
  
  import javax.naming.Name;
 
 
A factory for JNDI Resource managed objects. Responsible for creating a me.prettyprint.hector.api.Keyspace references for passing to me.prettyprint.hector.api.factory.HFactory. A limited set of configuration parameters are supported. These parameters are defined in a web application's context.xml file. Parameter descriptions can be found in me.prettyprint.cassandra.service.CassandraHostConfigurator

     <Resource name="cassandra/CassandraClientFactory"
               auth="Container"
               type="me.prettyprint.cassandra.api.Keyspace"
               factory="me.prettyprint.cassandra.jndi.CassandraClientJndiResourceFactory"
               hosts="cass1:9160,cass2:9160,cass3:9160"
               keyspace="Keyspace1"
               clusterName="Test Cluster" 
               maxActive="20"
               maxWaitTimeWhenExhausted="10"
               autoDiscoverHosts="true"
               runAutoDiscoveryAtStartup="true"/>      
 

Author(s):
Perry Hoekstra (dutchman_mn@charter.net)
zznate
Since:
0.5.1-8
 
 
 public class CassandraClientJndiResourceFactory implements ObjectFactory {
   private Logger log = LoggerFactory.getLogger(CassandraClientJndiResourceFactory.class);
   
   private Cluster cluster;
   private Keyspace keyspace;
  
  
Creates an object using the location or reference information specified.

Parameters:
object The possibly null object containing location or reference information that can be used in creating an object.
jndiName The name of this object relative to nameCtx, or null if no name is specified.
context The context relative to which the name parameter is specified, or null if name is relative to the default initial context.
environment The possibly null environment that is used in creating the object.
Returns:
Object - The object created; null if an object cannot be created.
Throws:
java.lang.Exception - if this object factory encountered an exception while attempting to create an object, and no other object factories are to be tried.
 
   public Object getObjectInstance(Object objectName jndiNameContext context,
       Hashtable<?, ?> environmentthrows Exception {  
     Reference resourceRef = null;
     
     if (object instanceof Reference) {
         resourceRef = (Referenceobject;
     } else {
       throw new Exception("Object provided is not a javax.naming.Reference type");
     }
     
     // config CassandraHostConfigurator  
     if (  == null ) {
       configure(resourceRef);
     }
 
     return ;
   }
   
   private void configure(Reference resourceRefthrows Exception {
  // required
     RefAddr hostsRefAddr = resourceRef.get("hosts");
     RefAddr clusterNameRef = resourceRef.get("clusterName");
     RefAddr keyspaceNameRef = resourceRef.get("keyspace");
     // optional
     RefAddr maxActiveRefAddr = resourceRef.get("maxActive");
     RefAddr maxWaitTimeWhenExhausted = resourceRef.get("maxWaitTimeWhenExhausted");
     RefAddr autoDiscoverHosts = resourceRef.get("autoDiscoverHosts");
     RefAddr runAutoDiscoverAtStartup = resourceRef.get("runAutoDiscoveryAtStartup");
     RefAddr retryDownedHostDelayInSeconds = resourceRef.get("retryDownedHostDelayInSeconds");
     
    if ( hostsRefAddr == null || hostsRefAddr.getContent() == null) {
      throw new Exception("A url and port on which Cassandra is installed and listening " + 
      "on must be provided as a ResourceParams in the context.xml");
    }        
    if ( autoDiscoverHosts != null ) {
      if ( runAutoDiscoverAtStartup  != null )
    }    
    if ( retryDownedHostDelayInSeconds != null ) {
      int retryDelay = Integer.parseInt((String)retryDownedHostDelayInSeconds.getContent());
      // disable retry if less than 1
      if ( retryDelay < 1 )
        .setRetryDownedHosts(false);      
    }
    if ( maxActiveRefAddr != null ) 
      .setMaxActive(Integer.parseInt((String)maxActiveRefAddr.getContent()));
    if ( maxWaitTimeWhenExhausted != null ) 
      .setMaxWaitTimeWhenExhausted(Integer.parseInt((String)maxWaitTimeWhenExhausted.getContent()));
    
    if ( .isDebugEnabled() )
      .debug("JNDI resource created with CassandraHostConfiguration: {}".getAutoDiscoverHosts());
    
     = HFactory.createCluster((String)clusterNameRef.getContent(), );
     = HFactory.createKeyspace((String)keyspaceNameRef.getContent(), );
  }
New to GrepCode? Check out our FAQ X