1
|
/**
|
2
|
* Copyright (C) 2014 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.remoting.source;
|
11
|
|
12
|
import static eu.etaxonomy.cdm.common.XmlHelp.getBeansRoot;
|
13
|
import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlBean;
|
14
|
import static eu.etaxonomy.cdm.common.XmlHelp.insertXmlValueProperty;
|
15
|
import static eu.etaxonomy.cdm.common.XmlHelp.saveToXml;
|
16
|
|
17
|
import java.util.ArrayList;
|
18
|
import java.util.Enumeration;
|
19
|
import java.util.List;
|
20
|
import java.util.Properties;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.jdom.Attribute;
|
24
|
import org.jdom.Element;
|
25
|
|
26
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
27
|
import eu.etaxonomy.cdm.common.XmlHelp;
|
28
|
import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils;
|
29
|
import eu.etaxonomy.cdm.config.CdmPersistentXMLSource;
|
30
|
import eu.etaxonomy.cdm.config.CdmPersistentXMLSource.CdmSourceProperties;
|
31
|
import eu.etaxonomy.cdm.config.ICdmPersistentSource;
|
32
|
|
33
|
/**
|
34
|
* Class representing a CDM remote source which can be persisted to a source config file
|
35
|
*/
|
36
|
public class CdmPersistentRemoteSource extends CdmRemoteSourceBase implements ICdmPersistentSource{
|
37
|
@SuppressWarnings("unused")
|
38
|
private static final Logger logger = Logger.getLogger(CdmPersistentRemoteSource.class);
|
39
|
|
40
|
public static final String REMOTESOURCE_BEAN_POSTFIX = "RemoteSource";
|
41
|
private String beanName;
|
42
|
private Properties cdmSourceProperties;
|
43
|
private List<Attribute> cdmSourceAttributes;
|
44
|
|
45
|
/**
|
46
|
* Creates a new instance of CdmPersistentRemoteSource
|
47
|
*
|
48
|
* @param name
|
49
|
* @param server
|
50
|
* @param port
|
51
|
* @param contextPath
|
52
|
* @param nomenclaturalCode
|
53
|
* @return
|
54
|
* @throws CdmRemoteSourceException
|
55
|
*/
|
56
|
public static CdmPersistentRemoteSource NewInstance(String remoteSource) throws CdmRemoteSourceException {
|
57
|
if(exists(remoteSource)) {
|
58
|
return new CdmPersistentRemoteSource(remoteSource);
|
59
|
} else {
|
60
|
throw new CdmRemoteSourceException("Remote source '" + remoteSource + "' does not exist in sources setting file");
|
61
|
}
|
62
|
}
|
63
|
|
64
|
/**S
|
65
|
* Creates a new CdmPersistentRemoteSource
|
66
|
*
|
67
|
* @param name
|
68
|
* @param server
|
69
|
* @param port
|
70
|
* @param contextPath
|
71
|
* @param nomenclaturalCode
|
72
|
* @throws CdmRemoteSourceException
|
73
|
*/
|
74
|
private CdmPersistentRemoteSource(String remoteSource) throws CdmRemoteSourceException {
|
75
|
|
76
|
CdmPersistentXMLSource cdmPersistentXMLSource = CdmPersistentXMLSource.NewInstance(remoteSource, REMOTESOURCE_BEAN_POSTFIX);
|
77
|
if(cdmPersistentXMLSource.getElement() != null) {
|
78
|
beanName = cdmPersistentXMLSource.getBeanName();
|
79
|
// properties from the persistent xml file
|
80
|
cdmSourceProperties = cdmPersistentXMLSource.getCdmSourceProperties();
|
81
|
cdmSourceAttributes = cdmPersistentXMLSource.getCdmSourceAttributes();
|
82
|
checkProperties(cdmSourceProperties);
|
83
|
} else {
|
84
|
throw new CdmRemoteSourceException("Remote source '" + remoteSource + "' does not exist in settings file");
|
85
|
}
|
86
|
}
|
87
|
|
88
|
private static void checkProperties(Properties cdmSourceProperties) throws CdmRemoteSourceException {
|
89
|
if(!cdmSourceProperties.containsKey(CdmSourceProperties.SERVER.toString())) {
|
90
|
throw new CdmRemoteSourceException("Could not initialise remote source. Required property '" + CdmSourceProperties.SERVER.toString() + "' is missing");
|
91
|
}
|
92
|
String port = (String)cdmSourceProperties.get(CdmSourceProperties.PORT.toString());
|
93
|
if(port == null || port.isEmpty()) {
|
94
|
throw new CdmRemoteSourceException("Could not initialise remote source. Required property '" + CdmSourceProperties.SERVER.toString() + "' is missing");
|
95
|
}
|
96
|
if(!cdmSourceProperties.containsKey(CdmSourceProperties.CONTEXTPATH.toString())) {
|
97
|
throw new CdmRemoteSourceException("Could not initialise remote source. Required property '" + CdmSourceProperties.SERVER.toString() + "' is missing");
|
98
|
}
|
99
|
}
|
100
|
|
101
|
@Override
|
102
|
public String getBeanName() {
|
103
|
return beanName;
|
104
|
}
|
105
|
|
106
|
@Override
|
107
|
public String getServer() {
|
108
|
return cdmSourceProperties.getProperty(CdmSourceProperties.SERVER.toString());
|
109
|
}
|
110
|
|
111
|
@Override
|
112
|
public int getPort() {
|
113
|
String port = CdmUtils.Nz(cdmSourceProperties.getProperty(CdmSourceProperties.PORT.toString()));
|
114
|
if (port == null || "".equals(port)){
|
115
|
return -1;
|
116
|
}else{
|
117
|
//TODO exception if non integer
|
118
|
return Integer.valueOf(port);
|
119
|
}
|
120
|
}
|
121
|
|
122
|
@Override
|
123
|
public String getContextPath() {
|
124
|
return cdmSourceProperties.getProperty(CdmSourceProperties.CONTEXTPATH.toString());
|
125
|
}
|
126
|
|
127
|
public static CdmPersistentRemoteSource save(String strRemoteSourceName, ICdmRemoteSource remoteSource) throws CdmRemoteSourceException {
|
128
|
Properties cdmSourceProperties = new Properties();
|
129
|
cdmSourceProperties.put(CdmSourceProperties.SERVER.toString(), remoteSource.getServer());
|
130
|
cdmSourceProperties.put(CdmSourceProperties.PORT.toString(), String.valueOf(remoteSource.getPort()));
|
131
|
cdmSourceProperties.put(CdmSourceProperties.CONTEXTPATH.toString(), remoteSource.getContextPath());
|
132
|
checkProperties(cdmSourceProperties);
|
133
|
return save(strRemoteSourceName, cdmSourceProperties);
|
134
|
}
|
135
|
|
136
|
private static CdmPersistentRemoteSource save(String strRemoteSourceName,Properties cdmSourceProperties) throws CdmRemoteSourceException {
|
137
|
|
138
|
//root
|
139
|
Element root = getBeansRoot(CdmPersistentSourceUtils.getCdmSourceInputStream());
|
140
|
if (root == null){
|
141
|
return null;
|
142
|
}
|
143
|
//bean
|
144
|
Element bean = XmlHelp.getFirstAttributedChild(root, "bean", "id", CdmPersistentSourceUtils.getBeanName(strRemoteSourceName, REMOTESOURCE_BEAN_POSTFIX));
|
145
|
if (bean != null){
|
146
|
bean.detach(); //delete old version if necessary
|
147
|
}
|
148
|
bean = insertXmlBean(root, CdmPersistentSourceUtils.getBeanName(strRemoteSourceName, REMOTESOURCE_BEAN_POSTFIX), CdmPersistentRemoteSource.class.getName());
|
149
|
|
150
|
|
151
|
Properties props = cdmSourceProperties;
|
152
|
Enumeration<?> e = props.propertyNames();
|
153
|
|
154
|
while (e.hasMoreElements()) {
|
155
|
String key = (String) e.nextElement();
|
156
|
insertXmlValueProperty(bean, key, props.getProperty(key));
|
157
|
}
|
158
|
|
159
|
//save
|
160
|
saveToXml(root.getDocument(),
|
161
|
CdmPersistentSourceUtils.getResourceDirectory(),
|
162
|
CdmPersistentXMLSource.CDMSOURCE_FILE_NAME,
|
163
|
XmlHelp.prettyFormat );
|
164
|
|
165
|
return NewInstance(strRemoteSourceName) ;
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* @param strDataSourceName
|
170
|
* @param dataSource
|
171
|
* @param code
|
172
|
* @return
|
173
|
* the updated dataSource, null if not succesful
|
174
|
*/
|
175
|
public static CdmPersistentRemoteSource update(String strRemoteSourceName,
|
176
|
ICdmRemoteSource remoteSource) throws CdmRemoteSourceException {
|
177
|
CdmPersistentSourceUtils.delete(CdmPersistentSourceUtils.getBeanName(strRemoteSourceName,REMOTESOURCE_BEAN_POSTFIX));
|
178
|
return save(strRemoteSourceName, remoteSource);
|
179
|
}
|
180
|
|
181
|
/**
|
182
|
* Tests existing of the datsource in the according config file.
|
183
|
* @return true if a datasource with the given name exists in the according datasource config file.
|
184
|
*/
|
185
|
public static boolean exists(String strRemoteSourceName){
|
186
|
Element bean = CdmPersistentSourceUtils.getCdmSourceBeanXml(strRemoteSourceName, REMOTESOURCE_BEAN_POSTFIX);
|
187
|
return (bean != null);
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* Returns a list of all datasources stored in the datasource config file
|
192
|
* @return all existing data sources
|
193
|
* @throws CdmRemoteSourceException
|
194
|
*/
|
195
|
@SuppressWarnings("unchecked")
|
196
|
static public List<CdmPersistentRemoteSource> getAllRemoteSources() throws CdmRemoteSourceException{
|
197
|
List<CdmPersistentRemoteSource> remoteSources = new ArrayList<CdmPersistentRemoteSource>();
|
198
|
|
199
|
Element root = getBeansRoot(CdmPersistentSourceUtils.getCdmSourceInputStream());
|
200
|
if (root == null){
|
201
|
return null;
|
202
|
}else{
|
203
|
List<Element> lsChildren = root.getChildren("bean", root.getNamespace());
|
204
|
|
205
|
for (Element elBean : lsChildren){
|
206
|
String strId = elBean.getAttributeValue("id");
|
207
|
if (strId != null && strId.endsWith(REMOTESOURCE_BEAN_POSTFIX)){
|
208
|
strId = strId.replace(REMOTESOURCE_BEAN_POSTFIX, "");
|
209
|
remoteSources.add(new CdmPersistentRemoteSource(strId));
|
210
|
}
|
211
|
}
|
212
|
}
|
213
|
return remoteSources;
|
214
|
}
|
215
|
|
216
|
@Override
|
217
|
public String toString(){
|
218
|
if (getName() != null){
|
219
|
return getName();
|
220
|
}else{
|
221
|
return super.toString();
|
222
|
}
|
223
|
}
|
224
|
}
|