Author: dkatayev
Date: 2009-11-26 08:56:29 -0500 (Thu, 26 Nov 2009)
New Revision: 883
Added:
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheFactory.java
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheInitException.java
Modified:
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheConfig.java
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/impl/CacheServiceImpl.java
Log:
EXOJCR-268 Improve the CacheService to allow complex implementation of ExoCache
Modified:
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheConfig.java
===================================================================
---
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheConfig.java 2009-11-26
13:49:07 UTC (rev 882)
+++
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheConfig.java 2009-11-26
13:56:29 UTC (rev 883)
@@ -23,7 +23,7 @@
* @since Feb 20, 2005
* @version $Id: ExoCacheConfig.java 5799 2006-05-28 17:55:42Z geaz $
*/
-public class ExoCacheConfig
+public class ExoCacheConfig implements Cloneable
{
private String name;
@@ -33,14 +33,16 @@
private long liveTime;
- private boolean distributed = false;
+ private boolean distributed;
- private boolean replicated = false;
+ private boolean replicated;
private String implementation;
- private boolean logEnabled = false;
+ private String type;
+ private boolean logEnabled;
+
public String getName()
{
return name;
@@ -113,6 +115,16 @@
implementation = alg;
}
+ public void setType(String type)
+ {
+ this.type = type;
+ }
+
+ public String getType()
+ {
+ return type;
+ }
+
public boolean isLogEnabled()
{
return logEnabled;
@@ -122,4 +134,20 @@
{
this.logEnabled = enableLogging;
}
+
+ /**
+ * @see java.lang.Object#clone()
+ */
+ @Override
+ public ExoCacheConfig clone() throws CloneNotSupportedException
+ {
+ try
+ {
+ return (ExoCacheConfig)super.clone();
+ }
+ catch (Exception e)
+ {
+ throw new AssertionError();
+ }
+ }
}
Added:
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheFactory.java
===================================================================
---
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheFactory.java
(rev 0)
+++
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheFactory.java 2009-11-26
13:56:29 UTC (rev 883)
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.cache;
+
+/**
+ * This class allows you to create a new instance of {@link
org.exoplatform.services.cache.ExoCache}
+ *
+ * @author <a href="mailto:dmitry.kataev@exoplatform.com">Dmytro
Katayev</a>
+ * @version $Id$
+ *
+ */
+public interface ExoCacheFactory
+{
+
+ /**
+ * Creates a new instance of {@link org.exoplatform.services.cache.ExoCache}
+ * @param config the cache to create
+ * @return the new instance of {@link org.exoplatform.services.cache.ExoCache}
+ * @exception ExoCacheInitException if an exception happens while initializing the
cache
+ */
+ public ExoCache createCache(ExoCacheConfig config) throws ExoCacheInitException;
+
+}
Property changes on:
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheFactory.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheInitException.java
===================================================================
---
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheInitException.java
(rev 0)
+++
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheInitException.java 2009-11-26
13:56:29 UTC (rev 883)
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2009 eXo Platform SAS.
+ *
+ * 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.exoplatform.services.cache;
+
+/**
+ * An exception that represents any type of exception that prevent the initialization of
+ * the {@link org.exoplatform.services.cache.ExoCache}
+ *
+ * @author <a href="mailto:dmitry.kataev@exoplatform.com">Dmytro
Katayev</a>
+ * @version $Id$
+ *
+ */
+public class ExoCacheInitException extends Exception
+{
+
+ /**
+ * The serial version UID
+ */
+ private static final long serialVersionUID = -5612051266167302943L;
+
+ /**
+ * {@inheritDoc}
+ */
+ public ExoCacheInitException(String message)
+ {
+ super(message);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public ExoCacheInitException(Throwable cause)
+ {
+ super(cause);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public ExoCacheInitException(String message, Throwable cause)
+ {
+ super(message, cause);
+ }
+
+}
Property changes on:
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/ExoCacheInitException.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/impl/CacheServiceImpl.java
===================================================================
---
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/impl/CacheServiceImpl.java 2009-11-26
13:49:07 UTC (rev 882)
+++
kernel/trunk/exo.kernel.component.cache/src/main/java/org/exoplatform/services/cache/impl/CacheServiceImpl.java 2009-11-26
13:56:29 UTC (rev 883)
@@ -25,6 +25,8 @@
import org.exoplatform.services.cache.ExoCache;
import org.exoplatform.services.cache.ExoCacheConfig;
import org.exoplatform.services.cache.ExoCacheConfigPlugin;
+import org.exoplatform.services.cache.ExoCacheFactory;
+import org.exoplatform.services.cache.ExoCacheInitException;
import org.exoplatform.services.cache.SimpleExoCache;
import java.io.Serializable;
@@ -40,19 +42,29 @@
@ManagedBy(CacheServiceManaged.class)
public class CacheServiceImpl implements CacheService
{
- private HashMap<String, ExoCacheConfig> configs_ = new HashMap<String,
ExoCacheConfig>();
+ private final HashMap<String, ExoCacheConfig> configs_ = new HashMap<String,
ExoCacheConfig>();
private final ConcurrentHashMap<String, ExoCache<? extends Serializable,
?>> cacheMap_ =
new ConcurrentHashMap<String, ExoCache<? extends Serializable, ?>>();
- private ExoCacheConfig defaultConfig_;
+ private final ExoCacheConfig defaultConfig_;
- private LoggingCacheListener loggingListener_;
+ private final LoggingCacheListener loggingListener_;
+ private final ExoCacheFactory factory_;
+
CacheServiceManaged managed;
+ /**
+ *
+ */
public CacheServiceImpl(InitParams params) throws Exception
{
+ this(params, null);
+ }
+
+ public CacheServiceImpl(InitParams params, ExoCacheFactory factory) throws Exception
+ {
List<ExoCacheConfig> configs =
params.getObjectParamValues(ExoCacheConfig.class);
for (ExoCacheConfig config : configs)
{
@@ -60,6 +72,7 @@
}
defaultConfig_ = configs_.get("default");
loggingListener_ = new LoggingCacheListener();
+ factory_ = factory == null ? new SimpleExoCacheFactory() : factory;
}
public void addExoCacheConfig(ComponentPlugin plugin)
@@ -111,40 +124,83 @@
ExoCacheConfig config = configs_.get(region);
if (config == null)
config = defaultConfig_;
- ExoCache<? extends Serializable, ?> cache;
- if (config.getImplementation() == null)
- {
- cache = new SimpleExoCache<Serializable, Object>();
- }
- else
- {
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- Class<ExoCache<? extends Serializable, ?>> clazz =
- (Class<ExoCache<? extends Serializable,
?>>)cl.loadClass(config.getImplementation());
- cache = clazz.newInstance();
- }
- cache.setName(region);
- cache.setLabel(config.getLabel());
- cache.setMaxSize(config.getMaxSize());
- cache.setLiveTime(config.getLiveTime());
- cache.setLogEnabled(config.isLogEnabled());
- if (cache.isLogEnabled())
- {
- cache.addCacheListener(loggingListener_);
- }
- //
+ // Ensure the configuration integrity
+ final ExoCacheConfig safeConfig = config.clone();
+ // Set the region as name
+ safeConfig.setName(region);
+ final ExoCache simple = factory_.createCache(safeConfig);
+
if (managed != null)
{
- managed.registerCache(cache);
+ managed.registerCache(simple);
}
-
- //
- return cache;
+ return simple;
}
public Collection<ExoCache<? extends Serializable, ?>>
getAllCacheInstances()
{
return cacheMap_.values();
}
+
+ /**
+ * Default implementation of an {@link
org.exoplatform.services.cache.ExoCacheFactory}
+ */
+ private class SimpleExoCacheFactory implements ExoCacheFactory
+ {
+
+ /**
+ * {@inheritDoc}
+ */
+ public ExoCache createCache(ExoCacheConfig config) throws ExoCacheInitException
+ {
+ final ExoCache simple = createCacheInstance(config);
+ simple.setName(config.getName());
+ simple.setLabel(config.getLabel());
+ simple.setMaxSize(config.getMaxSize());
+ simple.setLiveTime(config.getLiveTime());
+ // simple.setReplicated(config.isRepicated());
+ // simple.setDistributed(config.isDistributed());
+ // if (simple.isDistributed()) {
+ // simple.addCacheListener(distrbutedListener_);
+ // }
+ simple.setLogEnabled(config.isLogEnabled());
+ if (simple.isLogEnabled())
+ {
+ simple.addCacheListener(loggingListener_);
+ }
+ return simple;
+ }
+
+ /**
+ * Create a new instance of ExoCache according to the given configuration
+ * @param config the ExoCache configuration
+ * @return a new instance of ExoCache
+ * @throws ExoCacheInitException if any exception happens while initializing the
cache
+ */
+ @SuppressWarnings("unchecked")
+ private ExoCache createCacheInstance(ExoCacheConfig config) throws
ExoCacheInitException
+ {
+ if (config.getImplementation() == null)
+ {
+ // No implementation has been defined
+ return new SimpleExoCache();
+ }
+ else
+ {
+ // An implementation has been defined
+ final ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ try
+ {
+ final Class clazz = cl.loadClass(config.getImplementation());
+ return (ExoCache)clazz.newInstance();
+ }
+ catch (Exception e)
+ {
+ throw new ExoCacheInitException("Cannot create instance of ExoCache
of type "
+ + config.getImplementation(), e);
+ }
+ }
+ }
+ }
}