cancel
Showing results for 
Search instead for 
Did you mean: 

[Resolved] Module with properties files

dranakan
Champ on-the-rise
Champ on-the-rise
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 :

>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 Smiley Happy
4 REPLIES 4

cheffilet
Champ in-the-making
Champ in-the-making
You should take a look to the WebClassLoader instead of using your approach.

Somewhat like this should be taked:

getClass().getClassLoader().getResource("/your/classpath/right/here");‍‍‍‍‍

mrogers
Star Contributor
Star Contributor
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.

dranakan
Champ on-the-rise
Champ on-the-rise
Thank you Smiley Happy That was exactly that I was looking for….


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
The class to load the config file :
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) {            …‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

dranakan
Champ on-the-rise
Champ on-the-rise
Better way (to get the properties in the file) !

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‍‍