View Javadoc

1   package org.naftulin.configmgr.parsers;
2   
3   import java.net.URL;
4   import java.util.Collections;
5   import java.util.List;
6   
7   import org.naftulin.configmgr.ConfigurationManagementEntry;
8   import org.naftulin.configmgr.ConfigurationManagementEntryImpl;
9   import org.naftulin.configmgr.ConfigurationManagerException;
10  import org.naftulin.configmgr.ConfigurationType;
11  import org.naftulin.configmgr.content.NameValuePairImpl;
12  
13  /***
14   * Invokes {@link ExternalRecordAdapter external record adapter} with parameters specified in configuration record.
15   * Used to connect external configurations to be managed by configuration managment engine.
16   * 
17   * @author Henry Naftulin
18   * @since 1.0
19   */
20  public class ExternalConfigurationParserImpl implements ConfigEntryParser {
21  	private static final long serialVersionUID = 1L;
22  	private List<NameValuePairImpl> nameValuePairs;
23  	private String externalRecordAdapterClass;
24  	private ExternalRecordAdapter adapter;
25  	
26  	/***
27  	 * Sets the full class name for the {@link ExternalRecordAdapter ExternalRecordAdapter} class.
28  	 * @param externalRecordAdapterClass the full class name for the {@link ExternalRecordAdapter ExternalRecordAdapter} class.
29  	 */
30  	public synchronized void setExternalRecordAdapterClass(final String externalRecordAdapterClass) {
31  		this.externalRecordAdapterClass = externalRecordAdapterClass;
32  	}
33  
34  	/***
35  	 * Invokes {@link ExternalRecordAdapter external record adapter} with parameters specified in configuration record.
36  	 * If the adaptor is not created yet, create it and call load method, otherwise, calls reload.
37  	 * @param key the key for the external configuration.
38  	 * @param fileUrl ignored.
39  	 * @return the configuraion managment entry corresponding to the external configuration.
40  	 * @exception ConfigurationManagementEntry if external configuration cannot be instatiated or has problems 
41  	 * 		loading or reloading.
42  	 */
43  	public synchronized ConfigurationManagementEntry getConfigurationManagementEntry(final String key, final URL fileUrl) throws ConfigurationManagerException {
44  		ConfigurationManagementEntry entry = null;
45  		Object configuratonContent = null;
46  		//if adapter is not created yet for this configuration, create it and call load method,
47  		//otherwise call reload method.
48  		if (adapter == null) {
49  			if (key == null) { 
50  				throw new ConfigurationManagerException("key is null, please provide the key in you master configuraton file for all the external cofigurations");
51  			}
52  			if (externalRecordAdapterClass == null) {
53  				throw new ConfigurationManagerException("adater is null, please provide the custom created adaptor class (implemeting " + ExternalRecordAdapter.class.getName() +" interface) in you master configuraton file for external cofiguraton with key =" + key);
54  			}
55  			Class<ExternalRecordAdapter> adapterClass;
56  			try {				
57  				adapterClass = (Class<ExternalRecordAdapter>) Class.forName(externalRecordAdapterClass);
58  				
59  				adapter = adapterClass.newInstance();				
60  				configuratonContent = adapter.load(getNameValuePairs());
61  			} catch (ClassNotFoundException e) {
62  				throw new ConfigurationManagerException("adater implemented by class " + externalRecordAdapterClass +" was not found in the classpath, please provide the custom created adaptor class (implemeting " + ExternalRecordAdapter.class.getName() +" interface) in you master configuraton file for external cofiguraton with key =" + key, e);	
63  			} catch (InstantiationException e) {
64  				throw new ConfigurationManagerException("adater implemented by class " + externalRecordAdapterClass +" could not be instatiated. Please provide the custom created adaptor class (implemeting " + ExternalRecordAdapter.class.getName() +" interface) with default, no argument constructor, in you master configuraton file for external cofiguraton with key =" + key, e);
65  			} catch (IllegalAccessException e) {
66  				throw new ConfigurationManagerException("adater implemented by class " + externalRecordAdapterClass +" could not be instatiated for security reasons. Please provide the custom created adaptor class (implemeting " + ExternalRecordAdapter.class.getName() +" interface) with default, in you master configuraton file for external cofiguraton with key =" + key + ". You might also want to check the security policy that affects instatiation of this file.", e);
67  			}			
68  		} else {
69  			configuratonContent = adapter.reload(getNameValuePairs());			
70  		}
71  		entry = new ConfigurationManagementEntryImpl(key, "External Configuration inplemented by class " + externalRecordAdapterClass + " with the following info: " + adapter.getInfo(nameValuePairs), configuratonContent, this, ConfigurationType.EXTERNAL);
72  		return entry;
73  	}
74  
75  	/***
76  	 * Returns the named value list parsed from the master configuration.
77  	 * @return the named value list parsed from the master configuration.
78  	 */
79  	public List<NameValuePairImpl> getNameValuePairs() {
80  		return Collections.unmodifiableList(nameValuePairs);
81  	}
82  
83  	/***
84  	 * Sets the named value list
85  	 * @param nameValuePairs the named value pair list.
86  	 */
87  	public void setNameValuePairs(final List<NameValuePairImpl> nameValuePairs) {
88  		this.nameValuePairs = nameValuePairs;
89  	}
90  
91  }