Revision 47744
Added by Claudio Atzori almost 7 years ago
modules/cnr-service-common/tags/cnr-service-common-2.1.6/deploy.info | ||
---|---|---|
1 |
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet45/modules/cnr-service-common/trunk/", "deploy_repository": "dnet45-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-snapshots", "name": "cnr-service-common"} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/locators/ServiceRunningInstance.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.locators; |
|
2 |
|
|
3 |
import java.util.HashMap; |
|
4 |
import java.util.Map; |
|
5 |
|
|
6 |
import javax.xml.ws.WebServiceFeature; |
|
7 |
import javax.xml.ws.wsaddressing.W3CEndpointReference; |
|
8 |
|
|
9 |
import eu.dnetlib.common.rmi.BaseService; |
|
10 |
import eu.dnetlib.soap.cxf.StandaloneCxfEndpointReferenceBuilder; |
|
11 |
|
|
12 |
/** |
|
13 |
* This bean packages the minimum information for describing a service running instance. |
|
14 |
*/ |
|
15 |
public class ServiceRunningInstance { |
|
16 |
|
|
17 |
private String serviceId; |
|
18 |
private String url; |
|
19 |
private BaseService localService; |
|
20 |
private int usedDiskSpace = 0; |
|
21 |
private int handledDatastructures = 0; |
|
22 |
private Map<String, String> serviceProperties = new HashMap<String, String>(); |
|
23 |
|
|
24 |
public ServiceRunningInstance() {} |
|
25 |
|
|
26 |
public ServiceRunningInstance(final String serviceId, final String url) { |
|
27 |
this.serviceId = serviceId; |
|
28 |
this.url = url; |
|
29 |
} |
|
30 |
|
|
31 |
public ServiceRunningInstance(final String serviceId, final String url, final BaseService localService, final int usedDiskSpace, |
|
32 |
final int handledDatastructures, final Map<String, String> serviceProperties) { |
|
33 |
this.serviceId = serviceId; |
|
34 |
this.url = url; |
|
35 |
this.localService = localService; |
|
36 |
this.usedDiskSpace = usedDiskSpace; |
|
37 |
this.handledDatastructures = handledDatastructures; |
|
38 |
this.serviceProperties = serviceProperties; |
|
39 |
} |
|
40 |
|
|
41 |
public boolean isLocal() { |
|
42 |
return localService != null; |
|
43 |
} |
|
44 |
|
|
45 |
synchronized public <T extends BaseService> T obtainClient(final Class<T> clazz, final StandaloneCxfEndpointReferenceBuilder eprBuilder) { |
|
46 |
if (isLocal() && clazz.isInstance(localService)) { |
|
47 |
return clazz.cast(localService); |
|
48 |
} else { |
|
49 |
final W3CEndpointReference epr = eprBuilder.getEndpointReference(url, null, null, url + "?wsdl", null, null); |
|
50 |
return epr.getPort(clazz, new WebServiceFeature[] {}); |
|
51 |
} |
|
52 |
} |
|
53 |
|
|
54 |
public String getServiceId() { |
|
55 |
return serviceId; |
|
56 |
} |
|
57 |
|
|
58 |
public void setServiceId(final String serviceId) { |
|
59 |
this.serviceId = serviceId; |
|
60 |
} |
|
61 |
|
|
62 |
public String getUrl() { |
|
63 |
return url; |
|
64 |
} |
|
65 |
|
|
66 |
public void setUrl(final String url) { |
|
67 |
this.url = url; |
|
68 |
} |
|
69 |
|
|
70 |
public BaseService getLocalService() { |
|
71 |
return localService; |
|
72 |
} |
|
73 |
|
|
74 |
public void setLocalService(final BaseService localService) { |
|
75 |
this.localService = localService; |
|
76 |
} |
|
77 |
|
|
78 |
public Map<String, String> getServiceProperties() { |
|
79 |
return serviceProperties; |
|
80 |
} |
|
81 |
|
|
82 |
public void setServiceProperties(final Map<String, String> serviceProperties) { |
|
83 |
this.serviceProperties = serviceProperties; |
|
84 |
} |
|
85 |
|
|
86 |
public int getUsedDiskSpace() { |
|
87 |
return usedDiskSpace; |
|
88 |
} |
|
89 |
|
|
90 |
public void setUsedDiskSpace(final int usedDiskSpace) { |
|
91 |
this.usedDiskSpace = usedDiskSpace; |
|
92 |
} |
|
93 |
|
|
94 |
public int getHandledDatastructures() { |
|
95 |
return handledDatastructures; |
|
96 |
} |
|
97 |
|
|
98 |
public void setHandledDatastructures(final int handledDatastructures) { |
|
99 |
this.handledDatastructures = handledDatastructures; |
|
100 |
} |
|
101 |
|
|
102 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/locators/comparators/HandledDatastructuresComparator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.locators.comparators; |
|
2 |
|
|
3 |
import java.util.Comparator; |
|
4 |
|
|
5 |
import eu.dnetlib.enabling.locators.ServiceRunningInstance; |
|
6 |
|
|
7 |
public class HandledDatastructuresComparator implements Comparator<ServiceRunningInstance> { |
|
8 |
|
|
9 |
@Override |
|
10 |
public int compare(final ServiceRunningInstance s1, final ServiceRunningInstance s2) { |
|
11 |
return Integer.compare(s1.getHandledDatastructures(), s2.getHandledDatastructures()); |
|
12 |
} |
|
13 |
|
|
14 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/locators/comparators/PreferLocalRunningInstanceComparator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.locators.comparators; |
|
2 |
|
|
3 |
import java.util.Comparator; |
|
4 |
|
|
5 |
import eu.dnetlib.enabling.locators.ServiceRunningInstance; |
|
6 |
|
|
7 |
public class PreferLocalRunningInstanceComparator implements Comparator<ServiceRunningInstance> { |
|
8 |
|
|
9 |
@Override |
|
10 |
public int compare(final ServiceRunningInstance s1, final ServiceRunningInstance s2) { |
|
11 |
if (s1.isLocal()) { |
|
12 |
return -1; |
|
13 |
} else if (s2.isLocal()) { |
|
14 |
return 1; |
|
15 |
} else { |
|
16 |
return 0; |
|
17 |
} |
|
18 |
} |
|
19 |
|
|
20 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/locators/comparators/DiskSpaceComparator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.locators.comparators; |
|
2 |
|
|
3 |
import java.util.Comparator; |
|
4 |
|
|
5 |
import eu.dnetlib.enabling.locators.ServiceRunningInstance; |
|
6 |
|
|
7 |
public class DiskSpaceComparator implements Comparator<ServiceRunningInstance> { |
|
8 |
|
|
9 |
@Override |
|
10 |
public int compare(final ServiceRunningInstance s0, ServiceRunningInstance s1) { |
|
11 |
return Integer.compare(s0.getUsedDiskSpace(), s1.getUsedDiskSpace()); |
|
12 |
} |
|
13 |
|
|
14 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/locators/UniqueServiceLocator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.locators; |
|
2 |
|
|
3 |
import java.util.Comparator; |
|
4 |
import java.util.Set; |
|
5 |
|
|
6 |
import eu.dnetlib.common.rmi.BaseService; |
|
7 |
|
|
8 |
public interface UniqueServiceLocator { |
|
9 |
<T extends BaseService> T getService(Class<T> clazz); |
|
10 |
<T extends BaseService> T getService(Class<T> clazz, Comparator<ServiceRunningInstance> comparator); |
|
11 |
<T extends BaseService> T getService(Class<T> clazz, String profileId); |
|
12 |
<T extends BaseService> T getService(Class<T> clazz, boolean local); |
|
13 |
|
|
14 |
<T extends BaseService> String getServiceId(Class<T> clazz); |
|
15 |
<T extends BaseService> String getServiceId(Class<T> clazz, Comparator<ServiceRunningInstance> comparator); |
|
16 |
<T extends BaseService> String getServiceId(Class<T> clazz, String profileId); |
|
17 |
|
|
18 |
<T extends BaseService> Set<T> getAllServices(Class<T> clazz); |
|
19 |
<T extends BaseService> Set<String> getAllServiceIds(Class<T> clazz); |
|
20 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/locators/DefaultUniqueServiceLocator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.locators; |
|
2 |
|
|
3 |
import java.io.StringReader; |
|
4 |
import java.io.StringWriter; |
|
5 |
import java.util.*; |
|
6 |
|
|
7 |
import com.google.common.collect.Lists; |
|
8 |
import com.google.common.collect.Maps; |
|
9 |
import com.google.common.collect.Sets; |
|
10 |
import eu.dnetlib.common.rmi.BaseService; |
|
11 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; |
|
12 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
|
13 |
import eu.dnetlib.enabling.tools.registration.ServiceNameResolver; |
|
14 |
import eu.dnetlib.enabling.tools.registration.ValidatingServiceRegistrationManagerImpl; |
|
15 |
import eu.dnetlib.miscutils.collections.EnsureCollection; |
|
16 |
import eu.dnetlib.soap.cxf.StandaloneCxfEndpointReferenceBuilder; |
|
17 |
import org.apache.commons.lang.StringUtils; |
|
18 |
import org.apache.commons.lang.math.NumberUtils; |
|
19 |
import org.apache.commons.logging.Log; |
|
20 |
import org.apache.commons.logging.LogFactory; |
|
21 |
import org.dom4j.Document; |
|
22 |
import org.dom4j.DocumentException; |
|
23 |
import org.dom4j.Element; |
|
24 |
import org.dom4j.io.SAXReader; |
|
25 |
import org.springframework.beans.BeansException; |
|
26 |
import org.springframework.beans.factory.annotation.Autowired; |
|
27 |
import org.springframework.beans.factory.annotation.Required; |
|
28 |
import org.springframework.context.ApplicationContext; |
|
29 |
import org.springframework.context.ApplicationContextAware; |
|
30 |
|
|
31 |
public class DefaultUniqueServiceLocator implements UniqueServiceLocator, ApplicationContextAware { |
|
32 |
|
|
33 |
private ApplicationContext appContext; |
|
34 |
|
|
35 |
private Comparator<ServiceRunningInstance> defaultComparator; // = new |
|
36 |
// PreferLocalRunningInstanceComparator(); |
|
37 |
|
|
38 |
/** |
|
39 |
* An instance of isLookupService (local or stub) |
|
40 |
*/ |
|
41 |
@Autowired |
|
42 |
private ISLookUpService isLookupService; |
|
43 |
|
|
44 |
@Autowired |
|
45 |
private ServiceNameResolver serviceNameResolver; |
|
46 |
|
|
47 |
/** |
|
48 |
* XML Parser |
|
49 |
*/ |
|
50 |
private SAXReader reader = new SAXReader(); |
|
51 |
|
|
52 |
/** |
|
53 |
* build epr. |
|
54 |
*/ |
|
55 |
@Autowired |
|
56 |
private StandaloneCxfEndpointReferenceBuilder eprBuilder; |
|
57 |
|
|
58 |
/** |
|
59 |
* logger. |
|
60 |
*/ |
|
61 |
private static final Log log = LogFactory.getLog(DefaultUniqueServiceLocator.class); |
|
62 |
|
|
63 |
@Override |
|
64 |
public <T extends BaseService> T getService(final Class<T> clazz) { |
|
65 |
return getService(clazz, true); |
|
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public <T extends BaseService> T getService(final Class<T> clazz, final Comparator<ServiceRunningInstance> comparator) { |
|
70 |
final String serviceName = serviceNameResolver.getName(clazz); |
|
71 |
return findRunningInstances(serviceName, comparator).get(0).obtainClient(clazz, eprBuilder); |
|
72 |
} |
|
73 |
|
|
74 |
@Override |
|
75 |
public <T extends BaseService> T getService(final Class<T> clazz, final String profileId) { |
|
76 |
final String profile = obtainServiceProfile(profileId); |
|
77 |
|
|
78 |
try { |
|
79 |
return obtainRunningInstance(profile, obtainLocalServices()).obtainClient(clazz, eprBuilder); |
|
80 |
} catch (Exception e) { |
|
81 |
log.error("cannot instantiate service from id: " + profileId, e); |
|
82 |
throw new IllegalStateException("cannot instantiate service from id: " + profileId, e); |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
@Override |
|
87 |
public <T extends BaseService> T getService(final Class<T> clazz, final boolean local) { |
|
88 |
if (clazz.isInstance(isLookupService)) return clazz.cast(isLookupService); |
|
89 |
|
|
90 |
if (local) { |
|
91 |
try { |
|
92 |
final Map<String, T> beans = appContext.getBeansOfType(clazz); |
|
93 |
if ((beans != null) && !beans.isEmpty()) return beans.values().iterator().next(); |
|
94 |
} catch (Throwable e) { |
|
95 |
log.warn("No beans found in context, class " + clazz); |
|
96 |
} |
|
97 |
} |
|
98 |
|
|
99 |
return getService(clazz, defaultComparator); |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public <T extends BaseService> String getServiceId(final Class<T> clazz) { |
|
104 |
return getServiceId(clazz, defaultComparator); |
|
105 |
} |
|
106 |
|
|
107 |
@Override |
|
108 |
public <T extends BaseService> String getServiceId(final Class<T> clazz, final Comparator<ServiceRunningInstance> comparator) { |
|
109 |
return findRunningInstances(serviceNameResolver.getName(clazz), comparator).get(0).getServiceId(); |
|
110 |
} |
|
111 |
|
|
112 |
@Override |
|
113 |
public <T extends BaseService> String getServiceId(final Class<T> clazz, final String profileId) { |
|
114 |
final String profile = obtainServiceProfile(profileId); |
|
115 |
final ServiceRunningInstance instance = obtainRunningInstance(profile, obtainLocalServices()); |
|
116 |
return instance.getServiceId(); |
|
117 |
} |
|
118 |
|
|
119 |
@Override |
|
120 |
public <T extends BaseService> Set<T> getAllServices(final Class<T> clazz) { |
|
121 |
final Set<T> res = Sets.newHashSet(); |
|
122 |
for (ServiceRunningInstance instance : findRunningInstances(serviceNameResolver.getName(clazz), null)) { |
|
123 |
res.add(instance.obtainClient(clazz, eprBuilder)); |
|
124 |
} |
|
125 |
return res; |
|
126 |
} |
|
127 |
|
|
128 |
@Override |
|
129 |
public <T extends BaseService> Set<String> getAllServiceIds(final Class<T> clazz) { |
|
130 |
final Set<String> res = Sets.newHashSet(); |
|
131 |
for (ServiceRunningInstance instance : findRunningInstances(serviceNameResolver.getName(clazz), null)) { |
|
132 |
res.add(instance.getServiceId()); |
|
133 |
} |
|
134 |
return res; |
|
135 |
} |
|
136 |
|
|
137 |
private synchronized <T extends BaseService> ServiceRunningInstance obtainRunningInstance(final String profile, final Map<String, BaseService> locals) { |
|
138 |
try { |
|
139 |
final Document doc = reader.read(new StringReader(profile)); |
|
140 |
final String url = doc.valueOf("//PROTOCOL[@name = 'SOAP']/@address"); |
|
141 |
final String id = doc.valueOf("//RESOURCE_IDENTIFIER/@value"); |
|
142 |
final Map<String, String> props = Maps.newHashMap(); |
|
143 |
final BaseService local = locals.containsKey(id) ? locals.get(id) : null; |
|
144 |
final int usedDiskpace = NumberUtils.toInt(doc.valueOf("//USED_DISKSPACE"), 0); |
|
145 |
final int handledDatastructures = NumberUtils.toInt(doc.valueOf("//HANDLED_DATASTRUCTURE"), 0);; |
|
146 |
|
|
147 |
for (Object o : doc.selectNodes("//SERVICE_PROPERTIES/PROPERTY")) { |
|
148 |
final Element p = (Element) o; |
|
149 |
props.put(p.valueOf("@key"), p.valueOf("@value")); |
|
150 |
} |
|
151 |
|
|
152 |
return new ServiceRunningInstance(id, url, local, usedDiskpace, handledDatastructures, props); |
|
153 |
} catch (DocumentException e) { |
|
154 |
log.error("Error parsing profile: " + profile, e); |
|
155 |
throw new RuntimeException("Error parsing profile: " + profile, e); |
|
156 |
} |
|
157 |
} |
|
158 |
|
|
159 |
private List<ServiceRunningInstance> findRunningInstances(final String serviceName, final Comparator<ServiceRunningInstance> comparator) { |
|
160 |
final List<ServiceRunningInstance> list = findRegisteredServices(serviceName); |
|
161 |
|
|
162 |
if (list.isEmpty()) { |
|
163 |
log.error("Service not found, name: " + serviceName); |
|
164 |
throw new RuntimeException("Service not found, name: " + serviceName); |
|
165 |
} |
|
166 |
|
|
167 |
if (comparator != null) { |
|
168 |
Collections.sort(list, comparator); |
|
169 |
} |
|
170 |
|
|
171 |
return list; |
|
172 |
} |
|
173 |
|
|
174 |
private List<ServiceRunningInstance> findRegisteredServices(final String serviceName) { |
|
175 |
log.debug("searching for service: " + serviceName); |
|
176 |
|
|
177 |
final String xquery = "for $x in collection('/db/DRIVER/ServiceResources/" + serviceName + "ResourceType') return $x"; |
|
178 |
log.debug(xquery); |
|
179 |
|
|
180 |
try { |
|
181 |
final List<String> services = isLookupService.quickSearchProfile(xquery); |
|
182 |
final List<ServiceRunningInstance> instances = Lists.newArrayList(); |
|
183 |
final Map<String, BaseService> locals = obtainLocalServices(); |
|
184 |
|
|
185 |
for (final String source : EnsureCollection.list(services)) { |
|
186 |
final ServiceRunningInstance instance = obtainRunningInstance(source, locals); |
|
187 |
instances.add(instance); |
|
188 |
} |
|
189 |
return instances; |
|
190 |
} catch (final Exception e) { |
|
191 |
throw new IllegalStateException("cannot locate service " + serviceName, e); |
|
192 |
} |
|
193 |
} |
|
194 |
|
|
195 |
private Map<String, BaseService> obtainLocalServices() { |
|
196 |
final Map<String, BaseService> locals = Maps.newHashMap(); |
|
197 |
|
|
198 |
for (ValidatingServiceRegistrationManagerImpl r : appContext.getBeansOfType(ValidatingServiceRegistrationManagerImpl.class).values()) { |
|
199 |
if (r.getService() instanceof BaseService) { |
|
200 |
if (!StringUtils.isBlank(r.getProfileId())) { |
|
201 |
final BaseService baseService = (BaseService) r.getService(); |
|
202 |
if (baseService != null) { |
|
203 |
locals.put(r.getProfileId(), baseService); |
|
204 |
log.debug(" -> Service: " + r.getService().getClass().getName() + " has id " + r.getServiceProfile().getResourceId()); |
|
205 |
} |
|
206 |
} |
|
207 |
} |
|
208 |
} |
|
209 |
return locals; |
|
210 |
} |
|
211 |
|
|
212 |
private String obtainServiceProfile(final String profileId) { |
|
213 |
final StringWriter sw = new StringWriter(); |
|
214 |
sw.append("let $uri:=/RESOURCE_PROFILE/HEADER[./RESOURCE_IDENTIFIER/@value='"); |
|
215 |
sw.append(profileId); |
|
216 |
sw.append("']/RESOURCE_URI/@value/string()"); |
|
217 |
sw.append("\n\n"); |
|
218 |
sw.append("for $x in collection('/db/DRIVER/ServiceResources')"); |
|
219 |
sw.append("\n"); |
|
220 |
sw.append("where $x/RESOURCE_PROFILE/HEADER/RESOURCE_URI/@value = $uri"); |
|
221 |
sw.append("\n"); |
|
222 |
sw.append("return $x"); |
|
223 |
|
|
224 |
final String xq = sw.toString(); |
|
225 |
|
|
226 |
try { |
|
227 |
return isLookupService.getResourceProfileByQuery(xq); |
|
228 |
} catch (ISLookUpException e) { |
|
229 |
log.error("cannot locate service using query: " + xq, e); |
|
230 |
throw new IllegalStateException("cannot locate service using query: " + xq, e); |
|
231 |
} |
|
232 |
} |
|
233 |
|
|
234 |
@Override |
|
235 |
public void setApplicationContext(final ApplicationContext appContext) throws BeansException { |
|
236 |
this.appContext = appContext; |
|
237 |
} |
|
238 |
|
|
239 |
public Comparator<ServiceRunningInstance> getDefaultComparator() { |
|
240 |
return defaultComparator; |
|
241 |
} |
|
242 |
|
|
243 |
@Required |
|
244 |
public void setDefaultComparator(final Comparator<ServiceRunningInstance> defaultComparator) { |
|
245 |
this.defaultComparator = defaultComparator; |
|
246 |
} |
|
247 |
|
|
248 |
public ISLookUpService getIsLookupService() { |
|
249 |
return isLookupService; |
|
250 |
} |
|
251 |
|
|
252 |
public void setIsLookupService(final ISLookUpService isLookupService) { |
|
253 |
this.isLookupService = isLookupService; |
|
254 |
} |
|
255 |
|
|
256 |
public ServiceNameResolver getServiceNameResolver() { |
|
257 |
return serviceNameResolver; |
|
258 |
} |
|
259 |
|
|
260 |
public void setServiceNameResolver(final ServiceNameResolver serviceNameResolver) { |
|
261 |
this.serviceNameResolver = serviceNameResolver; |
|
262 |
} |
|
263 |
|
|
264 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/common/StoppableDetails.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.common; |
|
2 |
|
|
3 |
public class StoppableDetails { |
|
4 |
|
|
5 |
public enum StopStatus { |
|
6 |
RUNNING, STOPPED, STOPPING |
|
7 |
} |
|
8 |
|
|
9 |
private String name; |
|
10 |
private String message; |
|
11 |
private StopStatus status; |
|
12 |
|
|
13 |
public StoppableDetails() {} |
|
14 |
|
|
15 |
public StoppableDetails(String name, String message, StopStatus status) { |
|
16 |
this.name = name; |
|
17 |
this.message = message; |
|
18 |
this.status = status; |
|
19 |
} |
|
20 |
|
|
21 |
public String getName() { |
|
22 |
return name; |
|
23 |
} |
|
24 |
|
|
25 |
public void setName(String name) { |
|
26 |
this.name = name; |
|
27 |
} |
|
28 |
|
|
29 |
public String getMessage() { |
|
30 |
return message; |
|
31 |
} |
|
32 |
|
|
33 |
public void setMessage(String message) { |
|
34 |
this.message = message; |
|
35 |
} |
|
36 |
|
|
37 |
public StopStatus getStatus() { |
|
38 |
return status; |
|
39 |
} |
|
40 |
|
|
41 |
public void setStatus(StopStatus status) { |
|
42 |
this.status = status; |
|
43 |
} |
|
44 |
|
|
45 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/common/Stoppable.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.common; |
|
2 |
|
|
3 |
public interface Stoppable { |
|
4 |
void stop(); |
|
5 |
void resume(); |
|
6 |
StoppableDetails getStopDetails(); |
|
7 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/is/registry/schema/OpaqueResourceValidator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.registry.schema; |
|
2 |
|
|
3 |
import eu.dnetlib.enabling.tools.OpaqueResource; |
|
4 |
|
|
5 |
/** |
|
6 |
* validates the conformity of a resource to a give resource type. |
|
7 |
* |
|
8 |
* @author marko |
|
9 |
* |
|
10 |
*/ |
|
11 |
public interface OpaqueResourceValidator { |
|
12 |
/** |
|
13 |
* check if the given resource is valid according to it's schema. |
|
14 |
* |
|
15 |
* @param resource opaque resource |
|
16 |
* @throws ValidationException thrown if the validation fails, along with a description of the cause. |
|
17 |
*/ |
|
18 |
void validate(OpaqueResource resource) throws ValidationException; |
|
19 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/is/registry/schema/ValidationException.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.registry.schema; |
|
2 |
|
|
3 |
|
|
4 |
/** |
|
5 |
* encapsulates a schema validation exception. |
|
6 |
* |
|
7 |
* @author marko |
|
8 |
* |
|
9 |
*/ |
|
10 |
public class ValidationException extends Exception { |
|
11 |
|
|
12 |
/** |
|
13 |
* version. |
|
14 |
*/ |
|
15 |
private static final long serialVersionUID = -6886927707534508655L; |
|
16 |
|
|
17 |
/** |
|
18 |
* construct a validation exception based upon an encapsulated cause. |
|
19 |
* @param cause cause |
|
20 |
*/ |
|
21 |
public ValidationException(final Throwable cause) { |
|
22 |
super(cause); |
|
23 |
} |
|
24 |
|
|
25 |
/** |
|
26 |
* construct a validation exception with a message. |
|
27 |
* |
|
28 |
* @param message message |
|
29 |
*/ |
|
30 |
public ValidationException(final String message) { |
|
31 |
super(message); |
|
32 |
} |
|
33 |
|
|
34 |
|
|
35 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/is/store/AbstractContentInitializer.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.store; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.StringWriter; |
|
5 |
import java.net.URL; |
|
6 |
|
|
7 |
import javax.xml.parsers.ParserConfigurationException; |
|
8 |
import javax.xml.xpath.XPathExpressionException; |
|
9 |
|
|
10 |
import org.apache.commons.io.FilenameUtils; |
|
11 |
import org.apache.commons.io.IOUtils; |
|
12 |
import org.apache.commons.logging.Log; |
|
13 |
import org.apache.commons.logging.LogFactory; |
|
14 |
import org.springframework.beans.factory.annotation.Required; |
|
15 |
import org.xml.sax.SAXException; |
|
16 |
|
|
17 |
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException; |
|
18 |
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService; |
|
19 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator; |
|
20 |
import eu.dnetlib.enabling.tools.StreamOpaqueResource; |
|
21 |
|
|
22 |
/** |
|
23 |
* Abstract resource loading code. |
|
24 |
* |
|
25 |
* @author marko, michele |
|
26 |
* |
|
27 |
*/ |
|
28 |
public class AbstractContentInitializer { |
|
29 |
|
|
30 |
/** |
|
31 |
* logger. |
|
32 |
*/ |
|
33 |
private static final Log log = LogFactory.getLog(AbstractContentInitializer.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
34 |
/** |
|
35 |
* service locator. |
|
36 |
*/ |
|
37 |
private UniqueServiceLocator serviceLocator; |
|
38 |
/** |
|
39 |
* helper class used to bypass the registry and import resources as-is from a backup dump. |
|
40 |
*/ |
|
41 |
private BulkResourceImporter bulkImporter; |
|
42 |
|
|
43 |
private int timeToSleep; |
|
44 |
|
|
45 |
/** |
|
46 |
* register a schema from a local resource. |
|
47 |
* |
|
48 |
* @param url |
|
49 |
* url |
|
50 |
* @throws IOException |
|
51 |
* happens |
|
52 |
* @throws ISRegistryException |
|
53 |
* could happen |
|
54 |
*/ |
|
55 |
protected void registerSchema(final URL url) throws IOException, ISRegistryException { |
|
56 |
final String resourceType = FilenameUtils.getBaseName(url.getPath()); |
|
57 |
log.debug("registering schema: " + resourceType); |
|
58 |
|
|
59 |
final StringWriter writer = new StringWriter(); |
|
60 |
IOUtils.copy(url.openStream(), writer); |
|
61 |
ISRegistryService service = null; |
|
62 |
while (service == null) { |
|
63 |
try { |
|
64 |
service = serviceLocator.getService(ISRegistryService.class, true); |
|
65 |
service.addResourceType(resourceType, writer.getBuffer().toString()); |
|
66 |
log.info("The is registry service is ready "); |
|
67 |
} catch (Exception e) { |
|
68 |
log.fatal("The is registry service is not ready ", e); |
|
69 |
try { |
|
70 |
Thread.sleep(timeToSleep); |
|
71 |
} catch (InterruptedException e1) { |
|
72 |
log.error(e1); |
|
73 |
} |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* register a profile from a local resource. |
|
81 |
* |
|
82 |
* @param url |
|
83 |
* url |
|
84 |
* @throws IOException |
|
85 |
* could happen |
|
86 |
* @throws ISRegistryException |
|
87 |
* could happen |
|
88 |
* @throws ParserConfigurationException |
|
89 |
* could happen |
|
90 |
* @throws SAXException |
|
91 |
* could happen |
|
92 |
* @throws XPathExpressionException |
|
93 |
* could happen |
|
94 |
*/ |
|
95 |
protected void registerProfile(final URL url) throws IOException, ISRegistryException, XPathExpressionException, SAXException, ParserConfigurationException { |
|
96 |
log.debug("registering profile: " + url); |
|
97 |
|
|
98 |
bulkImporter.importResource(new StreamOpaqueResource(url.openStream())); |
|
99 |
} |
|
100 |
|
|
101 |
@Required |
|
102 |
public void setBulkImporter(final BulkResourceImporter bulkImporter) { |
|
103 |
this.bulkImporter = bulkImporter; |
|
104 |
} |
|
105 |
|
|
106 |
public BulkResourceImporter getBulkImporter() { |
|
107 |
return bulkImporter; |
|
108 |
} |
|
109 |
|
|
110 |
/** |
|
111 |
* @return the timeToSleep |
|
112 |
*/ |
|
113 |
public int getTimeToSleep() { |
|
114 |
return timeToSleep; |
|
115 |
} |
|
116 |
|
|
117 |
/** |
|
118 |
* @param timeToSleep |
|
119 |
* the timeToSleep to set |
|
120 |
*/ |
|
121 |
@Required |
|
122 |
public void setTimeToSleep(final int timeToSleep) { |
|
123 |
this.timeToSleep = timeToSleep; |
|
124 |
} |
|
125 |
|
|
126 |
public UniqueServiceLocator getServiceLocator() { |
|
127 |
return serviceLocator; |
|
128 |
} |
|
129 |
|
|
130 |
@Required |
|
131 |
public void setServiceLocator(final UniqueServiceLocator serviceLocator) { |
|
132 |
this.serviceLocator = serviceLocator; |
|
133 |
} |
|
134 |
|
|
135 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/is/store/BulkResourceImporter.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.store; |
|
2 |
|
|
3 |
import org.springframework.beans.factory.annotation.Required; |
|
4 |
|
|
5 |
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException; |
|
6 |
import eu.dnetlib.enabling.is.registry.schema.OpaqueResourceValidator; |
|
7 |
import eu.dnetlib.enabling.is.registry.schema.ValidationException; |
|
8 |
import eu.dnetlib.enabling.is.store.rmi.ISStoreException; |
|
9 |
import eu.dnetlib.enabling.is.store.rmi.ISStoreService; |
|
10 |
import eu.dnetlib.enabling.locators.UniqueServiceLocator; |
|
11 |
import eu.dnetlib.enabling.tools.OpaqueResource; |
|
12 |
import eu.dnetlib.enabling.tools.XQueryUtils; |
|
13 |
|
|
14 |
/** |
|
15 |
* This class implements a bulk resource import, i.e. importing stuff straight into the store, bypassing checks and policies imposed by the |
|
16 |
* registry service. |
|
17 |
* |
|
18 |
* TODO: move from registry based to store based. |
|
19 |
* |
|
20 |
* @author marko |
|
21 |
* |
|
22 |
*/ |
|
23 |
public class BulkResourceImporter { |
|
24 |
|
|
25 |
/** |
|
26 |
* xquery utils, needed to map resources with the xmldb collection names. |
|
27 |
*/ |
|
28 |
private XQueryUtils xqueryUtils; |
|
29 |
|
|
30 |
/** |
|
31 |
* service locator. |
|
32 |
*/ |
|
33 |
private UniqueServiceLocator serviceLocator; |
|
34 |
|
|
35 |
/** |
|
36 |
* resource validator. |
|
37 |
*/ |
|
38 |
private OpaqueResourceValidator resourceValidator; |
|
39 |
|
|
40 |
/** |
|
41 |
* set to false to skip validation. |
|
42 |
*/ |
|
43 |
private boolean validating = true; |
|
44 |
|
|
45 |
/** |
|
46 |
* bulk loading enabled. |
|
47 |
*/ |
|
48 |
private boolean enabled = true; |
|
49 |
|
|
50 |
/** |
|
51 |
* register a resource bypassing the checks. |
|
52 |
* |
|
53 |
* @param resource |
|
54 |
* a resource |
|
55 |
* @throws ISRegistryException |
|
56 |
* could happen |
|
57 |
*/ |
|
58 |
public void importResource(final OpaqueResource resource) throws ISRegistryException { |
|
59 |
try { |
|
60 |
if (validating) { |
|
61 |
resourceValidator.validate(resource); |
|
62 |
} |
|
63 |
serviceLocator.getService(ISStoreService.class, true).insertXML(xqueryUtils.getFileName(resource), xqueryUtils.getCollectionAbsPath(resource), |
|
64 |
resource.asString()); |
|
65 |
} catch (final ISStoreException e) { |
|
66 |
throw new ISRegistryException(e); |
|
67 |
} catch (final ValidationException e) { |
|
68 |
throw new ISRegistryException(e); |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
public XQueryUtils getXqueryUtils() { |
|
73 |
return xqueryUtils; |
|
74 |
} |
|
75 |
|
|
76 |
@Required |
|
77 |
public void setXqueryUtils(final XQueryUtils xqueryUtils) { |
|
78 |
this.xqueryUtils = xqueryUtils; |
|
79 |
} |
|
80 |
|
|
81 |
@Required |
|
82 |
public void setResourceValidator(final OpaqueResourceValidator resourceValidator) { |
|
83 |
this.resourceValidator = resourceValidator; |
|
84 |
} |
|
85 |
|
|
86 |
public OpaqueResourceValidator getResourceValidator() { |
|
87 |
return resourceValidator; |
|
88 |
} |
|
89 |
|
|
90 |
public void setValidating(final boolean validating) { |
|
91 |
this.validating = validating; |
|
92 |
} |
|
93 |
|
|
94 |
public boolean isValidating() { |
|
95 |
return validating; |
|
96 |
} |
|
97 |
|
|
98 |
public boolean isEnabled() { |
|
99 |
return enabled; |
|
100 |
} |
|
101 |
|
|
102 |
public void setEnabled(final boolean enabled) { |
|
103 |
this.enabled = enabled; |
|
104 |
} |
|
105 |
|
|
106 |
public UniqueServiceLocator getServiceLocator() { |
|
107 |
return serviceLocator; |
|
108 |
} |
|
109 |
|
|
110 |
@Required |
|
111 |
public void setServiceLocator(final UniqueServiceLocator serviceLocator) { |
|
112 |
this.serviceLocator = serviceLocator; |
|
113 |
} |
|
114 |
|
|
115 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/tools/DefaultServiceLocatorLocationScorer.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import java.net.MalformedURLException; |
|
4 |
import java.net.URL; |
|
5 |
|
|
6 |
import javax.annotation.Resource; |
|
7 |
|
|
8 |
import eu.dnetlib.soap.cxf.StandaloneCxfEndpointReferenceBuilder; |
|
9 |
|
|
10 |
/** |
|
11 |
* Assign better scores for near services. Can be configured. |
|
12 |
* |
|
13 |
* @author marko |
|
14 |
* |
|
15 |
*/ |
|
16 |
@Deprecated |
|
17 |
public class DefaultServiceLocatorLocationScorer implements DynamicServiceLocatorLocationScorer { |
|
18 |
|
|
19 |
/** |
|
20 |
* default score assigned when the other service has the same host. |
|
21 |
*/ |
|
22 |
private static final int LOCAL_HOST_SCORE = 5; |
|
23 |
|
|
24 |
/** |
|
25 |
* default score assigned when the other service has the same host and port (same container). |
|
26 |
*/ |
|
27 |
private static final int LOCAL_PORT_SCORE = 10; |
|
28 |
|
|
29 |
/** |
|
30 |
* default score assigned when the other service has the same host and port (same container and context). |
|
31 |
*/ |
|
32 |
private static final int LOCAL_SRV_SCORE = 15; |
|
33 |
|
|
34 |
|
|
35 |
/** |
|
36 |
* score assigned when the other service has the same host. |
|
37 |
*/ |
|
38 |
private int localHostScore = LOCAL_HOST_SCORE; |
|
39 |
|
|
40 |
/** |
|
41 |
* score assigned when the other service has the same host and port (same container). |
|
42 |
*/ |
|
43 |
private int localPortScore = LOCAL_PORT_SCORE; |
|
44 |
|
|
45 |
/** |
|
46 |
* score assigned when the other service has the same host and port (same container and context). |
|
47 |
*/ |
|
48 |
private int localSrvScore = LOCAL_SRV_SCORE; |
|
49 |
|
|
50 |
/** |
|
51 |
* build epr. |
|
52 |
*/ |
|
53 |
@Resource |
|
54 |
private StandaloneCxfEndpointReferenceBuilder eprBuilder; |
|
55 |
|
|
56 |
/** |
|
57 |
* {@inheritDoc} |
|
58 |
* @throws MalformedURLException |
|
59 |
* @see eu.dnetlib.enabling.tools.DynamicServiceLocatorLocationScorer#score(java.net.URL) |
|
60 |
*/ |
|
61 |
@Override |
|
62 |
public int score(final URL url) throws MalformedURLException { |
|
63 |
final URL localBase = new URL(eprBuilder.getBaseAddress()); |
|
64 |
if (url.toString().startsWith(localBase.toString())) |
|
65 |
return localSrvScore; |
|
66 |
|
|
67 |
if (localBase.getHost().equals(url.getHost())) { |
|
68 |
if (localBase.getPort() == url.getPort()) |
|
69 |
return localPortScore; |
|
70 |
return localHostScore; |
|
71 |
} |
|
72 |
return 0; |
|
73 |
} |
|
74 |
|
|
75 |
public StandaloneCxfEndpointReferenceBuilder getEprBuilder() { |
|
76 |
return eprBuilder; |
|
77 |
} |
|
78 |
|
|
79 |
public void setEprBuilder(final StandaloneCxfEndpointReferenceBuilder eprBuilder) { |
|
80 |
this.eprBuilder = eprBuilder; |
|
81 |
} |
|
82 |
|
|
83 |
public int getLocalHostScore() { |
|
84 |
return localHostScore; |
|
85 |
} |
|
86 |
|
|
87 |
public void setLocalHostScore(final int localHostScore) { |
|
88 |
this.localHostScore = localHostScore; |
|
89 |
} |
|
90 |
|
|
91 |
public int getLocalPortScore() { |
|
92 |
return localPortScore; |
|
93 |
} |
|
94 |
|
|
95 |
public void setLocalPortScore(final int localPortScore) { |
|
96 |
this.localPortScore = localPortScore; |
|
97 |
} |
|
98 |
|
|
99 |
public int getLocalSrvScore() { |
|
100 |
return localSrvScore; |
|
101 |
} |
|
102 |
|
|
103 |
public void setLocalSrvScore(final int localSrvScore) { |
|
104 |
this.localSrvScore = localSrvScore; |
|
105 |
} |
|
106 |
|
|
107 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/tools/DynamicServiceLocatorLocationScorer.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import java.net.MalformedURLException; |
|
4 |
import java.net.URL; |
|
5 |
|
|
6 |
/** |
|
7 |
* Implementors of this interface provide custom methods to assign scores to services based solely on their location. |
|
8 |
* @author marko |
|
9 |
* |
|
10 |
*/ |
|
11 |
@Deprecated |
|
12 |
public interface DynamicServiceLocatorLocationScorer { |
|
13 |
/** |
|
14 |
* Compute the score assigned to a given service location (url). |
|
15 |
* |
|
16 |
* @param url service url |
|
17 |
* @return score (the higher the better) |
|
18 |
* @throws MalformedURLException could happen |
|
19 |
*/ |
|
20 |
int score(URL url) throws MalformedURLException; |
|
21 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/tools/NullHNMLocator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
/** |
|
4 |
* simplest HNM locator: doesn't find any. |
|
5 |
* |
|
6 |
* @author marko |
|
7 |
* |
|
8 |
*/ |
|
9 |
public class NullHNMLocator implements HNMLocator { |
|
10 |
|
|
11 |
/** |
|
12 |
* {@inheritDoc} |
|
13 |
* |
|
14 |
* @see eu.dnetlib.enabling.tools.HNMLocator#getHNMForUrl(java.lang.String) |
|
15 |
*/ |
|
16 |
@Override |
|
17 |
public String getHNMForUrl(final String url) { |
|
18 |
return ""; |
|
19 |
} |
|
20 |
|
|
21 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/tools/DOMOpaqueResource.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import java.io.StringWriter; |
|
4 |
import java.util.Date; |
|
5 |
|
|
6 |
import javax.xml.transform.Transformer; |
|
7 |
import javax.xml.transform.TransformerConfigurationException; |
|
8 |
import javax.xml.transform.TransformerException; |
|
9 |
import javax.xml.transform.TransformerFactory; |
|
10 |
import javax.xml.transform.TransformerFactoryConfigurationError; |
|
11 |
import javax.xml.transform.dom.DOMSource; |
|
12 |
import javax.xml.transform.stream.StreamResult; |
|
13 |
import javax.xml.xpath.XPath; |
|
14 |
import javax.xml.xpath.XPathConstants; |
|
15 |
import javax.xml.xpath.XPathExpressionException; |
|
16 |
import javax.xml.xpath.XPathFactory; |
|
17 |
|
|
18 |
import org.apache.commons.logging.Log; |
|
19 |
import org.apache.commons.logging.LogFactory; |
|
20 |
import org.w3c.dom.Document; |
|
21 |
import org.w3c.dom.Element; |
|
22 |
|
|
23 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
24 |
|
|
25 |
/** |
|
26 |
* OpaqueResource holding a plain old DOM document. |
|
27 |
* |
|
28 |
* @author marko |
|
29 |
* |
|
30 |
*/ |
|
31 |
public class DOMOpaqueResource implements OpaqueResource { |
|
32 |
/** |
|
33 |
* xpath expression error message. |
|
34 |
*/ |
|
35 |
private static final String XPATH_ERROR = "cannot compile xpath expression"; |
|
36 |
|
|
37 |
/** |
|
38 |
* value attribute. |
|
39 |
*/ |
|
40 |
private static final String VALUE_ATTR = "value"; |
|
41 |
|
|
42 |
/** |
|
43 |
* logger. |
|
44 |
*/ |
|
45 |
private static final Log log = LogFactory.getLog(DOMOpaqueResource.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
46 |
|
|
47 |
/** |
|
48 |
* resource identifier. |
|
49 |
*/ |
|
50 |
private String resourceId; |
|
51 |
|
|
52 |
/** |
|
53 |
* resource type. |
|
54 |
*/ |
|
55 |
private String resourceType; |
|
56 |
|
|
57 |
/** |
|
58 |
* resource kind. |
|
59 |
*/ |
|
60 |
private String resourceKind; |
|
61 |
|
|
62 |
/** |
|
63 |
* resource uri. |
|
64 |
*/ |
|
65 |
private String resourceUri; |
|
66 |
|
|
67 |
/** |
|
68 |
* modification time stamp. |
|
69 |
*/ |
|
70 |
private Date modificationDate; |
|
71 |
|
|
72 |
/** |
|
73 |
* original document DOM. |
|
74 |
*/ |
|
75 |
private Document dom; |
|
76 |
|
|
77 |
/** |
|
78 |
* xslt transformer instance. |
|
79 |
*/ |
|
80 |
private Transformer transformer; |
|
81 |
|
|
82 |
/** |
|
83 |
* construct a DOMOpaqueInstance from a W3C DOM document. |
|
84 |
* |
|
85 |
* @param dom |
|
86 |
* DOM document |
|
87 |
* @throws XPathExpressionException |
|
88 |
* happens |
|
89 |
*/ |
|
90 |
public DOMOpaqueResource(final Document dom) throws XPathExpressionException { |
|
91 |
this.dom = dom; |
|
92 |
|
|
93 |
try { |
|
94 |
transformer = TransformerFactory.newInstance().newTransformer(); |
|
95 |
} catch (TransformerConfigurationException e) { |
|
96 |
throw new IllegalStateException("transformer configuration", e); |
|
97 |
} catch (TransformerFactoryConfigurationError e) { |
|
98 |
throw new IllegalStateException("transformer configuration", e); |
|
99 |
} |
|
100 |
|
|
101 |
final XPath xpath = XPathFactory.newInstance().newXPath(); |
|
102 |
|
|
103 |
this.resourceId = xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_IDENTIFIER/@value", dom); |
|
104 |
this.resourceType = xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_TYPE/@value", dom); |
|
105 |
this.resourceKind = xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_KIND/@value", dom); |
|
106 |
this.resourceUri = xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_URI/@value", dom); |
|
107 |
|
|
108 |
String modificationDateSource = xpath.evaluate("/RESOURCE_PROFILE/HEADER/DATE_OF_CREATION/@value", dom); |
|
109 |
|
|
110 |
try { |
|
111 |
this.modificationDate = new DateUtils().parse(modificationDateSource); |
|
112 |
} catch (IllegalStateException e) { |
|
113 |
log.debug("invalid date '" + modificationDateSource + "'", e); |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
/** |
|
118 |
* {@inheritDoc} |
|
119 |
* |
|
120 |
* @see eu.dnetlib.enabling.tools.OpaqueResource#asDom() |
|
121 |
*/ |
|
122 |
@Override |
|
123 |
public Document asDom() { |
|
124 |
return getDom(); |
|
125 |
} |
|
126 |
|
|
127 |
/** |
|
128 |
* {@inheritDoc} |
|
129 |
* |
|
130 |
* @see eu.dnetlib.enabling.tools.OpaqueResource#asString() |
|
131 |
*/ |
|
132 |
@Override |
|
133 |
public String asString() { |
|
134 |
final StringWriter writer = new StringWriter(); |
|
135 |
|
|
136 |
try { |
|
137 |
transformer.transform(new DOMSource(getDom()), new StreamResult(writer)); |
|
138 |
} catch (TransformerException e) { |
|
139 |
log.fatal("cannot serialize document", e); |
|
140 |
return null; |
|
141 |
} |
|
142 |
return writer.toString(); |
|
143 |
} |
|
144 |
|
|
145 |
public Document getDom() { |
|
146 |
return dom; |
|
147 |
} |
|
148 |
|
|
149 |
public void setDom(final Document dom) { |
|
150 |
this.dom = dom; |
|
151 |
} |
|
152 |
|
|
153 |
@Override |
|
154 |
public String getResourceId() { |
|
155 |
return resourceId; |
|
156 |
} |
|
157 |
|
|
158 |
@Override |
|
159 |
public String getResourceType() { |
|
160 |
return resourceType; |
|
161 |
} |
|
162 |
|
|
163 |
public void setResourceType(final String resourceType) { |
|
164 |
this.resourceType = resourceType; |
|
165 |
} |
|
166 |
|
|
167 |
@Override |
|
168 |
public String getResourceKind() { |
|
169 |
return resourceKind; |
|
170 |
} |
|
171 |
|
|
172 |
/** |
|
173 |
* {@inheritDoc} |
|
174 |
* |
|
175 |
* @see eu.dnetlib.enabling.tools.OpaqueResource#setResourceKind(java.lang.String) |
|
176 |
*/ |
|
177 |
@Override |
|
178 |
public void setResourceKind(final String resourceKind) { |
|
179 |
try { |
|
180 |
final XPath xpath = XPathFactory.newInstance().newXPath(); |
|
181 |
final Element kindEl = (Element) xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_KIND", asDom(), XPathConstants.NODE); |
|
182 |
kindEl.setAttribute(VALUE_ATTR, resourceKind); |
|
183 |
this.resourceKind = resourceKind; |
|
184 |
} catch (XPathExpressionException e) { |
|
185 |
throw new IllegalStateException(XPATH_ERROR, e); |
|
186 |
} |
|
187 |
|
|
188 |
} |
|
189 |
|
|
190 |
/** |
|
191 |
* {@inheritDoc} |
|
192 |
* |
|
193 |
* @see eu.dnetlib.enabling.tools.OpaqueResource#setResourceId(java.lang.String) |
|
194 |
*/ |
|
195 |
@Override |
|
196 |
public void setResourceId(final String identifier) { |
|
197 |
try { |
|
198 |
final XPath xpath = XPathFactory.newInstance().newXPath(); |
|
199 |
final Element idEl = (Element) xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_IDENTIFIER", asDom(), XPathConstants.NODE); |
|
200 |
idEl.setAttribute(VALUE_ATTR, identifier); |
|
201 |
resourceId = identifier; |
|
202 |
} catch (XPathExpressionException e) { |
|
203 |
throw new IllegalStateException(XPATH_ERROR, e); |
|
204 |
} |
|
205 |
} |
|
206 |
|
|
207 |
@Override |
|
208 |
public Date getModificationDate() { |
|
209 |
return modificationDate; |
|
210 |
} |
|
211 |
|
|
212 |
/** |
|
213 |
* {@inheritDoc} |
|
214 |
* |
|
215 |
* @see eu.dnetlib.enabling.tools.OpaqueResource#setModificationDate(java.util.Date) |
|
216 |
*/ |
|
217 |
@Override |
|
218 |
public void setModificationDate(final Date modificationDate) { |
|
219 |
try { |
|
220 |
final XPath xpath = XPathFactory.newInstance().newXPath(); |
|
221 |
final Element idEl = (Element) xpath.evaluate("/RESOURCE_PROFILE/HEADER/DATE_OF_CREATION", asDom(), XPathConstants.NODE); |
|
222 |
if (idEl == null) { |
|
223 |
log.warn("resource with type " + getResourceType() + " has no date of creation element"); |
|
224 |
return; |
|
225 |
} |
|
226 |
idEl.setAttribute(VALUE_ATTR, new DateUtils(modificationDate).getDateAsISO8601String()); |
|
227 |
this.modificationDate = modificationDate; |
|
228 |
} catch (XPathExpressionException e) { |
|
229 |
throw new IllegalStateException(XPATH_ERROR, e); |
|
230 |
} |
|
231 |
|
|
232 |
} |
|
233 |
|
|
234 |
@Override |
|
235 |
public String getResourceUri() { |
|
236 |
return resourceUri; |
|
237 |
} |
|
238 |
|
|
239 |
/** |
|
240 |
* {@inheritDoc} |
|
241 |
* |
|
242 |
* @see eu.dnetlib.enabling.tools.OpaqueResource#setResourceUri(java.lang.String) |
|
243 |
*/ |
|
244 |
@Override |
|
245 |
public void setResourceUri(final String resourceUri) { |
|
246 |
try { |
|
247 |
final XPath xpath = XPathFactory.newInstance().newXPath(); |
|
248 |
final Element uriEl = (Element) xpath.evaluate("/RESOURCE_PROFILE/HEADER/RESOURCE_URI", asDom(), XPathConstants.NODE); |
|
249 |
uriEl.setAttribute(VALUE_ATTR, resourceUri); |
|
250 |
this.resourceUri = resourceUri; |
|
251 |
} catch (XPathExpressionException e) { |
|
252 |
throw new IllegalStateException(XPATH_ERROR, e); |
|
253 |
} |
|
254 |
} |
|
255 |
|
|
256 |
public Transformer getTransformer() { |
|
257 |
return transformer; |
|
258 |
} |
|
259 |
|
|
260 |
public void setTransformer(final Transformer transformer) { |
|
261 |
this.transformer = transformer; |
|
262 |
} |
|
263 |
|
|
264 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/tools/OpaqueResource.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import java.util.Date; |
|
4 |
|
|
5 |
import org.w3c.dom.Document; |
|
6 |
|
|
7 |
/** |
|
8 |
* Some services, in particular the enabling layer, needs to manipulate all types of resources without knowing the exact |
|
9 |
* structure of their content, but only some well defined properties defined here. |
|
10 |
* |
|
11 |
* <p> |
|
12 |
* Different wrappers can be provided for different kind of resources. For example resources returned from the xmldb |
|
13 |
* layer can be cheaply mapped to an OpaqueResource, without going to full xml->bean conversion. |
|
14 |
* </p> |
|
15 |
* |
|
16 |
* @author marko |
|
17 |
* |
|
18 |
*/ |
|
19 |
public interface OpaqueResource { |
|
20 |
/** |
|
21 |
* Resource type. |
|
22 |
* |
|
23 |
* @return resource type string |
|
24 |
*/ |
|
25 |
String getResourceType(); |
|
26 |
|
|
27 |
/** |
|
28 |
* Resource kind. |
|
29 |
* |
|
30 |
* @return resource kind string |
|
31 |
*/ |
|
32 |
String getResourceKind(); |
|
33 |
|
|
34 |
/** |
|
35 |
* Resource identifier. |
|
36 |
* |
|
37 |
* @return resource identifier string |
|
38 |
*/ |
|
39 |
String getResourceId(); |
|
40 |
|
|
41 |
/** |
|
42 |
* Resource URI. |
|
43 |
* |
|
44 |
* @return resource uri string |
|
45 |
*/ |
|
46 |
String getResourceUri(); |
|
47 |
|
|
48 |
/** |
|
49 |
* get modification time stamp. |
|
50 |
* |
|
51 |
* @return time stamp |
|
52 |
*/ |
|
53 |
Date getModificationDate(); |
|
54 |
|
|
55 |
/** |
|
56 |
* Implementors may need to serialize the resource to a xml string representation. |
|
57 |
* |
|
58 |
* @return xml serialization |
|
59 |
*/ |
|
60 |
String asString(); |
|
61 |
|
|
62 |
/** |
|
63 |
* Implementors may store the DOM in the first place. Otherwise they should |
|
64 |
* return a parsed w3c DOM instance. |
|
65 |
* |
|
66 |
* @return DOM document |
|
67 |
*/ |
|
68 |
Document asDom(); |
|
69 |
|
|
70 |
/** |
|
71 |
* change the resource identifier. |
|
72 |
* |
|
73 |
* @param identifier new identifier |
|
74 |
*/ |
|
75 |
void setResourceId(String identifier); |
|
76 |
|
|
77 |
/** |
|
78 |
* change the resource kind. |
|
79 |
* |
|
80 |
* @param kind new kind |
|
81 |
*/ |
|
82 |
void setResourceKind(String kind); |
|
83 |
|
|
84 |
/** |
|
85 |
* change the resource uri. |
|
86 |
* |
|
87 |
* @param uri new uri |
|
88 |
*/ |
|
89 |
void setResourceUri(String uri); |
|
90 |
|
|
91 |
/** |
|
92 |
* set modification timestamp. |
|
93 |
* |
|
94 |
* @param timeStamp modification time stamp |
|
95 |
*/ |
|
96 |
void setModificationDate(Date timeStamp); |
|
97 |
|
|
98 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/tools/ServiceEnumerator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
/** |
|
6 |
* A service enumerator returns a bunch of service descriptions. The logic depends on the service enumerator. |
|
7 |
* |
|
8 |
* @author marko |
|
9 |
* |
|
10 |
*/ |
|
11 |
@Deprecated |
|
12 |
public interface ServiceEnumerator<T> { |
|
13 |
/** |
|
14 |
* Obtain a list of services. |
|
15 |
* |
|
16 |
* @return a list of service running instance descriptions |
|
17 |
*/ |
|
18 |
List<ServiceRunningInstance<T>> getServices(); |
|
19 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/tools/HNMLocator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
/** |
|
4 |
* finds an HNM profile for a giver service url. |
|
5 |
* |
|
6 |
* @author marko |
|
7 |
* |
|
8 |
*/ |
|
9 |
public interface HNMLocator { |
|
10 |
|
|
11 |
/** |
|
12 |
* finds an HNM profile for a giver service url. |
|
13 |
* |
|
14 |
* @param url |
|
15 |
* service address |
|
16 |
* @return hnm id |
|
17 |
*/ |
|
18 |
String getHNMForUrl(String url); |
|
19 |
} |
modules/cnr-service-common/tags/cnr-service-common-2.1.6/src/main/java/eu/dnetlib/enabling/tools/DynamicServiceEnumerator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.StringReader; |
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.HashMap; |
|
7 |
import java.util.List; |
|
8 |
import java.util.Map; |
|
9 |
|
|
10 |
import javax.annotation.Resource; |
|
11 |
import javax.xml.parsers.DocumentBuilder; |
|
12 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
13 |
import javax.xml.parsers.ParserConfigurationException; |
|
14 |
import javax.xml.ws.wsaddressing.W3CEndpointReference; |
|
15 |
import javax.xml.xpath.XPath; |
|
16 |
import javax.xml.xpath.XPathConstants; |
|
17 |
import javax.xml.xpath.XPathExpressionException; |
|
18 |
import javax.xml.xpath.XPathFactory; |
|
19 |
|
|
20 |
import org.apache.commons.logging.Log; |
|
21 |
import org.apache.commons.logging.LogFactory; |
|
22 |
import org.springframework.beans.factory.annotation.Required; |
|
23 |
import org.w3c.dom.Document; |
|
24 |
import org.w3c.dom.Element; |
|
25 |
import org.w3c.dom.NodeList; |
|
26 |
import org.xml.sax.InputSource; |
|
27 |
import org.xml.sax.SAXException; |
|
28 |
|
|
29 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; |
|
30 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
|
31 |
import eu.dnetlib.enabling.tools.registration.ServiceNameResolver; |
|
32 |
import eu.dnetlib.miscutils.collections.EnsureCollection; |
|
33 |
import eu.dnetlib.soap.cxf.StandaloneCxfEndpointReferenceBuilder; |
|
34 |
|
|
35 |
/** |
|
36 |
* Enumerates all services of a given type. |
|
37 |
* |
|
38 |
* @author marko |
|
39 |
* |
|
40 |
* @param <T> |
|
41 |
* service class |
|
42 |
*/ |
|
43 |
@Deprecated |
|
44 |
public class DynamicServiceEnumerator<T> implements ServiceEnumerator<T> { |
|
45 |
|
|
46 |
/** |
|
47 |
* logger. |
|
48 |
*/ |
|
49 |
private static final Log log = LogFactory.getLog(DynamicServiceEnumerator.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
50 |
|
|
51 |
/** |
|
52 |
* service class |
|
53 |
*/ |
|
54 |
private Class<T> clazz; |
Also available in: Unified diff
cleanup