Project

General

Profile

« Previous | Next » 

Revision 47828

[maven-release-plugin] copy for tag cnr-resultset-client-2.1.6

View differences:

modules/cnr-resultset-client/tags/cnr-resultset-client-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-resultset-client/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-resultset-client"}
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/src/test/java/eu/dnetlib/enabling/resultset/ResultSetPageProviderTest.java
1
package eu.dnetlib.enabling.resultset;
2

  
3
import static org.junit.Assert.assertEquals;
4
import static org.mockito.Matchers.anyInt;
5
import static org.mockito.Matchers.anyString;
6
import static org.mockito.Mockito.times;
7
import static org.mockito.Mockito.verify;
8
import static org.mockito.Mockito.when;
9

  
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.invocation.InvocationOnMock;
17
import org.mockito.junit.MockitoJUnitRunner;
18

  
19
import org.mockito.stubbing.Answer;
20

  
21
import com.google.common.collect.Lists;
22

  
23
import eu.dnetlib.enabling.resultset.client.ResultSetPageProvider;
24
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
25

  
26
@RunWith(MockitoJUnitRunner.class)
27
public class ResultSetPageProviderTest {
28

  
29
	private static final String RSID = "RS_123";
30
	private static final int PAGE_SIZE = 10;
31
	private static final String RSSTATUS = "closed";
32
	
33
	// Class Under test
34
	private ResultSetPageProvider pageProvider;
35

  
36
	@Mock
37
	private ResultSetService mockResultSet;
38
	
39
	private class ResultAnswer implements Answer<List<String>> {
40
		private int size;
41
		
42
		public ResultAnswer(int size) {
43
			this.size = size;
44
		}
45

  
46
		@Override
47
		public List<String> answer(InvocationOnMock invocation) throws Throwable {
48
			Object[] args = invocation.getArguments();
49
			int from = Integer.parseInt(args[1].toString());
50
			int to = Integer.parseInt(args[2].toString());
51

  
52
			
53
			if (to > size) to = size;
54
			if (from > to) throw new Exception("FROM IS GREATER THAN TO");
55
			
56
			List<String> list = Lists.newArrayList();
57
			for (int i = from; i<=to; i++) {
58
				list.add("RECORD " + i);
59
			}
60
			return list;
61
		}
62
	}
63
	
64
	@Before
65
	public void setUp() throws Exception {
66
		pageProvider = new ResultSetPageProvider(mockResultSet, RSID);
67
		pageProvider.setMaxWaitTime(60000);
68
		pageProvider.setPageSize(PAGE_SIZE);
69
	}
70

  
71
	@Test
72
	public void testNextPage5() throws Exception {
73
		performTest(5);
74
	}
75

  
76
	@Test
77
	public void testNextPage10() throws Exception {
78
		performTest(10);
79
	}
80

  
81
	@Test
82
	public void testNextPage15() throws Exception {
83
		performTest(15);
84
	}
85

  
86
	@Test
87
	public void testNextPage20() throws Exception {
88
		performTest(20);
89
	}
90

  
91
	@Test
92
	public void testNextPage250() throws Exception {
93
		performTest(250);
94
	}
95

  
96
	@Test
97
	public void testNextPage254() throws Exception {
98
		performTest(254);
99
	}
100

  
101
	public void performTest(int size) throws Exception {
102
		when(mockResultSet.getRSStatus(RSID)).thenReturn(RSSTATUS);
103
		when(mockResultSet.getNumberOfElements(RSID)).thenReturn(size);
104
		when(mockResultSet.getResult(anyString(), anyInt(), anyInt(), anyString())).thenAnswer(new ResultAnswer(size));
105
		
106
		List<String> list = Lists.newArrayList();
107
		
108
		while (true) {
109
			List<String> res = pageProvider.nextPage();
110
			if (res == null || res.isEmpty()) break;
111
			list.addAll(res);
112
		}
113
		
114
		assertEquals(list.size(), size);
115
		verify(mockResultSet, times(((size - 1) / PAGE_SIZE) + 1)).getResult(anyString(), anyInt(), anyInt(), anyString());
116
	}
117

  
118
}
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/src/main/java/eu/dnetlib/enabling/resultset/client/utils/ResultSetTimeoutException.java
1
package eu.dnetlib.enabling.resultset.client.utils;
2

  
3
public class ResultSetTimeoutException extends RuntimeException {
4

  
5
	private static final long serialVersionUID = -3713991101055085620L;
6
	
7
	public ResultSetTimeoutException(String message) {
8
		super(message);
9
	}
10

  
11
}
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/src/main/java/eu/dnetlib/enabling/resultset/client/utils/EPRUtils.java
1
package eu.dnetlib.enabling.resultset.client.utils;
2

  
3
import java.io.StringReader;
4

  
5
import javax.xml.transform.stream.StreamSource;
6
import javax.xml.ws.EndpointReference;
7
import javax.xml.ws.wsaddressing.W3CEndpointReference;
8

  
9
/**
10
 * 
11
 * @author claudio
12
 *
13
 */
14
public class EPRUtils {
15

  
16
	/**
17
	 * builds an epr from its string representation
18
	 * 
19
	 * @param epr
20
	 * 			String epr
21
	 * @return 
22
	 * 			W3CEndpointReference epr
23
	 * 		
24
	 */
25
	public W3CEndpointReference getEpr(String epr) {
26
		return (W3CEndpointReference) EndpointReference.readFrom(new StreamSource(new StringReader(epr)));
27
	}
28
	
29
}
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/src/main/java/eu/dnetlib/enabling/resultset/client/utils/ResultSetRuntimeException.java
1
package eu.dnetlib.enabling.resultset.client.utils;
2

  
3
public class ResultSetRuntimeException extends RuntimeException {
4

  
5
	/**
6
	 * 
7
	 */
8
	private static final long serialVersionUID = -5131499590327995897L;
9
	
10
	public ResultSetRuntimeException(String message) {
11
		super(message);
12
	}
13
	
14
	public ResultSetRuntimeException(Throwable e) {
15
		super(e);
16
	}
17

  
18
}
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/src/main/java/eu/dnetlib/enabling/resultset/client/IterableResultSetClient.java
1
package eu.dnetlib.enabling.resultset.client;
2

  
3
import java.util.Iterator;
4

  
5
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
6

  
7
/**
8
 * 
9
 * @author claudio
10
 *
11
 */
12
public class IterableResultSetClient implements Iterable<String> {
13

  
14
	/**
15
	 * reference to resultset service.
16
	 */
17
	private ResultSetService resultSet;
18
	
19
	/**
20
	 * resultset id
21
	 */
22
	private String rsId;
23
	
24
	/**
25
	 * page size.
26
	 */
27
	private int pageSize;
28
	
29
	/**
30
	 * timeout
31
	 */
32
	private long timeout;
33
	
34
	public IterableResultSetClient(ResultSetService resultSet, String rsId, int pageSize) {
35
		this.resultSet = resultSet;
36
		this.rsId = rsId;
37
		this.pageSize = pageSize;
38
		this.timeout = 0;
39
	} 
40
	
41
	public IterableResultSetClient(ResultSetService resultSet, String rsId, int pageSize, long timeout) {
42
		this(resultSet, rsId, pageSize);
43
		this.timeout = timeout;
44
	} 
45
	
46
	@Override
47
	public Iterator<String> iterator() {
48
		if (timeout == 0)
49
			return new ResultSetClientIterator(resultSet, rsId, pageSize);
50
		return new ResultSetClientIterator(resultSet, rsId, pageSize, timeout);
51
	}
52

  
53
	public int getPageSize() {
54
		return pageSize;
55
	}
56

  
57
	public void setPageSize(int pageSize) {
58
		this.pageSize = pageSize;
59
	}
60

  
61
}
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/src/main/java/eu/dnetlib/enabling/resultset/client/ResultSetClientFactory.java
1
package eu.dnetlib.enabling.resultset.client;
2

  
3
import java.util.Map;
4
import javax.xml.ws.BindingProvider;
5
import javax.xml.ws.wsaddressing.W3CEndpointReference;
6

  
7
import eu.dnetlib.enabling.resultset.client.utils.EPRUtils;
8
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
9
import eu.dnetlib.enabling.tools.ServiceResolver;
10

  
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13

  
14
import org.apache.cxf.endpoint.Client;
15
import org.apache.cxf.frontend.ClientProxy;
16

  
17
import org.apache.cxf.transport.http.HTTPConduit;
18
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
19
import org.springframework.beans.factory.annotation.Required;
20

  
21
/**
22
 * 
23
 * @author claudio
24
 * 
25
 */
26
public class ResultSetClientFactory implements ResultSetClient {
27

  
28
	/**
29
	 * logger
30
	 */
31
	private static final Log log = LogFactory.getLog(ResultSetClientFactory.class);
32

  
33
	private final static long DEFAULT_CONNECT_TIMEOUT = 10000;
34

  
35
	private final static long DEFAULT_REQUEST_TIMEOUT = 60000;
36

  
37
	private final static int DEFAULT_PAGE_SIZE = 100;
38

  
39
	/**
40
	 * used to resolve the epr references to the service endpoint
41
	 */
42
	private ServiceResolver serviceResolver;
43

  
44
	/**
45
	 * utility object
46
	 */
47
	private EPRUtils eprUtils;
48

  
49
	/**
50
	 * actual page size
51
	 */
52
	private int pageSize;
53

  
54
	/**
55
	 * request timeout
56
	 */
57
	private long timeout;
58

  
59
	/**
60
	 * request timeout
61
	 */
62
	private long connectTimeout;
63

  
64
	public ResultSetClientFactory() {
65
		this(DEFAULT_PAGE_SIZE, DEFAULT_REQUEST_TIMEOUT, DEFAULT_CONNECT_TIMEOUT);
66
	}
67

  
68
	/**
69
	 * 
70
	 * @param pageSize
71
	 * @param timeout
72
	 * @throws IllegalArgumentException
73
	 */
74
	public ResultSetClientFactory(int pageSize, long timeout) throws IllegalArgumentException {
75
		this(pageSize, timeout, DEFAULT_CONNECT_TIMEOUT);
76
	}
77

  
78
	/**
79
	 *
80
	 * @param pageSize
81
	 * @param timeout
82
	 *          time to wait for server response before throwing a timeout exception
83
	 * @param connectTimeout
84
	 *          time to wait for server to accept the connection before throwing a connection timeout exception
85
	 * @throws IllegalArgumentException
86
	 */
87
	public ResultSetClientFactory(int pageSize, long timeout, long connectTimeout) throws IllegalArgumentException {
88
		if (pageSize <= 0 || timeout <= 0 || connectTimeout <= 0) {
89
			throw new IllegalArgumentException("parameters pageSize, timeout and connectTimeout must be greater than zero");
90
		}
91
		log.info(String.format("creating new ResultSetClientIterableFactory with pageSize (%s), read timeout (%s) and connect timeout (%s)",
92
				pageSize, timeout, connectTimeout));
93
		this.pageSize = pageSize;
94
		this.timeout = timeout;
95
		this.connectTimeout = connectTimeout;
96
	}
97

  
98
	/**
99
	 * 
100
	 * @param epr
101
	 * @param pageSize
102
	 * @return
103
	 */
104
	@Override
105
	public IterableResultSetClient getClient(W3CEndpointReference epr, int pageSize) {
106
		final ResultSetService resultSet = getResultSetService(epr, getConnectTimeout(), getTimeout());
107
		final String rsId = serviceResolver.getResourceIdentifier(epr);
108

  
109
		//using given pageSize and default timeout
110
		return new IterableResultSetClient(resultSet, rsId, pageSize, getTimeout());
111
	}
112

  
113
	/**
114
	 * 
115
	 * @param epr
116
	 * @return
117
	 */
118
	@Override
119
	public IterableResultSetClient getClient(W3CEndpointReference epr) {
120
		final ResultSetService resultSet = getResultSetService(epr, getConnectTimeout(), getTimeout());
121
		final String rsId = serviceResolver.getResourceIdentifier(epr);
122

  
123
		//using default pageSize and timeouts
124
		return new IterableResultSetClient(resultSet, rsId, getPageSize(), getTimeout());
125
	}
126

  
127
	private ResultSetService getResultSetService(final W3CEndpointReference epr, final long connectTimeout, final long requestTimeout) {
128
		final ResultSetService service = serviceResolver.getService(ResultSetService.class, epr);
129

  
130
		log.debug(String.format("creting resultSet service stub (%s) with connectTimeout(%s), requestTimeout(%s)", service.getClass().getName(), connectTimeout, requestTimeout));
131

  
132
		if(service instanceof Client) {
133
			log.debug(String.format("setting timeouts for %s", Client.class));
134
			final Client client = ClientProxy.getClient(service);
135
			final HTTPConduit http = (HTTPConduit) client.getConduit();
136
			final HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
137

  
138
			httpClientPolicy.setConnectionTimeout(connectTimeout);
139
			httpClientPolicy.setAllowChunking(false);
140
			httpClientPolicy.setReceiveTimeout(requestTimeout);
141

  
142
			http.setClient(httpClientPolicy);
143
		} else if (service instanceof BindingProvider) {
144
			log.debug(String.format("setting timeouts for %s", BindingProvider.class));
145
			final Map<String, Object> requestContext = ((BindingProvider) service).getRequestContext();
146

  
147
			// can't be sure about which will be used. Set them all.
148
			requestContext.put("com.sun.xml.internal.ws.request.timeout", requestTimeout);
149
			requestContext.put("com.sun.xml.internal.ws.connect.timeout", connectTimeout);
150

  
151
			requestContext.put("com.sun.xml.ws.request.timeout", requestTimeout);
152
			requestContext.put("com.sun.xml.ws.connect.timeout", connectTimeout);
153

  
154
			requestContext.put("javax.xml.ws.client.receiveTimeout", requestTimeout);
155
			requestContext.put("javax.xml.ws.client.connectionTimeout", connectTimeout);
156
		}
157

  
158
		return service;
159
	}
160

  
161
	/**
162
	 * 
163
	 * @param stringEpr
164
	 * @param pageSize
165
	 * @return
166
	 */
167
	@Override
168
	public IterableResultSetClient getClient(String stringEpr, int pageSize) {
169
		return getClient(eprUtils.getEpr(stringEpr), pageSize);
170
	}
171

  
172
	/**
173
	 * 
174
	 * @param stringEpr
175
	 * @return
176
	 */
177
	@Override
178
	public IterableResultSetClient getClient(String stringEpr) {
179
		return getClient(eprUtils.getEpr(stringEpr));
180
	}
181

  
182
	@Required
183
	public void setServiceResolver(ServiceResolver serviceResolver) {
184
		this.serviceResolver = serviceResolver;
185
	}
186

  
187
	@Required
188
	public void setEprUtils(EPRUtils eprUtils) {
189
		this.eprUtils = eprUtils;
190
	}
191

  
192
	public int getPageSize() {
193
		return pageSize;
194
	}
195

  
196
	@Required
197
	public void setPageSize(int pageSize) {
198
		this.pageSize = pageSize;
199
	}
200

  
201
	public long getTimeout() {
202
		return timeout;
203
	}
204

  
205
	@Required
206
	public void setTimeout(long timeout) {
207
		this.timeout = timeout;
208
	}
209

  
210
	public long getConnectTimeout() {
211
		return connectTimeout;
212
	}
213

  
214
	public void setConnectTimeout(final long connectTimeout) {
215
		this.connectTimeout = connectTimeout;
216
	}
217
}
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/src/main/java/eu/dnetlib/enabling/resultset/client/ResultSetPageProvider.java
1
package eu.dnetlib.enabling.resultset.client;
2

  
3
import java.util.List;
4
import java.util.NoSuchElementException;
5

  
6
import org.apache.commons.logging.Log;
7
import org.apache.commons.logging.LogFactory;
8

  
9
import eu.dnetlib.enabling.resultset.client.utils.ResultSetRuntimeException;
10
import eu.dnetlib.enabling.resultset.client.utils.ResultSetTimeoutException;
11
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
12
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
13

  
14
/**
15
 * 
16
 * @author claudio
17
 * 
18
 */
19
public class ResultSetPageProvider {
20

  
21
	/**
22
	 * logger
23
	 */
24
	private static final Log log = LogFactory.getLog(ResultSetPageProvider.class);
25

  
26
	/**
27
	 * the resultset service
28
	 */
29
	private final ResultSetService resultSet;
30

  
31
	/**
32
	 * the resultset id
33
	 */
34
	private final String rsId;
35

  
36
	/**
37
	 * positional value of the getResult requests
38
	 */
39
	private int fromPosition;
40

  
41
	/**
42
	 * positional value of the getResult requests
43
	 */
44
	private int toPosition;
45

  
46
	/**
47
	 * actual page size
48
	 */
49
	private int pageSize;
50

  
51
	/**
52
	 * default page size
53
	 */
54
	private static int DEFAULT_PAGE_SIZE = 10;
55

  
56
	/**
57
	 * default max waiting time
58
	 */
59
	private static long DEFAULT_MAX_WAIT_TIME = 30000;
60

  
61
	/**
62
	 * actual max waiting time
63
	 */
64
	private long maxWaitTime;
65

  
66
	/**
67
	 * current wait time
68
	 */
69
	private long waitTime;
70

  
71
	/**
72
	 * request counter used to calculate the waitTime
73
	 */
74
	private int delayCount;
75

  
76
	/**
77
	 * resultset status
78
	 */
79
	private String RSStatus;
80

  
81
	/**
82
	 * number of elements in the resultset
83
	 */
84
	private int numberOfElements;
85

  
86
	private final static String RS_CLOSED = "closed";
87

  
88
	private final static String RS_OPEN = "open";
89

  
90
	/**
91
	 * 
92
	 * @param resultSet
93
	 * @param rsId
94
	 * @throws ResultSetException
95
	 */
96
	public ResultSetPageProvider(final ResultSetService resultSet, final String rsId) throws ResultSetRuntimeException {
97

  
98
		this.resultSet = resultSet;
99
		this.rsId = rsId;
100
		this.pageSize = DEFAULT_PAGE_SIZE;
101
		this.maxWaitTime = DEFAULT_MAX_WAIT_TIME;
102
		fromPosition = toPosition = 0;
103
		delayCount = 0;
104
		waitTime = 0;
105
		updateResultSetStatus();
106
	}
107

  
108
	public ResultSetPageProvider(final ResultSetService resultSet, final String rsId, final int pageSize) throws ResultSetRuntimeException {
109

  
110
		this(resultSet, rsId);
111
		this.pageSize = pageSize;
112
	}
113

  
114
	/**
115
	 * 
116
	 * @return
117
	 * @throws ResultSetTimeoutException
118
	 * @throws ResultSetException
119
	 */
120
	public List<String> nextPage() throws ResultSetTimeoutException, ResultSetRuntimeException {
121
		do {
122
			updateResultSetStatus();
123
			int availableElements = numberOfElements - toPosition;
124
			log.debug("availableElements: " + availableElements);
125
			if (availableElements > 0) {
126
				fromPosition = toPosition + 1;
127
				if (availableElements < pageSize) {
128
					toPosition = (fromPosition + availableElements) - 1;
129
				} else {
130
					toPosition = (fromPosition + pageSize) - 1;
131
					delayCount = 0;
132
				}
133
				log.debug(" - getting result from " + fromPosition + " to " + toPosition + ", numberOfElements: " + numberOfElements + ", availableElements: "
134
						+ availableElements);
135
				try {
136
					return resultSet.getResult(rsId, fromPosition, toPosition, "waiting");
137
				} catch (ResultSetException e) {
138
					log.info(e);
139
					throw new NoSuchElementException(e.getMessage());
140
				}
141
			}
142
			if (RSStatus.equals(RS_CLOSED) && (availableElements == 0)) return null;
143
			else {
144
				stopAndWait(++delayCount);
145
			}
146
		} while (true);
147
	}
148

  
149
	/**
150
	 * 
151
	 * @param delayCount
152
	 * @throws ResultSetTimeoutException
153
	 */
154
	private void stopAndWait(final int delayCount) throws ResultSetTimeoutException {
155
		try {
156

  
157
			waitTime = (long) ((Math.pow(1.2, 10 + delayCount) * 10L)) + 200;
158
			// waitTime = (long) Math.exp(delayCount);
159
			if (waitTime > maxWaitTime) {
160
				log.warn("Timeout getting elements from resultset: " + waitTime + ". next poll would wait more than " + maxWaitTime + " ms");
161
				throw new ResultSetTimeoutException("Timeout getting elements from resultset: next poll would wait more than " + maxWaitTime + " ms");
162
			}
163
			log.debug("resultset client is going to sleep for: " + waitTime);
164
			// System.out.println("resultset client is going to sleep for: " + waitTime);
165

  
166
			Thread.sleep(waitTime);
167
		} catch (InterruptedException e) {
168
			log.error("resultSetClient got InterruptedException", e);
169
		}
170
	}
171

  
172
	/**
173
	 * updates the
174
	 * 
175
	 * @throws ResultSetException
176
	 */
177
	private void updateResultSetStatus() throws ResultSetRuntimeException {
178

  
179
		try {
180
			RSStatus = resultSet.getRSStatus(rsId);
181
			numberOfElements = resultSet.getNumberOfElements(rsId);
182
			// System.out.println("updateResultSetStatus: size is " + numberOfElements + " and status is " + RSStatus);
183
		} catch (ResultSetException e) {
184
			log.warn(e);
185
			throw new ResultSetRuntimeException(e);
186
		}
187
	}
188

  
189
	public int getPageSize() {
190
		return pageSize;
191
	}
192

  
193
	public void setPageSize(final int pageSize) {
194
		if (pageSize <= 0) throw new IllegalArgumentException("parameter 'pageSize' must be grater than zero");
195
		this.pageSize = pageSize;
196
	}
197

  
198
	@Deprecated
199
	public boolean hasElements() throws ResultSetRuntimeException {
200
		updateResultSetStatus();
201
		if (RSStatus.equals(RS_OPEN)) return true;
202
		if (RSStatus.equals(RS_CLOSED) && (numberOfElements == 0)) return false;
203
		return true;
204
	}
205

  
206
	public void setMaxWaitTime(final long maxWaitTime) {
207
		if (maxWaitTime <= 0) throw new IllegalArgumentException("parameter 'maxWaitTime' must be grater than zero");
208
		this.maxWaitTime = maxWaitTime;
209
	}
210

  
211
}
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/src/main/java/eu/dnetlib/enabling/resultset/client/ResultSetClientIterator.java
1
package eu.dnetlib.enabling.resultset.client;
2

  
3
import java.util.Iterator;
4
import java.util.LinkedList;
5
import java.util.List;
6
import java.util.NoSuchElementException;
7
import java.util.Queue;
8

  
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11

  
12
import eu.dnetlib.enabling.resultset.client.utils.ResultSetRuntimeException;
13
import eu.dnetlib.enabling.resultset.client.utils.ResultSetTimeoutException;
14
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
15
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
16

  
17
/**
18
 * 
19
 * @author claudio
20
 *
21
 */
22
public class ResultSetClientIterator implements Iterator<String> {
23

  
24
	/**
25
	 * logger
26
	 */
27
	private static final Log log = LogFactory.getLog(ResultSetClientIterator.class);
28
	
29
	/**
30
	 * the page source 
31
	 */
32
	private ResultSetPageProvider pageProvider;
33
	
34
	/**
35
	 * buffer used to provide a single element for the next() method
36
	 */
37
	private Queue<String> buffer;
38
	
39

  
40
	public ResultSetClientIterator(ResultSetService resultSet, String rsId) 
41
		throws ResultSetRuntimeException {
42
		
43
		pageProvider = new ResultSetPageProvider(resultSet, rsId);
44
		buffer = new LinkedList<String>();
45
	}
46
	
47
	public ResultSetClientIterator(ResultSetService resultSet, String rsId, int pageSize) {
48
		this(resultSet, rsId);
49
		pageProvider.setPageSize(pageSize);
50
	}
51
	
52
	public ResultSetClientIterator(ResultSetService resultSet, String rsId, int pageSize, long timeout) {
53
		this(resultSet, rsId, pageSize);
54
		pageProvider.setMaxWaitTime(timeout);
55
	}
56
	
57
	/**
58
	 * Tries to refill the buffer with a nextPage()
59
	 * @return true if the buffer was filled successfully, false otherwise
60
	 * @throws ResultSetTimeoutException in case of timeout
61
	 * @throws ResultSetException 
62
	 * @throws ResultSetException 
63
	 */
64
	private boolean refillBuffer() throws ResultSetTimeoutException, ResultSetRuntimeException {
65
		List<String> page = pageProvider.nextPage();
66
		if (page != null && !page.isEmpty()) {
67
			buffer.addAll(page);
68
			return true;
69
		}
70
		return false;
71
	}
72
	
73
	@Override
74
	public boolean hasNext() {
75
		if (!buffer.isEmpty())
76
			return true;
77
		return refillBuffer();
78
	}
79

  
80
	@Override
81
	public String next() {
82
		if (!hasNext()) {
83
			log.info("NoSuchElementException");
84
			throw new NoSuchElementException();
85
		}
86
		return buffer.poll();
87
	}
88

  
89
	@Override
90
	public void remove() {
91
		throw new UnsupportedOperationException();
92
	}
93

  
94
}
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/src/main/java/eu/dnetlib/enabling/resultset/client/ResultSetClient.java
1
package eu.dnetlib.enabling.resultset.client;
2

  
3

  
4
import javax.xml.ws.wsaddressing.W3CEndpointReference;
5

  
6
/**
7
 * 
8
 * @author claudio
9
 *
10
 */
11
public interface ResultSetClient {
12

  
13
	/**
14
	 * 
15
	 * @param epr
16
	 * @return
17
	 */
18
	public IterableResultSetClient getClient(W3CEndpointReference epr);
19
	
20
	/**
21
	 * 
22
	 * @param epr
23
	 * @return
24
	 */
25
	public IterableResultSetClient getClient(String epr);
26
	
27
	/**
28
	 * 
29
	 * @param epr
30
	 * @param pageSize
31
	 * @return
32
	 */
33
	public IterableResultSetClient getClient(W3CEndpointReference epr, int pageSize);
34
	
35
	/**
36
	 * 
37
	 * @param epr
38
	 * @param pageSize
39
	 * @return
40
	 */
41
	public IterableResultSetClient getClient(String epr, int pageSize);
42
	
43
	
44
}
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/src/main/resources/eu/dnetlib/enabling/resultset/client/applicationContext-resultsetClient.properties
1
services.is.resultset.client.timeout=60000
2
services.is.resultset.client.connecttimeout=10000
3
services.is.resultset.client.pagesize=100
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/src/main/resources/eu/dnetlib/enabling/resultset/client/applicationContext-resultsetClient.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:jaxws="http://cxf.apache.org/jaxws"
4
	xmlns:sec="http://cxf.apache.org/configuration/security" xmlns:wsa="http://cxf.apache.org/ws/addressing"
5
	xmlns:p="http://www.springframework.org/schema/p" xmlns:http="http://cxf.apache.org/transports/http/configuration"
6
	xmlns:t="http://dnetlib.eu/springbeans/t" xmlns:template="http://dnetlib.eu/springbeans/template"
7
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
8
                                    http://cxf.apache.org/ws/addressing http://cxf.apache.org/schemas/ws-addr-conf.xsd
9
                                    http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd
10
                                    http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
11
                            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
12
                            http://dnetlib.eu/springbeans/template http://dnetlib.eu/springbeans/template.xsd">
13
	<!-- beans -->
14
	<bean id="resultSetClientFactory"
15
		class="eu.dnetlib.enabling.resultset.client.ResultSetClientFactory"
16
		p:serviceResolver-ref="serviceResolver" p:eprUtils-ref="eprUtils"
17
		p:pageSize="${services.is.resultset.client.pagesize}"
18
		p:timeout="${services.is.resultset.client.timeout}"
19
		p:connectTimeout="${services.is.resultset.client.connecttimeout}"/>
20

  
21
	<bean id="eprUtils" class="eu.dnetlib.enabling.resultset.client.utils.EPRUtils" />
22

  
23
</beans>
modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6/pom.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3
	<parent>
4
		<groupId>eu.dnetlib</groupId>
5
		<artifactId>dnet45-parent</artifactId>
6
		<version>1.0.0</version>
7
	</parent>
8
	<modelVersion>4.0.0</modelVersion>
9
	<groupId>eu.dnetlib</groupId>
10
	<artifactId>cnr-resultset-client</artifactId>
11
	<packaging>jar</packaging>
12
	<version>2.1.6</version>
13
	<scm>
14
		<developerConnection>scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet45/modules/cnr-resultset-client/tags/cnr-resultset-client-2.1.6</developerConnection>
15
	</scm>
16
	<dependencies>
17
		<dependency>
18
			<groupId>junit</groupId>
19
			<artifactId>junit</artifactId>
20
			<version>${junit.version}</version>
21
			<scope>test</scope>
22
		</dependency>
23
		<dependency>
24
			<groupId>eu.dnetlib</groupId>
25
			<artifactId>cnr-resultset-service</artifactId>
26
			<version>[2.0.0,3.0.0)</version>
27
		</dependency>
28
		<dependency>
29
			<groupId>org.apache.cxf</groupId>
30
			<artifactId>cxf-rt-transports-http</artifactId>
31
			<version>${cxf.version}</version>
32
		</dependency>
33
		<dependency>
34
			<groupId>org.mockito</groupId>
35
			<artifactId>mockito-core</artifactId>
36
			<version>${mockito.version}</version>
37
			<scope>test</scope>
38
		</dependency>
39
	</dependencies>
40
</project>

Also available in: Unified diff