cancel
Showing results for 
Search instead for 
Did you mean: 

Best way to inject .properties file to a webscript

mangar
Star Contributor
Star Contributor
I have my own properties file, alfresco.properties,  and I want to know the best way to add it to a webscript. I tried this:


    @Resource(name = "alfresco")
    private Properties alfrescoProperties;


but I get a null pointer. I assume that I could to this:


        Properties props = new Properties();
        props.load(AlfrescoServiceImpl.class.getClassLoader().getResourceAsStream("alfresco.properties"));


but I don't want to load that every time the webscript is called.

What is the best way to do this?
3 REPLIES 3

kaynezhang
World-Class Innovator
World-Class Innovator
Why not just use webscrtip Scoped Config(webscript Configuration Document) or use Global Config.
You can get more information about Global and Scoped Config at http://wiki.alfresco.com/wiki/Web_Scripts

2 things, 

First, I should have said that I am using Java for my webscripts, so how would I get the config object?

Second,  I have an n-tiered architecture, with alfresco as a strictly back end system.  So I have a jar file (I call MyAlfrescoCommons.jar) that has all the code to access alfresco anywhere in the system. it has a .properties file.  I want to keep these properties in one place, so another config file for my webscripts is not exactly what I want.

kaynezhang
World-Class Innovator
World-Class Innovator

Every Web Script can declare a web script configuration file ,it is an XML file packaged with the Web Script and named like this

helloworld.get.desc.xml
helloworld.get.js
helloworld.get.html.ftl
helloworld.get.config.xml

Although it is a little complex ,it is able to get the config object from java.
1.first in your execute method call createTemplateParameters or createScriptParameters method to init config.xml file
2.then get config object from return model
 ConfigModel config = model.get("config")



If you want to use properties file ,you have 2 options;
option 1, add a init method in your class, and configured it in spring bean,like below

public classs YourWebScript{
public init(){
Properties props = new Properties();
        props.load(AlfrescoServiceImpl.class.getClassLoader().getResourceAsStream("alfresco.properties"));
}
}
<bean id="webscript.com.your webscritp.get"
      class="com.YourWebScript"
      parent="webscript" init-method="init">
   <property name="repository" ref="repositoryHelper" />
   <property name="serviceRegistry" ref="ServiceRegistry" />
</bean>


option 2: override public void init(Container container, Description description) method of AbstractWebScript

public classs YourWebScript{
public void init(Container container, Description description){
   super.init(container, description);
Properties props = new Properties();
        props.load(AlfrescoServiceImpl.class.getClassLoader().getResourceAsStream("alfresco.properties"));
}
}