cancel
Showing results for 
Search instead for 
Did you mean: 

Reading alfresco-global.properties from Web Script?

netsi42
Champ in-the-making
Champ in-the-making
I have several properties that are different for each environment we are running (e.g. Test, Dev, etc).  I would like to be able to access these conveniently in alfresco-global.properties directly from a Web Script i.e. using something like ${config.global.server.my_prop_name.value}.  The closest I came gave me the error:
Exception:  org.alfresco.config.ConfigException - 03060000 Reading the Server config via the generic interfaces is not supported

I can create a file such as webscript-framework-config-custom.xml (in /shared/classes/alfresco/web-extension) and read it fine - but again, it would be great to have all values in one spot. 

I'm hoping there's a way to either read alfresco-global.properties directly, or indirectly via a reference in webscript-framework-config-custom.xml (or maybe even through the *.get.config.xml file?).  Or maybe there is a better way to handle environment-dependent properties?  We're using Enterprise 3.2R.

Thanks in advance.
9 REPLIES 9

kevinr
Star Contributor
Star Contributor
There is not a way to do this currently via a JavaScript backed WebScript - you could do it via a Java bean backed one. Please raise an enhancement request in JIRA.

Cheers,

Kev

dynamolalit
Champ on-the-rise
Champ on-the-rise
Hi KevinR,

How can i read custom properties from java bean.

Can you give some more details.

kevinr
Star Contributor
Star Contributor
Hi,

I mean you can create a Java bean backed WebScript:
http://wiki.alfresco.com/wiki/Web_Scripts#Java-Backed_Implementations
http://wiki.alfresco.com/wiki/Java-backed_Web_Scripts_Samples#Overview

For your Java WebScript bean impl, you can inject the global properties as a dependency. The bean is called "global-properties" and it's a java.util.Properties type. Then you can read whatever you want from that value.

The other option is to create a simple Java bean that exposes those properties, make that a custom script object available to all webscripts, and then access that in your usual JavaScript based webscripts.
http://wiki.alfresco.com/wiki/3.2_JavaScript_API#Adding_Custom_Script_APIs

Cheers,

Kev

dynamolalit
Champ on-the-rise
Champ on-the-rise
Hi,

I used Java Bean to read alfresco-global.properties file under WEB-INF/classes.Code for KmPropertyReader.java  :


package com.xxxx.alfresco.km.property;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
* @author Lalit Jangra.
* Class to read property from a property file.
*/

public class KmPropertyReader {
   
   private static Logger logger = Logger.getLogger(KmPropertyReader.class);
   public String property;
   Properties properties = new Properties();
   public static final String PROPERTIES_FILE = "alfresco-global.properties";

   public KmPropertyReader() {
      ClassLoader classLoader = Thread.currentThread()
            .getContextClassLoader();
      InputStream inputStream = classLoader
            .getResourceAsStream(PROPERTIES_FILE);
      try {
         properties.load(inputStream);
      } catch (IOException e) {
         logger
               .error("Error while loading property file in KmPropertyReader() : "
                     + e.getMessage());
         e.printStackTrace();
      } finally {
         if (inputStream != null) {
            try {
               inputStream.close();
            } catch (IOException e) {
               logger
                     .error("Error while closing property file in KmPropertyReader() : "
                           + e.getMessage());
               e.printStackTrace();
            }
         }
      }
   }

   public String getProperty(String property) {
      return properties.getProperty(property);
   }

   public void setProperty(String property) {
      this.property = property;
   }
}

Its working fine.

Is there any way that i can get properties from ..\tomcat\shared\classes\alfresco-global.properties file?

kevinr
Star Contributor
Star Contributor
you can inject the global properties as a dependency. The bean is called "global-properties" and it's a java.util.Properties type. Then you can read whatever you want from that value.

dynamolalit
Champ on-the-rise
Champ on-the-rise
Hi Kevin,

Got it now.

Thanks a lot!

mrksjs
Champ on-the-rise
Champ on-the-rise
seem to be unable to expose the global properties.

i have defined
module-context.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>

   <bean id="global-properties" parent="baseJavaScriptExtension" class="java.util.Properties">
      <property name="extensionName">
         <value>globalProperties</value>
      </property>
   </bean>

</beans>
in my module,

and I m trying to call 'globalProperties' in my webscript, which is not defined and therefore throws an exception. how must the definition look like??

kevinr
Star Contributor
Star Contributor
I think you are confused with your Bean IDs - the bean you *want* to reference is already called "global-properties" - in your code you posted your have actually overriden that bean with a different implementation.

mrksjs
Champ on-the-rise
Champ on-the-rise
Indeed!

I thought it would be possible to access the alfresco-global.properties directly from the javascript controller of a web script. but this is not the case.