[Resolved] Module with properties files
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2009 07:45 AM
Hello,
I would like to create my own properties files for my module. These properties are for example an adresse host. My module has the directory like this :
To access my file, I will use : File file = new File(???). My problem is the access to this file. In my java class, I would to have access to this place : (c:\alfresco\tomcat\webapps\alfresco\).
Are they a variable containing the path to c:\alfresco\tomcat\webapps\alfresco\ ? (the goal is to have a module working on tomcat and Jboss (the path are differents).
Thanks
I would like to create my own properties files for my module. These properties are for example an adresse host. My module has the directory like this :
>config … myConfigModule.properties>source/java … Java class which need myConfigModule.properties
To access my file, I will use : File file = new File(???). My problem is the access to this file. In my java class, I would to have access to this place : (c:\alfresco\tomcat\webapps\alfresco\).
Are they a variable containing the path to c:\alfresco\tomcat\webapps\alfresco\ ? (the goal is to have a module working on tomcat and Jboss (the path are differents).
Thanks

Labels:
- Labels:
-
Archive
4 REPLIES 4

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2009 05:44 AM
You should take a look to the WebClassLoader instead of using your approach.
Somewhat like this should be taked:
Somewhat like this should be taked:
getClass().getClassLoader().getResource("/your/classpath/right/here");
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2009 05:49 AM
The way alfresco does it is to load properties files from the classpath rather than going for absolute file names. Then it doesn't matter that your application servers are set up slightly differently.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2009 11:48 AM
Thank you
That was exactly that I was looking for….
I resume what I have done if someone is interested.

I resume what I have done if someone is interested.
- The properties file is in : C:\Alfresco\tomcat\webapps\alfresco\WEB-INF\classes\alfresco\module\truc\config\configModule.properties
The path to find this file is : /alfresco/module/truc/config/configModule.properties
My method to read the file is static, that is why the classloader is declared like this
import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import org.apache.log4j.Logger;public class Tools implements configurationModule { private static Logger logger = Logger.getLogger(Tools.class); private static ClassLoader classLoader=Tools.class.getClassLoader(); // The method accessing the config file (file="/alfresco/module/truc/config/configModule.properties") public static String getValueInConfigFile(String file, String tagToRead) { StringBuilder contents = new StringBuilder(); String valueTag = null; try { InputStream stream = classLoader.getResourceAsStream(file); BufferedReader input = new BufferedReader(new InputStreamReader(stream)); try { String line = null; // not declared within while loop while ((line = input.readLine()) != null) { …
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-24-2009 07:48 AM
Better way (to get the properties in the file) !
Java offers a reader for properties files. This work like this :
In the configModule.properties :
Java offers a reader for properties files. This work like this :
import java.util.Properties;… Properties prop = new Properties(); try{ prop.load(getClass().getResourceAsStream("/alfresco/module/truc/config/configModule.properties")); } catch(Exception e){ System.err.println("Can not load configModule.properties"); } // Get the values String server = prop.getProperty("server");
In the configModule.properties :
server=10.10.10.2
