Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for 
change notification.

The following page has been changed by UlrichStaerk:
http://wiki.apache.org/tapestry/Tapestry5HowToReadSymbolsFromPropertiesFile

------------------------------------------------------------------------------
  I wanted to have a SymbolProvider that looks for configuration symbols in a 
properties file either located somewhere on the filesystem or in the classpath. 
I therefore wrote a PropertiesFileSymbolProvider that implements SymbolProvider:
  
  {{{
- import java.io.File;
+ import java.io.FileInputStream;
+ import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.URL;
@@ -27, +28 @@

  import org.apache.tapestry5.ioc.services.SymbolProvider;
  import org.slf4j.Logger;
  
- public class PropertiesFileSymbolProvider implements SymbolProvider {
+ public class PropertiesFileSymbolProvider implements SymbolProvider
- 
+ {
      private Properties properties;
  
+     /**
+      * Instantiate a new PropertiesFileSymbolProvider using a given resource 
name
+      * 
+      * @param logger the logger to log error messages to
+      * @param resourceName the name of the resource to load
+      * @param classPath whether to look on the classpath or filesystem
+      */
-     public PropertiesFileSymbolProvider(Logger logger, String resourceName, 
boolean classPath) {
+     public PropertiesFileSymbolProvider(Logger logger, String resourceName, 
boolean classPath)
+     {
+         properties = new Properties();
+         
-         try {
+         try
+         {
+             InputStream in;
+             
-             if (classPath) {
+             if (classPath)
-                 URL url = ClassLoader.getSystemResource(resourceName);
+                 in = ClassLoader.getSystemResourceAsStream(resourceName);
-                 if (url == null)
-                     throw new IllegalArgumentException(
-                             "properties file resource not found on classpath: 
" + resourceName);
-                 load(logger, url);
-             } else {
+             else
-                 File f = new File(resourceName);
+                 in = new FileInputStream(resourceName);
-                 if (f.exists()) {
-                     load(logger, f.toURL());
-                 } else {
-                     logger.error("properties file not found: " + 
resourceName);
-                 }
+ 
+             // ClassLoader.getSystemResourceAsStream() returns null if
+             // the resource cannot be found on the classpath
+             if (in == null)
+                 throw new FileNotFoundException();
-             }
+             
+             properties.load(in);
+ 
+         } catch (FileNotFoundException e)
+         {
+             String msg = "Could not find '" + resourceName + "'";
+             
+             logger.error(msg);
+             
+             throw new IllegalArgumentException(msg, e);
-         } catch (IOException e) {
+         } catch (IOException e)
+         {
-             logger.error("IOException while loading '" + resourceName + "': " 
+ e.getMessage());
+             String msg = "IOException while loading '" + resourceName + "': " 
+ e.getMessage();
+             
+             logger.error(msg);
+             
+             throw new IllegalArgumentException(msg, e);
          }
      }
- 
+     
+     /**
+      * Instantiate a PropertiesFileSymbolProvider using a given InputStream
+      * 
+      * @param logger the logger
+      * @param in an InputStream representing the resource
+      */
+     public PropertiesFileSymbolProvider(Logger logger, InputStream in)
+     {
+         initialize(logger, in);
+     }
+     
+     /**
+      * Instantiate a PropertiesFileSymbolProvider from a given URL.
+      * 
+      * @param logger the logger
+      * @param url an URL to open
+      */
-     public PropertiesFileSymbolProvider(Logger logger, URL url) {
+     public PropertiesFileSymbolProvider(Logger logger, URL url)
+     {
-         try {
+         try
-             load(logger, url);
+         {
+             initialize(logger, url.openStream());
-         } catch (IOException e) {
+         } catch (IOException e)
-             logger.error("IOException while loading properties file from '" 
-                     + url + "': " + e.getMessage());
+         {
+             String msg = "IOException while opening URL '" + url + "': " + 
e.getMessage();
+             
+             logger.error(msg);
+             
+             throw new IllegalArgumentException(msg, e);
          }
      }
- 
-     public PropertiesFileSymbolProvider(Logger logger, InputStream in) {
+     
+     private void initialize(Logger logger, InputStream in)
+     {
+         properties = new Properties();
-         try {
+         try
-             load(logger, in);
+         {
+             properties.load(in);
-         } catch (IOException e) {
+         } catch (IOException e)
+         {
-             logger.error("IOException while loading properties file: " + 
e.getMessage());
+             String msg = "IOEception while loading properties: " + 
e.getMessage();
+             
+             logger.error(msg);
+             
+             throw new IllegalArgumentException(msg, e);
          }
+         
-     }
- 
-     public void load(Logger logger, URL url) throws IOException {
-         if (url == null) throw new IllegalArgumentException("URL is 
required!");
-         load(logger, url.openStream());
-     }
- 
-     public void load(Logger logger, InputStream in) throws IOException {
-         if (in == null) throw new IllegalArgumentException("input stream is 
required!");
-         properties = new Properties();
-         properties.load(in);
      }
      
-     public String valueForSymbol(String symbolName) {
+     public String valueForSymbol(String arg0)
+     {
-         return properties.getProperty(symbolName);
+         return properties.getProperty(arg0);
      }
- 
  }
  }}}
  

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

Reply via email to