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 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | 15 | 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 | |
|
28 | |
|
29 | |
|
30 | |
public synchronized void setExternalRecordAdapterClass(final String externalRecordAdapterClass) { |
31 | 5 | this.externalRecordAdapterClass = externalRecordAdapterClass; |
32 | 5 | } |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public synchronized ConfigurationManagementEntry getConfigurationManagementEntry(final String key, final URL fileUrl) throws ConfigurationManagerException { |
44 | 15 | ConfigurationManagementEntry entry = null; |
45 | 15 | Object configuratonContent = null; |
46 | |
|
47 | |
|
48 | 15 | if (adapter == null) { |
49 | 5 | if (key == null) { |
50 | 0 | throw new ConfigurationManagerException("key is null, please provide the key in you master configuraton file for all the external cofigurations"); |
51 | |
} |
52 | 5 | if (externalRecordAdapterClass == null) { |
53 | 0 | 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 | 5 | adapterClass = (Class<ExternalRecordAdapter>) Class.forName(externalRecordAdapterClass); |
58 | |
|
59 | 5 | adapter = adapterClass.newInstance(); |
60 | 5 | configuratonContent = adapter.load(getNameValuePairs()); |
61 | 0 | } catch (ClassNotFoundException e) { |
62 | 0 | 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 | 0 | } catch (InstantiationException e) { |
64 | 0 | 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 | 0 | } catch (IllegalAccessException e) { |
66 | 0 | 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 | 10 | configuratonContent = adapter.reload(getNameValuePairs()); |
70 | |
} |
71 | 15 | entry = new ConfigurationManagementEntryImpl(key, "External Configuration inplemented by class " + externalRecordAdapterClass + " with the following info: " + adapter.getInfo(nameValuePairs), configuratonContent, this, ConfigurationType.EXTERNAL); |
72 | 15 | return entry; |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public List<NameValuePairImpl> getNameValuePairs() { |
80 | 15 | return Collections.unmodifiableList(nameValuePairs); |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public void setNameValuePairs(final List<NameValuePairImpl> nameValuePairs) { |
88 | 5 | this.nameValuePairs = nameValuePairs; |
89 | 5 | } |
90 | |
|
91 | |
} |