cancel
Showing results for 
Search instead for 
Did you mean: 

Setting properties that are shared by multiple web scripts

vince20151
Champ in-the-making
Champ in-the-making
I have DB settings (such as dbuser, password, URL, etc) that are shared by several Javaback webscripts. I originally place the config properties file in the local webscript location where the webscript desc.xml and the webscript free marker are placed. Per tips from this post.
http://forums.alfresco.com/forum/developer-discussions/web-scripts/how-add-more-properties-propertie...

However, with more and more webscripts sharing the same information, this becomes error prone. I like to place this file in a central location where all of my webscripts can get to. How do I accomplish this? Is there a place to put this file or an existing file to modify and what would be the java method to get to it and read it. Thanks a bunch!
9 REPLIES 9

kaynezhang
World-Class Innovator
World-Class Innovator
You can use Global and Scoped Config
Global and scoped config is read by the Alfresco Configuration Service. By default the following files are read:
alfresco/webscript-framework-config.xml
alfresco/extension/webscript-framework-config-custom.xml
any webscript-framework-config-custom.xml found in the META-INF folder of any JAR on the classpath

please refer to http://wiki.alfresco.com/wiki/Web_Scripts

Thanks for the solution.
I was able to add properties to alfresco-global.properties file and retrieve it via injecting the Properties bean into my webscript bean. The reference in my been definition includes this line
property name="properties" ref="global-properties" /
In my Java code, via the setProperties method, I get a Properties object back that contains properties in the alfresco-global.properties file. However, this seems to contain properties for alfresco-global.properties only. If I have a separate properties file, can this be done and how?
Thanks a bunch!

kaynezhang
World-Class Innovator
World-Class Innovator
Why not just add your settings in web-scripts-config-custom.xml file

The example in the listed URL shows how to access the elements in webscript-framework-config-custom.xml in JavaScript. Not sure how it is done in Java. Would appreciate any additional reference or code sample.
Thanks a bunch!

kaynezhang
World-Class Innovator
World-Class Innovator
In java you can define a spring bean of your class and inject configService bean to your class.
Like this

1.define your class and place it into {SHARED_CLASS}\

org.springframework.extensions.config.ConfigService
public class YourClass{
   private ConfigService configService;

//setter method
    public void setConfigService(ConfigService configService)
    {
        this.configService = configService;
    }

}


2.define your spring bean in *****-context.xml and place your file in {SHARED_CLASS}\alfresco\extension

   <bean id="yourbeanid" class="YourClass" >
      <property name="configService" ref="webscripts.config" />
   </bean>


3.add your configuration sections in  web-scripts-config-custom.xml file and place it in {SHARED_CLASS}\alfresco\extension,for exampl in global config add following code


<config>
  <server>
    <url>jdbc.***</url>
    <driver>org.myql.***</driver
    <username>user</username>
  </server>
</config>



4.in your  class(YourClass),get the configuration like this
<code>
  Config config = configService.getGlobalConfig();
ConfigElement server = config.get("server");
String url = server.getChildValue("url");
<code>

vince20151
Champ in-the-making
Champ in-the-making
Thanks for the code sample. I tried but not getting the desired result. Does not seem like it hits the file so the setting I'm looking for always return null.
Here are what I have done.

Place this exact content in a file webscript-framework-config-custom under \tomcat\shared\classes\alfresco\extension
<alfresco-config>
   <config>
     <dbentity>
       <dbpassword>password123</dbpassword>
     </dbentity>
   </config>
</alfresco-config>

Register the reference to configService in my bean definition
<property name="configService" ref="webscripts.config" />

Java code to read the file
Add the setter method
public void setConfigService(ConfigService myConfigService) {
      this.myConfigService = myConfigService;

Code in main process
Config myConfig = myConfigService.getGlobalConfig();
      ConfigElement dbEntity = myConfig.getConfigElement("dbentity");
      String dbPassword = "notfound";
      if (dbEntity != null)
          dbPassword = dbEntity.getChildValue("dbpassword");
      /* even try to get a list of elements */
      Map myMap = myConfig.getConfigElements();
            Iterator it = myMap.entrySet().iterator();
       while (it.hasNext()) {
           Map.Entry pairs = (Map.Entry)it.next();
          dbPassword = " - " + dbPassword;
       }
dbPassword = dbPassword + " - " + (new Integer (myMap.size()).toString());

dbPassword always return "notfound - 0" with 0 as the size of the map.

I think the code is correct but the server is not getting to the file. I even placed the properties in this file name and location alfresco/webscript-framework-config.xml and still get nothing (per instructions by this URL http://wiki.alfresco.com/wiki/Web_Scripts#Web_Script_Configuration). I also tried place it in the META-INF folder of jar file and put the jar file in this path tomcat\webapps\alfresco\WEB-INF\lib (per instruction from same URL) and still getting nothing back.

Perhaps there is something real simple I have omitted.
Any advice?
Thanks a bunch!

kaynezhang
World-Class Innovator
World-Class Innovator
rename your file webscript-framework-config-custom to  web-scripts-config-custom.xml and restart your server

vince20151
Champ in-the-making
Champ in-the-making
That did it. By renaming the file from webscript-framework-config-custom to web-scripts-config-custom.xml and restart the server, it now works. Who would have known to use that name. I searched everywhere but couldn't figure out that needs to be the name to use. Just for my info, how did you know or have that info?

Thanks a bunch!

kaynezhang
World-Class Innovator
World-Class Innovator
I have already read that part of source code .Any way ,it works good luck.