Revision 33760
Added by Michele Artini almost 10 years ago
modules/dnet-datasource-manager-service/trunk/src/main/java/eu/dnetlib/datasource/common/utils/DefaultDatasourceUpdater.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.common.utils; |
|
2 |
|
|
3 |
import java.io.StringReader; |
|
4 |
import java.util.Map; |
|
5 |
|
|
6 |
import javax.annotation.Resource; |
|
7 |
|
|
8 |
import org.apache.commons.lang.StringUtils; |
|
9 |
import org.apache.commons.logging.Log; |
|
10 |
import org.apache.commons.logging.LogFactory; |
|
11 |
import org.dom4j.Document; |
|
12 |
import org.dom4j.Element; |
|
13 |
import org.dom4j.Node; |
|
14 |
import org.dom4j.io.SAXReader; |
|
15 |
|
|
16 |
import com.google.common.collect.Maps; |
|
17 |
|
|
18 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
|
19 |
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService; |
|
20 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator; |
|
21 |
|
|
22 |
public class DefaultDatasourceUpdater implements DatasourceUpdater { |
|
23 |
|
|
24 |
public static final String OVERRIDING_COMPLIANCE_FIELD = "overriding_compliance"; |
|
25 |
|
|
26 |
private static final Log log = LogFactory.getLog(DefaultDatasourceUpdater.class); |
|
27 |
|
|
28 |
private static final String REPOSITORY_RESOURCE_TYPE = "RepositoryServiceResourceType"; |
|
29 |
|
|
30 |
@Resource |
|
31 |
private UniqueServiceLocator serviceLocator; |
|
32 |
|
|
33 |
@Override |
|
34 |
public boolean updateApiExtraFields(final String repoId, final String ifaceId, final Map<String, String> fields) throws DatasourceUpdaterException { |
|
35 |
try { |
|
36 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(repoId); |
|
37 |
|
|
38 |
final SAXReader reader = new SAXReader(); |
|
39 |
final Document doc = reader.read(new StringReader(profile)); |
|
40 |
|
|
41 |
final Element iface = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']"); |
|
42 |
if (iface != null) { |
|
43 |
|
|
44 |
while (iface.selectNodes("./INTERFACE_EXTRA_FIELD").size() > 0) { |
|
45 |
iface.selectSingleNode("./INTERFACE_EXTRA_FIELD").detach(); |
|
46 |
} |
|
47 |
for (Map.Entry<String, String> e : fields.entrySet()) { |
|
48 |
if (e.getValue() != null && !e.getValue().isEmpty()) { |
|
49 |
final Element field = iface.addElement("INTERFACE_EXTRA_FIELD"); |
|
50 |
field.addAttribute("name", e.getKey()); |
|
51 |
field.addText(e.getValue()); |
|
52 |
} |
|
53 |
} |
|
54 |
serviceLocator.getService(ISRegistryService.class).updateProfile(repoId, doc.asXML(), REPOSITORY_RESOURCE_TYPE); |
|
55 |
} else { |
|
56 |
log.error("Invalid interface: " + ifaceId); |
|
57 |
throw new DatasourceUpdaterException("Missing interface: " + ifaceId); |
|
58 |
} |
|
59 |
} catch (Exception e) { |
|
60 |
log.error("Error updating API of profile: " + repoId); |
|
61 |
throw new DatasourceUpdaterException("Error updating API of profile: " + repoId, e); |
|
62 |
} |
|
63 |
return true; |
|
64 |
|
|
65 |
} |
|
66 |
|
|
67 |
@Override |
|
68 |
public boolean updateApiAccessParams(final String repoId, final String ifaceId, final Map<String, String> params) throws DatasourceUpdaterException { |
|
69 |
try { |
|
70 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(repoId); |
|
71 |
|
|
72 |
final SAXReader reader = new SAXReader(); |
|
73 |
final Document doc = reader.read(new StringReader(profile)); |
|
74 |
|
|
75 |
final Element accessNode = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']/ACCESS_PROTOCOL"); |
|
76 |
if (accessNode != null) { |
|
77 |
while (accessNode.attributes().size() > 0) { |
|
78 |
accessNode.selectSingleNode("@*").detach(); |
|
79 |
} |
|
80 |
for (Map.Entry<String, String> e : params.entrySet()) { |
|
81 |
if (e.getValue() != null && !e.getValue().isEmpty()) { |
|
82 |
if (e.getKey().equalsIgnoreCase("baseUrl")) { |
|
83 |
doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']/BASE_URL").setText(e.getValue()); |
|
84 |
} else { |
|
85 |
accessNode.addAttribute(e.getKey(), e.getValue()); |
|
86 |
} |
|
87 |
} |
|
88 |
} |
|
89 |
serviceLocator.getService(ISRegistryService.class).updateProfile(repoId, doc.asXML(), REPOSITORY_RESOURCE_TYPE); |
|
90 |
} else { |
|
91 |
log.error("Invalid interface: " + ifaceId); |
|
92 |
throw new DatasourceUpdaterException("Missing interface: " + ifaceId); |
|
93 |
} |
|
94 |
} catch (Exception e) { |
|
95 |
log.error("Error updating API of profile: " + repoId); |
|
96 |
throw new DatasourceUpdaterException("Error updating API of profile: " + repoId, e); |
|
97 |
} |
|
98 |
return true; |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
|
102 |
public boolean overrideCompliance(final String repoId, final String ifaceId, final String compliance) throws DatasourceUpdaterException { |
|
103 |
try { |
|
104 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(repoId); |
|
105 |
|
|
106 |
final SAXReader reader = new SAXReader(); |
|
107 |
final Document doc = reader.read(new StringReader(profile)); |
|
108 |
|
|
109 |
final Element iface = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']"); |
|
110 |
if (iface != null) { |
|
111 |
final Map<String, String> fields = Maps.newHashMap(); |
|
112 |
|
|
113 |
if (!StringUtils.isEmpty(compliance)) { |
|
114 |
fields.put(OVERRIDING_COMPLIANCE_FIELD, compliance); |
|
115 |
} |
|
116 |
|
|
117 |
while (iface.selectNodes("./INTERFACE_EXTRA_FIELD").size() > 0) { |
|
118 |
final Node node = iface.selectSingleNode("./INTERFACE_EXTRA_FIELD"); |
|
119 |
final String name = node.valueOf("@name"); |
|
120 |
|
|
121 |
if (!name.equals(OVERRIDING_COMPLIANCE_FIELD)) { |
|
122 |
fields.put(node.valueOf("@name"), node.getText()); |
|
123 |
} |
|
124 |
node.detach(); |
|
125 |
} |
|
126 |
|
|
127 |
for (Map.Entry<String, String> e : fields.entrySet()) { |
|
128 |
if (e.getValue() != null && !e.getValue().isEmpty()) { |
|
129 |
final Element field = iface.addElement("INTERFACE_EXTRA_FIELD"); |
|
130 |
field.addAttribute("name", e.getKey()); |
|
131 |
field.addText(e.getValue()); |
|
132 |
} |
|
133 |
} |
|
134 |
serviceLocator.getService(ISRegistryService.class).updateProfile(repoId, doc.asXML(), REPOSITORY_RESOURCE_TYPE); |
|
135 |
} else { |
|
136 |
log.error("Invalid interface: " + ifaceId); |
|
137 |
throw new DatasourceUpdaterException("Missing interface: " + ifaceId); |
|
138 |
} |
|
139 |
} catch (Exception e) { |
|
140 |
log.error("Error updating API of profile: " + repoId); |
|
141 |
throw new DatasourceUpdaterException("Error updating API of profile: " + repoId, e); |
|
142 |
} |
|
143 |
|
|
144 |
return true; |
|
145 |
} |
|
146 |
} |
modules/dnet-datasource-manager-service/trunk/src/main/java/eu/dnetlib/datasource/common/utils/DatasourceUpdaterException.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.common.utils; |
|
2 |
|
|
3 |
public class DatasourceUpdaterException extends Exception { |
|
4 |
|
|
5 |
/** |
|
6 |
* |
|
7 |
*/ |
|
8 |
private static final long serialVersionUID = 184962100806362741L; |
|
9 |
|
|
10 |
public DatasourceUpdaterException() { |
|
11 |
super(); |
|
12 |
} |
|
13 |
|
|
14 |
public DatasourceUpdaterException(final String message, final Throwable cause) { |
|
15 |
super(message, cause); |
|
16 |
} |
|
17 |
|
|
18 |
public DatasourceUpdaterException(final String message) { |
|
19 |
super(message); |
|
20 |
} |
|
21 |
|
|
22 |
public DatasourceUpdaterException(final Throwable cause) { |
|
23 |
super(cause); |
|
24 |
} |
|
25 |
|
|
26 |
} |
modules/dnet-datasource-manager-service/trunk/src/main/java/eu/dnetlib/datasource/common/utils/DatasourceUpdater.java | ||
---|---|---|
1 |
package eu.dnetlib.datasource.common.utils; |
|
2 |
|
|
3 |
import java.util.Map; |
|
4 |
|
|
5 |
public interface DatasourceUpdater { |
|
6 |
|
|
7 |
public boolean updateApiExtraFields(String repoId, String ifaceId, Map<String, String> fields) throws DatasourceUpdaterException; |
|
8 |
|
|
9 |
public boolean updateApiAccessParams(String repoId, String ifaceId, Map<String, String> params) throws DatasourceUpdaterException; |
|
10 |
|
|
11 |
public boolean overrideCompliance(String repoId, String ifaceId, String compliance) throws DatasourceUpdaterException; |
|
12 |
|
|
13 |
} |
modules/dnet-datasource-manager-service/trunk/src/main/java/eu/dnetlib/enabling/datasources/DatasourceManagerServiceImpl.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.datasources; |
|
2 |
|
|
3 |
import java.io.StringReader; |
|
4 |
import java.util.Date; |
|
5 |
import java.util.List; |
|
6 |
import java.util.Map; |
|
7 |
|
|
8 |
import javax.annotation.Resource; |
|
9 |
|
|
10 |
import org.apache.commons.lang.StringUtils; |
|
11 |
import org.apache.commons.logging.Log; |
|
12 |
import org.apache.commons.logging.LogFactory; |
|
13 |
import org.dom4j.Document; |
|
14 |
import org.dom4j.Element; |
|
15 |
import org.dom4j.Node; |
|
16 |
import org.dom4j.io.SAXReader; |
|
17 |
|
|
18 |
import com.google.common.collect.Maps; |
|
19 |
|
|
20 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceConstants; |
|
21 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceDesc; |
|
22 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerService; |
|
23 |
import eu.dnetlib.enabling.datasources.rmi.DatasourceManagerServiceException; |
|
24 |
import eu.dnetlib.enabling.datasources.rmi.IfaceDesc; |
|
25 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
|
26 |
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService; |
|
27 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator; |
|
28 |
import eu.dnetlib.enabling.tools.AbstractBaseService; |
|
29 |
|
|
30 |
public class DatasourceManagerServiceImpl extends AbstractBaseService implements DatasourceManagerService { |
|
31 |
|
|
32 |
private static final Log log = LogFactory.getLog(DatasourceManagerServiceImpl.class); |
|
33 |
|
|
34 |
private static final String REPOSITORY_RESOURCE_TYPE = "RepositoryServiceResourceType"; |
|
35 |
|
|
36 |
@Resource |
|
37 |
private UniqueServiceLocator serviceLocator; |
|
38 |
|
|
39 |
@Override |
|
40 |
public boolean addDatasource(final DatasourceDesc ds) throws DatasourceManagerServiceException { |
|
41 |
// TODO Auto-generated method stub |
|
42 |
return false; |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public boolean deleteDatasource(final String dsId) throws DatasourceManagerServiceException { |
|
47 |
// TODO Auto-generated method stub |
|
48 |
return false; |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public DatasourceDesc getDatasource(final String dsId) throws DatasourceManagerServiceException { |
|
53 |
// TODO Auto-generated method stub |
|
54 |
return null; |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public List<DatasourceDesc> listAllDatasources() throws DatasourceManagerServiceException { |
|
59 |
// TODO Auto-generated method stub |
|
60 |
return null; |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public List<DatasourceDesc> listDatasourcesUsingFilter(final String compliance, |
|
65 |
final String contentDescription, |
|
66 |
final String iisProcessingWorkflow, |
|
67 |
final String collectedFrom) |
|
68 |
throws DatasourceManagerServiceException { |
|
69 |
// TODO Auto-generated method stub |
|
70 |
return null; |
|
71 |
} |
|
72 |
|
|
73 |
@Override |
|
74 |
public boolean updateLevelOfCompliance(final String dsId, final String ifaceId, final String level) throws DatasourceManagerServiceException { |
|
75 |
// TODO Auto-generated method stub |
|
76 |
return false; |
|
77 |
} |
|
78 |
|
|
79 |
@Override |
|
80 |
public boolean updateBaseUrl(final String dsId, final String ifaceId, final String baseUrl) throws DatasourceManagerServiceException { |
|
81 |
// TODO Auto-generated method stub |
|
82 |
return false; |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public boolean updateActivationStatus(final String dsId, final String ifaceId, final boolean active) throws DatasourceManagerServiceException { |
|
87 |
// TODO Auto-generated method stub |
|
88 |
return false; |
|
89 |
} |
|
90 |
|
|
91 |
@Override |
|
92 |
public boolean updateContentDescription(final String dsId, final String ifaceId, final String desc) throws DatasourceManagerServiceException { |
|
93 |
// TODO Auto-generated method stub |
|
94 |
return false; |
|
95 |
} |
|
96 |
|
|
97 |
@Override |
|
98 |
public boolean setIisProcessingWorkflow(final String dsId, final String ifaceId, final String wf) throws DatasourceManagerServiceException { |
|
99 |
// TODO Auto-generated method stub |
|
100 |
return false; |
|
101 |
} |
|
102 |
|
|
103 |
@Override |
|
104 |
public boolean updateExtraField(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal) |
|
105 |
throws DatasourceManagerServiceException { |
|
106 |
// TODO Auto-generated method stub |
|
107 |
return false; |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public boolean updateAccessParam(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal) |
|
112 |
throws DatasourceManagerServiceException { |
|
113 |
// TODO Auto-generated method stub |
|
114 |
return false; |
|
115 |
} |
|
116 |
|
|
117 |
@Override |
|
118 |
public boolean deleteAccessParamOrExtraField(final String dsId, final String ifaceId, final String field) throws DatasourceManagerServiceException { |
|
119 |
// TODO Auto-generated method stub |
|
120 |
return false; |
|
121 |
} |
|
122 |
|
|
123 |
@Override |
|
124 |
public boolean addInterface(final String dsId, final IfaceDesc iface) throws DatasourceManagerServiceException { |
|
125 |
// TODO Auto-generated method stub |
|
126 |
return false; |
|
127 |
} |
|
128 |
|
|
129 |
@Override |
|
130 |
public boolean deleteInterface(final String dsId, final String ifaceId) throws DatasourceManagerServiceException { |
|
131 |
// TODO Auto-generated method stub |
|
132 |
return false; |
|
133 |
} |
|
134 |
|
|
135 |
@Override |
|
136 |
public boolean updateSQL(final String dsId, final String sql, final boolean delete) throws DatasourceManagerServiceException { |
|
137 |
// TODO Auto-generated method stub |
|
138 |
return false; |
|
139 |
} |
|
140 |
|
|
141 |
@Override |
|
142 |
public Date findNextScheduledExecution(final String dsId, final String ifaceId) throws DatasourceManagerServiceException { |
|
143 |
// TODO Auto-generated method stub |
|
144 |
return null; |
|
145 |
} |
|
146 |
|
|
147 |
@Override |
|
148 |
public boolean bulkUpdateApiExtraFields(final String repoId, final String ifaceId, final Map<String, String> fields) |
|
149 |
throws DatasourceManagerServiceException { |
|
150 |
try { |
|
151 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(repoId); |
|
152 |
|
|
153 |
final SAXReader reader = new SAXReader(); |
|
154 |
final Document doc = reader.read(new StringReader(profile)); |
|
155 |
|
|
156 |
final Element iface = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']"); |
|
157 |
if (iface != null) { |
|
158 |
|
|
159 |
while (iface.selectNodes("./INTERFACE_EXTRA_FIELD").size() > 0) { |
|
160 |
iface.selectSingleNode("./INTERFACE_EXTRA_FIELD").detach(); |
|
161 |
} |
|
162 |
for (Map.Entry<String, String> e : fields.entrySet()) { |
|
163 |
if (e.getValue() != null && !e.getValue().isEmpty()) { |
|
164 |
final Element field = iface.addElement("INTERFACE_EXTRA_FIELD"); |
|
165 |
field.addAttribute("name", e.getKey()); |
|
166 |
field.addText(e.getValue()); |
|
167 |
} |
|
168 |
} |
|
169 |
serviceLocator.getService(ISRegistryService.class).updateProfile(repoId, doc.asXML(), REPOSITORY_RESOURCE_TYPE); |
|
170 |
} else { |
|
171 |
log.error("Invalid interface: " + ifaceId); |
|
172 |
throw new DatasourceManagerServiceException("Missing interface: " + ifaceId); |
|
173 |
} |
|
174 |
} catch (Exception e) { |
|
175 |
log.error("Error updating API of profile: " + repoId); |
|
176 |
throw new DatasourceManagerServiceException("Error updating API of profile: " + repoId, e); |
|
177 |
} |
|
178 |
return true; |
|
179 |
|
|
180 |
} |
|
181 |
|
|
182 |
@Override |
|
183 |
public boolean bulkUpdateApiAccessParams(final String repoId, final String ifaceId, final Map<String, String> params) |
|
184 |
throws DatasourceManagerServiceException { |
|
185 |
try { |
|
186 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(repoId); |
|
187 |
|
|
188 |
final SAXReader reader = new SAXReader(); |
|
189 |
final Document doc = reader.read(new StringReader(profile)); |
|
190 |
|
|
191 |
final Element accessNode = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']/ACCESS_PROTOCOL"); |
|
192 |
if (accessNode != null) { |
|
193 |
while (accessNode.attributes().size() > 0) { |
|
194 |
accessNode.selectSingleNode("@*").detach(); |
|
195 |
} |
|
196 |
for (Map.Entry<String, String> e : params.entrySet()) { |
|
197 |
if (e.getValue() != null && !e.getValue().isEmpty()) { |
|
198 |
if (e.getKey().equalsIgnoreCase("baseUrl")) { |
|
199 |
doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']/BASE_URL").setText(e.getValue()); |
|
200 |
} else { |
|
201 |
accessNode.addAttribute(e.getKey(), e.getValue()); |
|
202 |
} |
|
203 |
} |
|
204 |
} |
|
205 |
serviceLocator.getService(ISRegistryService.class).updateProfile(repoId, doc.asXML(), REPOSITORY_RESOURCE_TYPE); |
|
206 |
} else { |
|
207 |
log.error("Invalid interface: " + ifaceId); |
|
208 |
throw new DatasourceManagerServiceException("Missing interface: " + ifaceId); |
|
209 |
} |
|
210 |
} catch (Exception e) { |
|
211 |
log.error("Error updating API of profile: " + repoId); |
|
212 |
throw new DatasourceManagerServiceException("Error updating API of profile: " + repoId, e); |
|
213 |
} |
|
214 |
return true; |
|
215 |
} |
|
216 |
|
|
217 |
@Override |
|
218 |
public boolean overrideCompliance(final String repoId, final String ifaceId, final String compliance) throws DatasourceManagerServiceException { |
|
219 |
try { |
|
220 |
final String profile = serviceLocator.getService(ISLookUpService.class).getResourceProfile(repoId); |
|
221 |
|
|
222 |
final SAXReader reader = new SAXReader(); |
|
223 |
final Document doc = reader.read(new StringReader(profile)); |
|
224 |
|
|
225 |
final Element iface = (Element) doc.selectSingleNode("//INTERFACE[@id='" + ifaceId + "']"); |
|
226 |
if (iface != null) { |
|
227 |
final Map<String, String> fields = Maps.newHashMap(); |
|
228 |
|
|
229 |
if (!StringUtils.isEmpty(compliance)) { |
|
230 |
fields.put(DatasourceConstants.OVERRIDING_COMPLIANCE_FIELD, compliance); |
|
231 |
} |
|
232 |
|
|
233 |
while (iface.selectNodes("./INTERFACE_EXTRA_FIELD").size() > 0) { |
|
234 |
final Node node = iface.selectSingleNode("./INTERFACE_EXTRA_FIELD"); |
|
235 |
final String name = node.valueOf("@name"); |
|
236 |
|
|
237 |
if (!name.equals(DatasourceConstants.OVERRIDING_COMPLIANCE_FIELD)) { |
|
238 |
fields.put(node.valueOf("@name"), node.getText()); |
|
239 |
} |
|
240 |
node.detach(); |
|
241 |
} |
|
242 |
|
|
243 |
for (Map.Entry<String, String> e : fields.entrySet()) { |
|
244 |
if (e.getValue() != null && !e.getValue().isEmpty()) { |
|
245 |
final Element field = iface.addElement("INTERFACE_EXTRA_FIELD"); |
|
246 |
field.addAttribute("name", e.getKey()); |
|
247 |
field.addText(e.getValue()); |
|
248 |
} |
|
249 |
} |
|
250 |
serviceLocator.getService(ISRegistryService.class).updateProfile(repoId, doc.asXML(), REPOSITORY_RESOURCE_TYPE); |
|
251 |
} else { |
|
252 |
log.error("Invalid interface: " + ifaceId); |
|
253 |
throw new DatasourceManagerServiceException("Missing interface: " + ifaceId); |
|
254 |
} |
|
255 |
} catch (Exception e) { |
|
256 |
log.error("Error updating API of profile: " + repoId); |
|
257 |
throw new DatasourceManagerServiceException("Error updating API of profile: " + repoId, e); |
|
258 |
} |
|
259 |
|
|
260 |
return true; |
|
261 |
} |
|
262 |
|
|
263 |
} |
Also available in: Unified diff
partial implementation