Project

General

Profile

« Previous | Next » 

Revision 45119

codebase used to migrate to java8 the production system

View differences:

modules/cnr-resultset-client/trunk/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-resultset-client/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-resultset-client"}
modules/cnr-resultset-client/trunk/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.runners.MockitoJUnit44Runner;
18
import org.mockito.stubbing.Answer;
19

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

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

  
25
@RunWith(MockitoJUnit44Runner.class)
26
public class ResultSetPageProviderTest {
27

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

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

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

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

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

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

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

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

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

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

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

  
117
}
modules/cnr-resultset-client/trunk/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/trunk/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/trunk/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/trunk/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/trunk/src/main/java/eu/dnetlib/enabling/resultset/client/ResultSetClientFactory.java
1
package eu.dnetlib.enabling.resultset.client;
2

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

  
5
import eu.dnetlib.enabling.resultset.client.utils.EPRUtils;
6
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
7
import eu.dnetlib.enabling.tools.ServiceResolver;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.apache.cxf.endpoint.Client;
11
import org.apache.cxf.frontend.ClientProxy;
12
import org.apache.cxf.transport.http.HTTPConduit;
13
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
14
import org.springframework.beans.factory.annotation.Required;
15

  
16
/**
17
 * 
18
 * @author claudio
19
 * 
20
 */
21
public class ResultSetClientFactory implements ResultSetClient {
22

  
23
	/**
24
	 * logger
25
	 */
26
	private static final Log log = LogFactory.getLog(ResultSetClientFactory.class);
27

  
28
	private final static long DEFAULT_CONNECT_TIMEOUT = 10000;
29

  
30
	private final static long DEFAULT_REQUEST_TIMEOUT = 60000;
31

  
32
	private final static int DEFAULT_PAGE_SIZE = 100;
33

  
34
	/**
35
	 * used to resolve the epr references to the service endpoint
36
	 */
37
	private ServiceResolver serviceResolver;
38

  
39
	/**
40
	 * utility object
41
	 */
42
	private EPRUtils eprUtils;
43

  
44
	/**
45
	 * actual page size
46
	 */
47
	private int pageSize;
48

  
49
	/**
50
	 * request timeout
51
	 */
52
	private long timeout;
53

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

  
59
	public ResultSetClientFactory() {
60
		this(DEFAULT_PAGE_SIZE, DEFAULT_REQUEST_TIMEOUT, DEFAULT_CONNECT_TIMEOUT);
61
	}
62

  
63
	/**
64
	 * 
65
	 * @param pageSize
66
	 * @param timeout
67
	 * @throws IllegalArgumentException
68
	 */
69
	public ResultSetClientFactory(int pageSize, long timeout) throws IllegalArgumentException {
70
		this(pageSize, timeout, DEFAULT_CONNECT_TIMEOUT);
71
	}
72

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

  
93
	/**
94
	 * 
95
	 * @param epr
96
	 * @param pageSize
97
	 * @return
98
	 */
99
	@Override
100
	public IterableResultSetClient getClient(W3CEndpointReference epr, int pageSize) {
101
		final ResultSetService resultSet = getResultSetService(epr, getConnectTimeout(), getTimeout());
102
		final String rsId = serviceResolver.getResourceIdentifier(epr);
103

  
104
		//using given pageSize and default timeout
105
		return new IterableResultSetClient(resultSet, rsId, pageSize, getTimeout());
106
	}
107

  
108
	/**
109
	 * 
110
	 * @param epr
111
	 * @return
112
	 */
113
	@Override
114
	public IterableResultSetClient getClient(W3CEndpointReference epr) {
115
		final ResultSetService resultSet = getResultSetService(epr, getConnectTimeout(), getTimeout());
116
		final String rsId = serviceResolver.getResourceIdentifier(epr);
117

  
118
		//using default pageSize and timeouts
119
		return new IterableResultSetClient(resultSet, rsId, getPageSize(), getTimeout());
120
	}
121

  
122
	private ResultSetService getResultSetService(final W3CEndpointReference epr, final long connectTimeout, final long requestTimeout) {
123
		final ResultSetService service = serviceResolver.getService(ResultSetService.class, epr);
124

  
125
		if (log.isDebugEnabled()) {
126
			log.debug(String.format("creting resultSet service stub with connectTimeout(%s), requestTimeout(%s)", connectTimeout, requestTimeout));
127
		}
128

  
129
		if(service instanceof Client) {
130
			final Client client = ClientProxy.getClient(service);
131
			final HTTPConduit http = (HTTPConduit) client.getConduit();
132
			final HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
133

  
134
			httpClientPolicy.setConnectionTimeout(connectTimeout);
135
			httpClientPolicy.setAllowChunking(false);
136
			httpClientPolicy.setReceiveTimeout(requestTimeout);
137

  
138
			http.setClient(httpClientPolicy);
139
		}
140
		return service;
141
	}
142

  
143
	/**
144
	 * 
145
	 * @param stringEpr
146
	 * @param pageSize
147
	 * @return
148
	 */
149
	@Override
150
	public IterableResultSetClient getClient(String stringEpr, int pageSize) {
151
		return getClient(eprUtils.getEpr(stringEpr), pageSize);
152
	}
153

  
154
	/**
155
	 * 
156
	 * @param stringEpr
157
	 * @return
158
	 */
159
	@Override
160
	public IterableResultSetClient getClient(String stringEpr) {
161
		return getClient(eprUtils.getEpr(stringEpr));
162
	}
163

  
164
	@Required
165
	public void setServiceResolver(ServiceResolver serviceResolver) {
166
		this.serviceResolver = serviceResolver;
167
	}
168

  
169
	@Required
170
	public void setEprUtils(EPRUtils eprUtils) {
171
		this.eprUtils = eprUtils;
172
	}
173

  
174
	public int getPageSize() {
175
		return pageSize;
176
	}
177

  
178
	@Required
179
	public void setPageSize(int pageSize) {
180
		this.pageSize = pageSize;
181
	}
182

  
183
	public long getTimeout() {
184
		return timeout;
185
	}
186

  
187
	@Required
188
	public void setTimeout(long timeout) {
189
		this.timeout = timeout;
190
	}
191

  
192
	public long getConnectTimeout() {
193
		return connectTimeout;
194
	}
195

  
196
	public void setConnectTimeout(final long connectTimeout) {
197
		this.connectTimeout = connectTimeout;
198
	}
199
}
modules/cnr-resultset-client/trunk/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/trunk/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/trunk/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/trunk/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/trunk/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/trunk/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>dnet-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.2-SNAPSHOT</version>
13
	<scm>
14
		<developerConnection>scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/cnr-resultset-client/trunk</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.mockito</groupId>
30
			<artifactId>mockito-core</artifactId>
31
			<version>1.6</version>
32
			<scope>test</scope>
33
		</dependency>
34
	</dependencies>
35
</project>
modules/cnr-resultset-client/tags/cnr-resultset-client-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-resultset-client/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-resultset-client"}
modules/cnr-resultset-client/tags/cnr-resultset-client-1.0.0/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.runners.MockitoJUnit44Runner;
18
import org.mockito.stubbing.Answer;
19

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

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

  
25
@RunWith(MockitoJUnit44Runner.class)
26
public class ResultSetPageProviderTest {
27

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

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

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

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

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

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

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

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

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

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

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

  
117
}
modules/cnr-resultset-client/tags/cnr-resultset-client-1.0.0/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-1.0.0/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-1.0.0/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-1.0.0/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-1.0.0/src/main/java/eu/dnetlib/enabling/resultset/client/ResultSetClientFactory.java
1
package eu.dnetlib.enabling.resultset.client;
2

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

  
5
import org.apache.commons.logging.Log;
6
import org.apache.commons.logging.LogFactory;
7
import org.springframework.beans.factory.annotation.Required;
8

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

  
13
/**
14
 * 
15
 * @author claudio
16
 * 
17
 */
18
public class ResultSetClientFactory implements ResultSetClient {
19

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

  
25
	/**
26
	 * used to resolve the epr references to the service endpoint
27
	 */
28
	private ServiceResolver serviceResolver;
29

  
30
	/**
31
	 * utility object
32
	 */
33
	private EPRUtils eprUtils;
34

  
35
	/**
36
	 * actual page size
37
	 */
38
	private int pageSize;
39

  
40
	/**
41
	 * actual timeout
42
	 */
43
	private long timeout;
44

  
45
	public ResultSetClientFactory() {
46
		log.info("creating new ResultSetClientIterableFactory with default pageSize and timeout");
47
	}
48

  
49
	/**
50
	 * 
51
	 * @param pageSize
52
	 * @param timeout
53
	 * @throws IllegalArgumentException
54
	 */
55
	public ResultSetClientFactory(int pageSize, long timeout) throws IllegalArgumentException {
56
		if (pageSize <= 0 || timeout <= 0)
57
			throw new IllegalArgumentException("parameters pageSize and timeout must be greater than zero");
58
		this.pageSize = pageSize;
59
		this.timeout = timeout;
60
	}
61

  
62
	/**
63
	 * 
64
	 * @param epr
65
	 * @param pageSize
66
	 * @return
67
	 */
68
	@Override
69
	public IterableResultSetClient getClient(W3CEndpointReference epr, int pageSize) {
70
		final ResultSetService resultSet = serviceResolver.getService(ResultSetService.class, epr);
71
		final String rsId = serviceResolver.getResourceIdentifier(epr);
72

  
73
		//using given pageSize and default timeout
74
		return new IterableResultSetClient(resultSet, rsId, pageSize, timeout);
75
	}
76

  
77
	/**
78
	 * 
79
	 * @param epr
80
	 * @return
81
	 */
82
	@Override
83
	public IterableResultSetClient getClient(W3CEndpointReference epr) {
84
		final ResultSetService resultSet = serviceResolver.getService(ResultSetService.class, epr);
85
		final String rsId = serviceResolver.getResourceIdentifier(epr);
86

  
87
		//using default pageSize and timeout
88
		return new IterableResultSetClient(resultSet, rsId, pageSize, timeout);
89
	}
90

  
91
	/**
92
	 * 
93
	 * @param stringEpr
94
	 * @param pageSize
95
	 * @return
96
	 */
97
	@Override
98
	public IterableResultSetClient getClient(String stringEpr, int pageSize) {
99
		return getClient(eprUtils.getEpr(stringEpr), pageSize);
100
	}
101

  
102
	/**
103
	 * 
104
	 * @param stringEpr
105
	 * @return
106
	 */
107
	@Override
108
	public IterableResultSetClient getClient(String stringEpr) {
109
		return getClient(eprUtils.getEpr(stringEpr));
110
	}
111

  
112
	@Required
113
	public void setServiceResolver(ServiceResolver serviceResolver) {
114
		this.serviceResolver = serviceResolver;
115
	}
116

  
117
	@Required
118
	public void setEprUtils(EPRUtils eprUtils) {
119
		this.eprUtils = eprUtils;
120
	}
121

  
122
	public int getPageSize() {
123
		return pageSize;
124
	}
125

  
126
	@Required
127
	public void setPageSize(int pageSize) {
128
		this.pageSize = pageSize;
129
	}
130

  
131
	public long getTimeout() {
132
		return timeout;
133
	}
134

  
135
	@Required
136
	public void setTimeout(long timeout) {
137
		this.timeout = timeout;
138
	}
139

  
140
}
modules/cnr-resultset-client/tags/cnr-resultset-client-1.0.0/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(ResultSetService resultSet, 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(ResultSetService resultSet, String rsId, 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.info(" - getting result from " + fromPosition + " to " + toPosition + ", numberOfElements: " + numberOfElements + ", availableElements: "+ availableElements);
134
				//System.out.println(" - getting result from " + fromPosition + " to " + toPosition + ", numberOfElements: " + numberOfElements + ", availableElements: "+ 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))
143
			if (RSStatus.equals(RS_CLOSED) && availableElements == 0)
144
				return null;
145
			else
146
				stopAndWait(++delayCount);
147
		} while (true);
148
	}
149

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

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff