X-Git-Url: http://source.jalview.org/gitweb/?p=vamsas.git;a=blobdiff_plain;f=src%2Fuk%2Fac%2Fvamsas%2Fclient%2Fsimpleclient%2FWatcherElement.java;h=80f9606da87ef33b208f2b3d9865b80177abcdfe;hp=b3235d54af0f3fb0f5cf534cb04a2dd39f88a2a2;hb=844ccad5a3fcbedec17b2af66d460f31abc7cff1;hpb=6f33f705957d674dc2ab6c994a6ea87f7a91f40f diff --git a/src/uk/ac/vamsas/client/simpleclient/WatcherElement.java b/src/uk/ac/vamsas/client/simpleclient/WatcherElement.java index b3235d5..80f9606 100644 --- a/src/uk/ac/vamsas/client/simpleclient/WatcherElement.java +++ b/src/uk/ac/vamsas/client/simpleclient/WatcherElement.java @@ -1,16 +1,40 @@ +/* + * This file is part of the Vamsas Client version 0.1. + * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, + * Andrew Waterhouse and Dominik Lindner. + * + * Earlier versions have also been incorporated into Jalview version 2.4 + * since 2008, and TOPALi version 2 since 2007. + * + * The Vamsas Client 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 3 of the License, or + * (at your option) any later version. + * + * The Vamsas Client 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 the Vamsas Client. If not, see . + */ package uk.ac.vamsas.client.simpleclient; - - public abstract class WatcherElement { - private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(VamsasFileWatcherElement.class); + private static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory + .getLog(VamsasFileWatcherElement.class); + protected FileWatcher watcher = null; + protected WatcherCallBack handler = null; + /** * set this to false to stop the thread */ protected boolean watchForChange = true; + /** * true when the handler is being called for this watcher */ @@ -19,77 +43,78 @@ public abstract class WatcherElement { public WatcherElement(WatcherCallBack handler) { this.handler = handler; } + /** - * will instruct watcher to stop and wait around for one WATCH_SLEEP - * before returning. If no thread is running then it returns immediately. + * will instruct watcher to stop and wait around for one WATCH_SLEEP before + * returning. If no thread is running then it returns immediately. */ public void haltWatch() { // set the flag to skip this watch element. - watchForChange=false; + watchForChange = false; if (log.isDebugEnabled()) - log.debug("haltWatch on "+watcher.getSubject()); + log.debug("haltWatch on " + watcher.getSubject()); endWatch(); if (log.isDebugEnabled()) - log.debug("haltWatch completed on "+watcher.getSubject()); + log.debug("haltWatch completed on " + watcher.getSubject()); } + /** - * called by haltWatch before - * clearing the FileWatcher reference. - * + * called by haltWatch before clearing the FileWatcher reference. + * */ protected abstract void endWatch(); + /** - * called to generate the watcher object - * by enableWatch and in doWatch - * + * called to generate the watcher object by enableWatch and in doWatch + * */ protected abstract void initWatch(); + /** * implemented for debug information purposes. + * * @return Informative string about what the watcher is watching */ protected abstract String getSubject(); + /** - * must be called by implementations of - * enablewatch + * must be called by implementations of enablewatch */ protected void enableWatch() { if (log.isDebugEnabled()) - log.debug("enableWatch on "+getSubject()); - watchForChange=true; + log.debug("enableWatch on " + getSubject()); + watchForChange = true; initWatch(); if (log.isDebugEnabled()) - log.debug("enableWatch returning on "+getSubject()); + log.debug("enableWatch returning on " + getSubject()); } /** * Originally from the uk.ac.vamsas.test.simpleclient.ArchiveClient method + * * @return true if the handler was called for a changeEvent */ public boolean doWatch() { - if (!watchForChange || handler==null) + if (!watchForChange || handler == null) return false; - if (watcher==null) + if (watcher == null) initWatch(); // somehow not done the first time - handlerCalled=false; - Lock doclock=null; + handlerCalled = false; + Lock doclock = null; try { - doclock=watcher.getChangedState(); + doclock = watcher.getChangedState(); } catch (Exception e) { - log.error("Whilst watching "+watcher.getSubject(), e); + log.error("Whilst watching " + watcher.getSubject(), e); } - if (doclock==null) + if (doclock == null) return false; - /* handlerCalled=true; - if (log.isDebugEnabled()) - log.debug("Triggering watchEvent for change on "+watcher.getSubject()); - boolean finish=!handler.handleWatchEvent(this, doclock); - doclock=null; // TODO: check that lock should really be released rather than dereferenced - if (finish) - haltWatch(); - else - enableWatch(); - handlerCalled=false;*/ + /* + * handlerCalled=true; if (log.isDebugEnabled()) + * log.debug("Triggering watchEvent for change on "+watcher.getSubject()); + * boolean finish=!handler.handleWatchEvent(this, doclock); doclock=null; // + * TODO: check that lock should really be released rather than dereferenced + * if (finish) haltWatch(); else enableWatch(); handlerCalled=false; + */ this.callHandler(doclock); return true; } @@ -97,33 +122,36 @@ public abstract class WatcherElement { /** * Calls the current eventhandler * - * @param doclock the lock on the watch file + * @param doclock + * the lock on the watch file */ - protected void callHandler(Lock doclock) - { - if (log.isDebugEnabled()) - log.debug("Triggering watchEvent for change on "+watcher.getSubject()); - boolean finish=!handler.handleWatchEvent(this, doclock); - doclock=null; // TODO: check that lock should really be released rather than dereferenced - if (finish) - haltWatch(); - else - enableWatch(); - handlerCalled=false; + protected void callHandler(Lock doclock) { + if (log.isDebugEnabled()) + log.debug("Triggering watchEvent for change on " + watcher.getSubject()); + boolean finish = !handler.handleWatchEvent(this, doclock); + doclock = null; // TODO: check that lock should really be released rather + // than dereferenced + if (finish) + haltWatch(); + else + enableWatch(); + handlerCalled = false; } - + /** * @return the handler */ public WatcherCallBack getHandler() { return handler; } + /** * @return the handlerCalled */ public boolean isHandlerCalled() { return handlerCalled; } + /** * * @return true if watcher is enabled @@ -131,12 +159,15 @@ public abstract class WatcherElement { public boolean isWatchEnabled() { return watchForChange; } + /** - * @param handler the handler to set + * @param handler + * the handler to set */ public void setHandler(WatcherCallBack handler) { this.handler = handler; } + /** * @return the watcher */