Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

How to store my application's settings in WAS DB

Hi,

I have developed a web application using some EJB's.

Currently the application uses a text .ini file stored in the fle system to store several settings,

I would like to store the settings in WAS DB and configure them using Visual Admin, how can I do that?

Thanks

Former Member
replied

Hi Shachar,

1) Place sap.application.global.properties file in META-INF folder of your EAR project.

2) In META-INF\application-j2ee-engine.xml put

<i>

<reference

reference-type="hard">

<reference-target

provider-name="sap.com"

target-type="service">configuration</reference-target>

</reference>

</i>

3) In your EJB project add to classpath C:\Program Files\SAP\JDT\eclipse\plugins\com.sap.tc.ap\comp\SAP-JEE\DCs\sap.com\configuration\_comp\gen\default\public\default\lib\java\configuration.jar

4) To change property go to Visual Admin->Your SID->Server->Services->Configuration Adapter->Runtime->Display Configuration->Configurations->apps-><PROVIDER NAME>-><APPLICATION NAME>->appcfg->Propertysheet application.global.properties. To edit properties you should swith to edit mode (left button on the top)).

5) To access property from code you can use something like:

import com.sap.engine.frame.core.configuration.ChangeEvent;
import com.sap.engine.frame.core.configuration.Configuration;
import com.sap.engine.frame.core.configuration.ConfigurationChangedListener;
import com.sap.engine.frame.core.configuration.ConfigurationContext;
import com.sap.engine.frame.core.configuration.ConfigurationHandler;
import com.sap.engine.frame.core.configuration.addons.PropertySheet;
import com.sap.engine.services.configuration.ConfigurationRuntimeInterface;
import com.sap.engine.services.configuration.appconfiguration.ApplicationConfigurationHandler;

...
	public Properties getProperties() throws ServiceLocatorException 
	{
		if(null==_properties || _configurationChanged)
		{
			try 
			{
				_properties = loadProperties();
			}
			catch (Exception e) 
			{
				//handle exception
			}
			finally
			{
				_configurationChanged = false;
			}		
		}
		return _properties;  
	}
	
	private Properties loadProperties() throws Exception
	{
		final ConfigurationHandler configHandler = getConfigHandler();
		final Configuration config = configHandler.openConfiguration(ServiceLocator.CONFIGURATION_PATH, 
																ApplicationConfigurationHandler.READ_ACCESS);
		final Map values = config.getAllConfigEntries();
		final Iterator iterator = values.entrySet().iterator();
		final Properties properties =  new Properties();
		
		while(iterator.hasNext()) 
		{
			final Map.Entry entry = (Map.Entry)iterator.next();
			final String key = (String)entry.getKey();
						
			if(key.startsWith(PropertySheet.CUSTOM_CHAR)) 
			{
				properties.put(key.substring(1), entry.getValue());
			}
			else if(key.startsWith(PropertySheet.DEFAULT_CHAR)) 
			{
				if(properties.get(key.substring(1))==null) 
				{
					properties.put(key.substring(1), entry.getValue());
				}
			}
		}
		
		return properties; 
	}
	
	private ConfigurationHandler getConfigHandler() throws Exception 
	{ 
		ConfigurationRuntimeInterface configInterface = null;
		boolean isNew = false;
		
		try 
		{
			if (!cache.containsKey(CONFIGURATION_SERVICE_JNDI_NAME)) 
			{
				isNew = true;
			}
			configInterface = (ConfigurationRuntimeInterface) getObject(CONFIGURATION_SERVICE_JNDI_NAME);
		} catch(ClassCastException e) {
			configInterface = (ConfigurationRuntimeInterface) reloadObject(CONFIGURATION_SERVICE_JNDI_NAME);
			isNew = true;
		}
		
		final ConfigurationContext configContext = configInterface.getConfigurationContext();
		final ConfigurationHandler configHandler = configContext.getConfigurationHandler();
		
		if(isNew)
		{
			configHandler.addConfigurationChangedListener(PROPERTY_CHANGED_LISTENER, ServiceLocator.CONFIGURATION_PATH );
		}
		return configHandler;
	}
	
	private static final ConfigurationChangedListener PROPERTY_CHANGED_LISTENER = new PropertyChangedListener();
	
	private static class PropertyChangedListener implements ConfigurationChangedListener, Serializable
	{
		public void configurationChanged(ChangeEvent changeevent)
		{
			_configurationChanged = true;
		}
	}

private InitialContext ic;
private Map cache; //used to hold references for re-use

private static ServiceLocator me;

static {
  try {
	me = new ServiceLocator();
  } catch(ServiceLocatorException se) {
	System.err.println(se);
	se.printStackTrace(System.err);
  }
}

private ServiceLocator() throws ServiceLocatorException  {
  try {
	ic = new InitialContext();
	cache = Collections.synchronizedMap(new HashMap());
  } catch (NamingException ne) {
		throw new ServiceLocatorException(ErrorConstants.INITIAL_CONTEXT_EXCEPTION ,ne);
  } catch (Exception e) {
		throw new ServiceLocatorException(ErrorConstants.GENERIC_SERVICE_LOCATOR_ERROR ,e);
   }
}

public Object getObject(String objectName) throws ServiceLocatorException {
  Object obj = null;
  try { 
	if (cache.containsKey(objectName)) {
		obj = cache.get(objectName);
	} else {         
		obj = ic.lookup(objectName);
		cache.put(objectName, obj);
	}
   } catch (NamingException ne) {
		throw new ServiceLocatorException(ErrorConstants.OBJECT_LOOKUP_EXCEPTION,ne);
   } catch (Exception e) {
		throw new ServiceLocatorException(ErrorConstants.GENERIC_SERVICE_LOCATOR_ERROR ,e);
   }

   return obj;
}

	private static boolean _configurationChanged = false;	
	private static Properties _properties;
		
	private static final String CONFIGURATION_PATH              = "apps/<PROVIDER NAME>/<APPLICATION NAME>/appcfg/application.global.properties";
	private static final String CONFIGURATION_SERVICE_JNDI_NAME = "configuration";

Best regards, Maksim Rashchynski.

Message was edited by: Maksim Rashchynski

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question