[jboss-cvs] JBossAS SVN: r72595 - projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Tue Apr 22 22:30:30 EDT 2008
Author: bstansberry at jboss.com
Date: 2008-04-22 22:30:30 -0400 (Tue, 22 Apr 2008)
New Revision: 72595
Added:
projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/CacheManagerLocator.java
projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/PojoCacheManager.java
projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/PojoCacheManagerLocator.java
Log:
[JBCLUSTER-195] Add CacheManager and PojoCacheManager service locators
Added: projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/CacheManagerLocator.java
===================================================================
--- projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/CacheManagerLocator.java (rev 0)
+++ projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/CacheManagerLocator.java 2008-04-23 02:30:30 UTC (rev 72595)
@@ -0,0 +1,173 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.ha.framework.server;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingException;
+
+import org.jboss.cache.CacheManager;
+import org.jboss.ha.framework.interfaces.HAPartition;
+import org.jboss.logging.Logger;
+
+/**
+ * Service Locator utility for locating a {@link PojoCacheManager}. Maintains
+ * an internal ref to a manager, and if it that is null,
+ * will attempt to find it in JNDI using a standard naming pattern.
+ *
+ * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
+ * @version $Revision: 1.1 $
+ */
+public class CacheManagerLocator
+{
+ private static final Logger log = Logger.getLogger(CacheManagerLocator.class);
+
+ private static CacheManagerLocator sharedInstance = new CacheManagerLocator();
+
+ /** Name of the standard JNDI context under which HAPartitions are bound */
+ public static final String STANDARD_JNDI_NAME = "java:CacheManager";
+
+ private CacheManager registeredManager;
+
+ /**
+ * Gets the
+ * @return the shared CacheManagerLocator; will not be <code>null</code>
+ */
+ public static CacheManagerLocator getCacheManagerLocator()
+ {
+ return sharedInstance;
+ }
+
+ /**
+ * Allows replacement of the default implementation.
+ *
+ * @param shared the locator to use. Cannot be <code>null</code>.
+ */
+ protected static void setSharedInstance(CacheManagerLocator shared)
+ {
+ if (shared == null)
+ throw new IllegalArgumentException("shared cannot be null");
+
+ sharedInstance = shared;
+ }
+
+ /**
+ * Allow subclasses to create a new HAPartitionLocator.
+ */
+ protected CacheManagerLocator()
+ {
+ }
+
+ /**
+ * Locates and returns the {@link HAPartition} whose partition name matches
+ * the given <code>partitionName</code>.
+ *
+ * @param jndiProperties any naming properties to pass to new InitialContext()
+ * if JNDI lookup is needed.
+ * @return the partition. Will not return <code>null</code>
+ *
+ * @throws IllegalStateException if no cache manager can be found
+ */
+ public CacheManager getCacheManager(Hashtable jndiProperties)
+ {
+ CacheManager manager = registeredManager;
+ if (manager == null)
+ {
+ try
+ {
+ manager = findInJndi(jndiProperties);
+ }
+ catch (NamingException e)
+ {
+ log.error("Problem finding CacheManager in JNDI", e);
+ }
+ }
+
+ if (manager == null)
+ throw new IllegalStateException("CacheManager not found");
+
+ return manager;
+ }
+
+ /**
+ * Register a CacheManager with this locator.
+ *
+ * @param partition the partition
+ */
+ public void registerCacheManager(CacheManager manager)
+ {
+ if (manager != null)
+ {
+ if (registeredManager != null && manager != registeredManager)
+ throw new IllegalStateException("CacheManager already registered");
+ registeredManager = manager;
+ }
+ }
+
+ public CacheManager getDirectlyRegisteredManager()
+ {
+ return registeredManager;
+ }
+
+ /**
+ * Deregister a partition from this locator.
+ *
+ * @param partition the partition
+ */
+ public void deregisterCacheManager()
+ {
+ registeredManager = null;
+ }
+
+ /**
+ * Find the given CacheManager in JNDI under a
+ * {@link #STANDARD_JNDI_NAME standard binding}.
+ *
+ * @param jndiProperties any naming properties to pass to new InitialContext()
+ *
+ * @return the CacheManager, or <code>null</code>
+ *
+ * @throws NamingException if there is a problem with the naming context. Will
+ * not throw NameNotFoundException; if not found, will
+ * return <code>null</code>.
+ *
+ * @see #STANDARD_JNDI_NAME
+ */
+ protected CacheManager findInJndi(Hashtable jndiProperties) throws NamingException
+ {
+ try
+ {
+ Context ctx = new InitialContext(jndiProperties);
+ return (CacheManager) ctx.lookup(STANDARD_JNDI_NAME);
+ }
+ catch (NameNotFoundException e)
+ {
+ // just not there
+ return null;
+ }
+
+ }
+}
Added: projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/PojoCacheManager.java
===================================================================
--- projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/PojoCacheManager.java (rev 0)
+++ projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/PojoCacheManager.java 2008-04-23 02:30:30 UTC (rev 72595)
@@ -0,0 +1,111 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.ha.framework.server;
+
+import java.util.Set;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.config.Configuration;
+import org.jboss.cache.pojo.PojoCache;
+import org.jgroups.ChannelFactory;
+
+/**
+ * Factory and registry for PojoCache instances configured using
+ * named configurations.
+ *
+ * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
+ * @version $Revision: 1 $
+ */
+public interface PojoCacheManager
+{
+
+
+ /**
+ * Gets the names of all the configurations of which this object
+ * is aware.
+ *
+ * @return a set of names. Will not be <code>null</code>.
+ */
+ Set<String> getConfigurationNames();
+
+ /**
+ * Gets the name of all caches that are registered, i.e. that can be
+ * by a call to {@link #getCache(String, boolean) getCache(name, false)}.
+ *
+ * @return a set of names. Will not be <code>null</code>.
+ */
+ Set<String> getPojoCacheNames();
+
+ /**
+ * Gets the JGroups <code>ChannelFactory</code> that will be injected
+ * into any {@link Configuration} that has a
+ * {@link Configuration#getMultiplexerStack() multiplexer stack}
+ * configured.
+ *
+ * @return
+ */
+ ChannelFactory getChannelFactory();
+
+ /**
+ * Get a cache configured according to the given configuration name,
+ * optionally instantiating the cache if it hasn't already been
+ * instantiated.
+ * <p>
+ * The caller is free to invoke the {@link Cache#create()} and
+ * {@link Cache#start()} lifecycle methods on the returned cache, but
+ * the @link Cache#stop()} and {@link Cache#destroy()} methods should not
+ * be invoked, since it is quite possible other users are still using the
+ * cache. Use {@link #releaseCache(String)} to notify this
+ * registry that the caller is no longer using a cache; let the registry
+ * control stopping and destroying the cache.
+ * </p>
+ * <p>
+ * If invoking this method leads to the instantiation of the cache,
+ * <code>create()</code> and <code>start()</code> will not be invoked
+ * on the cache before it is returned.
+ * </p>
+ *
+ * @param configName the name of the configuration
+ * @param create should the cache be instantiated if it
+ * hasn't already been?
+ * @return the cache, or <code>null</code> if
+ * <code>create</code> is false and the cache hasn't
+ * been created previously.
+ *
+ * @throws IllegalArgumentException if this object is unaware of
+ * <code>configName</code>; i.e. the set
+ * returned from {@link #getConfigurationNames()}
+ * does not include <code>configName</code>
+ * @throws Exception if there is a problem instantiating the cache
+ */
+ PojoCache getPojoCache(String configName, boolean create) throws Exception;
+
+ /**
+ * Notifies the registry that the caller is no longer using the given
+ * cache. The registry may perform cleanup operations, such as
+ * stopping and destroying the cache.
+ *
+ * @param configName
+ */
+ void releaseCache(String configName);
+}
\ No newline at end of file
Added: projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/PojoCacheManagerLocator.java
===================================================================
--- projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/PojoCacheManagerLocator.java (rev 0)
+++ projects/cluster/ha-server-api/trunk/src/main/java/org/jboss/ha/framework/server/PojoCacheManagerLocator.java 2008-04-23 02:30:30 UTC (rev 72595)
@@ -0,0 +1,176 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.ha.framework.server;
+
+import java.util.Hashtable;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingException;
+
+import org.jboss.ha.framework.interfaces.HAPartition;
+import org.jboss.logging.Logger;
+
+/**
+ * Service Locator utility for locating a {@link PojoCacheManager}. Maintains
+ * an internal ref to a manager, and if it that is null,
+ * will attempt to find it in JNDI using a standard naming pattern.
+ *
+ * @author <a href="brian.stansberry at jboss.com">Brian Stansberry</a>
+ * @version $Revision: 1.1 $
+ */
+public class PojoCacheManagerLocator
+{
+ private static final Logger log = Logger.getLogger(PojoCacheManagerLocator.class);
+
+ private static PojoCacheManagerLocator sharedInstance = new PojoCacheManagerLocator();
+
+ /** Name of the standard JNDI context under which PojoCacheManagers are bound */
+ public static final String STANDARD_JNDI_NAME = "java:CacheManager";
+
+ private PojoCacheManager registeredManager;
+
+ /**
+ * Gets the
+ * @return the shared CacheManagerLocator; will not be <code>null</code>
+ */
+ public static PojoCacheManagerLocator getCacheManagerLocator()
+ {
+ return sharedInstance;
+ }
+
+ /**
+ * Allows replacement of the default implementation.
+ *
+ * @param shared the locator to use. Cannot be <code>null</code>.
+ */
+ protected static void setSharedInstance(PojoCacheManagerLocator shared)
+ {
+ if (shared == null)
+ throw new IllegalArgumentException("shared cannot be null");
+
+ sharedInstance = shared;
+ }
+
+ /**
+ * Allow subclasses to create a new HAPartitionLocator.
+ */
+ protected PojoCacheManagerLocator()
+ {
+ }
+
+ /**
+ * Locates and returns the {@link HAPartition} whose partition name matches
+ * the given <code>partitionName</code>.
+ *
+ * @param jndiProperties any naming properties to pass to new InitialContext()
+ * if JNDI lookup is needed.
+ * @return the partition. Will not return <code>null</code>
+ *
+ * @throws IllegalStateException if no cache manager can be found
+ */
+ public PojoCacheManager getCacheManager(Hashtable jndiProperties)
+ {
+ PojoCacheManager manager = registeredManager;
+ if (manager == null)
+ {
+ try
+ {
+ manager = findInJndi(jndiProperties);
+ }
+ catch (NamingException e)
+ {
+ log.error("Problem finding CacheManager in JNDI", e);
+ }
+ }
+
+ if (manager == null)
+ throw new IllegalStateException("CacheManager not found");
+
+ return manager;
+ }
+
+ /**
+ * Register a CacheManager with this locator.
+ *
+ * @param partition the partition
+ */
+ public void registerCacheManager(PojoCacheManager manager)
+ {
+ if (manager != null)
+ {
+ if (registeredManager != null && manager != registeredManager)
+ throw new IllegalStateException("CacheManager already registered");
+ registeredManager = manager;
+ }
+ }
+
+ public PojoCacheManager getDirectlyRegisteredManager()
+ {
+ return registeredManager;
+ }
+
+ /**
+ * Deregister a partition from this locator.
+ *
+ * @param partition the partition
+ */
+ public void deregisterCacheManager()
+ {
+ registeredManager = null;
+ }
+
+ /**
+ * Find the given CacheManager in JNDI under a
+ * {@link #STANDARD_JNDI_NAME standard binding}.
+ *
+ * @param jndiProperties any naming properties to pass to new InitialContext()
+ *
+ * @return the CacheManager, or <code>null</code>
+ *
+ * @throws NamingException if there is a problem with the naming context. Will
+ * not throw NameNotFoundException; if not found, will
+ * return <code>null</code>.
+ *
+ * @see #STANDARD_JNDI_NAME
+ */
+ protected PojoCacheManager findInJndi(Hashtable jndiProperties) throws NamingException
+ {
+ PojoCacheManager result = null;
+ try
+ {
+ Context ctx = new InitialContext(jndiProperties);
+ Object mgr = ctx.lookup(STANDARD_JNDI_NAME);
+ if (mgr instanceof PojoCacheManager)
+ result = (PojoCacheManager) mgr;
+ }
+ catch (NameNotFoundException e)
+ {
+ // just not there
+ }
+
+ return result;
+
+ }
+}
More information about the jboss-cvs-commits
mailing list