[dna-commits] DNA SVN: r399 - in trunk/connectors/dna-connector-jbosscache/src: main/resources/org/jboss/dna/connector/jbosscache and 1 other directories.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Thu Aug 7 11:42:07 EDT 2008


Author: rhauch
Date: 2008-08-07 11:42:07 -0400 (Thu, 07 Aug 2008)
New Revision: 399

Modified:
   trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheConnectorI18n.java
   trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheSource.java
   trunk/connectors/dna-connector-jbosscache/src/main/resources/org/jboss/dna/connector/jbosscache/JBossCacheConnectorI18n.properties
   trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheSourceTest.java
Log:
DNA-83 - Federate content from JBoss Cache instance(s)
http://jira.jboss.com/jira/browse/DNA-83

Changed how the JBossCacheSource is turned into a JNDI Reference, removing all static references to the instances.

Modified: trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheConnectorI18n.java
===================================================================
--- trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheConnectorI18n.java	2008-08-07 15:38:59 UTC (rev 398)
+++ trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheConnectorI18n.java	2008-08-07 15:42:07 UTC (rev 399)
@@ -32,6 +32,10 @@
 
     public static I18n connectorName;
     public static I18n nodeDoesNotExist;
+    public static I18n propertyIsRequired;
+    public static I18n errorSerializingCachePolicyInSource;
+    public static I18n objectFoundInJndiWasNotCache;
+    public static I18n objectFoundInJndiWasNotCacheFactory;
 
     static {
         try {

Modified: trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheSource.java
===================================================================
--- trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheSource.java	2008-08-07 15:38:59 UTC (rev 398)
+++ trunk/connectors/dna-connector-jbosscache/src/main/java/org/jboss/dna/connector/jbosscache/JBossCacheSource.java	2008-08-07 15:42:07 UTC (rev 399)
@@ -21,36 +21,53 @@
  */
 package org.jboss.dna.connector.jbosscache;
 
-import java.util.Collections;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Hashtable;
-import java.util.Set;
+import java.util.Map;
 import java.util.UUID;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
+import javax.naming.BinaryRefAddr;
 import javax.naming.Context;
 import javax.naming.InitialContext;
-import javax.naming.NamingException;
 import javax.naming.RefAddr;
 import javax.naming.Reference;
+import javax.naming.Referenceable;
 import javax.naming.StringRefAddr;
 import javax.naming.spi.ObjectFactory;
-import net.jcip.annotations.GuardedBy;
 import net.jcip.annotations.ThreadSafe;
 import org.jboss.cache.Cache;
 import org.jboss.cache.CacheFactory;
 import org.jboss.cache.DefaultCacheFactory;
-import org.jboss.dna.common.util.ArgCheck;
+import org.jboss.dna.common.i18n.I18n;
 import org.jboss.dna.spi.DnaLexicon;
 import org.jboss.dna.spi.cache.CachePolicy;
 import org.jboss.dna.spi.connector.AbstractRepositorySource;
 import org.jboss.dna.spi.connector.RepositoryConnection;
+import org.jboss.dna.spi.connector.RepositorySource;
 import org.jboss.dna.spi.connector.RepositorySourceCapabilities;
+import org.jboss.dna.spi.connector.RepositorySourceException;
 import org.jboss.dna.spi.graph.Name;
+import org.jboss.dna.spi.graph.Property;
 
 /**
+ * A repository source that uses a JBoss Cache instance to manage the content. This source is capable of using an existing
+ * {@link Cache} instance or creating a new instance. This process is controlled entirely by the JavaBean properties of the
+ * JBossCacheSource instance.
+ * <p>
+ * This source first attempts to find an existing cache in {@link #getCacheJndiName() JNDI}. If none is found, then it attempts to
+ * create a cache instance using the {@link CacheFactory} found in {@link #getCacheFactoryJndiName() JNDI} (or the
+ * {@link DefaultCacheFactory} if no such factory is available) and the {@link #getCacheConfigurationName() cache configuration
+ * name} if supplied or the default configuration if not set.
+ * </p>
+ * <p>
+ * Like other {@link RepositorySource} classes, instances of JBossCacheSource can be placed into JNDI and do support the creation
+ * of {@link Referenceable JNDI referenceable} objects and resolution of references into JBossCacheSource.
+ * 
  * @author Randall Hauch
  */
 @ThreadSafe
@@ -59,49 +76,24 @@
     private static final long serialVersionUID = 1L;
     public static final String DEFAULT_UUID_PROPERTY_NAME = DnaLexicon.PropertyNames.UUID;
 
-    private static final ConcurrentMap<String, JBossCacheSource> sources = new ConcurrentHashMap<String, JBossCacheSource>();
-    private static final ReadWriteLock sourcesLock = new ReentrantReadWriteLock();
+    protected static final String ROOT_NODE_UUID = "rootNodeUuid";
+    protected static final String SOURCE_NAME = "sourceName";
+    protected static final String DEFAULT_CACHE_POLICY = "defaultCachePolicy";
+    protected static final String CACHE_CONFIGURATION_NAME = "cacheConfigurationName";
+    protected static final String CACHE_FACTORY_JNDI_NAME = "cacheFactoryJndiName";
+    protected static final String CACHE_JNDI_NAME = "cacheJndiName";
+    protected static final String UUID_PROPERTY_NAME = "uuidPropertyName";
+    protected static final String RETRY_LIMIT = "retryLimit";
 
-    /**
-     * Get the names of the in-memory repository sources that are currently registered
-     * 
-     * @return the unmodifiable set of names
-     */
-    public static Set<String> getSourceNames() {
-        Lock lock = sourcesLock.readLock();
-        try {
-            lock.lock();
-            return Collections.unmodifiableSet(sources.keySet());
-        } finally {
-            lock.unlock();
-        }
-    }
-
-    /**
-     * Get the source with the supplied name.
-     * 
-     * @param name the name
-     * @return the source, or null if there is no source with the supplied name
-     */
-    public static JBossCacheSource getSource( String name ) {
-        Lock lock = sourcesLock.readLock();
-        try {
-            lock.lock();
-            return sources.get(name);
-        } finally {
-            lock.unlock();
-        }
-    }
-
-    @GuardedBy( "sourcesLock" )
     private String name;
-    @GuardedBy( "this" )
-    private String jndiName;
     private UUID rootNodeUuid = UUID.randomUUID();
     private CachePolicy defaultCachePolicy;
     private String cacheConfigurationName;
+    private String cacheFactoryJndiName;
+    private String cacheJndiName;
     private String uuidPropertyName = DEFAULT_UUID_PROPERTY_NAME;
     private transient Cache<Name, Object> cache;
+    private transient Context jndiContext;
 
     /**
      * Create a repository source instance.
@@ -110,6 +102,23 @@
     }
 
     /**
+     * {@inheritDoc}
+     */
+    public String getName() {
+        return this.name;
+    }
+
+    /**
+     * Set the name of this source
+     * 
+     * @param name the name for this source
+     */
+    public synchronized void setName( String name ) {
+        if (this.name == name || this.name != null && this.name.equals(name)) return; // unchanged
+        this.name = name;
+    }
+
+    /**
      * Get the default cache policy for this source, or null if the global default cache policy should be used
      * 
      * @return the default cache policy, or null if this source has no explicit default cache policy
@@ -121,145 +130,303 @@
     /**
      * @param defaultCachePolicy Sets defaultCachePolicy to the specified value.
      */
-    public void setDefaultCachePolicy( CachePolicy defaultCachePolicy ) {
+    public synchronized void setDefaultCachePolicy( CachePolicy defaultCachePolicy ) {
+        if (this.defaultCachePolicy == defaultCachePolicy || this.defaultCachePolicy != null
+            && this.defaultCachePolicy.equals(defaultCachePolicy)) return; // unchanged
         this.defaultCachePolicy = defaultCachePolicy;
     }
 
     /**
-     * @return rootNodeUuid
+     * Get the name in JNDI of a {@link Cache} instance that should be used by this source.
+     * <p>
+     * This source first attempts to find an existing cache in {@link #getCacheJndiName() JNDI}. If none is found, then it
+     * attempts to create a cache instance using the {@link CacheFactory} found in {@link #getCacheFactoryJndiName() JNDI} (or the
+     * {@link DefaultCacheFactory} if no such factory is available) and the {@link #getCacheConfigurationName() cache
+     * configuration name} if supplied or the default configuration if not set.
+     * </p>
+     * 
+     * @return the JNDI name of the {@link Cache} instance that should be used, or null if the cache is to be created with a cache
+     *         factory {@link #getCacheFactoryJndiName() found in JNDI} using the specified {@link #getCacheConfigurationName()
+     *         cache configuration name}.
+     * @see #setCacheJndiName(String)
+     * @see #getCacheConfigurationName()
+     * @see #getCacheFactoryJndiName()
      */
-    public UUID getRootNodeUuid() {
-        return this.rootNodeUuid;
+    public String getCacheJndiName() {
+        return cacheJndiName;
     }
 
     /**
-     * @param rootNodeUuid Sets rootNodeUuid to the specified value.
+     * Set the name in JNDI of a {@link Cache} instance that should be used by this source.
+     * <p>
+     * This source first attempts to find an existing cache in {@link #getCacheJndiName() JNDI}. If none is found, then it
+     * attempts to create a cache instance using the {@link CacheFactory} found in {@link #getCacheFactoryJndiName() JNDI} (or the
+     * {@link DefaultCacheFactory} if no such factory is available) and the {@link #getCacheConfigurationName() cache
+     * configuration name} if supplied or the default configuration if not set.
+     * </p>
+     * 
+     * @param cacheJndiName the JNDI name of the {@link Cache} instance that should be used, or null if the cache is to be created
+     *        with a cache factory {@link #getCacheFactoryJndiName() found in JNDI} using the specified
+     *        {@link #getCacheConfigurationName() cache configuration name}.
+     * @see #getCacheJndiName()
+     * @see #getCacheConfigurationName()
+     * @see #getCacheFactoryJndiName()
      */
-    public void setRootNodeUuid( UUID rootNodeUuid ) {
-        this.rootNodeUuid = rootNodeUuid != null ? rootNodeUuid : UUID.randomUUID();
+    public synchronized void setCacheJndiName( String cacheJndiName ) {
+        if (this.cacheJndiName == cacheJndiName || this.cacheJndiName != null && this.cacheJndiName.equals(cacheJndiName)) return; // unchanged
+        this.cacheJndiName = cacheJndiName;
     }
 
     /**
-     * @return uuidPropertyName
+     * Get the name in JNDI of a {@link CacheFactory} instance that should be used to create the cache for this source.
+     * <p>
+     * This source first attempts to find an existing cache in {@link #getCacheJndiName() JNDI}. If none is found, then it
+     * attempts to create a cache instance using the {@link CacheFactory} found in {@link #getCacheFactoryJndiName() JNDI} (or the
+     * {@link DefaultCacheFactory} if no such factory is available) and the {@link #getCacheConfigurationName() cache
+     * configuration name} if supplied or the default configuration if not set.
+     * </p>
+     * 
+     * @return the JNDI name of the {@link CacheFactory} instance that should be used, or null if the {@link DefaultCacheFactory}
+     *         should be used if a cache is to be created
+     * @see #setCacheFactoryJndiName(String)
+     * @see #getCacheConfigurationName()
+     * @see #getCacheJndiName()
      */
-    public String getUuidPropertyName() {
-        return this.uuidPropertyName;
+    public String getCacheFactoryJndiName() {
+        return cacheFactoryJndiName;
     }
 
     /**
-     * @param uuidPropertyName Sets uuidPropertyName to the specified value.
+     * Set the name in JNDI of a {@link CacheFactory} instance that should be used to obtain the {@link Cache} instance used by
+     * this source.
+     * <p>
+     * This source first attempts to find an existing cache in {@link #getCacheJndiName() JNDI}. If none is found, then it
+     * attempts to create a cache instance using the {@link CacheFactory} found in {@link #getCacheFactoryJndiName() JNDI} (or the
+     * {@link DefaultCacheFactory} if no such factory is available) and the {@link #getCacheConfigurationName() cache
+     * configuration name} if supplied or the default configuration if not set.
+     * </p>
+     * 
+     * @param jndiName the JNDI name of the {@link CacheFactory} instance that should be used, or null if the
+     *        {@link DefaultCacheFactory} should be used if a cache is to be created
+     * @see #setCacheFactoryJndiName(String)
+     * @see #getCacheConfigurationName()
+     * @see #getCacheJndiName()
      */
-    public synchronized void setUuidPropertyName( String uuidPropertyName ) {
-        this.uuidPropertyName = uuidPropertyName != null ? uuidPropertyName.trim() : DEFAULT_UUID_PROPERTY_NAME;
+    public synchronized void setCacheFactoryJndiName( String jndiName ) {
+        if (this.cacheFactoryJndiName == jndiName || this.cacheFactoryJndiName != null
+            && this.cacheFactoryJndiName.equals(jndiName)) return; // unchanged
+        this.cacheFactoryJndiName = jndiName;
     }
 
     /**
-     * If you use this to set a JNDI name, this source will be bound to that name using the default {@link InitialContext}. You
-     * can also do this manually if you have additional requirements.
+     * Get the name of the configuration that should be used if a {@link Cache cache} is to be created using the
+     * {@link CacheFactory} found in JNDI or the {@link DefaultCacheFactory} if needed.
+     * <p>
+     * This source first attempts to find an existing cache in {@link #getCacheJndiName() JNDI}. If none is found, then it
+     * attempts to create a cache instance using the {@link CacheFactory} found in {@link #getCacheFactoryJndiName() JNDI} (or the
+     * {@link DefaultCacheFactory} if no such factory is available) and the {@link #getCacheConfigurationName() cache
+     * configuration name} if supplied or the default configuration if not set.
+     * </p>
      * 
-     * @param name the JNDI name
-     * @throws NamingException if there is a problem registering this object
-     * @see #getJndiName()
+     * @return the name of the configuration that should be passed to the {@link CacheFactory}, or null if the default
+     *         configuration should be used
+     * @see #setCacheConfigurationName(String)
+     * @see #getCacheFactoryJndiName()
+     * @see #getCacheJndiName()
      */
-    public void setJndiName( String name ) throws NamingException {
-        setJndiName(name, null);
+    public String getCacheConfigurationName() {
+        return cacheConfigurationName;
     }
 
     /**
-     * Register this source in JNDI under the supplied name using the supplied context. to set a JNDI name, this source will be
-     * bound to that name using the default {@link InitialContext}. You can also do this manually if you have additional
-     * requirements.
+     * Get the name of the configuration that should be used if a {@link Cache cache} is to be created using the
+     * {@link CacheFactory} found in JNDI or the {@link DefaultCacheFactory} if needed.
+     * <p>
+     * This source first attempts to find an existing cache in {@link #getCacheJndiName() JNDI}. If none is found, then it
+     * attempts to create a cache instance using the {@link CacheFactory} found in {@link #getCacheFactoryJndiName() JNDI} (or the
+     * {@link DefaultCacheFactory} if no such factory is available) and the {@link #getCacheConfigurationName() cache
+     * configuration name} if supplied or the default configuration if not set.
+     * </p>
      * 
-     * @param name the JNDI name, or null if this object is to no longer be registered
-     * @param context the JNDI context, or null if the {@link InitialContext} should be used
-     * @throws NamingException if there is a problem registering this object
-     * @see #getJndiName()
+     * @param cacheConfigurationName the name of the configuration that should be passed to the {@link CacheFactory}, or null if
+     *        the default configuration should be used
+     * @see #getCacheConfigurationName()
+     * @see #getCacheFactoryJndiName()
+     * @see #getCacheJndiName()
      */
-    public synchronized void setJndiName( String name,
-                                          Context context ) throws NamingException {
-        ArgCheck.isNotNull(name, "name");
-        if (context == null) context = new InitialContext();
+    public synchronized void setCacheConfigurationName( String cacheConfigurationName ) {
+        if (this.cacheConfigurationName == cacheConfigurationName || this.cacheConfigurationName != null
+            && this.cacheConfigurationName.equals(cacheConfigurationName)) return; // unchanged
+        this.cacheConfigurationName = cacheConfigurationName;
+    }
 
-        // First register in JNDI under the new name ...
-        if (name != null) {
-            context.bind(name, this);
-        }
-        // Second, unregister from JNDI if there is already a name ...
-        if (jndiName != null && !jndiName.equals(name)) {
-            context.unbind(jndiName);
-        }
-        // Record the new name ...
-        this.jndiName = name;
+    /**
+     * Get the UUID of the root node for the cache. If the cache exists, this UUID is not used but is instead set to the UUID of
+     * the existing root node.
+     * 
+     * @return the UUID of the root node for the cache.
+     */
+    public String getRootNodeUuid() {
+        return this.rootNodeUuid.toString();
     }
 
     /**
-     * Gets the JNDI name this source is bound to. Only valid if you used setJNDIName to bind it.
+     * Get the UUID of the root node for the cache. If the cache exists, this UUID is not used but is instead set to the UUID of
+     * the existing root node.
      * 
-     * @return the JNDI name, or null if it is not bound in JNDI
-     * @see #setJndiName(String)
+     * @return the UUID of the root node for the cache.
      */
-    public synchronized String getJndiName() {
-        return jndiName;
+    public UUID getRootNodeUuidObject() {
+        return this.rootNodeUuid;
     }
 
     /**
-     * {@inheritDoc}
+     * Set the UUID of the root node in this repository. If the cache exists, this UUID is not used but is instead set to the UUID
+     * of the existing root node.
+     * 
+     * @param rootNodeUuid the UUID of the root node for the cache, or null if the UUID should be randomly generated
      */
-    public String getName() {
-        Lock lock = sourcesLock.readLock();
-        try {
-            lock.lock();
-            return this.name;
-        } finally {
-            lock.unlock();
-        }
+    public synchronized void setRootNodeUuid( String rootNodeUuid ) {
+        UUID uuid = null;
+        if (rootNodeUuid == null) uuid = UUID.randomUUID();
+        else uuid = UUID.fromString(rootNodeUuid);
+        if (this.rootNodeUuid.equals(uuid)) return; // unchanged
+        this.rootNodeUuid = uuid;
     }
 
     /**
-     * @param name Sets name to the specified value.
-     * @return true if the name was changed, or false if an existing instance already exists with that name
+     * Get the {@link Property#getName() property name} where the UUID is stored for each node.
+     * 
+     * @return the name of the UUID property; never null
      */
-    public boolean setName( String name ) {
-        Lock lock = sourcesLock.writeLock();
-        try {
-            lock.lock();
-            // Determine if this name is allowed ...
-            if (name != null && sources.containsKey(name)) return false;
+    public String getUuidPropertyName() {
+        return this.uuidPropertyName;
+    }
 
-            // Remove this object under its current name
-            if (this.name != null) {
-                sources.remove(this.name);
-            }
-            // Register this object under the new name
-            this.name = name;
-            if (this.name != null) {
-                sources.put(this.name, this);
-            }
-            return true;
-        } finally {
-            lock.unlock();
-        }
+    /**
+     * Set the {@link Property#getName() property name} where the UUID is stored for each node.
+     * 
+     * @param uuidPropertyName the name of the UUID property, or null if the {@link #DEFAULT_UUID_PROPERTY_NAME default name}
+     *        should be used
+     */
+    public synchronized void setUuidPropertyName( String uuidPropertyName ) {
+        if (uuidPropertyName == null || uuidPropertyName.trim().length() == 0) uuidPropertyName = DEFAULT_UUID_PROPERTY_NAME;
+        if (this.uuidPropertyName.equals(uuidPropertyName)) return; // unchanged
+        this.uuidPropertyName = uuidPropertyName;
     }
 
     /**
      * {@inheritDoc}
      */
+    @SuppressWarnings( "unchecked" )
     @Override
     protected synchronized RepositoryConnection createConnection() {
+        if (getName() == null) {
+            I18n msg = JBossCacheConnectorI18n.propertyIsRequired;
+            throw new RepositorySourceException(getName(), msg.text("name"));
+        }
+        if (getUuidPropertyName() == null) {
+            I18n msg = JBossCacheConnectorI18n.propertyIsRequired;
+            throw new RepositorySourceException(getName(), msg.text("uuidPropertyName"));
+        }
         if (this.cache == null) {
-            CacheFactory<Name, Object> factory = new DefaultCacheFactory<Name, Object>();
-            cache = factory.createCache(cacheConfigurationName);
+            // First look for an existing cache instance in JNDI ...
+            Context context = getContext();
+            String jndiName = this.getCacheJndiName();
+            if (jndiName != null && jndiName.trim().length() != 0) {
+                Object object = null;
+                try {
+                    if (context == null) context = new InitialContext();
+                    object = context.lookup(jndiName);
+                    if (object != null) cache = (Cache<Name, Object>)object;
+                } catch (ClassCastException err) {
+                    I18n msg = JBossCacheConnectorI18n.objectFoundInJndiWasNotCache;
+                    String className = object != null ? object.getClass().getName() : "null";
+                    throw new RepositorySourceException(getName(), msg.text(jndiName, this.getName(), className), err);
+                } catch (Throwable err) {
+                    // try loading
+                }
+            }
+            if (cache == null) {
+                // Then look for a cache factory in JNDI ...
+                CacheFactory<Name, Object> cacheFactory = null;
+                jndiName = getCacheFactoryJndiName();
+                if (jndiName != null && jndiName.trim().length() != 0) {
+                    Object object = null;
+                    try {
+                        if (context == null) context = new InitialContext();
+                        object = context.lookup(jndiName);
+                        if (object != null) cacheFactory = (CacheFactory<Name, Object>)object;
+                    } catch (ClassCastException err) {
+                        I18n msg = JBossCacheConnectorI18n.objectFoundInJndiWasNotCacheFactory;
+                        String className = object != null ? object.getClass().getName() : "null";
+                        throw new RepositorySourceException(getName(), msg.text(jndiName, this.getName(), className), err);
+                    } catch (Throwable err) {
+                        // try loading
+                    }
+                }
+                if (cacheFactory == null) cacheFactory = new DefaultCacheFactory<Name, Object>();
+
+                // Now, get the configuration name ...
+                String configName = this.getCacheConfigurationName();
+                if (configName != null) {
+                    cache = cacheFactory.createCache(configName);
+                } else {
+                    cache = cacheFactory.createCache();
+                }
+            }
         }
         return new JBossCacheConnection(this, this.cache);
     }
 
+    protected Context getContext() {
+        return this.jndiContext;
+    }
+
+    protected synchronized void setContext( Context context ) {
+        this.jndiContext = context;
+    }
+
     /**
      * {@inheritDoc}
      */
-    public Reference getReference() {
+    public synchronized Reference getReference() {
         String className = getClass().getName();
-        String factoryClassName = className;
-        return new Reference(className, new StringRefAddr("DnaConnectorJBossCacheSource", getName()), factoryClassName, null);
+        String factoryClassName = this.getClass().getName();
+        Reference ref = new Reference(className, factoryClassName, null);
+
+        if (getName() != null) {
+            ref.add(new StringRefAddr(SOURCE_NAME, getName()));
+        }
+        if (getRootNodeUuid() != null) {
+            ref.add(new StringRefAddr(ROOT_NODE_UUID, getRootNodeUuid().toString()));
+        }
+        if (getUuidPropertyName() != null) {
+            ref.add(new StringRefAddr(UUID_PROPERTY_NAME, getUuidPropertyName()));
+        }
+        if (getCacheJndiName() != null) {
+            ref.add(new StringRefAddr(CACHE_JNDI_NAME, getCacheJndiName()));
+        }
+        if (getCacheFactoryJndiName() != null) {
+            ref.add(new StringRefAddr(CACHE_FACTORY_JNDI_NAME, getCacheFactoryJndiName()));
+        }
+        if (getCacheConfigurationName() != null) {
+            ref.add(new StringRefAddr(CACHE_CONFIGURATION_NAME, getCacheConfigurationName()));
+        }
+        if (getDefaultCachePolicy() != null) {
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            CachePolicy policy = getDefaultCachePolicy();
+            try {
+                ObjectOutputStream oos = new ObjectOutputStream(baos);
+                oos.writeObject(policy);
+                ref.add(new BinaryRefAddr(DEFAULT_CACHE_POLICY, baos.toByteArray()));
+            } catch (IOException e) {
+                I18n msg = JBossCacheConnectorI18n.errorSerializingCachePolicyInSource;
+                throw new RepositorySourceException(getName(), msg.text(policy.getClass().getName(), getName()), e);
+            }
+        }
+        ref.add(new StringRefAddr(RETRY_LIMIT, Integer.toString(getRetryLimit())));
+        return ref;
     }
 
     /**
@@ -268,13 +435,51 @@
     public Object getObjectInstance( Object obj,
                                      javax.naming.Name name,
                                      Context nameCtx,
-                                     Hashtable<?, ?> environment ) {
+                                     Hashtable<?, ?> environment ) throws Exception {
         if (obj instanceof Reference) {
+            Map<String, Object> values = new HashMap<String, Object>();
             Reference ref = (Reference)obj;
-            if (ref.getClassName().equals(getClass().getName())) {
-                RefAddr addr = ref.get("DnaConnectorJBossCacheSource");
-                return JBossCacheSource.getSource((String)addr.getContent());
+            Enumeration<?> en = ref.getAll();
+            while (en.hasMoreElements()) {
+                RefAddr subref = (RefAddr)en.nextElement();
+                if (subref instanceof StringRefAddr) {
+                    String key = subref.getType();
+                    Object value = subref.getContent();
+                    if (value != null) values.put(key, value.toString());
+                } else if (subref instanceof BinaryRefAddr) {
+                    String key = subref.getType();
+                    Object value = subref.getContent();
+                    if (value instanceof byte[]) {
+                        // Deserialize ...
+                        ByteArrayInputStream bais = new ByteArrayInputStream((byte[])value);
+                        ObjectInputStream ois = new ObjectInputStream(bais);
+                        value = ois.readObject();
+                        values.put(key, value);
+                    }
+                }
             }
+            String sourceName = (String)values.get(SOURCE_NAME);
+            String rootNodeUuidString = (String)values.get(ROOT_NODE_UUID);
+            String uuidPropertyName = (String)values.get(UUID_PROPERTY_NAME);
+            String cacheJndiName = (String)values.get(CACHE_JNDI_NAME);
+            String cacheFactoryJndiName = (String)values.get(CACHE_FACTORY_JNDI_NAME);
+            String cacheConfigurationName = (String)values.get(CACHE_CONFIGURATION_NAME);
+            Object defaultCachePolicy = values.get(DEFAULT_CACHE_POLICY);
+            String retryLimit = (String)values.get(RETRY_LIMIT);
+
+            // Create the source instance ...
+            JBossCacheSource source = new JBossCacheSource();
+            if (sourceName != null) source.setName(sourceName);
+            if (rootNodeUuidString != null) source.setRootNodeUuid(rootNodeUuidString);
+            if (uuidPropertyName != null) source.setUuidPropertyName(uuidPropertyName);
+            if (cacheJndiName != null) source.setCacheJndiName(cacheJndiName);
+            if (cacheFactoryJndiName != null) source.setCacheFactoryJndiName(cacheFactoryJndiName);
+            if (cacheConfigurationName != null) source.setCacheConfigurationName(cacheConfigurationName);
+            if (defaultCachePolicy instanceof CachePolicy) {
+                source.setDefaultCachePolicy((CachePolicy)defaultCachePolicy);
+            }
+            if (retryLimit != null) source.setRetryLimit(Integer.parseInt(retryLimit));
+            return source;
         }
         return null;
     }

Modified: trunk/connectors/dna-connector-jbosscache/src/main/resources/org/jboss/dna/connector/jbosscache/JBossCacheConnectorI18n.properties
===================================================================
--- trunk/connectors/dna-connector-jbosscache/src/main/resources/org/jboss/dna/connector/jbosscache/JBossCacheConnectorI18n.properties	2008-08-07 15:38:59 UTC (rev 398)
+++ trunk/connectors/dna-connector-jbosscache/src/main/resources/org/jboss/dna/connector/jbosscache/JBossCacheConnectorI18n.properties	2008-08-07 15:42:07 UTC (rev 399)
@@ -21,4 +21,8 @@
 #
 
 connectorName = JBoss Cache Connector
-nodeDoesNotExist = Could not find an existing node at {0}
\ No newline at end of file
+nodeDoesNotExist = Could not find an existing node at {0}
+propertyIsRequired = The {0} property is required but has no value
+errorSerializingCachePolicyInSource = Error serializing a {0} instance owned by the {1} JBossCacheSource
+objectFoundInJndiWasNotCache = Object in JNDI at {0} found by JBossCacheSource {1} was expected to be a org.jboss.cache.Cache<org.jboss.dna.spi.graph.Name,Object> but instead was {2}
+objectFoundInJndiWasNotCacheFactory = Object in JNDI at {0} found by JBossCacheSource {1} was expected to be a org.jboss.cache.CacheFactory<org.jboss.dna.spi.graph.Name,Object> but instead was {2}
\ No newline at end of file

Modified: trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheSourceTest.java
===================================================================
--- trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheSourceTest.java	2008-08-07 15:38:59 UTC (rev 398)
+++ trunk/connectors/dna-connector-jbosscache/src/test/java/org/jboss/dna/connector/jbosscache/JBossCacheSourceTest.java	2008-08-07 15:42:07 UTC (rev 399)
@@ -63,16 +63,16 @@
 
     @Test
     public void shouldAllowSettingName() {
-        assertThat(source.setName("Something"), is(true));
+        source.setName("Something");
         assertThat(source.getName(), is("Something"));
-        assertThat(source.setName("another name"), is(true));
+        source.setName("another name");
         assertThat(source.getName(), is("another name"));
     }
 
     @Test
-    public void shouldNotAllowSettingNameToNull() {
-        assertThat(source.setName("some name"), is(true));
-        assertThat(source.setName(null), is(true));
+    public void shouldAllowSettingNameToNull() {
+        source.setName("some name");
+        source.setName(null);
         assertThat(source.getName(), is(nullValue()));
     }
 }




More information about the dna-commits mailing list