Author: nbelaevski
Date: 2007-05-02 13:38:50 -0400 (Wed, 02 May 2007)
New Revision: 157
Added:
trunk/framework/src/main/java/org/ajax4jsf/cache/CacheConfigurationLoader.java
trunk/framework/src/main/java/org/ajax4jsf/cache/ServletContextInitMap.java
Modified:
trunk/framework/src/main/java/org/ajax4jsf/cache/CacheFactory.java
trunk/framework/src/main/java/org/ajax4jsf/cache/CacheManager.java
trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCache.java
trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCacheFactory.java
trunk/framework/src/main/java/org/ajax4jsf/cache/OSCacheCache.java
trunk/framework/src/main/java/org/ajax4jsf/cache/OSCacheCacheFactory.java
trunk/framework/src/main/java/org/ajax4jsf/framework/resource/InternetResourceService.java
trunk/framework/src/test/java/org/ajax4jsf/cache/LRUMapCacheThreadedTest.java
Log:
AJSF-39: configuration added, console logging removed
Added: trunk/framework/src/main/java/org/ajax4jsf/cache/CacheConfigurationLoader.java
===================================================================
--- trunk/framework/src/main/java/org/ajax4jsf/cache/CacheConfigurationLoader.java
(rev 0)
+++
trunk/framework/src/main/java/org/ajax4jsf/cache/CacheConfigurationLoader.java 2007-05-02
17:38:50 UTC (rev 157)
@@ -0,0 +1,33 @@
+/**
+ * License Agreement.
+ *
+ * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.ajax4jsf.cache;
+
+import java.util.Properties;
+
+/**
+ * @author Nick Belaevski - nbelaevski(a)exadel.com
+ * created 02.05.2007
+ *
+ */
+public interface CacheConfigurationLoader {
+ public Properties loadProperties(String name);
+}
Modified: trunk/framework/src/main/java/org/ajax4jsf/cache/CacheFactory.java
===================================================================
--- trunk/framework/src/main/java/org/ajax4jsf/cache/CacheFactory.java 2007-05-02 17:14:44
UTC (rev 156)
+++ trunk/framework/src/main/java/org/ajax4jsf/cache/CacheFactory.java 2007-05-02 17:38:50
UTC (rev 157)
@@ -14,8 +14,9 @@
* @param env implementation specific environment parameters passed to the
* CacheFactory.
* @param cacheLoader implementation of the {@link CacheLoader} to use
+ * @param cacheConfigurationloader TODO
* @return an implementation specific Cache object.
* @throws CacheException if any error occurs.
*/
- public Cache createCache(Map env, CacheLoader cacheLoader) throws CacheException;
+ public Cache createCache(Map env, CacheLoader cacheLoader, CacheConfigurationLoader
cacheConfigurationloader) throws CacheException;
}
Modified: trunk/framework/src/main/java/org/ajax4jsf/cache/CacheManager.java
===================================================================
--- trunk/framework/src/main/java/org/ajax4jsf/cache/CacheManager.java 2007-05-02 17:14:44
UTC (rev 156)
+++ trunk/framework/src/main/java/org/ajax4jsf/cache/CacheManager.java 2007-05-02 17:38:50
UTC (rev 157)
@@ -6,13 +6,19 @@
import java.util.Map;
import java.util.Properties;
+import org.ajax4jsf.framework.resource.InternetResourceService;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
/**
* CacheManager is used in J2SE environments for looking up named caches.
*/
public class CacheManager {
+ private static final Log log = LogFactory.getLog(CacheManager.class);
private static final String FACTORY_PROPERTY_NAME =
"org.ajax4jsf.cache.CacheFactory";
private static final String DEFAULT_FACTORY_NAME =
"org.ajax4jsf.cache.OSCacheCacheFactory";
+ public static final String CACHE_MANAGER_FACTORY_CLASS =
"org.ajax4jsf.cache.CACHE_MANAGER_FACTORY_CLASS";
protected static CacheManager instance = new CacheManager();
@@ -35,9 +41,10 @@
caches.put(cacheName, cache);
}
- public CacheFactory getCacheFactory() throws CacheException {
- String factoryName = findFactory(FACTORY_PROPERTY_NAME);
-
+ public CacheFactory getCacheFactory(Map env) throws CacheException {
+ String factoryName = findFactory(FACTORY_PROPERTY_NAME, env);
+ log.info("Selected [" + factoryName + "] cache factory");
+
try {
ClassLoader cl = findClassLoader();
Class spiClass = Class.forName(factoryName, true, cl);
@@ -63,8 +70,11 @@
return s == null || "".equals(s);
}
- String findFactory(String factoryId) {
+ String findFactory(String factoryId, Map env) {
+ String envFactoryClass = (String) env.get(CACHE_MANAGER_FACTORY_CLASS);
+ if (!isEmptyString(envFactoryClass)) return envFactoryClass;
+
// Use the system property first
try {
String factoryClass = System.getProperty(factoryId);
Modified: trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCache.java
===================================================================
--- trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCache.java 2007-05-02 17:14:44
UTC (rev 156)
+++ trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCache.java 2007-05-02 17:38:50
UTC (rev 157)
@@ -1,6 +1,24 @@
/**
- *
+ * License Agreement.
+ *
+ * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+
package org.ajax4jsf.cache;
import java.util.HashMap;
Modified: trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCacheFactory.java
===================================================================
--- trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCacheFactory.java 2007-05-02
17:14:44 UTC (rev 156)
+++ trunk/framework/src/main/java/org/ajax4jsf/cache/LRUMapCacheFactory.java 2007-05-02
17:38:50 UTC (rev 157)
@@ -1,11 +1,32 @@
/**
- *
+ * License Agreement.
+ *
+ * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+
package org.ajax4jsf.cache;
import java.util.Map;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
/**
* @author Nick - mailto:nbelaevski@exadel.com
* created 01.05.2007
@@ -13,9 +34,24 @@
*/
public class LRUMapCacheFactory implements CacheFactory {
- public Cache createCache(Map env, CacheLoader cacheLoader)
+ private static final Log log = LogFactory.getLog(LRUMapCacheFactory.class);
+
+ public final static String CACHE_SIZE_PARAMETER =
"org.ajax4jsf.cache.LRU_MAP_CACHE_SIZE";
+
+ public Cache createCache(Map env, CacheLoader cacheLoader, CacheConfigurationLoader
cacheConfigurationloader)
throws CacheException {
- return new LRUMapCache(cacheLoader);
+
+ log.info("Creating LRUMap cache instance using parameters: " + env);
+
+ String size = (String) env.get(CACHE_SIZE_PARAMETER);
+ if (size == null || size.length() == 0) {
+ log.info("Creating LRUMap cache instance of default capacity");
+ return new LRUMapCache(cacheLoader);
+ } else {
+ int parsedSize = Integer.parseInt(size);
+ log.info("Creating LRUMap cache instance of " + parsedSize + " items
capacity");
+ return new LRUMapCache(cacheLoader, parsedSize);
+ }
}
}
Modified: trunk/framework/src/main/java/org/ajax4jsf/cache/OSCacheCache.java
===================================================================
--- trunk/framework/src/main/java/org/ajax4jsf/cache/OSCacheCache.java 2007-05-02 17:14:44
UTC (rev 156)
+++ trunk/framework/src/main/java/org/ajax4jsf/cache/OSCacheCache.java 2007-05-02 17:38:50
UTC (rev 157)
@@ -1,6 +1,24 @@
/**
- *
+ * License Agreement.
+ *
+ * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+
package org.ajax4jsf.cache;
import java.util.Collection;
Modified: trunk/framework/src/main/java/org/ajax4jsf/cache/OSCacheCacheFactory.java
===================================================================
--- trunk/framework/src/main/java/org/ajax4jsf/cache/OSCacheCacheFactory.java 2007-05-02
17:14:44 UTC (rev 156)
+++ trunk/framework/src/main/java/org/ajax4jsf/cache/OSCacheCacheFactory.java 2007-05-02
17:38:50 UTC (rev 157)
@@ -1,10 +1,31 @@
/**
- *
+ * License Agreement.
+ *
+ * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+
package org.ajax4jsf.cache;
import java.util.Map;
+import java.util.Properties;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;
@@ -15,12 +36,20 @@
*/
public class OSCacheCacheFactory implements CacheFactory {
- /* (non-Javadoc)
+ private static final Log log = LogFactory.getLog(OSCacheCacheFactory.class);
+
+ /* (non-Javadoc)
* @see org.ajax4jsf.framework.resource.cache.CacheFactory#createCache(java.util.Map,
org.ajax4jsf.framework.resource.cache.CacheLoader)
*/
- public Cache createCache(Map env, CacheLoader cacheLoader)
+ public Cache createCache(Map env, CacheLoader cacheLoader, CacheConfigurationLoader
cacheConfigurationloader)
throws CacheException {
- GeneralCacheAdministrator cacheAdministrator = new GeneralCacheAdministrator();
+ // Load our implementation properties
+ Properties cacheProperties =
cacheConfigurationloader.loadProperties("oscache.properties");
+
cacheProperties.putAll(cacheConfigurationloader.loadProperties("/oscache.properties"));
+ cacheProperties.putAll(env);
+
+ log.info("Creating OSCache cache instance using parameters: " +
cacheProperties);
+ GeneralCacheAdministrator cacheAdministrator = new
GeneralCacheAdministrator(cacheProperties);
return new OSCacheCache(cacheAdministrator.getCache(), cacheLoader);
}
Added: trunk/framework/src/main/java/org/ajax4jsf/cache/ServletContextInitMap.java
===================================================================
--- trunk/framework/src/main/java/org/ajax4jsf/cache/ServletContextInitMap.java
(rev 0)
+++ trunk/framework/src/main/java/org/ajax4jsf/cache/ServletContextInitMap.java 2007-05-02
17:38:50 UTC (rev 157)
@@ -0,0 +1,119 @@
+/**
+ * License Agreement.
+ *
+ * Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library 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 library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.ajax4jsf.cache;
+
+import java.util.AbstractMap;
+import java.util.AbstractSet;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.ServletContext;
+
+/**
+ * @author Nick Belaevski - nbelaevski(a)exadel.com
+ * created 02.05.2007
+ *
+ */
+public class ServletContextInitMap extends AbstractMap {
+ private ServletContext servletContext;
+
+ public ServletContextInitMap(ServletContext servletContext) {
+ super();
+ this.servletContext = servletContext;
+ }
+
+ public Set entrySet() {
+ return new AbstractSet() {
+
+ public Iterator iterator() {
+ return new Iterator() {
+ private Enumeration initNames = servletContext.getInitParameterNames();
+
+ public boolean hasNext() {
+ return initNames.hasMoreElements();
+ }
+
+ public Object next() {
+ String key = (String) initNames.nextElement();
+ String value = servletContext.getInitParameter(key);
+
+ return new ServletContextInitMapEntry(key, value);
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ };
+ }
+
+ public int size() {
+ int result = 0;
+ Enumeration initNames = servletContext.getInitParameterNames();
+ while (initNames.hasMoreElements()) {
+ result++;
+ }
+
+ return result;
+ }
+
+ public boolean isEmpty() {
+ return !servletContext.getInitParameterNames().hasMoreElements();
+ }
+ };
+ }
+
+}
+
+class ServletContextInitMapEntry implements Map.Entry {
+
+ private Object key;
+ private Object value;
+
+ public ServletContextInitMapEntry(Object key, Object value) {
+ super();
+ this.key = key;
+ this.value = value;
+ }
+
+ public ServletContextInitMapEntry(Object key) {
+ super();
+ this.key = key;
+ }
+
+ public Object getKey() {
+ return key;
+ }
+
+ public Object getValue() {
+ return value;
+ }
+
+ public Object setValue(Object value) {
+ Object oldValue = this.value;
+ this.value = value;
+ return oldValue;
+ }
+
+}
\ No newline at end of file
Modified:
trunk/framework/src/main/java/org/ajax4jsf/framework/resource/InternetResourceService.java
===================================================================
---
trunk/framework/src/main/java/org/ajax4jsf/framework/resource/InternetResourceService.java 2007-05-02
17:14:44 UTC (rev 156)
+++
trunk/framework/src/main/java/org/ajax4jsf/framework/resource/InternetResourceService.java 2007-05-02
17:38:50 UTC (rev 157)
@@ -23,9 +23,7 @@
import java.io.IOException;
import java.io.InputStream;
-import java.util.Collection;
import java.util.Date;
-import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@@ -42,9 +40,12 @@
import javax.servlet.http.HttpServletResponse;
import org.ajax4jsf.cache.Cache;
+import org.ajax4jsf.cache.CacheConfigurationLoader;
import org.ajax4jsf.cache.CacheException;
+import org.ajax4jsf.cache.CacheFactory;
import org.ajax4jsf.cache.CacheLoader;
import org.ajax4jsf.cache.CacheManager;
+import org.ajax4jsf.cache.ServletContextInitMap;
import org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter;
import org.ajax4jsf.framework.ajax.xmlfilter.CacheContent;
import org.ajax4jsf.framework.util.config.WebXml;
@@ -53,7 +54,7 @@
import org.apache.commons.logging.LogFactory;
-public class InternetResourceService implements CacheLoader {
+public class InternetResourceService implements CacheLoader, CacheConfigurationLoader {
private static final Log log = LogFactory
.getLog(InternetResourceService.class);
@@ -96,17 +97,15 @@
// this.cacheEnabled = false;
//this.cacheAdmin = null;
} else {
- // Load our implementation properties
- Properties cacheProperties = getProperties("oscache.properties");
- cacheProperties.putAll(getProperties("/oscache.properties"));
//this.cacheAdmin = ServletCacheAdministrator.getInstance(
// servletContext, cacheProperties);
- Map env = new HashMap();
try {
- this.cache = CacheManager.getInstance().getCacheFactory().createCache(env, this);
+ CacheManager cacheManager = CacheManager.getInstance();
+ Map env = new ServletContextInitMap(servletContext);
+ CacheFactory cacheFactory = cacheManager.getCacheFactory(env);
+ this.cache = cacheFactory.createCache(env, this, this);
} catch (CacheException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ throw new FacesException(e.getMessage(), e);
}
}
// Create Resource-specific Faces Lifecycle instance.
@@ -230,8 +229,12 @@
}
}*/
} catch (CacheException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ log.error(
+ Messages.getMessage(Messages.SEND_RESOURCE_ERROR),
+ e);
+ throw new ServletException(Messages.getMessage(
+ Messages.SEND_RESOURCE_ERROR_2, e.getMessage()),
+ e);
}
} else {
sendResource(resource, request, response, resourceDataForKey);
@@ -361,12 +364,8 @@
resourceContext.setResourceData(cacheKey.getResourceData());
try {
getLifecycle().send(resourceContext, cacheKey.getResource());
- } catch (FacesException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
} catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ throw new CacheException(e.getMessage(), e);
}
resourceContext.release();
@@ -375,9 +374,8 @@
return resourceContext.getContent();
}
- public Map loadAll(Collection keys, Object context) throws CacheException {
- // TODO Auto-generated method stub
- return null;
+ public Properties loadProperties(String name) {
+ return getProperties(name);
}
}
Modified: trunk/framework/src/test/java/org/ajax4jsf/cache/LRUMapCacheThreadedTest.java
===================================================================
---
trunk/framework/src/test/java/org/ajax4jsf/cache/LRUMapCacheThreadedTest.java 2007-05-02
17:14:44 UTC (rev 156)
+++
trunk/framework/src/test/java/org/ajax4jsf/cache/LRUMapCacheThreadedTest.java 2007-05-02
17:38:50 UTC (rev 157)
@@ -27,7 +27,7 @@
};
private static final int COUNT = 2000;
- private static final int PASS_COUNT = 200;
+ private static final int PASS_COUNT = 2;
public void testCache() throws Exception {
@@ -39,7 +39,7 @@
try {
for (int i = 0; i < COUNT; i++) {
- threads[i] = new TestThread(cache, new Integer(new Random().nextInt(10)));
+ threads[i] = new LRUMapCacheTestThread(cache, new Integer(new
Random().nextInt(10)));
threads[i].start();
}
} catch (Exception e) {
@@ -55,11 +55,11 @@
}
}
-class TestThread extends Thread {
+class LRUMapCacheTestThread extends Thread {
private Cache cache;
private Integer idx;
- public TestThread(Cache cache, Integer idx) {
+ public LRUMapCacheTestThread(Cache cache, Integer idx) {
super();
this.cache = cache;
this.idx = idx;