cancel
Showing results for 
Search instead for 
Did you mean: 

How to store my application's settings in WAS DB

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

former_member182372
Active Contributor
0 Kudos

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

Answers (5)

Answers (5)

0 Kudos

Hi Maksim,

Thanks for the excellent information. I encountered one problem trying to run your code: I get a ClassNotFoundException on calling addConfigurationChangedListener(). Any thoughts?

Regards,

Thorsten

PS Here is the stacktrace:

[code]

com.sap.engine.services.rmi_p4.exception.P4BaseRuntimeException: I/O operation failed : java.lang.ClassNotFoundException:

dk.applicon.xi.edichange.bean.ConfigurationHelper :

at com.sap.engine.services.rmi_p4.server.P4ObjectBrokerServerImpl.getException(P4ObjectBrokerServerImpl.java:1171)

at com.sap.engine.services.rmi_p4.reflect.LocalInvocationHandler.replicateParameters(LocalInvocationHandler.java:101)

at com.sap.engine.services.rmi_p4.reflect.LocalInvocationHandler.invokeInternal(LocalInvocationHandler.java:73)

at com.sap.engine.services.rmi_p4.reflect.AbstractInvocationHandler.invoke(AbstractInvocationHandler.java:53)

at $Proxy347.addConfigurationChangedListener(Unknown Source)

at dk.applicon.xi.edichange.bean.ConfigurationHelper.getConfigHandler(ConfigurationHelper.java:106)

at dk.applicon.xi.edichange.bean.ConfigurationHelper.loadProperties(ConfigurationHelper.java:56)

at dk.applicon.xi.edichange.bean.ConfigurationHelper.getProperties(ConfigurationHelper.java:47)

at dk.applicon.xi.edichange.bean.XIEdichangeBean.process(XIEdichangeBean.java:103)

at com.sap.aii.af.mp.module.ModuleLocalLocalObjectImpl0.process(ModuleLocalLocalObjectImpl0.java:103)

at com.sap.aii.af.mp.ejb.ModuleProcessorBean.process(ModuleProcessorBean.java:261)

at com.sap.aii.af.mp.processor.ModuleProcessorLocalLocalObjectImpl0.process(ModuleProcessorLocalLocalObjectImpl0.java:103)

at com.sap.aii.adapter.file.File2XI.send(File2XI.java:3156)

at com.sap.aii.adapter.file.File2XI.processFileList(File2XI.java:1309)

at com.sap.aii.adapter.file.File2XI.invoke(File2XI.java:658)

at com.sap.aii.af.service.scheduler.JobBroker$Worker.run(JobBroker.java:460)

at com.sap.engine.core.thread.impl3.ActionObject.run(ActionObject.java:37)

at java.security.AccessController.doPrivileged(Native Method)

at com.sap.engine.core.thread.impl3.SingleThread.execute(SingleThread.java:100)

at com.sap.engine.core.thread.impl3.SingleThread.run(SingleThread.java:170)

Caused by: java.lang.ClassNotFoundException: dk.applicon.xi.edichange.bean.ConfigurationHelper

at com.sap.engine.boot.FileClassLoader.findClass(FileClassLoader.java:648)

at com.sap.engine.boot.FileClassLoader.loadClass(FileClassLoader.java:561)

at com.sap.engine.boot.FileClassLoader.loadClass(FileClassLoader.java:551)

at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:219)

at com.sap.engine.services.rmi_p4.ReplicateInputStream.resolveClass(ReplicateInputStream.java:68)

at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1513)

at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1435)

at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1626)

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1274)

at java.io.ObjectInputStream.readArray(ObjectInputStream.java:1603)

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1271)

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:324)

at com.sap.engine.services.rmi_p4.reflect.LocalInvocationHandler.replicateParameters(LocalInvocationHandler.java:99)

... 18 more

[/code]

Former Member
0 Kudos

Hi Maxim,

Thanks a lot for your help.

Shachar,

Former Member
0 Kudos

Hi Maxim,

In your previous answer you mentioned '...You can store XML files under package folder..' what is the package folder?

Regards,

Former Member
0 Kudos

Hi

Its like you can place all you xml files under some java package for ex com.sagi.conf .

You can load the xml file's in ur java code by calling classsloader.getResourceAsStream("test.xml")

hope this helps, please mark points for helpful answers

regards

rajesh kr

Former Member
0 Kudos

Hi Maxim,

My situation is that I have several XML files containing complex configurations, (for example UI definitions), I would like to deploy them with the application (or in other way) and be able to edit them via Visual Admin, (similar to the way that log-configuration.xml is configured).

I have seen that I can add sub-configurations (files) using Visual Admin but I don't see a way to deploy them with the application.

Regards,

former_member182372
Active Contributor
0 Kudos

Hi Shachar,

Sorry, I don`t know how you can edit custom configuration XML using VA.

Best regards, Maksim Rashchynski

Former Member
0 Kudos

Hi

Thanks for the quick answer.

The visual admin part works well (I didn't check the java calls yet)

Could you tell how to store configuration XML files as well?

former_member182372
Active Contributor
0 Kudos

Hi Shachar,

Did not really understand you. As you can see in VA configuration of properties is done by plain text editing. What kind of XML configuration do you need? You can store XML files under package folder and load it in Java code. But you can not change it in VA.

Best regards, Maksim Rashchynski.