Skip to content

Commit

Permalink
Resources: rename resourcewatcher to notificationdispatcher. Improve …
Browse files Browse the repository at this point in the history
…utility/convenience methods javadoc
  • Loading branch information
NielsCharlier committed Dec 21, 2015
1 parent 40d0989 commit 0b07a85
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 78 deletions.
Expand Up @@ -2,8 +2,8 @@

import java.util.logging.Logger;
import org.geoserver.platform.resource.ResourceNotification;
import org.geoserver.platform.resource.ResourceWatcher;
import org.geoserver.platform.resource.SimpleResourceWatcher;
import org.geoserver.platform.resource.ResourceNotificationDispatcher;
import org.geoserver.platform.resource.SimpleResourceNotificationDispatcher;
import org.geotools.util.logging.Logging;
import org.springframework.beans.factory.InitializingBean;

Expand All @@ -13,17 +13,17 @@
import com.hazelcast.core.MessageListener;

/**
* A {@link ResourceWatcher} implementation .
* A {@link ResourceNotificationDispatcher} implementation .
* <p>
* A Spring bean of this type shall be configured in the project's {@code applicationContext.xml}
* spring configuration file in order for {@link ResourceStore} to find it.
*
*/
public class HzResourceWatcher extends SimpleResourceWatcher implements InitializingBean, MessageListener<ResourceNotification> {
public class HzResourceNotificationDispatcher extends SimpleResourceNotificationDispatcher implements InitializingBean, MessageListener<ResourceNotification> {

static final String TOPIC_NAME = "resourceWatcher";

private static final Logger LOGGER = Logging.getLogger(HzResourceWatcher.class);
private static final Logger LOGGER = Logging.getLogger(HzResourceNotificationDispatcher.class);

private HzCluster cluster;

Expand Down
Expand Up @@ -15,7 +15,7 @@
import org.easymock.IAnswer;
import org.geoserver.platform.resource.AbstractResourceWatcherTest;
import org.geoserver.platform.resource.ResourceNotification;
import org.geoserver.platform.resource.ResourceWatcher;
import org.geoserver.platform.resource.ResourceNotificationDispatcher;

import com.hazelcast.core.Cluster;
import com.hazelcast.core.HazelcastInstance;
Expand All @@ -30,10 +30,10 @@
*/
public class HzResourceWatcherTest extends AbstractResourceWatcherTest {

private HzResourceWatcher resourceWatcher;
private HzResourceNotificationDispatcher resourceWatcher;

@Override
protected ResourceWatcher initWatcher() throws Exception {
protected ResourceNotificationDispatcher initWatcher() throws Exception {
final Capture<MessageListener<ResourceNotification>> captureTopicListener = new Capture<MessageListener<ResourceNotification>>();
final Capture<ResourceNotification> captureTopicPublish = new Capture<ResourceNotification>();

Expand All @@ -43,7 +43,7 @@ protected ResourceWatcher initWatcher() throws Exception {
final HzCluster hzCluster = createMock(HzCluster.class);

expect(hz.getCluster()).andStubReturn(cluster);
expect(hz.<ResourceNotification> getTopic(HzResourceWatcher.TOPIC_NAME)).andStubReturn(topic);
expect(hz.<ResourceNotification> getTopic(HzResourceNotificationDispatcher.TOPIC_NAME)).andStubReturn(topic);
expect(topic.addMessageListener(capture(captureTopicListener))).andReturn("fake-id");
topic.publish(EasyMock.capture(captureTopicPublish));
expectLastCall().andStubAnswer(new IAnswer<Object>() {
Expand All @@ -66,7 +66,7 @@ public Object answer() throws Throwable {

replay(cluster, topic, hz, hzCluster);

resourceWatcher = new HzResourceWatcher();
resourceWatcher = new HzResourceNotificationDispatcher();
resourceWatcher.setCluster(hzCluster);
resourceWatcher.afterPropertiesSet();
return resourceWatcher;
Expand Down
Expand Up @@ -29,9 +29,9 @@
import org.geoserver.platform.resource.ResourceListener;
import org.geoserver.platform.resource.ResourceNotification;
import org.geoserver.platform.resource.ResourceStore;
import org.geoserver.platform.resource.ResourceWatcher;
import org.geoserver.platform.resource.ResourceNotificationDispatcher;
import org.geoserver.platform.resource.Resources;
import org.geoserver.platform.resource.SimpleResourceWatcher;
import org.geoserver.platform.resource.SimpleResourceNotificationDispatcher;

/**
* Implementation of ResourceStore backed by a JDBC DirectoryStructure.
Expand All @@ -47,7 +47,7 @@ public class JDBCResourceStore implements ResourceStore {
/** LockProvider used to secure resources for exclusive access */
protected LockProvider lockProvider = new NullLockProvider();

protected ResourceWatcher resourceWatcher = new SimpleResourceWatcher();
protected ResourceNotificationDispatcher resourceWatcher = new SimpleResourceNotificationDispatcher();

protected JDBCDirectoryStructure dir;
protected ResourceCache cache;
Expand Down Expand Up @@ -76,7 +76,7 @@ public void setLockProvider(LockProvider lockProvider) {
*
* @param resourceWatcher
*/
public void setResourceWatcher(ResourceWatcher resourceWatcher) {
public void setResourceWatcher(ResourceNotificationDispatcher resourceWatcher) {
this.resourceWatcher = resourceWatcher;
}

Expand Down Expand Up @@ -177,7 +177,7 @@ public InputStream in() {

@Override
public OutputStream out() {
List<ResourceNotification.Event> events = SimpleResourceWatcher.createEvents(this,
List<ResourceNotification.Event> events = SimpleResourceNotificationDispatcher.createEvents(this,
ResourceNotification.Kind.ENTRY_CREATE);
if (entry.createResource()) {
resourceWatcher.changed(new ResourceNotification(path(), ResourceNotification.Kind.ENTRY_CREATE,
Expand Down Expand Up @@ -283,7 +283,7 @@ public boolean delete() {
List<Lock> locks = new ArrayList<Lock>();
lockRecursively(locks);
try {
List<ResourceNotification.Event> events = SimpleResourceWatcher.createEvents(this,
List<ResourceNotification.Event> events = SimpleResourceNotificationDispatcher.createEvents(this,
ResourceNotification.Kind.ENTRY_DELETE);
if (entry.delete()) {
resourceWatcher.changed(new ResourceNotification(path(), ResourceNotification.Kind.ENTRY_DELETE,
Expand All @@ -308,9 +308,9 @@ private void lockRecursively(List<Lock> locks) {

@Override
public boolean renameTo(Resource dest) {
List<ResourceNotification.Event> eventsDelete = SimpleResourceWatcher.createEvents(this,
List<ResourceNotification.Event> eventsDelete = SimpleResourceNotificationDispatcher.createEvents(this,
ResourceNotification.Kind.ENTRY_DELETE);
List<ResourceNotification.Event> eventsRename = SimpleResourceWatcher.createRenameEvents(this, dest);
List<ResourceNotification.Event> eventsRename = SimpleResourceNotificationDispatcher.createRenameEvents(this, dest);
boolean result;
if (dest instanceof JDBCResource) {
result = entry.renameTo(((JDBCResource) dest).entry);
Expand Down Expand Up @@ -358,7 +358,7 @@ public CachingOutputStreamWrapper(File tempFile) throws IOException {
public void close() throws IOException {
final Lock lock = lock();
try {
List<ResourceNotification.Event> events = SimpleResourceWatcher.createEvents(JDBCResource.this,
List<ResourceNotification.Event> events = SimpleResourceNotificationDispatcher.createEvents(JDBCResource.this,
ResourceNotification.Kind.ENTRY_MODIFY);

entry.setContent(new FileInputStream(tempFile));
Expand Down
Expand Up @@ -11,7 +11,7 @@
import org.geoserver.platform.resource.LockProvider;
import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.ResourceStore;
import org.geoserver.platform.resource.ResourceWatcher;
import org.geoserver.platform.resource.ResourceNotificationDispatcher;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

Expand Down Expand Up @@ -58,7 +58,7 @@ public void setLockProvider(LockProvider lockProvider) {
*
* @param resourceWatcher
*/
public void setResourceWatcher(ResourceWatcher resourceWatcher) {
public void setResourceWatcher(ResourceNotificationDispatcher resourceWatcher) {
if (resourceStore instanceof JDBCResourceStore) {
((JDBCResourceStore) resourceStore).setResourceWatcher(resourceWatcher);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/src/main/java/applicationContext.xml
Expand Up @@ -16,8 +16,8 @@
</bean>
<bean id="lockProviderInitializer" class="org.geoserver.config.LockProviderInitializer"/>


<bean id="resourceWatcher" class="org.geoserver.platform.resource.SimpleResourceWatcher"/>
<!-- used by alternative resource stores -->
<bean id="resourceNotificationDispatcher" class="org.geoserver.platform.resource.SimpleResourceNotificationDispatcher"/>

<!-- resources -->
<bean id="dataDirectoryResourceStore" class="org.geoserver.platform.resource.DataDirectoryResourceStore">
Expand Down
Expand Up @@ -177,55 +177,36 @@ public boolean remove(String path) {
}

/**
* Used to look up files based on user provided url (or path).
* Used to look up resources based on user provided url (or path) using the Data Directory as base directory.
*
* This method (originally from vfny GeoserverDataDirectory) is used to process a URL provided
* by a user: <i>Given a path, tries to interpret it as a file into the data directory, or as an absolute
* location, and returns the actual absolute location of the file.</i>
* Convenience method for Resources.fromURL(resources.get(Paths.BASE), url)
*
* Over time this url method has grown in the telling to support:
* <ul>
* <li>Actual URL to external resource using http or ftp protocol - will return null</li>
* <li>Resource URL - will support resources from resource store</li>
* <li>File URL - will support absolute file references</li>
* <li>File URL - will support relative file references - this is deprecated, use resource: instead</li>
* <li>Fake URLs - sde://user:pass@server:port - will return null.</li>
* <li>path - user supplied file path (operating specific specific)</li>
* </ul>
* See {@link Resources#fromURL(Resource, String)}
*
* Note that the baseDirectory is optional (and may be null).
*
* @param url File URL or path relative to data directory
*
* @return File indicated by provided URL
*/
public Resource fromURL(String url) {
return Resources.fromURL(resources.get(Paths.BASE), url);
}

/**
* Used to look up files based on user provided url.
* Used to look up resources based on user provided url using the Data Directory as base directory.
*
* Convenience method for Resources.fromURL(resources.get(Paths.BASE), url)
*
* Supports
* <li>Actual URL to external resource using http or ftp protocol - will return null</li>
* <li>Resource URL - will support resources from resource store</li>
* <li>File URL - will support absolute file references</li>
* <li>File URL - will support relative file references - this is deprecated, use resource: instead</li>
* <li>Fake URLs - sde://user:pass@server:port - will return null.</li>
* See {@link Resources#fromURL(Resource, URL)}
*
* @param url the url
* @return corresponding Resource
*/
public Resource fromURL(URL url) {
return Resources.fromURL(resources.get(Paths.BASE), url);
}

/**
* Creates resource from a path, if the path is relative it will return a resource from the ResourceStore
* otherwise it will return a file based resource
* Used to look up resources based on user provided path using the Data Directory as base directory.
*
* Convenience method for Resources.fromPath(resources.get(Paths.BASE), path)
*
* See {@link Resources#fromPath(Resource, String)}
*
* @param path relative or absolute path
* @return resource
*/
public Resource fromPath(String path) {
return Resources.fromPath(path, resources.get(Paths.BASE));
Expand Down
Expand Up @@ -11,7 +11,7 @@
* @author Niels Charlier
*
*/
public interface ResourceWatcher {
public interface ResourceNotificationDispatcher {

/**
* Add resource listener to this watcher.
Expand All @@ -37,7 +37,7 @@ public interface ResourceWatcher {
* Events should be propagated to children and parents automatically where applicable,to avoid unnecessary
* communication between GeoServer instances in a clustered environment.
* (Delete notifications are propagated to their children. All operations are propagated to their parents.)
* See {@link SimpleResourceWatcher} for an example.
* See {@link SimpleResourceNotificationDispatcher} for an example.
*
* @param notification
*/
Expand Down
Expand Up @@ -485,9 +485,9 @@ public static Resource createRandom(String prefix, String suffix, Resource dir)


/**
* Used to look up files based on user provided url (or path).
* Used to look up resources based on user provided url (or path) using the Data Directory as base directory.
*
* This method (originally from vfny GeoserverDataDirectory) is used to process a URL provided
* This method is used to process a URL provided
* by a user: <i>Given a path, tries to interpret it as a file into the data directory, or as an absolute
* location, and returns the actual absolute location of the file.</i>
*
Expand All @@ -501,20 +501,18 @@ public static Resource createRandom(String prefix, String suffix, Resource dir)
* <li>path - user supplied file path (operating specific specific)</li>
* </ul>
*
* Note that the baseDirectory is optional (and may be null).
*
* @param url File URL or path relative to data directory
*
* @return File indicated by provided URL
* @return Resource indicated by provided URL
*/
public static Resource fromURL(String path) {
return ((GeoServerResourceLoader) GeoServerExtensions.bean("resourceLoader")).fromURL(path);
}

/**
* Used to look up files based on user provided url (or path).
* Used to look up resources based on user provided url (or path).
*
* This method (originally from vfny GeoserverDataDirectory) is used to process a URL provided
* This method is used to process a URL provided
* by a user: <i>iven a path, tries to interpret it as a file into the data directory, or as an absolute
* location, and returns the actual absolute location of the file.</i>
*
Expand All @@ -533,7 +531,7 @@ public static Resource fromURL(String path) {
* @param baseDirectory Optional base directory used to resolve relative file URLs
* @param url File URL or path relative to data directory
*
* @return File indicated by provided URL
* @return Resource indicated by provided URL
*/
public static Resource fromURL(Resource baseDirectory, String url) {
String ss;
Expand Down Expand Up @@ -587,7 +585,7 @@ public static Resource fromURL(Resource baseDirectory, String url) {
}

/**
* Used to look up files based on user provided url.
* Used to look up resources based on user provided url, using the Data Directory as base directory.
*
* Supports
* <li>Actual URL to external resource using http or ftp protocol - will return null</li>
Expand All @@ -596,7 +594,6 @@ public static Resource fromURL(Resource baseDirectory, String url) {
* <li>File URL - will support relative file references - this is deprecated, use resource: instead</li>
* <li>Fake URLs - sde://user:pass@server:port - will return null.</li>
*
* @param baseDirectory base directory for resource: or relative file: paths
* @param url the url
* @return corresponding Resource
*/
Expand All @@ -605,7 +602,7 @@ public static Resource fromURL(URL url) {
}

/**
* Used to look up files based on user provided url.
* Used to look up a resource based on user provided url.
*
* Supports
* <li>Actual URL to external resource using http or ftp protocol - will return null</li>
Expand Down
Expand Up @@ -22,7 +22,7 @@
* @author Niels Charlier
*
*/
public class SimpleResourceWatcher implements ResourceWatcher {
public class SimpleResourceNotificationDispatcher implements ResourceNotificationDispatcher {

private Map<String, List<ResourceListener>> handlers = new HashMap<String, List<ResourceListener>>();

Expand Down
Expand Up @@ -23,7 +23,7 @@
public abstract class AbstractResourceWatcherTest {

protected FileSystemResourceStore store;
protected ResourceWatcher watcher;
protected ResourceNotificationDispatcher watcher;

protected static class CheckingResourceListener implements ResourceListener {
private boolean checked = false;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void setup() throws Exception {
watcher = initWatcher();
}

protected abstract ResourceWatcher initWatcher() throws Exception;
protected abstract ResourceNotificationDispatcher initWatcher() throws Exception;

@Test
public void testDeleteNotification() {
Expand All @@ -86,7 +86,7 @@ public void testDeleteNotification() {
watcher.addListener(res.get("DirC/FileC1"), chkFileC1);
watcher.addListener(res.get("DirC/FileC2"), chkFileC2);

List<Event> events = SimpleResourceWatcher.createEvents(res, Kind.ENTRY_DELETE);
List<Event> events = SimpleResourceNotificationDispatcher.createEvents(res, Kind.ENTRY_DELETE);
watcher.changed(new ResourceNotification("DirA", Kind.ENTRY_DELETE, System.currentTimeMillis(), events));

//test that listeners received events
Expand Down Expand Up @@ -119,7 +119,7 @@ public void testModifyNotification() {
watcher.addListener(store.get("DirA/DirC"), chkDirC);
watcher.addListener(store.get("DirA"), chkDirA);

List<Event> events = SimpleResourceWatcher.createEvents(res, Kind.ENTRY_MODIFY);
List<Event> events = SimpleResourceNotificationDispatcher.createEvents(res, Kind.ENTRY_MODIFY);
watcher.changed(new ResourceNotification("DirA/DirC/FileC1", Kind.ENTRY_MODIFY, System.currentTimeMillis(), events));

//test that listeners received events
Expand Down Expand Up @@ -148,7 +148,7 @@ public void testCreateNotification() {
watcher.addListener(store.get("DirA/DirC"), chkDirC);
watcher.addListener(store.get("DirA"), chkDirA);

List<Event> events = SimpleResourceWatcher.createEvents(res, Kind.ENTRY_CREATE);
List<Event> events = SimpleResourceNotificationDispatcher.createEvents(res, Kind.ENTRY_CREATE);
watcher.changed(new ResourceNotification("DirA/DirC/DirD/FileQ", Kind.ENTRY_CREATE, System.currentTimeMillis(), events));

//test that listeners received events
Expand Down

0 comments on commit 0b07a85

Please sign in to comment.