[jboss-svn-commits] JBL Code SVN: r14820 - in labs/shotoku/trunk: shotoku-base/src/java/org/jboss/shotoku/feeds and 5 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Sep 3 05:30:45 EDT 2007
Author: adamw
Date: 2007-09-03 05:30:44 -0400 (Mon, 03 Sep 2007)
New Revision: 14820
Modified:
labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/cache/ShotokuPropertiesWatcher.java
labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/feeds/FeedsService.java
labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/CacheItem.java
labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/CacheItemDataSource.java
labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/ValueChange.java
labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem.java
labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem2.java
labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem3.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java
labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java
labs/shotoku/trunk/shotoku-file-access/src/java/org/jboss/shotoku/fileaccess/FileAccessConfigurationWatcher.java
Log:
Init values; removing administrated services.
Modified: labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/cache/ShotokuPropertiesWatcher.java
===================================================================
--- labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/cache/ShotokuPropertiesWatcher.java 2007-09-03 08:47:20 UTC (rev 14819)
+++ labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/cache/ShotokuPropertiesWatcher.java 2007-09-03 09:30:44 UTC (rev 14820)
@@ -28,9 +28,9 @@
return null;
}
- public Properties init(K key) {
+ public ValueInit<? extends Properties> init(K key) {
addWatchedPath(key, getConfigFileName());
- return getProperties(key);
+ return ValueInit.realValue(getProperties(key));
}
protected ValueChange<Properties> update(K key, Properties currentObject,
Modified: labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/feeds/FeedsService.java
===================================================================
--- labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/feeds/FeedsService.java 2007-09-03 08:47:20 UTC (rev 14819)
+++ labs/shotoku/trunk/shotoku-base/src/java/org/jboss/shotoku/feeds/FeedsService.java 2007-09-03 09:30:44 UTC (rev 14820)
@@ -1,13 +1,16 @@
package org.jboss.shotoku.feeds;
-import org.jboss.shotoku.service.AdministratedService;
+import java.util.Properties;
+
+import org.jboss.shotoku.cache.CacheItemUser;
import org.jboss.shotoku.feeds.data.Feed;
import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
+import org.jboss.shotoku.tools.Pair;
/**
* @author Adam Warski (adamw at aster.pl)
*/
-public interface FeedsService extends AdministratedService {
+public interface FeedsService {
public String getProperty(String id, String name);
public Feed getFeed(String id, String name, String type)
throws FeedDoesNotExistException;
@@ -16,4 +19,15 @@
public void setRemoteConnectionTimeout(int remoteConnectionTimeout);
public int getRemoteReadTimeout();
public void setRemoteReadTimeout(int remoteReadTimeout);
+
+ public CacheItemUser<Pair<String, String>, Properties> getConf();
+
+ /*
+ * SERVICE MANAGEMENT METHODS
+ */
+
+ public void create() throws Exception;
+ public void start() throws Exception;
+ public void stop();
+ public void destroy();
}
Modified: labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/CacheItem.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/CacheItem.java 2007-09-03 08:47:20 UTC (rev 14819)
+++ labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/CacheItem.java 2007-09-03 09:30:44 UTC (rev 14820)
@@ -312,13 +312,13 @@
if (!keysUpdates.containsKey(key)) {
synchronized (this) {
if (!keysUpdates.containsKey(key)) {
- T ret = dataSource.init(key);
+ ValueInit<? extends T> ret = dataSource.init(key);
try {
if (!registered) {
throw new UnregisteredException();
}
- service.put(fqn, key, ret);
+ service.put(fqn, key, ret.getValue());
} catch (CacheException e) {
log.error("Error while putting a value for key \""
+ key + "\" in cache item " + this.getClass()
@@ -327,9 +327,15 @@
}
// We have to initialize a new object.
- keysUpdates.put(key, System.currentTimeMillis());
+ if (ret.hasRealValue()) {
+ // First update after the cache item interval.
+ keysUpdates.put(key, System.currentTimeMillis());
+ } else {
+ // First update as soon as possible.
+ keysUpdates.put(key, (long) 0);
+ }
- return ret;
+ return ret.getValue();
}
}
}
@@ -362,10 +368,19 @@
keysDuringUpdate.add(key);
try {
- ValueChange<T> change = dataSource.update(key, get(key));
+ ValueChange<? extends T> change = dataSource.update(key, get(key));
if (change.hasValue()) {
put(key, change.getValue());
}
+
+ // Checking if there is anything to release.
+ if (change.getRelease() != null) {
+ try {
+ change.getRelease().release();
+ } catch (Throwable t) {
+ // Ignoring any exceptions.
+ }
+ }
} catch (UnregisteredException e) {
} finally {
keysInUpdate.remove(key);
Modified: labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/CacheItemDataSource.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/CacheItemDataSource.java 2007-09-03 08:47:20 UTC (rev 14819)
+++ labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/CacheItemDataSource.java 2007-09-03 09:30:44 UTC (rev 14820)
@@ -51,17 +51,29 @@
* shouldn't be changed.</li>
* <li>{@link ValueChange#changeTo(Object)} to bind a new value to the given key in
* the cache.</li>
+ * <li>{@link ValueChange#changeToReleaseOld(Object, Releasable)} to bind a ne value to the given
+ * key in the cache, and then release the value.</li>
* </ul>
*/
- public ValueChange<T> update(K key, T currentObject);
+ public ValueChange<? extends T> update(K key, T currentObject);
/**
* Called when the user demands an object which hasn't been accessed
- * before, and thus, which hasn't been yet initialized.
+ * before, and thus, which hasn't been yet initialized.
* @param key Key of the object to initialize.
- * @return Initial value of an object with the given key.
+ * @return Can return either:
+ * <ul>
+ * <li>{@link ValueInit#realValue(Object)} to indicate that the value returned is a "real" one;
+ * it will be updated after this cache item's interval.</li>
+ * <li>{@link ValueInit#dummyValue(Object)} to indicate that the value returned is a "dummy" one,
+ * and that it should be updated as soon as possible, that is, on the nearest cache service update.
+ * This is only useful if the cache item interval is significantly larger then the service interval.
+ * A specific use case is when it is not acceptable to construct a new value, that will be held in
+ * the cache, as a blocking operation, but instead return a new "dummy" value which will be quickly
+ * replaced by a real one, by an update in the background.
+ * </ul>
*/
- public T init(K key);
+ public ValueInit<? extends T> init(K key);
/**
*
Modified: labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/ValueChange.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/ValueChange.java 2007-09-03 08:47:20 UTC (rev 14819)
+++ labs/shotoku/trunk/shotoku-cache/cache-base/src/java/org/jboss/shotoku/cache/ValueChange.java 2007-09-03 09:30:44 UTC (rev 14820)
@@ -25,26 +25,33 @@
/**
* A class which represents either a change of value, and holds the new value,
* or no value change. Instances can be obtained using the
- * {@link ValueChange#noChange()} and {@link ValueChange#changeTo(Object)} methods.
+ * {@link ValueChange#noChange()}, {@link ValueChange#changeTo(Object)} and
+ * {@link ValueChange#changeToReleaseOld(Object, Releasable)} methods.
* @author <a href="mailto:adam.warski at jboss.org">Adam Warski</a>
*/
public class ValueChange<T> {
private boolean hasValue;
private T newValue;
+ private Releasable release;
- private ValueChange(boolean hasValue, T newValue) {
+ private ValueChange(boolean hasValue, T newValue, Releasable release) {
this.hasValue = hasValue;
this.newValue = newValue;
+ this.release = release;
}
public static <T> ValueChange<T> noChange() {
- return new ValueChange<T>(false, null);
+ return new ValueChange<T>(false, null, null);
}
public static <T> ValueChange<T> changeTo(T newValue) {
- return new ValueChange<T>(true, newValue);
+ return new ValueChange<T>(true, newValue, null);
}
+ public static <T> ValueChange<T> changeToReleaseOld(T newValue, Releasable oldValue) {
+ return new ValueChange<T>(true, newValue, oldValue);
+ }
+
public T getValue() {
return newValue;
}
@@ -52,4 +59,8 @@
public boolean hasValue() {
return hasValue;
}
+
+ public Releasable getRelease() {
+ return release;
+ }
}
Modified: labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem.java 2007-09-03 08:47:20 UTC (rev 14819)
+++ labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem.java 2007-09-03 09:30:44 UTC (rev 14820)
@@ -2,10 +2,11 @@
import org.jboss.shotoku.cache.CacheItemDataSource;
import org.jboss.shotoku.cache.ValueChange;
+import org.jboss.shotoku.cache.ValueInit;
public class TestCacheItem implements CacheItemDataSource<String, Integer> {
- public Integer init(String key) {
- return 0;
+ public ValueInit<? extends Integer> init(String key) {
+ return ValueInit.realValue(0);
}
public ValueChange<Integer> update(String key, Integer currentObject) {
Modified: labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem2.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem2.java 2007-09-03 08:47:20 UTC (rev 14819)
+++ labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem2.java 2007-09-03 09:30:44 UTC (rev 14820)
@@ -2,10 +2,11 @@
import org.jboss.shotoku.cache.CacheItemDataSource;
import org.jboss.shotoku.cache.ValueChange;
+import org.jboss.shotoku.cache.ValueInit;
public class TestCacheItem2 implements CacheItemDataSource<String, Integer> {
- public Integer init(String key) {
- return 0;
+ public ValueInit<? extends Integer> init(String key) {
+ return ValueInit.realValue(0);
}
public ValueChange<Integer> update(String key, Integer currentObject) {
Modified: labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem3.java
===================================================================
--- labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem3.java 2007-09-03 08:47:20 UTC (rev 14819)
+++ labs/shotoku/trunk/shotoku-cache/cache-test/src/java/org/jboss/shotoku/cache/test/TestCacheItem3.java 2007-09-03 09:30:44 UTC (rev 14820)
@@ -2,10 +2,11 @@
import org.jboss.shotoku.cache.CacheItemDataSource;
import org.jboss.shotoku.cache.ValueChange;
+import org.jboss.shotoku.cache.ValueInit;
public class TestCacheItem3 implements CacheItemDataSource<String, Integer> {
- public Integer init(String key) {
- return 0;
+ public ValueInit<? extends Integer> init(String key) {
+ return ValueInit.realValue(0);
}
public ValueChange<Integer> update(String key, Integer currentObject) {
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java 2007-09-03 08:47:20 UTC (rev 14819)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java 2007-09-03 09:30:44 UTC (rev 14820)
@@ -2,6 +2,7 @@
import java.util.Map;
+import org.jboss.shotoku.cache.Releasable;
import org.jboss.shotoku.feeds.data.Feed;
import org.jboss.shotoku.feeds.data.NormalFeed;
import org.jboss.shotoku.feeds.data.special.SpecialFeed;
@@ -9,7 +10,7 @@
import org.jboss.shotoku.feeds.tag.FeedTagHandler;
import org.jboss.shotoku.feeds.variables.VariableResolverManager;
-public interface FeedsDescriptor {
+public interface FeedsDescriptor extends Releasable{
public abstract String getProperty(String name);
Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java
===================================================================
--- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java 2007-09-03 08:47:20 UTC (rev 14819)
+++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/service/FeedsServiceImpl.java 2007-09-03 09:30:44 UTC (rev 14820)
@@ -5,24 +5,17 @@
import org.jboss.annotation.ejb.Depends;
import org.jboss.shotoku.cache.CacheItem;
import org.jboss.shotoku.cache.CacheItemUser;
-import org.jboss.shotoku.feeds.DummyFeedsDescriptor;
import org.jboss.shotoku.feeds.FeedsService;
-import org.jboss.shotoku.feeds.FeedsDescriptorImpl;
import org.jboss.shotoku.feeds.FeedsConfigurationWatcher;
import org.jboss.shotoku.feeds.FeedsDescriptor;
import org.jboss.shotoku.feeds.exceptions.FeedDoesNotExistException;
import org.jboss.shotoku.feeds.data.Feed;
import org.jboss.shotoku.feeds.tools.FeedsConstants;
-import org.jboss.shotoku.feeds.tools.FeedsTools;
-import org.jboss.shotoku.service.AdministratedServiceImpl;
-import org.jboss.shotoku.service.AdministratedService;
import org.jboss.shotoku.ContentManager;
import org.jboss.shotoku.tools.Constants;
import org.jboss.shotoku.tools.Pair;
import javax.ejb.Local;
-import java.util.HashMap;
-import java.util.Map;
import java.util.Properties;
/**
@@ -32,34 +25,21 @@
@Local(FeedsServiceLocal.class)
@Management(FeedsService.class)
@Depends(Constants.SHOTOKU_SERVICE_NAME)
-public class FeedsServiceImpl extends AdministratedServiceImpl
- implements FeedsService, FeedsServiceLocal {
+public class FeedsServiceImpl implements FeedsService, FeedsServiceLocal {
//private static final Logger log = Logger.getLogger(AdministratedService.class);
/*
* FeedsService implementation.
*/
- private static CacheItemUser<Pair<String, String>, Properties> conf;
-
- private Map<String, FeedsDescriptor> fds;
+ private CacheItemUser<Pair<String, String>, Properties> conf;
+ private CacheItemUser<String, FeedsDescriptor> feeds;
private int remoteConnectionTimeout;
private int remoteReadTimeout;
- private synchronized FeedsDescriptor getFeedsDescriptor(String id) {
- if (!fds.containsKey(id)) {
- // Just marking the feed for renewal on the nearest update.
- fds.put(id, new DummyFeedsDescriptor(id));
-
- // And interrupting the update thread to update asap.
- Thread ut = getUpdateThread();
- if (ut != null) {
- ut.interrupt();
- }
- }
-
- return fds.get(id);
+ private FeedsDescriptor getFeedsDescriptor(String id) {
+ return feeds.get(id);
}
public String getProperty(String id, String name) {
@@ -72,72 +52,27 @@
}
/*
- * Update method.
- */
-
- public void update() {
- // Updating descriptors.
- for (String id : fds.keySet()) {
- FeedsDescriptor fd = fds.get(id);
- fds.put(id, new FeedsDescriptorImpl(id, conf, getRemoteConnectionTimeout(),
- getRemoteReadTimeout()));
- fd.release();
- }
- }
-
- /*
* Service management.
*/
public void create() throws Exception {
- super.create();
-
conf = CacheItem.create(new FeedsConfigurationWatcher());
-
- setTimerInterval(ContentManager.getProperty(FeedsConstants.PROPERTY_INTERVAL, 600000));
+
setRemoteReadTimeout(ContentManager.getProperty(FeedsConstants.PROPERTY_READ_TIMEOUT, 3000));
setRemoteConnectionTimeout(ContentManager.getProperty(FeedsConstants.PROPERTY_CONN_TIMEOUT, 5000));
-
- fds = new HashMap<String, FeedsDescriptor>();
-
- super.afterCreate();
}
public void start() throws Exception {
- super.start();
- startUpdateThread();
- super.afterStart();
+ feeds = CacheItem.create(new FeedsCacheItemDataSource(this));
+ feeds.setInterval(ContentManager.getProperty(FeedsConstants.PROPERTY_INTERVAL, 600000));
}
+
+ public void stop() {
+ feeds.unregister();
+ }
public void destroy() {
-
}
- /*
- * Administrated service implementation.
- */
-
- public String getServiceId() {
- return "ShotokuFeedsService";
- }
-
- public String getServiceName() {
- return "Feeds service";
- }
-
- public String getServiceDescription() {
- StringBuffer sb = new StringBuffer("Shotoku feeds service is " +
- "handling the following feed configurations: ");
- for (String id : fds.keySet()) {
- sb.append(id).append(" ");
- }
-
- return sb.append(".").toString();
- }
-
- public AdministratedService getServiceInstance() {
- return FeedsTools.getService();
- }
-
public int getRemoteConnectionTimeout() {
return remoteConnectionTimeout;
}
@@ -153,6 +88,8 @@
public void setRemoteReadTimeout(int remoteReadTimeout) {
this.remoteReadTimeout = remoteReadTimeout;
}
-
-
+
+ public CacheItemUser<Pair<String, String>, Properties> getConf() {
+ return conf;
+ }
}
Modified: labs/shotoku/trunk/shotoku-file-access/src/java/org/jboss/shotoku/fileaccess/FileAccessConfigurationWatcher.java
===================================================================
--- labs/shotoku/trunk/shotoku-file-access/src/java/org/jboss/shotoku/fileaccess/FileAccessConfigurationWatcher.java 2007-09-03 08:47:20 UTC (rev 14819)
+++ labs/shotoku/trunk/shotoku-file-access/src/java/org/jboss/shotoku/fileaccess/FileAccessConfigurationWatcher.java 2007-09-03 09:30:44 UTC (rev 14820)
@@ -5,6 +5,7 @@
import org.jboss.shotoku.cache.ShotokuResourceWatcher;
import org.jboss.shotoku.cache.ChangeType;
import org.jboss.shotoku.cache.ValueChange;
+import org.jboss.shotoku.cache.ValueInit;
import org.jboss.shotoku.tools.Pair;
import org.apache.log4j.Logger;
@@ -37,9 +38,9 @@
return new FileAccessConfiguration(props);
}
- public FileAccessConfiguration init(Pair<String, String> key) {
+ public ValueInit<FileAccessConfiguration> init(Pair<String, String> key) {
addWatchedPath(key, CONFIG_FILE_NAME);
- return getConfiguration(key);
+ return ValueInit.realValue(getConfiguration(key));
}
protected ContentManager initContentManager(Pair<String, String> key) {
More information about the jboss-svn-commits
mailing list