Project

General

Profile

« Previous | Next » 

Revision 29074

cleaning

View differences:

modules/dnet-index-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/dnet-index-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/dnet-index-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/dnet-index-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/dnet-index-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 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/dnet-index-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(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

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

  
173
	/**
174
	 * updates the
175
	 * 
176
	 * @throws ResultSetException
177
	 */
178
	private void updateResultSetStatus() throws ResultSetRuntimeException {
179
		
180
		try {
181
			RSStatus = resultSet.getRSStatus(rsId);
182
			numberOfElements = resultSet.getNumberOfElements(rsId);
183
			//System.out.println("updateResultSetStatus: size is " + numberOfElements + " and status is " + RSStatus);
184
		} catch (ResultSetException e) {
185
			log.warn(e);
186
			throw new ResultSetRuntimeException(e);
187
		}
188
	}
189

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

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

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

  
210
	public void setMaxWaitTime(long maxWaitTime) {
211
		if (maxWaitTime <= 0)
212
			throw new IllegalArgumentException("parameter 'maxWaitTime' must be grater than zero");
213
		this.maxWaitTime = maxWaitTime;
214
	}
215

  
216
}
modules/dnet-index-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/dnet-index-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
}

Also available in: Unified diff