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 NeemePraks:
http://wiki.apache.org/tapestry/Tapestry5HowToReadSymbolsFromPropertiesFile

The comment on the change is:
improved PropertiesFileSymbolProvider implementation to be more generic

------------------------------------------------------------------------------
  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.IOException;
+ import java.io.InputStream;
+ import java.net.URL;
+ import java.util.Properties;
+ 
+ import org.apache.tapestry5.ioc.services.SymbolProvider;
+ import org.slf4j.Logger;
+ 
- public class PropertiesFileSymbolProvider implements SymbolProvider
+ public class PropertiesFileSymbolProvider implements SymbolProvider {
- {
+ 
      private Properties properties;
  
-     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) {
-                 in = ClassLoader.getSystemResourceAsStream(resourceName);
+                 URL url = ClassLoader.getSystemResource(resourceName);
+                 if (url == null)
+                     throw new IllegalArgumentException(
+                             "properties file resource not found on classpath: 
" + resourceName);
+                 load(logger, url);
-             else
+             } else {
-                 in = new FileInputStream(resourceName);
+                 File f = new File(resourceName);
- 
-             // ClassLoader.getSystemResourceAsStream() returns null if
-             // the resource cannot be found on the classpath
-             if (in == null)
-                 throw new FileNotFoundException();
+                 if (f.exists()) {
+                     load(logger, f.toURL());
+                 } else {
+                     logger.error("properties file not found: " + 
resourceName);
+                 }
-             
+             }
-             properties.load(in);
- 
-         } catch (FileNotFoundException e)
-         {
-             logger.error("Could not find '" + resourceName + "'");
-         } catch (IOException e)
+         } catch (IOException e) {
-         {
              logger.error("IOException while loading '" + resourceName + "': " 
+ e.getMessage());
          }
      }
+ 
+     public PropertiesFileSymbolProvider(Logger logger, URL url) {
+         try {
+             load(logger, url);
+         } catch (IOException e) {
+             logger.error("IOException while loading properties file from '" 
+                     + url + "': " + e.getMessage());
+         }
+     }
+ 
+     public PropertiesFileSymbolProvider(Logger logger, InputStream in) {
+         try {
+             load(logger, in);
+         } catch (IOException e) {
+             logger.error("IOException while loading properties file: " + 
e.getMessage());
+         }
+     }
+ 
+     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 arg0)
+     public String valueForSymbol(String symbolName) {
-     {
-         return properties.getProperty(arg0);
+         return properties.getProperty(symbolName);
      }
+ 
  }
  }}}
  

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

Reply via email to