FileDocCategorySizeDatePackage
ReloadMonitor.javaAPI DocGlassfish v2 API7325Fri May 04 22:35:42 BST 2007com.sun.enterprise.server

ReloadMonitor

public class ReloadMonitor extends AbstractMonitor
Monitors dynamic reload files for applications and stand alone modules from the server runtime.
atuthor
Nazrul Islam
since
JDK 1.4

Fields Summary
static final String
RELOAD_FILE
name of the file that user will update time stamp to trigger a reload
static final Logger
_logger
private static ReloadMonitor
_instance
singleton instance
Constructors Summary
private ReloadMonitor(long pollInterval)
Constructor - prohibits anyone from constructing this object.

param
pollInterval polling interval


                           
       
        super(pollInterval);
    
Methods Summary
static synchronized com.sun.enterprise.server.ReloadMonitorgetInstance(long pollInterval)
Returns the singleton instance. If the object was not created already, it uses the given polling interval.

param
pollInterval polling interval

        if (_instance == null) {
            _instance = new ReloadMonitor(pollInterval);
        }
        return _instance;
    
booleanremoveMonitoredEntry(java.lang.String id)
Removes the given application or stand alone module from the monitored list.

param
id registration name of application
return
true if removed successfully


        boolean removed = false;

        // returns false if application name is null
        if (id == null) {
            return removed;
        }

        synchronized (this._monitoredEntries) {
            Iterator iter = this._monitoredEntries.iterator();
            while (iter.hasNext()) {
                MonitorableEntry entry = (MonitorableEntry) iter.next();
                if ( id.equals(entry.getId()) ) {
                    this._monitoredEntries.remove(entry);
                    removed = true;
                    break;
                }
            }
        }

        return removed;
    
public voidrun()
This method gets called from the monitor thread. This goes through all the monitored entried and checks the time stamps. If any of the time stamps is modified, it makes a callback to its listener. The callbacks are single threaded, i.e., waits for one to finish before makes the second call.

The time stamp of the monitored entry is set to the current time stamp before the callback is made.


        try {
            ArrayList reloadList = new ArrayList();

            synchronized (_monitoredEntries) {
                Iterator iter = _monitoredEntries.iterator();
                MonitorableEntry entry = null;

                while (iter.hasNext()) {
                    entry                = (MonitorableEntry) iter.next();
                    File file            = entry.getMonitoredFile();
                    long lastModified    = file.lastModified();
                    long lastReloadedAt  = entry.getLastReloadedTimeStamp();

                    // time stamp is updated
                    if (lastModified > lastReloadedAt) {
                        // sets the time stamp so that it gets called once
                        entry.setLastReloadedTimeStamp(lastModified);

                        reloadList.add(entry);
                    }
                }
            }
            // found some entries with modified time stamp
            if (reloadList.size() > 0) {

                _logger.log(Level.FINEST,
                    "[ReloadMonitor] Monitor detected reloadable entry!");

                int size = reloadList.size();
                MonitorableEntry entry = null;

                for (int i=0; i<size; i++) {
                    entry = (MonitorableEntry) reloadList.get(i);

                    MonitorListener l = entry.getListener();

                    // calls back the listener
                    boolean success = l.reload(entry);

                    // log status
                    if (success) {
                        _logger.log(Level.INFO,
                            "core.application_reload_successful",
                            entry.getDisplayName());
                    } else {
                        _logger.log(Level.INFO,
                            "core.application_reload_failed",
                            entry.getDisplayName());
                    }
                }

                // Reload the web modules
                /*
                 * Remove compile time dependence on J2EERunner. Replace it
                 * temporarily with call to ReconfigHelper.
                J2EERunner.requestReconfiguration();
                */
                ReconfigHelper.sendReconfigMessage("");

                // removes all the entries after the call back
                reloadList.clear();
            }

        } catch (Throwable t) { 
            // catches any uncaught exceptions thrown from the handlers
            _logger.log(Level.WARNING, "core.exception", t);
        }