mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-10-04 11:26:44 -04:00
Clean up: Moving obsolete stuff to sandbox.
This commit is contained in:
@@ -1,142 +0,0 @@
|
||||
package com.twelvemonkeys.lang;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.UndeclaredThrowableException;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static com.twelvemonkeys.lang.Validate.notNull;
|
||||
|
||||
/**
|
||||
* ExceptionUtil
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/lang/ExceptionUtil.java#2 $
|
||||
*/
|
||||
public final class ExceptionUtil {
|
||||
|
||||
/**
|
||||
* Re-throws an exception, either as-is if the exception was already unchecked, otherwise wrapped in
|
||||
* a {@link RuntimeException}.
|
||||
* "Expected" exception types are wrapped in {@link RuntimeException}s, while
|
||||
* "unexpected" exception types are wrapped in {@link java.lang.reflect.UndeclaredThrowableException}s.
|
||||
*
|
||||
* @param pThrowable the exception to launder
|
||||
* @param pExpectedTypes the types of exception the code is expected to throw
|
||||
*/
|
||||
/*public*/ static void launder(final Throwable pThrowable, Class<? extends Throwable>... pExpectedTypes) {
|
||||
if (pThrowable instanceof Error) {
|
||||
throw (Error) pThrowable;
|
||||
}
|
||||
if (pThrowable instanceof RuntimeException) {
|
||||
throw (RuntimeException) pThrowable;
|
||||
}
|
||||
|
||||
for (Class<? extends Throwable> expectedType : pExpectedTypes) {
|
||||
if (expectedType.isInstance(pThrowable)) {
|
||||
throw new RuntimeException(pThrowable);
|
||||
}
|
||||
}
|
||||
|
||||
throw new UndeclaredThrowableException(pThrowable);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "UnusedDeclaration"})
|
||||
static <T extends Throwable> void throwAs(final Class<T> pType, final Throwable pThrowable) throws T {
|
||||
throw (T) pThrowable;
|
||||
}
|
||||
|
||||
public static void throwUnchecked(final Throwable pThrowable) {
|
||||
throwAs(RuntimeException.class, pThrowable);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked"})
|
||||
/*public*/ static void handle(final Throwable pThrowable, final ThrowableHandler<? extends Throwable>... pHandlers) {
|
||||
handleImpl(pThrowable, (ThrowableHandler<Throwable>[]) pHandlers);
|
||||
}
|
||||
|
||||
private static void handleImpl(final Throwable pThrowable, final ThrowableHandler<Throwable>... pHandlers) {
|
||||
// TODO: Sort more specific throwable handlers before less specific?
|
||||
for (ThrowableHandler<Throwable> handler : pHandlers) {
|
||||
if (handler.handles(pThrowable)) {
|
||||
handler.handle(pThrowable);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Not handled, re-throw
|
||||
throwUnchecked(pThrowable);
|
||||
}
|
||||
|
||||
public static abstract class ThrowableHandler<T extends Throwable> {
|
||||
private final Class<? extends T>[] throwables;
|
||||
|
||||
protected ThrowableHandler(final Class<? extends T>... pThrowables) {
|
||||
throwables = notNull(pThrowables).clone();
|
||||
}
|
||||
|
||||
final public boolean handles(final Throwable pThrowable) {
|
||||
for (Class<? extends T> throwable : throwables) {
|
||||
if (throwable.isAssignableFrom(pThrowable.getClass())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public abstract void handle(T pThrowable);
|
||||
}
|
||||
|
||||
@SuppressWarnings({"InfiniteLoopStatement"})
|
||||
public static void main(String[] pArgs) {
|
||||
while (true) {
|
||||
foo();
|
||||
}
|
||||
}
|
||||
|
||||
private static void foo() {
|
||||
try {
|
||||
bar();
|
||||
}
|
||||
catch (Throwable t) {
|
||||
handle(t,
|
||||
new ThrowableHandler<IOException>(IOException.class) {
|
||||
public void handle(final IOException pThrowable) {
|
||||
System.out.println("IOException: " + pThrowable + " handled");
|
||||
}
|
||||
},
|
||||
new ThrowableHandler<Exception>(SQLException.class, NumberFormatException.class) {
|
||||
public void handle(final Exception pThrowable) {
|
||||
System.out.println("Exception: " + pThrowable + " handled");
|
||||
}
|
||||
},
|
||||
new ThrowableHandler<Throwable>(Throwable.class) {
|
||||
public void handle(final Throwable pThrowable) {
|
||||
System.err.println("Generic throwable: " + pThrowable + " NOT handled");
|
||||
throwUnchecked(pThrowable);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static void bar() {
|
||||
baz();
|
||||
}
|
||||
|
||||
@SuppressWarnings({"ThrowableInstanceNeverThrown"})
|
||||
private static void baz() {
|
||||
double random = Math.random();
|
||||
if (random < (1.0 / 3.0)) {
|
||||
throwUnchecked(new FileNotFoundException("FNF Boo"));
|
||||
}
|
||||
if (random < (2.0 / 3.0)) {
|
||||
throwUnchecked(new SQLException("SQL Boo"));
|
||||
}
|
||||
else {
|
||||
throwUnchecked(new Exception("Some Boo"));
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.util;
|
||||
|
||||
/**
|
||||
* AbstractResource class description.
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/AbstractResource.java#1 $
|
||||
*/
|
||||
abstract class AbstractResource implements Resource {
|
||||
protected final Object resourceId;
|
||||
protected final Object wrappedResource;
|
||||
|
||||
/**
|
||||
* Creates a {@code Resource}.
|
||||
*
|
||||
* @param pResourceId
|
||||
* @param pWrappedResource
|
||||
*/
|
||||
protected AbstractResource(Object pResourceId, Object pWrappedResource) {
|
||||
if (pResourceId == null) {
|
||||
throw new IllegalArgumentException("id == null");
|
||||
}
|
||||
if (pWrappedResource == null) {
|
||||
throw new IllegalArgumentException("resource == null");
|
||||
}
|
||||
|
||||
resourceId = pResourceId;
|
||||
wrappedResource = pWrappedResource;
|
||||
}
|
||||
|
||||
public final Object getId() {
|
||||
return resourceId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default implementation simply returns {@code asURL().toExternalForm()}.
|
||||
*
|
||||
* @return a string representation of this resource
|
||||
*/
|
||||
public String toString() {
|
||||
return asURL().toExternalForm();
|
||||
}
|
||||
|
||||
/**
|
||||
* Defautl implementation returns {@code mWrapped.hashCode()}.
|
||||
*
|
||||
* @return {@code mWrapped.hashCode()}
|
||||
*/
|
||||
public int hashCode() {
|
||||
return wrappedResource.hashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Default implementation
|
||||
*
|
||||
* @param pObject
|
||||
* @return
|
||||
*/
|
||||
public boolean equals(Object pObject) {
|
||||
return pObject instanceof AbstractResource
|
||||
&& wrappedResource.equals(((AbstractResource) pObject).wrappedResource);
|
||||
}
|
||||
}
|
@@ -1,78 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.util;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* FileResource class description.
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/FileResource.java#1 $
|
||||
*/
|
||||
final class FileResource extends AbstractResource {
|
||||
|
||||
/**
|
||||
* Creates a {@code FileResource}.
|
||||
*
|
||||
* @param pResourceId the resource id
|
||||
* @param pFile the file resource
|
||||
*/
|
||||
public FileResource(Object pResourceId, File pFile) {
|
||||
super(pResourceId, pFile);
|
||||
}
|
||||
|
||||
private File getFile() {
|
||||
return (File) wrappedResource;
|
||||
}
|
||||
|
||||
public URL asURL() {
|
||||
try {
|
||||
return getFile().toURL();
|
||||
}
|
||||
catch (MalformedURLException e) {
|
||||
throw new IllegalStateException("The file \"" + getFile().getAbsolutePath()
|
||||
+ "\" is not parseable as an URL: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream asStream() throws IOException {
|
||||
return new FileInputStream(getFile());
|
||||
}
|
||||
|
||||
public long lastModified() {
|
||||
return getFile().lastModified();
|
||||
}
|
||||
}
|
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.util;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* Resource class description.
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/Resource.java#1 $
|
||||
*/
|
||||
public interface Resource {
|
||||
/**
|
||||
* Returns the id of this resource.
|
||||
*
|
||||
* The id might be a {@code URL}, a {@code File} or a string
|
||||
* representation of some system resource.
|
||||
* It will always be equal to the {@code reourceId} parameter
|
||||
* sent to the {@link ResourceMonitor#addResourceChangeListener} method
|
||||
* for a given resource.
|
||||
*
|
||||
* @return the id of this resource
|
||||
*/
|
||||
public Object getId();
|
||||
|
||||
/**
|
||||
* Returns the {@code URL} for the resource.
|
||||
*
|
||||
* @return the URL for the resource
|
||||
*/
|
||||
public URL asURL();
|
||||
|
||||
/**
|
||||
* Opens an input stream, that reads from this reource.
|
||||
*
|
||||
* @return an intput stream, that reads frmo this resource.
|
||||
*
|
||||
* @throws IOException if an I/O exception occurs
|
||||
*/
|
||||
public InputStream asStream() throws IOException;
|
||||
|
||||
/**
|
||||
* Returns the last modified time.
|
||||
* Should only be used for comparison.
|
||||
*
|
||||
* @return the time of the last modification of this resource, or
|
||||
* {@code -1} if the last modification time cannot be determined.
|
||||
*/
|
||||
public long lastModified();
|
||||
}
|
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.util;
|
||||
|
||||
import java.util.EventListener;
|
||||
|
||||
/**
|
||||
* An {@code EventListener} that listens for updates in file or system
|
||||
* resources.
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/ResourceChangeListener.java#1 $
|
||||
*/
|
||||
public interface ResourceChangeListener extends EventListener {
|
||||
/**
|
||||
* Invoked when a resource is changed.
|
||||
* Implementations that listens for multiple eventes, should check that
|
||||
* {@code Resource.getId()} is equal to the {@code reourceId} parameter
|
||||
* sent to the {@link ResourceMonitor#addResourceChangeListener} method.
|
||||
*
|
||||
* @param pResource changed file/url/etc.
|
||||
*/
|
||||
void resourceChanged(Resource pResource);
|
||||
}
|
@@ -1,207 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
// TODO: Could this be used for change-aware classloader? Woo..
|
||||
/**
|
||||
* Monitors changes is files and system resources.
|
||||
*
|
||||
* Based on example code and ideas from
|
||||
* <A href="http://www.javaworld.com/javaworld/javatips/jw-javatip125.html">Java
|
||||
* Tip 125: Set your timer for dynamic properties</A>.
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/ResourceMonitor.java#1 $
|
||||
*/
|
||||
public abstract class ResourceMonitor {
|
||||
|
||||
private static final ResourceMonitor INSTANCE = new ResourceMonitor() {};
|
||||
|
||||
private Timer timer;
|
||||
|
||||
private final Map<Object, ResourceMonitorTask> timerEntries;
|
||||
|
||||
public static ResourceMonitor getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code ResourceMonitor}.
|
||||
*/
|
||||
protected ResourceMonitor() {
|
||||
// Create timer, run timer thread as daemon...
|
||||
timer = new Timer(true);
|
||||
timerEntries = new HashMap<Object, ResourceMonitorTask>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a monitored {@code Resource} with a {@code ResourceChangeListener}.
|
||||
*
|
||||
* The {@code reourceId} might be a {@code File} a {@code URL} or a
|
||||
* {@code String} containing a file path, or a path to a resource in the
|
||||
* class path. Note that class path resources are resolved using the
|
||||
* given {@code ResourceChangeListener}'s {@code ClassLoader}, then
|
||||
* the current {@code Thread}'s context class loader, if not found.
|
||||
*
|
||||
* @param pListener pListener to notify when the file changed.
|
||||
* @param pResourceId id of the resource to monitor (a {@code File}
|
||||
* a {@code URL} or a {@code String} containing a file path).
|
||||
* @param pPeriod polling pPeriod in milliseconds.
|
||||
*
|
||||
* @see ClassLoader#getResource(String)
|
||||
*/
|
||||
public void addResourceChangeListener(ResourceChangeListener pListener,
|
||||
Object pResourceId, long pPeriod) throws IOException {
|
||||
// Create the task
|
||||
ResourceMonitorTask task = new ResourceMonitorTask(pListener, pResourceId);
|
||||
|
||||
// Get unique Id
|
||||
Object resourceId = getResourceId(pResourceId, pListener);
|
||||
|
||||
// Remove the old task for this Id, if any, and register the new one
|
||||
synchronized (timerEntries) {
|
||||
removeListenerInternal(resourceId);
|
||||
timerEntries.put(resourceId, task);
|
||||
}
|
||||
|
||||
timer.schedule(task, pPeriod, pPeriod);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the {@code ResourceChangeListener} from the notification list.
|
||||
*
|
||||
* @param pListener the pListener to be removed.
|
||||
* @param pResourceId name of the resource to monitor.
|
||||
*/
|
||||
public void removeResourceChangeListener(ResourceChangeListener pListener, Object pResourceId) {
|
||||
synchronized (timerEntries) {
|
||||
removeListenerInternal(getResourceId(pResourceId, pListener));
|
||||
}
|
||||
}
|
||||
|
||||
private void removeListenerInternal(Object pResourceId) {
|
||||
ResourceMonitorTask task = timerEntries.remove(pResourceId);
|
||||
|
||||
if (task != null) {
|
||||
task.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
private Object getResourceId(Object pResourceName, ResourceChangeListener pListener) {
|
||||
return pResourceName.toString() + System.identityHashCode(pListener);
|
||||
}
|
||||
|
||||
private void fireResourceChangeEvent(ResourceChangeListener pListener, Resource pResource) {
|
||||
pListener.resourceChanged(pResource);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private class ResourceMonitorTask extends TimerTask {
|
||||
ResourceChangeListener mListener;
|
||||
Resource mMonitoredResource;
|
||||
long mLastModified;
|
||||
|
||||
public ResourceMonitorTask(ResourceChangeListener pListener, Object pResourceId) throws IOException {
|
||||
mListener = pListener;
|
||||
mLastModified = 0;
|
||||
|
||||
String resourceId = null;
|
||||
File file = null;
|
||||
URL url = null;
|
||||
if (pResourceId instanceof File) {
|
||||
file = (File) pResourceId;
|
||||
resourceId = file.getAbsolutePath(); // For use by exception only
|
||||
}
|
||||
else if (pResourceId instanceof URL) {
|
||||
url = (URL) pResourceId;
|
||||
if ("file".equals(url.getProtocol())) {
|
||||
file = new File(url.getFile());
|
||||
}
|
||||
resourceId = url.toExternalForm(); // For use by exception only
|
||||
}
|
||||
else if (pResourceId instanceof String) {
|
||||
resourceId = (String) pResourceId; // This one might be looked up
|
||||
file = new File((String) resourceId);
|
||||
}
|
||||
|
||||
if (file != null && file.exists()) {
|
||||
// Easy, this is a file
|
||||
mMonitoredResource = new FileResource(pResourceId, file);
|
||||
//System.out.println("File: " + mMonitoredResource);
|
||||
}
|
||||
else {
|
||||
// No file there, but is it on CLASSPATH?
|
||||
if (url == null) {
|
||||
url = pListener.getClass().getClassLoader().getResource(resourceId);
|
||||
}
|
||||
if (url == null) {
|
||||
url = Thread.currentThread().getContextClassLoader().getResource(resourceId);
|
||||
}
|
||||
|
||||
if (url != null && "file".equals(url.getProtocol())
|
||||
&& (file = new File(url.getFile())).exists()) {
|
||||
// It's a file in classpath, so try this as an optimization
|
||||
mMonitoredResource = new FileResource(pResourceId, file);
|
||||
//System.out.println("File: " + mMonitoredResource);
|
||||
}
|
||||
else if (url != null) {
|
||||
// No, not a file, might even be an external resource
|
||||
mMonitoredResource = new URLResource(pResourceId, url);
|
||||
//System.out.println("URL: " + mMonitoredResource);
|
||||
}
|
||||
else {
|
||||
throw new FileNotFoundException(resourceId);
|
||||
}
|
||||
}
|
||||
|
||||
mLastModified = mMonitoredResource.lastModified();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
long lastModified = mMonitoredResource.lastModified();
|
||||
|
||||
if (lastModified != mLastModified) {
|
||||
mLastModified = lastModified;
|
||||
fireResourceChangeEvent(mListener, mMonitoredResource);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,89 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2008, Harald Kuhr
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name "TwelveMonkeys" nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package com.twelvemonkeys.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
/**
|
||||
* URLResource class description.
|
||||
*
|
||||
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
|
||||
* @author last modified by $Author: haku $
|
||||
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/URLResource.java#1 $
|
||||
*/
|
||||
final class URLResource extends AbstractResource {
|
||||
|
||||
// NOTE: For the time being, we rely on the URL class (and helpers) to do
|
||||
// some smart caching and reuse of connections...
|
||||
// TODO: Change the implementation if this is a problem
|
||||
private long lastModified = -1;
|
||||
|
||||
/**
|
||||
* Creates a {@code URLResource}.
|
||||
*
|
||||
* @param pResourceId the resource id
|
||||
* @param pURL the URL resource
|
||||
*/
|
||||
public URLResource(Object pResourceId, URL pURL) {
|
||||
super(pResourceId, pURL);
|
||||
}
|
||||
|
||||
private URL getURL() {
|
||||
return (URL) wrappedResource;
|
||||
}
|
||||
|
||||
public URL asURL() {
|
||||
return getURL();
|
||||
}
|
||||
|
||||
public InputStream asStream() throws IOException {
|
||||
URLConnection connection = getURL().openConnection();
|
||||
connection.setAllowUserInteraction(false);
|
||||
connection.setUseCaches(true);
|
||||
return connection.getInputStream();
|
||||
}
|
||||
|
||||
public long lastModified() {
|
||||
try {
|
||||
URLConnection connection = getURL().openConnection();
|
||||
connection.setAllowUserInteraction(false);
|
||||
connection.setUseCaches(true);
|
||||
connection.setIfModifiedSince(lastModified);
|
||||
|
||||
lastModified = connection.getLastModified();
|
||||
}
|
||||
catch (IOException ignore) {
|
||||
}
|
||||
|
||||
return lastModified;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user