Revision 41593
Added by Sandro La Bruzzo over 8 years ago
modules/cnr-service-common/releases/1.0.0/deploy.info | ||
---|---|---|
1 |
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/cnr-service-common/trunk/", "deploy_repository": "dnet4-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/dnet4-snapshots", "name": "cnr-service-common"} |
modules/cnr-service-common/releases/1.0.0/src/test/java/eu/dnetlib/enabling/tools/DynamicServiceEnumeratorTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
import static org.junit.Assert.assertNotNull; |
|
5 |
import static org.junit.Assert.assertTrue; |
|
6 |
import static org.mockito.Matchers.anyString; |
|
7 |
import static org.mockito.Mockito.when; |
|
8 |
|
|
9 |
import java.util.ArrayList; |
|
10 |
import java.util.List; |
|
11 |
|
|
12 |
import org.junit.Before; |
|
13 |
import org.junit.Test; |
|
14 |
import org.junit.runner.RunWith; |
|
15 |
import org.mockito.Mock; |
|
16 |
import org.mockito.runners.MockitoJUnit44Runner; |
|
17 |
|
|
18 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; |
|
19 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
|
20 |
import eu.dnetlib.enabling.tools.registration.ServiceNameResolver; |
|
21 |
import eu.dnetlib.soap.cxf.StandaloneCxfEndpointReferenceBuilder; |
|
22 |
|
|
23 |
/** |
|
24 |
* Test the dynamic service enumerator. |
|
25 |
* |
|
26 |
* @author marko |
|
27 |
* |
|
28 |
*/ |
|
29 |
@RunWith(MockitoJUnit44Runner.class) |
|
30 |
public class DynamicServiceEnumeratorTest { |
|
31 |
|
|
32 |
/** |
|
33 |
* Dummy service interface. |
|
34 |
* |
|
35 |
* @author marko |
|
36 |
* |
|
37 |
*/ |
|
38 |
interface TestService { |
|
39 |
} |
|
40 |
|
|
41 |
/** |
|
42 |
* instance under test. |
|
43 |
*/ |
|
44 |
private transient DynamicServiceEnumerator<TestService> enumerator; |
|
45 |
|
|
46 |
/** |
|
47 |
* lookup service locator mock |
|
48 |
*/ |
|
49 |
@Mock |
|
50 |
private transient ISLookUpService lookUpService; |
|
51 |
|
|
52 |
/** |
|
53 |
* lookup mock locator. |
|
54 |
*/ |
|
55 |
private transient ServiceLocator<ISLookUpService> lookUpLocator; |
|
56 |
|
|
57 |
/** |
|
58 |
* service name resolver mock. |
|
59 |
*/ |
|
60 |
@Mock |
|
61 |
private transient ServiceNameResolver nameResolver; |
|
62 |
|
|
63 |
/** |
|
64 |
* epr builder. |
|
65 |
*/ |
|
66 |
StandaloneCxfEndpointReferenceBuilder eprBuilder; |
|
67 |
|
|
68 |
/** |
|
69 |
* common preparation. |
|
70 |
*/ |
|
71 |
@Before |
|
72 |
public void setUp() { |
|
73 |
lookUpLocator = new StaticServiceLocator<ISLookUpService>(lookUpService); |
|
74 |
eprBuilder = new StandaloneCxfEndpointReferenceBuilder(); |
|
75 |
|
|
76 |
enumerator = new DynamicServiceEnumerator<TestService>(TestService.class); |
|
77 |
enumerator.setLookUpLocator(lookUpLocator); |
|
78 |
enumerator.setServiceNameResolver(nameResolver); |
|
79 |
enumerator.setEprBuilder(eprBuilder); |
|
80 |
|
|
81 |
when(nameResolver.getName(TestService.class)).thenReturn("TestService"); |
|
82 |
} |
|
83 |
|
|
84 |
/** |
|
85 |
* test get services. |
|
86 |
* |
|
87 |
* @throws ISLookUpException mock exception |
|
88 |
*/ |
|
89 |
@Test |
|
90 |
public void testGetServices() throws ISLookUpException { |
|
91 |
List<String> services = new ArrayList<String>(); |
|
92 |
services.add("<service><id>123</id><url>http://test.com</url></service>"); |
|
93 |
|
|
94 |
when(lookUpService.quickSearchProfile(anyString())).thenReturn(services); |
|
95 |
|
|
96 |
List<ServiceRunningInstance<TestService>> instances = enumerator.getServices(); |
|
97 |
|
|
98 |
assertEquals("query size", 1, instances.size()); |
|
99 |
assertEquals("service id", "123", instances.get(0).getServiceId()); |
|
100 |
assertEquals("service url", "http://test.com", instances.get(0).getUrl()); |
|
101 |
assertNotNull("service epr", instances.get(0).getEpr()); |
|
102 |
} |
|
103 |
|
|
104 |
@Test |
|
105 |
public void testEmptyGetServices() throws ISLookUpException { |
|
106 |
List<String> services = new ArrayList<String>(); |
|
107 |
when(lookUpService.quickSearchProfile(anyString())).thenReturn(services); |
|
108 |
|
|
109 |
List<ServiceRunningInstance<TestService>> instances = enumerator.getServices(); |
|
110 |
assertTrue("empty", instances.isEmpty()); |
|
111 |
} |
|
112 |
} |
modules/cnr-service-common/releases/1.0.0/src/test/java/eu/dnetlib/enabling/tools/DynamicServiceLocatorTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; // NOPMD |
|
4 |
import static org.mockito.Matchers.*; // NOPMD |
|
5 |
import static org.mockito.Mockito.*; // NOPMD |
|
6 |
|
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
import javax.xml.ws.wsaddressing.W3CEndpointReference; |
|
10 |
|
|
11 |
import org.junit.Before; |
|
12 |
import org.junit.Test; |
|
13 |
import org.junit.runner.RunWith; |
|
14 |
import org.springframework.beans.factory.annotation.Autowired; |
|
15 |
import org.springframework.test.context.ContextConfiguration; |
|
16 |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
|
17 |
|
|
18 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; |
|
19 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
|
20 |
import eu.dnetlib.enabling.resultset.rmi.ResultSetService; |
|
21 |
|
|
22 |
/** |
|
23 |
* Test dynamic service locator. |
|
24 |
* |
|
25 |
* @author marko |
|
26 |
* |
|
27 |
*/ |
|
28 |
@RunWith(SpringJUnit4ClassRunner.class) |
|
29 |
@ContextConfiguration |
|
30 |
public class DynamicServiceLocatorTest { |
|
31 |
|
|
32 |
/** |
|
33 |
* instance under test. |
|
34 |
*/ |
|
35 |
@Autowired |
|
36 |
private transient DynamicServiceLocator<ResultSetService> serviceLocator; |
|
37 |
|
|
38 |
/** |
|
39 |
* list of mock service profiles saved in files. |
|
40 |
*/ |
|
41 |
|
|
42 |
@javax.annotation.Resource(name = "serviceProfiles") |
|
43 |
private transient List<String> serviceUris; |
|
44 |
|
|
45 |
/** |
|
46 |
* service resolver mock. |
|
47 |
*/ |
|
48 |
private transient ServiceResolver serviceResolver; |
|
49 |
|
|
50 |
/** |
|
51 |
* setup. |
|
52 |
* |
|
53 |
* @throws ISLookUpException |
|
54 |
* mock |
|
55 |
*/ |
|
56 |
@Before |
|
57 |
public void setUp() throws ISLookUpException { |
|
58 |
// final ISLookUpService lookupService = mock(ISLookUpService.class); |
|
59 |
// serviceLocator.setLookUpLocator(new StaticServiceLocator<ISLookUpService>(lookupService)); |
|
60 |
|
|
61 |
serviceResolver = mock(ServiceResolver.class); |
|
62 |
serviceLocator.setServiceResolver(serviceResolver); |
|
63 |
|
|
64 |
final ISLookUpService lookupService = serviceLocator.getLookUpLocator().getService(); |
|
65 |
when(lookupService.quickSearchProfile(anyString())).thenReturn(serviceUris); |
|
66 |
} |
|
67 |
|
|
68 |
/** |
|
69 |
* test get service. |
|
70 |
*/ |
|
71 |
@Test |
|
72 |
public void testGetService() { |
|
73 |
assertNull("dummy", serviceLocator.getService()); |
|
74 |
|
|
75 |
verify(serviceResolver).getService(eq(ResultSetService.class), (W3CEndpointReference) anyObject()); |
|
76 |
} |
|
77 |
|
|
78 |
} |
modules/cnr-service-common/releases/1.0.0/src/test/java/eu/dnetlib/enabling/tools/registration/InterfaceServiceNameResolverCompatibilityTest.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools.registration; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
|
|
5 |
import org.junit.Before; |
|
6 |
import org.junit.Test; |
|
7 |
|
|
8 |
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService; |
|
9 |
import eu.dnetlib.enabling.resultset.rmi.ResultSetService; |
|
10 |
|
|
11 |
public class InterfaceServiceNameResolverCompatibilityTest { |
|
12 |
|
|
13 |
private transient InterfaceServiceNameResolverCompatibility resolver; |
|
14 |
|
|
15 |
private interface IndexService {} |
|
16 |
|
|
17 |
private interface ISomethingService {} |
|
18 |
|
|
19 |
@Before |
|
20 |
public void setUp() throws Exception { |
|
21 |
resolver = new InterfaceServiceNameResolverCompatibility(); |
|
22 |
} |
|
23 |
|
|
24 |
@Test |
|
25 |
public void testGetNameClassOfQ() { |
|
26 |
assertEquals("IndexService", resolver.getName(IndexService.class)); |
|
27 |
assertEquals("SomethingService", resolver.getName(ISomethingService.class)); |
|
28 |
assertEquals("ISRegistryService", resolver.getName(ISRegistryService.class)); |
|
29 |
assertEquals("ResultSetService", resolver.getName(ResultSetService.class)); |
|
30 |
} |
|
31 |
|
|
32 |
} |
modules/cnr-service-common/releases/1.0.0/src/test/resources/eu/dnetlib/enabling/tools/DynamicServiceLocatorTest-context.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<beans xmlns="http://www.springframework.org/schema/beans" |
|
3 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" |
|
4 |
xmlns:util="http://www.springframework.org/schema/util" |
|
5 |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
|
6 |
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> |
|
7 |
|
|
8 |
<bean id="propertyConfigurer" |
|
9 |
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> |
|
10 |
<property name="properties"> |
|
11 |
<props> |
|
12 |
<prop key="transport.soap.baseAddress">http://localhost:8090/app/services/ |
|
13 |
</prop> |
|
14 |
<prop key="infrastructure.name">junitTest</prop> |
|
15 |
<prop key="transport.soap.force.local.address">true</prop> |
|
16 |
</props> |
|
17 |
</property> |
|
18 |
</bean> |
|
19 |
|
|
20 |
<import |
|
21 |
resource="classpath:/eu/dnetlib/soap/cxf/applicationContext-eprbuilders.xml" /> |
|
22 |
|
|
23 |
<bean id="defaultServiceNameResolver" |
|
24 |
class="eu.dnetlib.enabling.tools.registration.InterfaceServiceNameResolver" /> |
|
25 |
|
|
26 |
<bean id="dynamicLocator" class="eu.dnetlib.enabling.tools.DynamicServiceLocator" |
|
27 |
p:clazz="eu.dnetlib.enabling.resultset.rmi.ResultSetService"> |
|
28 |
<property name="lookUpLocator"> |
|
29 |
<bean class="eu.dnetlib.enabling.tools.StaticServiceLocator"> |
|
30 |
<property name="service"> |
|
31 |
<bean class="eu.dnetlib.test.utils.MockBeanFactory" p:clazz="eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService" /> |
|
32 |
</property> |
|
33 |
</bean> |
|
34 |
</property> |
|
35 |
</bean> |
|
36 |
|
|
37 |
<util:list id="serviceProfiles"> |
|
38 |
<value><![CDATA[<service><id>1234</id><url>http://localhost:1234/app/services/resultSet</url></service>]]> |
|
39 |
</value> |
|
40 |
<value><![CDATA[<service><id>1234</id><url>http://localhost:8090/otherContext/services/resultSet</url></service>]]> |
|
41 |
</value> |
|
42 |
<value><![CDATA[<service><id>1234</id><url>http://localhost:8090/app/services/resultSet</url></service>]]> |
|
43 |
</value> |
|
44 |
<value><![CDATA[<service><id>1234</id><url>http://192.168.1.12:8090/app/services/resultSet</url></service>]]> |
|
45 |
</value> |
|
46 |
</util:list> |
|
47 |
</beans> |
modules/cnr-service-common/releases/1.0.0/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 |
public interface DynamicServiceLocatorLocationScorer { |
|
12 |
/** |
|
13 |
* Compute the score assigned to a given service location (url). |
|
14 |
* |
|
15 |
* @param url service url |
|
16 |
* @return score (the higher the better) |
|
17 |
* @throws MalformedURLException could happen |
|
18 |
*/ |
|
19 |
int score(URL url) throws MalformedURLException; |
|
20 |
} |
modules/cnr-service-common/releases/1.0.0/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 |
public class DefaultServiceLocatorLocationScorer implements DynamicServiceLocatorLocationScorer { |
|
17 |
|
|
18 |
/** |
|
19 |
* default score assigned when the other service has the same host. |
|
20 |
*/ |
|
21 |
private static final int LOCAL_HOST_SCORE = 5; |
|
22 |
|
|
23 |
/** |
|
24 |
* default score assigned when the other service has the same host and port (same container). |
|
25 |
*/ |
|
26 |
private static final int LOCAL_PORT_SCORE = 10; |
|
27 |
|
|
28 |
/** |
|
29 |
* default score assigned when the other service has the same host and port (same container and context). |
|
30 |
*/ |
|
31 |
private static final int LOCAL_SRV_SCORE = 15; |
|
32 |
|
|
33 |
|
|
34 |
/** |
|
35 |
* score assigned when the other service has the same host. |
|
36 |
*/ |
|
37 |
private int localHostScore = LOCAL_HOST_SCORE; |
|
38 |
|
|
39 |
/** |
|
40 |
* score assigned when the other service has the same host and port (same container). |
|
41 |
*/ |
|
42 |
private int localPortScore = LOCAL_PORT_SCORE; |
|
43 |
|
|
44 |
/** |
|
45 |
* score assigned when the other service has the same host and port (same container and context). |
|
46 |
*/ |
|
47 |
private int localSrvScore = LOCAL_SRV_SCORE; |
|
48 |
|
|
49 |
/** |
|
50 |
* build epr. |
|
51 |
*/ |
|
52 |
@Resource |
|
53 |
private StandaloneCxfEndpointReferenceBuilder eprBuilder; |
|
54 |
|
|
55 |
/** |
|
56 |
* {@inheritDoc} |
|
57 |
* @throws MalformedURLException |
|
58 |
* @see eu.dnetlib.enabling.tools.DynamicServiceLocatorLocationScorer#score(java.net.URL) |
|
59 |
*/ |
|
60 |
@Override |
|
61 |
public int score(final URL url) throws MalformedURLException { |
|
62 |
final URL localBase = new URL(eprBuilder.getBaseAddress()); |
|
63 |
if (url.toString().startsWith(localBase.toString())) |
|
64 |
return localSrvScore; |
|
65 |
|
|
66 |
if (localBase.getHost().equals(url.getHost())) { |
|
67 |
if (localBase.getPort() == url.getPort()) |
|
68 |
return localPortScore; |
|
69 |
return localHostScore; |
|
70 |
} |
|
71 |
return 0; |
|
72 |
} |
|
73 |
|
|
74 |
public StandaloneCxfEndpointReferenceBuilder getEprBuilder() { |
|
75 |
return eprBuilder; |
|
76 |
} |
|
77 |
|
|
78 |
public void setEprBuilder(final StandaloneCxfEndpointReferenceBuilder eprBuilder) { |
|
79 |
this.eprBuilder = eprBuilder; |
|
80 |
} |
|
81 |
|
|
82 |
public int getLocalHostScore() { |
|
83 |
return localHostScore; |
|
84 |
} |
|
85 |
|
|
86 |
public void setLocalHostScore(final int localHostScore) { |
|
87 |
this.localHostScore = localHostScore; |
|
88 |
} |
|
89 |
|
|
90 |
public int getLocalPortScore() { |
|
91 |
return localPortScore; |
|
92 |
} |
|
93 |
|
|
94 |
public void setLocalPortScore(final int localPortScore) { |
|
95 |
this.localPortScore = localPortScore; |
|
96 |
} |
|
97 |
|
|
98 |
public int getLocalSrvScore() { |
|
99 |
return localSrvScore; |
|
100 |
} |
|
101 |
|
|
102 |
public void setLocalSrvScore(final int localSrvScore) { |
|
103 |
this.localSrvScore = localSrvScore; |
|
104 |
} |
|
105 |
|
|
106 |
} |
modules/cnr-service-common/releases/1.0.0/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 |
* @see eu.dnetlib.enabling.tools.HNMLocator#getHNMForUrl(java.lang.String) |
|
14 |
*/ |
|
15 |
@Override |
|
16 |
public String getHNMForUrl(final String url) { |
|
17 |
return ""; |
|
18 |
} |
|
19 |
|
|
20 |
} |
modules/cnr-service-common/releases/1.0.0/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 |
public interface ServiceEnumerator<T> { |
|
12 |
/** |
|
13 |
* Obtain a list of services. |
|
14 |
* |
|
15 |
* @return a list of service running instance descriptions |
|
16 |
*/ |
|
17 |
List<ServiceRunningInstance<T>> getServices(); |
|
18 |
} |
modules/cnr-service-common/releases/1.0.0/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/releases/1.0.0/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/releases/1.0.0/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 |
* finds an HNM profile for a giver service url. |
|
12 |
* |
|
13 |
* @param url service address |
|
14 |
* @return hnm id |
|
15 |
*/ |
|
16 |
String getHNMForUrl(String url); |
|
17 |
} |
modules/cnr-service-common/releases/1.0.0/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 |
public class DynamicServiceEnumerator<T> implements ServiceEnumerator<T> { |
|
44 |
|
|
45 |
/** |
|
46 |
* logger. |
|
47 |
*/ |
|
48 |
private static final Log log = LogFactory.getLog(DynamicServiceEnumerator.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
49 |
|
|
50 |
/** |
|
51 |
* service class |
|
52 |
*/ |
|
53 |
private Class<T> clazz; |
|
54 |
|
|
55 |
/** |
|
56 |
* lookup locator. |
|
57 |
*/ |
|
58 |
private ServiceLocator<ISLookUpService> lookUpLocator; |
|
59 |
|
|
60 |
/** |
|
61 |
* service name resolver. |
|
62 |
*/ |
|
63 |
@Resource |
|
64 |
private ServiceNameResolver serviceNameResolver; // NOPMD |
|
65 |
|
|
66 |
/** |
|
67 |
* build epr. |
|
68 |
*/ |
|
69 |
@Resource |
|
70 |
private StandaloneCxfEndpointReferenceBuilder eprBuilder; |
|
71 |
|
|
72 |
/** |
|
73 |
* default constructor, useful for spring based instantiation. |
|
74 |
*/ |
|
75 |
public DynamicServiceEnumerator() { |
|
76 |
// default |
|
77 |
} |
|
78 |
|
|
79 |
/** |
|
80 |
* Build a dynamic service enumerator for a given class |
|
81 |
* |
|
82 |
* @param clazz |
|
83 |
* class |
|
84 |
*/ |
|
85 |
public DynamicServiceEnumerator(final Class<T> clazz) { |
|
86 |
super(); |
|
87 |
this.clazz = clazz; |
|
88 |
} |
|
89 |
|
|
90 |
/** |
|
91 |
* {@inheritDoc} |
|
92 |
* |
|
93 |
* @see eu.dnetlib.enabling.tools.ServiceEnumerator#getServices() |
|
94 |
*/ |
|
95 |
@Override |
|
96 |
public List<ServiceRunningInstance<T>> getServices() { |
|
97 |
final String serviceName = serviceNameResolver.getName(clazz); |
|
98 |
log.debug("searching for service: " + serviceName); |
|
99 |
|
|
100 |
final String xquery = "for $x in collection('/db/DRIVER/ServiceResources')//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value/string() = '" |
|
101 |
+ serviceName |
|
102 |
+ "ResourceType'] return <service><id>{$x//RESOURCE_IDENTIFIER/@value/string()}</id><url>{$x//PROTOCOL[@name = 'SOAP']/@address/string()}</url><properties>{$x//SERVICE_PROPERTIES/PROPERTY}</properties></service>"; |
|
103 |
log.debug(xquery); |
|
104 |
|
|
105 |
final XPathFactory factory = XPathFactory.newInstance(); |
|
106 |
final XPath xpath = factory.newXPath(); |
|
107 |
|
|
108 |
final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
|
109 |
|
|
110 |
try { |
|
111 |
final DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); |
|
112 |
|
|
113 |
final List<String> services = lookUpLocator.getService().quickSearchProfile(xquery); |
|
114 |
final List<ServiceRunningInstance<T>> instances = new ArrayList<ServiceRunningInstance<T>>(); |
|
115 |
|
|
116 |
for (final String source : EnsureCollection.list(services)) { |
|
117 |
final Document doc = docBuilder.parse(new InputSource(new StringReader(source))); |
|
118 |
final String url = xpath.evaluate("//url", doc); |
|
119 |
final String serviceId = xpath.evaluate("//id", doc); |
|
120 |
|
|
121 |
final NodeList propElements = (NodeList) xpath.evaluate("//PROPERTY", doc, XPathConstants.NODESET); |
|
122 |
Map<String, String> props = new HashMap<String, String>(); |
|
123 |
|
|
124 |
for (int i = 0; i < propElements.getLength(); i++) { |
|
125 |
Element propElement = (Element) propElements.item(i); |
|
126 |
props.put(propElement.getAttribute("key"), propElement.getAttribute("value")); |
|
127 |
} |
|
128 |
|
|
129 |
final W3CEndpointReference epr = eprBuilder.getEndpointReference(url, null, null, url + "?wsdl", null, null); |
|
130 |
|
|
131 |
instances.add(new ServiceRunningInstance<T>(epr, serviceId, url, props)); |
|
132 |
} |
|
133 |
|
|
134 |
return instances; |
|
135 |
} catch (final ISLookUpException e) { |
|
136 |
throw new IllegalStateException("cannot locate service " + serviceName, e); |
|
137 |
} catch (final XPathExpressionException e) { |
|
138 |
throw new IllegalStateException("cannot locate service " + serviceName, e); |
|
139 |
} catch (final SAXException e) { |
|
140 |
throw new IllegalStateException("cannot locate service " + serviceName, e); |
|
141 |
} catch (final IOException e) { |
|
142 |
throw new IllegalStateException("cannot locate service " + serviceName, e); |
|
143 |
} catch (final ParserConfigurationException e) { |
|
144 |
throw new IllegalStateException("cannot locate service " + serviceName, e); |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
public Class<T> getClazz() { |
|
149 |
return clazz; |
|
150 |
} |
|
151 |
|
|
152 |
@Required |
|
153 |
public void setClazz(final Class<T> clazz) { |
|
154 |
this.clazz = clazz; |
|
155 |
} |
|
156 |
|
|
157 |
public ServiceLocator<ISLookUpService> getLookUpLocator() { |
|
158 |
return lookUpLocator; |
|
159 |
} |
|
160 |
|
|
161 |
public void setLookUpLocator(final ServiceLocator<ISLookUpService> lookUpLocator) { |
|
162 |
this.lookUpLocator = lookUpLocator; |
|
163 |
} |
|
164 |
|
|
165 |
public ServiceNameResolver getServiceNameResolver() { |
|
166 |
return serviceNameResolver; |
|
167 |
} |
|
168 |
|
|
169 |
public void setServiceNameResolver(final ServiceNameResolver serviceNameResolver) { |
|
170 |
this.serviceNameResolver = serviceNameResolver; |
|
171 |
} |
|
172 |
|
|
173 |
public StandaloneCxfEndpointReferenceBuilder getEprBuilder() { |
|
174 |
return eprBuilder; |
|
175 |
} |
|
176 |
|
|
177 |
public void setEprBuilder(StandaloneCxfEndpointReferenceBuilder eprBuilder) { |
|
178 |
this.eprBuilder = eprBuilder; |
|
179 |
} |
|
180 |
|
|
181 |
} |
modules/cnr-service-common/releases/1.0.0/src/main/java/eu/dnetlib/enabling/tools/DynamicHNMLocator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import org.apache.commons.logging.Log; |
|
4 |
import org.apache.commons.logging.LogFactory; |
|
5 |
|
|
6 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException; |
|
7 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; |
|
8 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
|
9 |
|
|
10 |
/** |
|
11 |
* Locates a registered HNM running on a given url. |
|
12 |
* |
|
13 |
* @author marko |
|
14 |
* |
|
15 |
*/ |
|
16 |
public class DynamicHNMLocator implements HNMLocator { |
|
17 |
|
|
18 |
/** |
|
19 |
* logger. |
|
20 |
*/ |
|
21 |
private static final Log log = LogFactory.getLog(DynamicHNMLocator.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
22 |
|
|
23 |
/** |
|
24 |
* is lookup locator. |
|
25 |
*/ |
|
26 |
ServiceLocator<ISLookUpService> lookupLocator; |
|
27 |
|
|
28 |
/** |
|
29 |
* {@inheritDoc} |
|
30 |
* |
|
31 |
* @see eu.dnetlib.enabling.tools.HNMLocator#getHNMForUrl(java.lang.String) |
|
32 |
*/ |
|
33 |
@Override |
|
34 |
public String getHNMForUrl(final String url) { |
|
35 |
|
|
36 |
final String prefix = url.substring(0, url.indexOf('/', "http://".length())); |
|
37 |
final String query = "collection('')//RESOURCE_PROFILE[.//RESOURCE_TYPE/@value = 'HostingNodeManagerServiceResourceType' and starts-with(.//RESOURCE_URI/@value, '" |
|
38 |
+ prefix + "')]//RESOURCE_IDENTIFIER/@value/string()"; |
|
39 |
|
|
40 |
try { |
|
41 |
return lookupLocator.getService().getResourceProfileByQuery(query); |
|
42 |
} catch (final ISLookUpDocumentNotFoundException e) { |
|
43 |
log.debug("doument not found for query: " + query); |
|
44 |
return null; |
|
45 |
} catch (final ISLookUpException e) { |
|
46 |
throw new IllegalStateException("cannot search hnm", e); |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
public ServiceLocator<ISLookUpService> getLookupLocator() { |
|
51 |
return lookupLocator; |
|
52 |
} |
|
53 |
|
|
54 |
public void setLookupLocator(final ServiceLocator<ISLookUpService> lookupLocator) { |
|
55 |
this.lookupLocator = lookupLocator; |
|
56 |
} |
|
57 |
|
|
58 |
} |
modules/cnr-service-common/releases/1.0.0/src/main/java/eu/dnetlib/enabling/tools/StreamOpaqueResource.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; // NOPMD |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.InputStream; |
|
5 |
import java.io.StringWriter; |
|
6 |
|
|
7 |
import javax.xml.parsers.ParserConfigurationException; |
|
8 |
import javax.xml.xpath.XPathExpressionException; |
|
9 |
|
|
10 |
import org.apache.commons.io.IOUtils; |
|
11 |
import org.xml.sax.SAXException; |
|
12 |
|
|
13 |
/** |
|
14 |
* implements a possibly optimized OpaqueResource constructed from a stream containing the xml source. |
|
15 |
* |
|
16 |
* <p>the optimization consists of postponing the parsing as possibly and parse the profile passing the stream |
|
17 |
* directly to the xml parser.<p> |
|
18 |
* |
|
19 |
* <p> |
|
20 |
* TODO: implement the optimization, currenly converts to string and reuses StringOpaqueResource |
|
21 |
* </p> |
|
22 |
* |
|
23 |
* @author marko |
|
24 |
* |
|
25 |
*/ |
|
26 |
public class StreamOpaqueResource extends StringOpaqueResource { |
|
27 |
|
|
28 |
/** |
|
29 |
* construct with an input stream. |
|
30 |
* |
|
31 |
* @param source source stream of xml text |
|
32 |
* @throws XPathExpressionException shouldn't happen |
|
33 |
* @throws SAXException could happen |
|
34 |
* @throws IOException could happen |
|
35 |
* @throws ParserConfigurationException shouldn't happen |
|
36 |
*/ |
|
37 |
public StreamOpaqueResource(final InputStream source) throws XPathExpressionException, SAXException, IOException, ParserConfigurationException { |
|
38 |
super(stringify(source)); |
|
39 |
} |
|
40 |
|
|
41 |
/** |
|
42 |
* helper method: reads a stream to a string. |
|
43 |
* |
|
44 |
* @param source input stream |
|
45 |
* @return string containing the whole stream content. |
|
46 |
* @throws IOException could happen. |
|
47 |
*/ |
|
48 |
private static String stringify(final InputStream source) throws IOException { |
|
49 |
final StringWriter writer = new StringWriter(); |
|
50 |
IOUtils.copy(source, writer); |
|
51 |
return writer.getBuffer().toString(); |
|
52 |
} |
|
53 |
|
|
54 |
} |
modules/cnr-service-common/releases/1.0.0/src/main/java/eu/dnetlib/enabling/tools/XQueryUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
/** |
|
4 |
* Implementations of this interface will provide functionalities useful to create xqueries or parts of thereof, like |
|
5 |
* xmldb collection names for a given profile. |
|
6 |
* |
|
7 |
* @author marko |
|
8 |
* |
|
9 |
*/ |
|
10 |
public interface XQueryUtils { |
|
11 |
/** |
|
12 |
* obtain the name of the xmldb collection for a given resource. |
|
13 |
* @param resource resource |
|
14 |
* @return path of the collection relative to the xmldb dnet root. |
|
15 |
*/ |
|
16 |
String getCollectionPath(OpaqueResource resource); |
|
17 |
|
|
18 |
/** |
|
19 |
* obtain the absolute xmldb collection path for a given resource. |
|
20 |
* @param resource resource |
|
21 |
* @return collection absolute path |
|
22 |
*/ |
|
23 |
String getCollectionAbsPath(OpaqueResource resource); |
|
24 |
|
|
25 |
/** |
|
26 |
* get the absolute xmldb collection path. |
|
27 |
* |
|
28 |
* @return root xmldb root collection path |
|
29 |
*/ |
|
30 |
String getRootCollection(); |
|
31 |
|
|
32 |
/** |
|
33 |
* Return the xmldb filename for a given resource. |
|
34 |
* |
|
35 |
* @param resource resource |
|
36 |
* @return xmldb filename |
|
37 |
*/ |
|
38 |
String getFileName(OpaqueResource resource); |
|
39 |
} |
modules/cnr-service-common/releases/1.0.0/src/main/java/eu/dnetlib/enabling/tools/AbstractBaseService.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import javax.jws.WebService; |
|
4 |
|
|
5 |
import org.apache.commons.logging.Log; |
|
6 |
import org.apache.commons.logging.LogFactory; |
|
7 |
import org.springframework.context.Lifecycle; |
|
8 |
|
|
9 |
import eu.dnetlib.common.rmi.BaseService; |
|
10 |
|
|
11 |
/** |
|
12 |
* This class contains default definition for BaseService contract and basic service lifecycle. |
|
13 |
* |
|
14 |
* TODO: split BaseService contract implementation from lifecycle and other helper method |
|
15 |
* |
|
16 |
* @author marko |
|
17 |
* |
|
18 |
*/ |
|
19 |
@WebService(targetNamespace = "http://services.dnetlib.eu/") |
|
20 |
public abstract class AbstractBaseService implements BaseService, Lifecycle { |
|
21 |
|
|
22 |
/** |
|
23 |
* logger. |
|
24 |
*/ |
|
25 |
private static final Log log = LogFactory // NOPMD by marko on 11/24/08 5:02 PM |
|
26 |
.getLog(AbstractBaseService.class); |
|
27 |
|
|
28 |
private boolean started = false; |
|
29 |
|
|
30 |
/** |
|
31 |
* {@inheritDoc} |
|
32 |
* |
|
33 |
* @see eu.dnetlib.common.rmi.BaseService#identify() |
|
34 |
*/ |
|
35 |
@Override |
|
36 |
public String identify() { |
|
37 |
return getClass().getName(); |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* {@inheritDoc} |
|
42 |
* |
|
43 |
* @see eu.dnetlib.common.rmi.BaseService#notify(java.lang.String, java.lang.String, java.lang.String, java.lang.String) |
|
44 |
*/ |
|
45 |
@Override |
|
46 |
public void notify(final String subscriptionId, final String topic, final String isId, final String message) { |
|
47 |
log.debug("got notification: " + topic + ", profile: " + isId + ", body: " + message); |
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* {@inheritDoc} |
|
52 |
* |
|
53 |
* @see eu.dnetlib.common.rmi.BaseService#start() |
|
54 |
*/ |
|
55 |
@Override |
|
56 |
public void start() { |
|
57 |
log.info("Starting service " + identify()); |
|
58 |
if (started) { |
|
59 |
log.warn("Service " + this + "already started, check bean initializations!"); |
|
60 |
} |
|
61 |
started = true; |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* {@inheritDoc} |
|
66 |
* |
|
67 |
* @see org.springframework.context.Lifecycle#isRunning() |
|
68 |
*/ |
|
69 |
@Override |
|
70 |
public boolean isRunning() { |
|
71 |
log.debug("called isRunning " + this); |
|
72 |
return false; |
|
73 |
} |
|
74 |
|
|
75 |
/** |
|
76 |
* {@inheritDoc} |
|
77 |
* |
|
78 |
* @see org.springframework.context.Lifecycle#stop() |
|
79 |
*/ |
|
80 |
@Override |
|
81 |
public void stop() { |
|
82 |
log.info("Stopping service " + this); |
|
83 |
} |
|
84 |
|
|
85 |
} |
modules/cnr-service-common/releases/1.0.0/src/main/java/eu/dnetlib/enabling/tools/ServiceLocator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
/** |
|
4 |
* A service locator provides a reference to a, possibly remote, service. |
|
5 |
* |
|
6 |
* @author marko |
|
7 |
* |
|
8 |
* @param <T> |
|
9 |
* the type of the service to return |
|
10 |
*/ |
|
11 |
public interface ServiceLocator<T> { |
|
12 |
/** |
|
13 |
* locate an return a service of this type. |
|
14 |
* |
|
15 |
* @return a service client instance |
|
16 |
*/ |
|
17 |
T getService(); |
|
18 |
} |
modules/cnr-service-common/releases/1.0.0/src/main/java/eu/dnetlib/enabling/tools/ServiceResolverChain.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.List; |
|
5 |
|
|
6 |
import javax.xml.ws.wsaddressing.W3CEndpointReference; |
|
7 |
|
|
8 |
/** |
|
9 |
* This resolver chains a list of resolvers. |
|
10 |
* |
|
11 |
* @author marko |
|
12 |
* |
|
13 |
*/ |
|
14 |
public class ServiceResolverChain implements ServiceResolver { |
|
15 |
|
|
16 |
/** |
|
17 |
* service resolvers in decreasing order or priority. |
|
18 |
*/ |
|
19 |
private List<ServiceResolver> resolvers = new ArrayList<ServiceResolver>(); |
|
20 |
|
|
21 |
/** |
|
22 |
* {@inheritDoc} |
|
23 |
* |
|
24 |
* @see eu.dnetlib.enabling.tools.ServiceResolver#getService(java.lang.Class, |
|
25 |
* javax.xml.ws.wsaddressing.W3CEndpointReference) |
|
26 |
*/ |
|
27 |
@Override |
|
28 |
public <T> T getService(final Class<T> clazz, final W3CEndpointReference epr) { |
|
29 |
for (ServiceResolver resolver : getResolvers()) { |
|
30 |
final T service = resolver.getService(clazz, epr); |
|
31 |
|
|
32 |
if (service != null) |
|
33 |
return service; |
|
34 |
} |
|
35 |
return null; |
|
36 |
} |
|
37 |
|
|
38 |
/** |
|
39 |
* {@inheritDoc} |
|
40 |
* @see eu.dnetlib.enabling.tools.ServiceResolver#getResourceIdentifier(javax.xml.ws.wsaddressing.W3CEndpointReference) |
|
41 |
*/ |
|
42 |
@Override |
|
43 |
public String getResourceIdentifier(final W3CEndpointReference epr) { |
|
44 |
for (ServiceResolver resolver : getResolvers()) { |
|
45 |
final String rsId = resolver.getResourceIdentifier(epr); |
|
46 |
|
|
47 |
if (rsId != null) |
|
48 |
return rsId; |
|
49 |
} |
|
50 |
return null; |
|
51 |
} |
|
52 |
|
|
53 |
public List<ServiceResolver> getResolvers() { |
|
54 |
return resolvers; |
|
55 |
} |
|
56 |
|
|
57 |
public void setResolvers(final List<ServiceResolver> resolvers) { |
|
58 |
this.resolvers = resolvers; |
|
59 |
} |
|
60 |
|
|
61 |
} |
modules/cnr-service-common/releases/1.0.0/src/main/java/eu/dnetlib/enabling/tools/ServiceRunningInstance.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import java.util.HashMap; |
|
4 |
import java.util.Map; |
|
5 |
|
|
6 |
import javax.xml.ws.wsaddressing.W3CEndpointReference; |
|
7 |
|
|
8 |
/** |
|
9 |
* This bean packages the minimum information for describing a service running instance. |
|
10 |
* |
|
11 |
* @author marko |
|
12 |
* |
|
13 |
*/ |
|
14 |
public class ServiceRunningInstance<T> { |
|
15 |
/** |
|
16 |
* service endpoint. |
|
17 |
*/ |
|
18 |
private W3CEndpointReference epr; |
|
19 |
|
|
20 |
/** |
|
21 |
* service resource id. |
|
22 |
*/ |
|
23 |
private String serviceId; |
|
24 |
|
|
25 |
/** |
|
26 |
* service endpoint url. |
|
27 |
*/ |
|
28 |
private String url; |
|
29 |
|
|
30 |
/** |
|
31 |
* arbitrary service properties stored in the profile. |
|
32 |
*/ |
|
33 |
private Map<String, String> serviceProperties; |
|
34 |
|
|
35 |
/** |
|
36 |
* Create a service running instance. |
|
37 |
* |
|
38 |
* @param epr service epr |
|
39 |
* @param serviceId service profile resource id |
|
40 |
* @param url service endpoint url |
|
41 |
*/ |
|
42 |
public ServiceRunningInstance(final W3CEndpointReference epr, final String serviceId, final String url) { |
|
43 |
this(epr, serviceId, url, new HashMap<String, String>()); |
|
44 |
} |
|
45 |
|
|
46 |
/** |
|
47 |
* Create a service running instance. |
|
48 |
* |
|
49 |
* @param epr service epr |
|
50 |
* @param serviceId service profile resource id |
|
51 |
* @param url service endpoint url |
|
52 |
* @param serviceProperties service property map |
|
53 |
*/ |
|
54 |
public ServiceRunningInstance(final W3CEndpointReference epr, final String serviceId, final String url, final Map<String, String> serviceProperties) { |
|
55 |
super(); |
|
56 |
this.epr = epr; |
|
57 |
this.serviceId = serviceId; |
|
58 |
this.url = url; |
|
59 |
this.serviceProperties = serviceProperties; |
|
60 |
} |
|
61 |
|
|
62 |
public W3CEndpointReference getEpr() { |
|
63 |
return epr; |
|
64 |
} |
|
65 |
|
|
66 |
public void setEpr(final W3CEndpointReference epr) { |
|
67 |
this.epr = epr; |
|
68 |
} |
|
69 |
|
|
70 |
public String getServiceId() { |
|
71 |
return serviceId; |
|
72 |
} |
|
73 |
|
|
74 |
public void setServiceId(final String serviceId) { |
|
75 |
this.serviceId = serviceId; |
|
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 String getUrl() { |
|
87 |
return url; |
|
88 |
} |
|
89 |
|
|
90 |
public void setUrl(String url) { |
|
91 |
this.url = url; |
Also available in: Unified diff
imported cnr-service-common into Dnet 5.0