commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rhino" <rhi...@sympatico.ca>
Subject Re: Properties Utility or Solution?
Date Fri, 02 Jan 2004 19:24:34 GMT
Have you considered using the Preferences API rather than Properties files?

I've been working with them for a while and they seem pretty good to me.
Mind you, I haven't been using hundreds of preferences, just a dozen (give
or take), so I can't tell you if the performance is better, worse, or the
same as using properties.

Rhino

----- Original Message ----- 
From: "news.basebeans.net" <lukas@somnia.com>
To: <commons-user@jakarta.apache.org>
Sent: Friday, January 02, 2004 11:49 AM
Subject: Re: Properties Utility or Solution?


> > ResourceBundle resources  =
> > ResourceBundle.getBundle("path.to.your.properties.file");
> >
> > do you need it to be???
> >
> > Now I would be curious to see if anyone can come up with a way to reload
> > a properties file without having to shutdown my appserver for example or
> > having to reload a context.
>
> Only because I hate condescension am I posting this.  Loading the bundle
> is not the problem... it's dealing with hundreds of lines of properties
> and non-specific data types that is annoying.  With the posted code, you
> can synch on the entire class if you're worried about multi-threads.
>
> The problem comes with the actual parsing of the data, and the get
> methods involved.  I was hoping for a much simpler XML format that
> knowingly parses data types, stores them as static data, and creates get
> methods for fast execution.
>
> Perhaps I'm going to have to write a new loader and XDoclet creator.
>
>
>
> public class MyProperties extends Thread
> {
>
>     private static final String SLEEP_TIME = "properties.sleep";
>     private static final long DEFAULT_TIME = 1200000;
>
>     /** singleton self-reference */
>     private static MyProperties mMyProperties = null;
>     /** Holds all properties */
>     private static Properties mProperties = new Properties();
>
>     /** Flag to keep this thread running.  When set to false, the thread
> will stop. */
>     private static boolean mRunning = true;
>
>     /** time the properties file was last modified */
>     private long mLastModified = 0;
>
>     /**
>      * <p>Retrieves each of the properties.  Assumes the Properties file
> has been loaded.</p>
>      */
>     private void readProperties()
>     {
>        // Read your props here.
>     } // private void readProperties()
>
>     /**
>      * The private constructor
>      */
>     private MyProperties()
>     {
>     } // private MyProperties()
>
>     /**
>      * Returns the instance of the MyProperties
>      *
>      * @return MyProperties
>      */
>     public static MyProperties getInstance()
>     {
>        if (mMyProperties == null)
>        {
>           mMyProperties = new MyProperties();
>        }
>        return mMyProperties;
>     } // public static MyProperties getInstance()
>
>     /**
>      * This method runs forever and checks if the properties file
>      * has been changed.  If it has it will call the reload() method.
>      *
>      * @return void
>      */
>     public void run()
>     {
>        try
>        {
>           if (this.mPath == null)
>           {
>              throw new Exception("You must call init before starting the
> MyProperties thread.");
>           }
>           while (mRunning)
>           {
>              if (this.hasFileChanged())
>              {
>                 this.load();
>              }
>              try
>              {
>                 sleep(this.mSleepTime);
>              }
>              catch (InterruptedException e)
>              {
>                 // This is fine... we want to shutdown now.
>                 mRunning = false ;
>              }
>           } // end while running
>        }
>        catch (Exception e)
>        {
>           mLogger.error("Exception in MyProperties.run()", e);
>        }
>        mLogger.error("MyProperties.run() - exiting...");
>     } // public void run()
>
>     /**
>      * This is an initialization method that is used to set two member
>      * variables and to initially load the properties file
>      *
>      * @param ServletContext pServletContext
>      * @param String pPath
>      * @return void
>      */
>     public void init(ServletContext pServletContext, String pPath)
>     {
>        this.mServletContext = pServletContext;
>        this.mPath = pPath;
>        this.load();
>     } // public void init(ServletContext pServletContext, String pPath)
>
>     /**
>      * <p>Removes the singleton from memory.  Useful for shutting down
> the system.</p>
>      */
>     public void shutdown()
>     {
>        mLogger.info("shutdown");
>        mRunning = false;
>        // kick it out of sleep mode in loop
>        this.interrupt();
>        mMyProperties = null;
>     } // public void shutdown()
>
>     /**
>      * <p>Loads the properties file as specified in mPath.</p>
>      */
>     private void load()
>     {
>        mLogger.info("load() - START");
>        InputStream lInStream =
> this.mServletContext.getResourceAsStream(this.mPath);
>
>        try
>        {
>           mProperties.load(lInStream);
>        }
>        catch (IOException e)
>        {
>           mLogger.error("load() - Error while loading resource.", e);
>        }
>        finally
>        {
>           if (lInStream != null)
>           {
>              try
>              {
>                 lInStream.close();
>              }
>              catch (IOException e)
>              {
>                 mLogger.error("load() - Error while trying to close the
> InputStream.", e);
>              }
>           }
>        }
>        // Load the properties
>        this.readProperties();
>        this.setSleepTime();
>     } // private void load()
>
>     /**
>      * Sets the SleepTime from the property "properties.sleep".
>      *
>      * @return void
>      */
>     private void setSleepTime()
>     {
>        try
>        {
>           this.mSleepTime =
> Long.parseLong(mProperties.getProperty(SLEEP_TIME));
>        }
>        catch (Exception e)
>        {
>           this.mSleepTime = DEFAULT_TIME;
>
>           mLogger.warn("Make sure the property:" + SLEEP_TIME + " is set
> in the " + this.mPath + " file.", e);
>           mLogger.warn("Defaulting SleepTime to " + DEFAULT_TIME + "
> millseconds");
>        }
>     } // private void setSleepTime()
>
>     /**
>      * Checks the files last modified date and compares it to date
>      * that was previously stored.  Returns "true" if the file
> <b>has</b> changed.
>      *
>      * @return boolean
>      */
>     private boolean hasFileChanged()
>     {
>        //mLogger.info("hasFileChanged() - START");
>
>        String lRealPath = this.mServletContext.getRealPath(this.mPath);
>        File lFile = new File(lRealPath);
>        long lCurrentLastMod = lFile.lastModified();
>        boolean lBool = false;
>
>        if (this.mLastModified == 0)
>        {
>           // First time in need to set the Last Modified member variable
>
>           this.mLastModified = lCurrentLastMod;
>           lBool = false;
>        }
>        else if (this.mLastModified == lCurrentLastMod)
>        {
>           // The File has NOT changed
>           lBool = false;
>        }
>        else
>        {
>           // The File has changed
>           this.mLastModified = lCurrentLastMod;
>           lBool = true;
>        }
>
>        return lBool;
>     } // private boolean hasFileChanged()
>
>
> } // public class MyProperties extends Properties implements Runnable
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message