I have a property file that I want to monitor for changes. If the property file changes, I want to reload the property object with the new properties from the property file without reloading my webapp.

For doing this I have a written a utility class that wakes up every N seconds and checks to see if the file has been modified. If the file has been modified, it calls a method where I reload my property object.

The PropertyLoader.loadProperties method code is in listing 1.1 below. Basically I am trying to load the resource as stream from the classloader:
     ....
     ....
if (loader == null) loader = Thread.currentThread().getContextClassLoader();
     ....
     .....
    in = loader.getResourceAsStream(name);
     .....

   if (THROW_ON_LOAD_FAILURE && (result == null))
   {
     // THE EXCEPTION BELOW IS THROWN ON THE FOLLOWING LINE
throw new IllegalArgumentException("could not load [" + name + "]" + " as a classloader resource");
   }

INFO: Illegal access: this web application instance has been stopped already. Could not load Site.properties. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. Exception in thread "Timer-14" java.lang.IllegalArgumentException: could not load [Site.properties] as a classloader resource at com.ac.utils.properties.PropertyLoader.loadProperties(PropertyLoader.java:79) at com.ac.utils.properties.PropertyLoader.loadProperties(PropertyLoader.java:93) at com.ac.tandoori.web.SiteProperties.fileChanged(SiteProperties.java:249) at com.ac.utils.io.FileMonitor.fireFileChangeEvent(FileMonitor.java:78) at com.ac.utils.io.FileMonitor$FileMonitorTask.run(FileMonitor.java:123)
      at java.util.TimerThread.mainLoop(Timer.java:512)
      at java.util.TimerThread.run(Timer.java:462)

I have initialized my FileMonitor in the contextInitialized method of my webapp. The FileMonitor uses a daemon TimerTask to do its job.

When my webapplication starts everything works fine. When I change the property file, my File monitor code kicks and invokes fireFileChangeEvent and result of that is a call to the PropertyLoader.loadProperties method. Why does it not find the file in the classloader at this time? Any ideas or pointers? Is it because my FileMonitor's timertask thread's classloader has not got the property file in its classpath?

OS: Ubuntu
Tomcat version: 5.5
Java: 1.5

Listing 1.1:

public static EnhancedProperties loadProperties(String name, ClassLoader loader)
{
if (name == null) throw new IllegalArgumentException("null input: name");

  if (name.startsWith("/")) name = name.substring(1);

  // if (name.endsWith (SUFFIX))
  // name = name.substring (0, name.length () - SUFFIX.length ());

  EnhancedProperties result = null;

  InputStream in = null;
  try
  {
if (loader == null) loader = Thread.currentThread().getContextClassLoader();

    if (!name.endsWith(SUFFIX)) name = name.concat(SUFFIX);

    // returns null on lookup failures:
    in = loader.getResourceAsStream(name);
    if (in != null)
    {
      result = new EnhancedProperties();
      result.load(in);
    }
  }
  catch (Exception e)
  {
    logger.error("Error loading " + name + ": ", e);
    result = null;
  }
  finally
  {
    if (in != null) try
    {
      in.close();
    }
    catch (Throwable ignore)
    {
    }
  }

  if (THROW_ON_LOAD_FAILURE && (result == null))
  {
throw new IllegalArgumentException("could not load [" + name + "]" + " as a classloader resource");
  }

  return result;
}

thanks,


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to