Project

General

Profile

1
package eu.dnetlib.enabling.is.store;
2

    
3
import java.util.ArrayList;
4
import java.util.Iterator;
5
import java.util.List;
6

    
7
import javax.annotation.Resource;
8
import javax.jws.WebService;
9
import javax.xml.ws.Endpoint;
10

    
11
import com.google.common.collect.Iterators;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.quartz.Job;
15
import org.quartz.JobExecutionException;
16
import org.springframework.beans.factory.annotation.Required;
17
import org.xmldb.api.base.XMLDBException;
18

    
19
import eu.dnetlib.enabling.is.store.rmi.ISStoreException;
20
import eu.dnetlib.enabling.is.store.rmi.ISStoreService;
21
import eu.dnetlib.enabling.resultset.ResultSetFactory;
22
import eu.dnetlib.enabling.tools.AbstractBaseService;
23
import eu.dnetlib.soap.EndpointReferenceBuilder;
24
import eu.dnetlib.xml.database.XMLDatabase;
25

    
26
/**
27
 * ISStore service implementation.
28
 * 
29
 * @author marko
30
 * 
31
 */
32
@WebService(targetNamespace = "http://services.dnetlib.eu/")
33
public class ISStoreServiceImpl extends AbstractBaseService implements ISStoreService { // NOPMD by marko on 11/24/08
34

    
35
	/**
36
	 * logger.
37
	 */
38
	private static final Log log = LogFactory.getLog(ISStoreServiceImpl.class); // NOPMD by marko on 11/24/08 4:46 PM
39

    
40
	/**
41
	 * xml database used by the ISStore.
42
	 */
43
	private XMLDatabase xmlDatabase;
44

    
45
	/**
46
	 * Service endpoint.
47
	 */
48
	private Endpoint endpoint;
49

    
50
	/**
51
	 * locally created resultsets will be created through this factory.
52
	 */
53
	private ResultSetFactory resultSetFactory;
54

    
55
	/**
56
	 * injected EPR builder.
57
	 */
58
	@Resource(name = "jaxwsEndpointReferenceBuilder")
59
	private EndpointReferenceBuilder<Endpoint> eprBuilder;
60

    
61
	/**
62
	 * initializer job
63
	 */
64
	private Job contentInitializerJob;
65

    
66
	@Override
67
	public void start() {
68
		super.start();
69

    
70
		new Thread(new Runnable() {
71

    
72
			@Override
73
			public void run() {
74
				try {
75
					Thread.sleep(500);
76
					contentInitializerJob.execute(null);
77
				} catch (JobExecutionException e) {
78
					log.fatal("failed to initialize xmldb", e);
79
				} catch (InterruptedException e) {
80
					log.fatal("failed to initialize xmldb", e);
81
				}
82
			}
83
		}).start();
84

    
85
	}
86

    
87
	/**
88
	 * web service context (not really useful right now).
89
	 */
90
	// @Resource
91
	// private WebServiceContext context;
92
	/**
93
	 * {@inheritDoc}
94
	 * 
95
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#createFileColl(java.lang.String)
96
	 */
97
	@Override
98
	public boolean createFileColl(final String fileColl) throws ISStoreException {
99
		try {
100
			xmlDatabase.createCollection(fileColl);
101
		} catch (XMLDBException e) {
102
			throw new ISStoreException(e);
103
		}
104
		return true;
105
	}
106

    
107
	/**
108
	 * {@inheritDoc}
109
	 * 
110
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#deleteFileColl(java.lang.String)
111
	 */
112
	@Override
113
	public boolean deleteFileColl(final String fileColl) throws ISStoreException {
114
		try {
115
			xmlDatabase.removeCollection(fileColl);
116
		} catch (XMLDBException e) {
117
			throw new ISStoreException(e);
118
		}
119
		return true;
120
	}
121

    
122
	/**
123
	 * {@inheritDoc}
124
	 * 
125
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#deleteXML(java.lang.String, java.lang.String)
126
	 */
127
	@Override
128
	public boolean deleteXML(final String fileName, final String fileColl) throws ISStoreException {
129
		try {
130
			return xmlDatabase.remove(fileName, fileColl);
131
		} catch (XMLDBException e) {
132
			throw new ISStoreException(e);
133
		}
134
	}
135

    
136
	/**
137
	 * {@inheritDoc}
138
	 * 
139
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#executeXUpdate(java.lang.String)
140
	 */
141
	@Override
142
	public boolean executeXUpdate(final String query) throws ISStoreException {
143
		getXMLbyQuery(query);
144
		return true;
145
	}
146

    
147
	/**
148
	 * {@inheritDoc}
149
	 * 
150
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#getFileColls()
151
	 */
152
	@Override
153
	public List<String> getFileColls() throws ISStoreException {
154
		try {
155
			return xmlDatabase.listChildCollections(xmlDatabase.getRootCollection());
156
		} catch (XMLDBException e) {
157
			throw new ISStoreException(e);
158
		}
159
	}
160

    
161
	/**
162
	 * {@inheritDoc}
163
	 * 
164
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#getFileNames(java.lang.String)
165
	 */
166
	@Override
167
	public List<String> getFileNames(final String fileColl) throws ISStoreException {
168
		try {
169
			return xmlDatabase.list(fileColl);
170
		} catch (XMLDBException e) {
171
			throw new ISStoreException(e);
172
		}
173
	}
174

    
175
	/**
176
	 * {@inheritDoc}
177
	 * 
178
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#getXML(java.lang.String, java.lang.String)
179
	 */
180
	@Override
181
	public String getXML(final String fileName, final String fileColl) throws ISStoreException {
182
		try {
183
			return xmlDatabase.read(fileName, fileColl);
184
		} catch (XMLDBException e) {
185
			throw new ISStoreException(e);
186
		}
187
	}
188

    
189
	/**
190
	 * {@inheritDoc}
191
	 * 
192
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#getXMLbyQuery(java.lang.String)
193
	 */
194
	@Override
195
	public String getXMLbyQuery(final String query) throws ISStoreException {
196
		log.debug(query);
197

    
198
		try {
199
			final Iterator<String> res = xmlDatabase.xquery(query);
200
			if (res == null) return null;
201
			if (!res.hasNext()) return null;
202
			return res.next();
203
		} catch (XMLDBException e) {
204
			throw new ISStoreException(e);
205
		}
206
	}
207

    
208
	/**
209
	 * {@inheritDoc}
210
	 * 
211
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#insertXML(java.lang.String, java.lang.String, java.lang.String)
212
	 */
213
	@Override
214
	public boolean insertXML(final String fileName, final String fileColl, final String file) throws ISStoreException {
215
		try {
216
			xmlDatabase.create(fileName, fileColl, file);
217
		} catch (XMLDBException e) {
218
			throw new ISStoreException(e);
219
		}
220
		return true;
221
	}
222

    
223
	/**
224
	 * {@inheritDoc}
225
	 * 
226
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#quickSearchXML(java.lang.String)
227
	 */
228
	@Override
229
	public List<String> quickSearchXML(final String query) throws ISStoreException {
230
		log.debug(query);
231
		try {
232
			final Iterator<String> res = xmlDatabase.xquery(query);
233
			if (res == null) return new ArrayList<>();
234

    
235
			final ArrayList<String> ans = new ArrayList<String>();
236
			Iterators.addAll(ans, res);
237
			return ans;
238
		} catch (XMLDBException e) {
239
			log.fatal("searching", e);
240
			throw new ISStoreException(e);
241
		}
242
	}
243

    
244
	/**
245
	 * {@inheritDoc}
246
	 * 
247
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#reindex()
248
	 */
249
	@Override
250
	public boolean reindex() {
251
		return true;
252
	}
253

    
254
	/**
255
	 * {@inheritDoc}
256
	 * 
257
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#sync()
258
	 */
259
	@Override
260
	public boolean sync() {
261
		log.info("TEST: " + endpoint);
262

    
263
		return true;
264
	}
265

    
266
	/**
267
	 * {@inheritDoc}
268
	 * 
269
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#updateXML(java.lang.String, java.lang.String, java.lang.String)
270
	 */
271
	@Override
272
	public boolean updateXML(final String fileName, final String fileColl, final String file) throws ISStoreException {
273
		try {
274
			xmlDatabase.update(fileName, fileColl, file);
275
		} catch (XMLDBException e) {
276
			throw new ISStoreException(e);
277
		}
278
		return true;
279
	}
280

    
281
	/**
282
	 * {@inheritDoc}
283
	 * 
284
	 * @see eu.dnetlib.enabling.is.store.rmi.ISStoreService#backup()
285
	 */
286
	@Override
287
	public String backup() throws ISStoreException {
288
		try {
289
			return xmlDatabase.backup();
290
		} catch (Exception e) {
291
			throw new ISStoreException(e);
292
		}
293
	}
294

    
295
	public XMLDatabase getXmlDatabase() {
296
		return xmlDatabase;
297
	}
298

    
299
	public void setXmlDatabase(final XMLDatabase xmlDatabase) {
300
		this.xmlDatabase = xmlDatabase;
301
	}
302

    
303
	@Override
304
	public boolean isRunning() {
305
		return true;
306
	}
307

    
308
	public Endpoint getEndpoint() {
309
		return endpoint;
310
	}
311

    
312
	public void setEndpoint(final Endpoint endpoint) {
313
		this.endpoint = endpoint;
314
	}
315

    
316
	protected EndpointReferenceBuilder<Endpoint> getEprBuilder() {
317
		return eprBuilder;
318
	}
319

    
320
	protected void setEprBuilder(final EndpointReferenceBuilder<Endpoint> eprBuilder) {
321
		this.eprBuilder = eprBuilder;
322
	}
323

    
324
	public ResultSetFactory getResultSetFactory() {
325
		return resultSetFactory;
326
	}
327

    
328
	@Required
329
	public void setResultSetFactory(final ResultSetFactory resultSetFactory) {
330
		this.resultSetFactory = resultSetFactory;
331
	}
332

    
333
	public Job getContentInitializerJob() {
334
		return contentInitializerJob;
335
	}
336

    
337
	public void setContentInitializerJob(final Job contentInitializerJob) {
338
		this.contentInitializerJob = contentInitializerJob;
339
	}
340

    
341
}
(1-1/2)