Revision 45508
Added by Michele Artini over 7 years ago
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/tools/BulkResourceImporter.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.util.Date; |
|
5 |
|
|
6 |
import org.apache.commons.io.FilenameUtils; |
|
7 |
import org.apache.commons.io.IOUtils; |
|
8 |
import org.apache.commons.logging.Log; |
|
9 |
import org.apache.commons.logging.LogFactory; |
|
10 |
import org.dom4j.Document; |
|
11 |
import org.dom4j.io.SAXReader; |
|
12 |
import org.springframework.beans.factory.annotation.Autowired; |
|
13 |
import org.springframework.beans.factory.annotation.Value; |
|
14 |
import org.springframework.context.ResourceLoaderAware; |
|
15 |
import org.springframework.core.io.Resource; |
|
16 |
import org.springframework.core.io.ResourceLoader; |
|
17 |
import org.springframework.core.io.support.ResourcePatternUtils; |
|
18 |
|
|
19 |
import eu.dnetlib.DnetConstants; |
|
20 |
import eu.dnetlib.enabling.is.DnetInformationServiceException; |
|
21 |
import eu.dnetlib.enabling.is.registry.schema.ProfileValidator; |
|
22 |
import eu.dnetlib.enabling.is.store.ISStore; |
|
23 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
24 |
|
|
25 |
public class BulkResourceImporter implements ResourceLoaderAware { |
|
26 |
|
|
27 |
static final Log log = LogFactory.getLog(BulkResourceImporter.class); |
|
28 |
|
|
29 |
private static boolean initialized = false; |
|
30 |
|
|
31 |
@Autowired |
|
32 |
private XQueryUtils xqueryUtils; |
|
33 |
|
|
34 |
@Autowired |
|
35 |
private ProfileValidator profileValidator; |
|
36 |
|
|
37 |
@Autowired |
|
38 |
private ISStore isStore; |
|
39 |
|
|
40 |
@Value("TODO") |
|
41 |
private String bootstrapDir; |
|
42 |
|
|
43 |
private ResourceLoader resourceLoader; |
|
44 |
|
|
45 |
public void startImport() throws DnetInformationServiceException { |
|
46 |
if (isInitialized()) { return; } |
|
47 |
|
|
48 |
log.info("Initializing store with some profiles and schemas ..."); |
|
49 |
|
|
50 |
log.info("Loading profiles and schemas from " + bootstrapDir); |
|
51 |
|
|
52 |
try { |
|
53 |
|
|
54 |
final long start = System.currentTimeMillis(); |
|
55 |
|
|
56 |
for (final Resource res : ResourcePatternUtils.getResourcePatternResolver(getResourceLoader()).getResources(bootstrapDir + "/**/*.xsd")) { |
|
57 |
registerSchema(res); |
|
58 |
} |
|
59 |
|
|
60 |
for (final Resource res : ResourcePatternUtils.getResourcePatternResolver(getResourceLoader()).getResources(bootstrapDir + "/**/*.xml")) { |
|
61 |
registerProfile(res); |
|
62 |
} |
|
63 |
|
|
64 |
log.info("bulk registration finished in: " + ((System.currentTimeMillis() - start) / 1000) + "s"); |
|
65 |
|
|
66 |
} catch (final IOException e) { |
|
67 |
throw new DnetInformationServiceException(e); |
|
68 |
} finally { |
|
69 |
log.info("INITIALIZED"); |
|
70 |
setInitialized(true); |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
private void registerSchema(final Resource res) throws DnetInformationServiceException { |
|
75 |
final String resourceType = FilenameUtils.getBaseName(res.getFilename()); |
|
76 |
try { |
|
77 |
isStore.insertXML(resourceType, xqueryUtils.getRootCollection() + DnetConstants.RESOURCE_TYPES, IOUtils.toString(res.getInputStream())); |
|
78 |
log.info("new schema imported: " + resourceType); |
|
79 |
} catch (final Throwable e) { |
|
80 |
log.error("error importing schema " + res.getFilename(), e); |
|
81 |
throw new DnetInformationServiceException(e); |
|
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
private void registerProfile(final Resource res) throws DnetInformationServiceException { |
|
86 |
log.debug("registering profile"); |
|
87 |
|
|
88 |
try { |
|
89 |
final Document doc = (new SAXReader()).read(res.getInputStream()); |
|
90 |
|
|
91 |
final String id = doc.valueOf("//RESOURCE_IDENTIFIER/@value"); |
|
92 |
final String type = doc.valueOf("//RESOURCE_TYPE/@value"); |
|
93 |
|
|
94 |
final String coll = xqueryUtils.getCollectionPath(doc); |
|
95 |
final String fileName = xqueryUtils.getFileName(id); |
|
96 |
final String date = new DateUtils(new Date()).getDateAsISO8601String(); |
|
97 |
|
|
98 |
doc.selectSingleNode("//DATE_OF_CREATION/@value").setText(date); |
|
99 |
|
|
100 |
final String finalDoc = doc.asXML(); |
|
101 |
|
|
102 |
if (profileValidator.isValid(type, finalDoc)) { |
|
103 |
isStore.insertXML(fileName, xqueryUtils.getRootCollection() + coll, finalDoc); |
|
104 |
} else { |
|
105 |
throw new DnetInformationServiceException("Invalid profile"); |
|
106 |
} |
|
107 |
log.info("new profile imported: " + xqueryUtils.getRootCollection() + coll + fileName); |
|
108 |
} catch (final Throwable e) { |
|
109 |
log.error("error importing profile " + res.getFilename()); |
|
110 |
throw new DnetInformationServiceException(e); |
|
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
public boolean isInitialized() { |
|
115 |
return initialized; |
|
116 |
} |
|
117 |
|
|
118 |
public void setInitialized(final boolean initialized) { |
|
119 |
BulkResourceImporter.initialized = initialized; |
|
120 |
} |
|
121 |
|
|
122 |
public ResourceLoader getResourceLoader() { |
|
123 |
return resourceLoader; |
|
124 |
} |
|
125 |
|
|
126 |
@Override |
|
127 |
public void setResourceLoader(final ResourceLoader resourceLoader) { |
|
128 |
this.resourceLoader = resourceLoader; |
|
129 |
} |
|
130 |
|
|
131 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/tools/XQueryUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.tools; |
|
2 |
|
|
3 |
import org.dom4j.Document; |
|
4 |
import org.springframework.stereotype.Component; |
|
5 |
|
|
6 |
import eu.dnetlib.miscutils.functional.hash.Hashing; |
|
7 |
|
|
8 |
@Component |
|
9 |
public class XQueryUtils { |
|
10 |
|
|
11 |
public String getCollectionAbsPath(final Document doc) { |
|
12 |
return getRootCollection() + getCollectionPath(doc); |
|
13 |
} |
|
14 |
|
|
15 |
public String getCollectionPath(final Document doc) { |
|
16 |
final StringBuilder buffer = new StringBuilder(); |
|
17 |
buffer.append(doc.valueOf("//RESOURCE_KIND/@value")); |
|
18 |
buffer.append('/'); |
|
19 |
buffer.append(doc.valueOf("//RESOURCE_TYPE/@value")); |
|
20 |
return buffer.toString(); |
|
21 |
} |
|
22 |
|
|
23 |
public String getRootCollection() { |
|
24 |
return "/db/DRIVER/"; |
|
25 |
} |
|
26 |
|
|
27 |
public String getFileName(final Document doc) { |
|
28 |
return doc.valueOf("//RESOURCE_IDENTIFIER/@value").split("_")[0]; |
|
29 |
} |
|
30 |
|
|
31 |
public String getCollectionName(final String resId) { |
|
32 |
final String[] components = resId.split("_"); |
|
33 |
if (components.length == 1) { return "DefaultCollection"; } |
|
34 |
return Hashing.decodeBase64(components[1]); |
|
35 |
} |
|
36 |
|
|
37 |
public String getFileName(final String resId) { |
|
38 |
return resId.split("_")[0]; |
|
39 |
} |
|
40 |
|
|
41 |
public String createResourceId(final String fileName, final String coll) { |
|
42 |
return fileName + "_" + Hashing.encodeBase64(coll); |
|
43 |
} |
|
44 |
|
|
45 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/DnetInformationServiceException.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by sandro on 3/16/16. |
|
5 |
*/ |
|
6 |
public class DnetInformationServiceException extends Exception { |
|
7 |
|
|
8 |
/** |
|
9 |
* |
|
10 |
*/ |
|
11 |
private static final long serialVersionUID = 1L; |
|
12 |
|
|
13 |
public DnetInformationServiceException() { |
|
14 |
super(); |
|
15 |
} |
|
16 |
|
|
17 |
public DnetInformationServiceException(final String message) { |
|
18 |
super(message); |
|
19 |
} |
|
20 |
|
|
21 |
public DnetInformationServiceException(final Throwable e) { |
|
22 |
super(e); |
|
23 |
} |
|
24 |
|
|
25 |
public DnetInformationServiceException(final String message, final Throwable cause) { |
|
26 |
super(message, cause); |
|
27 |
} |
|
28 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/controller/ISRegistryController.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.controller; // NOPMD |
|
2 |
|
|
3 |
import java.io.StringReader; |
|
4 |
import java.util.Date; |
|
5 |
import java.util.UUID; |
|
6 |
|
|
7 |
import org.apache.commons.logging.Log; |
|
8 |
import org.apache.commons.logging.LogFactory; |
|
9 |
import org.dom4j.Document; |
|
10 |
import org.dom4j.DocumentException; |
|
11 |
import org.dom4j.io.SAXReader; |
|
12 |
import org.springframework.beans.factory.annotation.Autowired; |
|
13 |
import org.springframework.beans.factory.annotation.Required; |
|
14 |
import org.springframework.web.bind.annotation.PathVariable; |
|
15 |
import org.springframework.web.bind.annotation.RequestBody; |
|
16 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
17 |
import org.springframework.web.bind.annotation.RequestMethod; |
|
18 |
import org.springframework.web.bind.annotation.RequestParam; |
|
19 |
import org.springframework.web.bind.annotation.RestController; |
|
20 |
|
|
21 |
import eu.dnetlib.DnetConstants; |
|
22 |
import eu.dnetlib.enabling.annotations.DnetService; |
|
23 |
import eu.dnetlib.enabling.annotations.DnetServiceType; |
|
24 |
import eu.dnetlib.enabling.is.DnetInformationServiceException; |
|
25 |
import eu.dnetlib.enabling.is.registry.schema.ProfileValidator; |
|
26 |
import eu.dnetlib.enabling.is.store.ISStore; |
|
27 |
import eu.dnetlib.enabling.tools.XQueryUtils; |
|
28 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
29 |
import eu.dnetlib.miscutils.functional.hash.Hashing; |
|
30 |
import eu.dnetlib.services.BaseService; |
|
31 |
|
|
32 |
@RestController("/register") |
|
33 |
@DnetService(DnetServiceType.IS_REGISTRY) |
|
34 |
public class ISRegistryController extends BaseService { |
|
35 |
|
|
36 |
private static final Log log = LogFactory.getLog(ISRegistryController.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
37 |
|
|
38 |
@Autowired |
|
39 |
private ISStore isStore; |
|
40 |
|
|
41 |
@Autowired |
|
42 |
private ProfileValidator profileValidator; |
|
43 |
|
|
44 |
@Autowired |
|
45 |
private XQueryUtils xqueryUtils; |
|
46 |
|
|
47 |
@RequestMapping(value = "schema/{name}", method = RequestMethod.POST) |
|
48 |
public boolean registerSchema(@PathVariable("name") final String resourceType, @RequestBody final String resourceSchema) |
|
49 |
throws DnetInformationServiceException { |
|
50 |
return isStore.insertXML(resourceType, xqueryUtils.getRootCollection() + DnetConstants.RESOURCE_TYPES, resourceSchema); |
|
51 |
} |
|
52 |
|
|
53 |
@RequestMapping(value = "schema/{name}", method = RequestMethod.DELETE) |
|
54 |
public boolean deleteSchema(@PathVariable("name") final String resourceType) throws DnetInformationServiceException { |
|
55 |
return isStore.deleteXML(resourceType, xqueryUtils.getRootCollection() + DnetConstants.RESOURCE_TYPES); |
|
56 |
} |
|
57 |
|
|
58 |
@RequestMapping(value = "profile/{id}", method = RequestMethod.DELETE) |
|
59 |
public boolean deleteProfile(@PathVariable("id") final String resId) throws DnetInformationServiceException { |
|
60 |
final boolean res = isStore.deleteXML(xqueryUtils.getFileName(resId), "/db/DRIVER/" + xqueryUtils.getCollectionName(resId)); |
|
61 |
|
|
62 |
if (!res) { throw new DnetInformationServiceException("document " + resId + " not found"); } |
|
63 |
|
|
64 |
return true; |
|
65 |
} |
|
66 |
|
|
67 |
@RequestMapping(value = "profile", method = RequestMethod.POST) |
|
68 |
public String registerProfile(@RequestBody final String profile) throws DnetInformationServiceException { |
|
69 |
log.debug("registering profile"); |
|
70 |
|
|
71 |
try { |
|
72 |
|
|
73 |
final Document doc = (new SAXReader()).read(new StringReader(profile)); |
|
74 |
|
|
75 |
final String coll = xqueryUtils.getCollectionPath(doc); |
|
76 |
final String fileName = UUID.randomUUID().toString(); |
|
77 |
final String newId = fileName + "_" + Hashing.encodeBase64(coll); |
|
78 |
final String date = new DateUtils(new Date()).getDateAsISO8601String(); |
|
79 |
|
|
80 |
final String type = doc.valueOf("//RESOURCE_TYPE/@value"); |
|
81 |
doc.selectSingleNode("//RESOURCE_IDENTIFIER/@value").setText(newId); |
|
82 |
doc.selectSingleNode("//DATE_OF_CREATION/@value").setText(date); |
|
83 |
|
|
84 |
final String finalDoc = doc.asXML(); |
|
85 |
|
|
86 |
if (profileValidator.isValid(type, finalDoc)) { |
|
87 |
isStore.insertXML(fileName, xqueryUtils.getRootCollection() + coll, finalDoc); |
|
88 |
return newId; |
|
89 |
} else { |
|
90 |
throw new DnetInformationServiceException("Invalid profile"); |
|
91 |
} |
|
92 |
} catch (final DocumentException e) { |
|
93 |
throw new DnetInformationServiceException(e); |
|
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
@RequestMapping(value = "profile/{id}", method = RequestMethod.POST) |
|
98 |
public boolean updateProfile(@PathVariable("id") final String resId, @RequestBody final String profile) throws DnetInformationServiceException { |
|
99 |
try { |
|
100 |
final String fileName = xqueryUtils.getFileName(resId); |
|
101 |
final String fileColl = xqueryUtils.getRootCollection() + xqueryUtils.getCollectionName(resId); |
|
102 |
final String oldProfileSrc = isStore.getXML(fileName, fileColl); |
|
103 |
|
|
104 |
if (oldProfileSrc == null) { throw new DnetInformationServiceException("cannot update a non existing profile " + resId); } |
|
105 |
|
|
106 |
final Document doc = (new SAXReader()).read(new StringReader(profile)); |
|
107 |
final String coll = xqueryUtils.getCollectionPath(doc); |
|
108 |
final String date = new DateUtils(new Date()).getDateAsISO8601String(); |
|
109 |
|
|
110 |
final String type = doc.valueOf("//RESOURCE_TYPE/@value"); |
|
111 |
doc.selectSingleNode("//RESOURCE_IDENTIFIER/@value").setText(resId); |
|
112 |
doc.selectSingleNode("//DATE_OF_CREATION/@value").setText(date); |
|
113 |
|
|
114 |
final String finalDoc = doc.asXML(); |
|
115 |
|
|
116 |
if (profileValidator.isValid(type, finalDoc)) { |
|
117 |
isStore.insertXML(fileName, xqueryUtils.getRootCollection() + coll, finalDoc); |
|
118 |
return true; |
|
119 |
} else { |
|
120 |
throw new DnetInformationServiceException("Invalid profile"); |
|
121 |
} |
|
122 |
} catch (final DocumentException e) { |
|
123 |
throw new DnetInformationServiceException(e); |
|
124 |
} |
|
125 |
} |
|
126 |
|
|
127 |
@RequestMapping("xupdate") |
|
128 |
public boolean executeXUpdate(@RequestParam("q") final String xquery) throws DnetInformationServiceException { |
|
129 |
return isStore.executeXUpdate(xquery); |
|
130 |
} |
|
131 |
|
|
132 |
public XQueryUtils getXqueryUtils() { |
|
133 |
return xqueryUtils; |
|
134 |
} |
|
135 |
|
|
136 |
@Required |
|
137 |
public void setXqueryUtils(final XQueryUtils xqueryUtils) { |
|
138 |
this.xqueryUtils = xqueryUtils; |
|
139 |
} |
|
140 |
|
|
141 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/controller/ISLookUpController.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.controller; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.apache.commons.logging.Log; |
|
7 |
import org.apache.commons.logging.LogFactory; |
|
8 |
import org.springframework.beans.factory.annotation.Autowired; |
|
9 |
import org.springframework.http.MediaType; |
|
10 |
import org.springframework.web.bind.annotation.PathVariable; |
|
11 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
12 |
import org.springframework.web.bind.annotation.RequestParam; |
|
13 |
import org.springframework.web.bind.annotation.RestController; |
|
14 |
|
|
15 |
import eu.dnetlib.DnetConstants; |
|
16 |
import eu.dnetlib.enabling.annotations.DnetService; |
|
17 |
import eu.dnetlib.enabling.annotations.DnetServiceType; |
|
18 |
import eu.dnetlib.enabling.is.DnetInformationServiceException; |
|
19 |
import eu.dnetlib.enabling.is.store.ISStore; |
|
20 |
import eu.dnetlib.enabling.tools.XQueryUtils; |
|
21 |
import eu.dnetlib.services.BaseService; |
|
22 |
|
|
23 |
@RestController("/lookup") |
|
24 |
@DnetService(DnetServiceType.IS_LOOKUP) |
|
25 |
public class ISLookUpController extends BaseService { |
|
26 |
|
|
27 |
// private static final String DB_BASE_DIR = "/db/DRIVER"; |
|
28 |
|
|
29 |
private static final String PROFILE_NOT_FOUND = "Profile not found"; |
|
30 |
|
|
31 |
private static final Log log = LogFactory.getLog(ISLookUpController.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
32 |
|
|
33 |
@Autowired |
|
34 |
private ISStore isStore; |
|
35 |
|
|
36 |
@Autowired |
|
37 |
private XQueryUtils xqueryUtils; |
|
38 |
|
|
39 |
@RequestMapping(value = "profile/{id}", produces = MediaType.APPLICATION_XML_VALUE) |
|
40 |
public String getProfile(@PathVariable("id") final String profId) throws DnetInformationServiceException { |
|
41 |
if (StringUtils.isBlank(profId)) { throw new DnetInformationServiceException("Invalid null profile ID: " + profId); } |
|
42 |
|
|
43 |
final String res = isStore.getXML( |
|
44 |
xqueryUtils.getFileName(profId), |
|
45 |
xqueryUtils.getRootCollection() + xqueryUtils.getCollectionName(profId)); |
|
46 |
|
|
47 |
if (res == null) { throw new DnetInformationServiceException("document " + profId + " not found"); } |
|
48 |
|
|
49 |
return res; |
|
50 |
|
|
51 |
} |
|
52 |
|
|
53 |
@RequestMapping(value = "findOne", produces = MediaType.TEXT_PLAIN_VALUE) |
|
54 |
public String findOne(@RequestParam("q") final String xquery) throws DnetInformationServiceException { |
|
55 |
final String resource = isStore.getXMLbyQuery(xquery); |
|
56 |
|
|
57 |
if ((resource == null) || resource.isEmpty()) { throw new DnetInformationServiceException(PROFILE_NOT_FOUND); } |
|
58 |
|
|
59 |
return resource; |
|
60 |
} |
|
61 |
|
|
62 |
@RequestMapping(value = "schema/{name}", produces = MediaType.APPLICATION_XML_VALUE) |
|
63 |
public String getSchema(@PathVariable("name") final String resourceType) throws DnetInformationServiceException { |
|
64 |
if (StringUtils.isBlank(resourceType)) { throw new DnetInformationServiceException("Invalid resourceType"); } |
|
65 |
|
|
66 |
return isStore.getXML(resourceType, xqueryUtils.getRootCollection() + DnetConstants.RESOURCE_TYPES); |
|
67 |
} |
|
68 |
|
|
69 |
@RequestMapping("schemas") |
|
70 |
public List<String> listSchemas() throws DnetInformationServiceException { |
|
71 |
return isStore.getFileNames(xqueryUtils.getRootCollection() + "/" + DnetConstants.RESOURCE_TYPES); |
|
72 |
} |
|
73 |
|
|
74 |
@RequestMapping(value = "find", produces = MediaType.TEXT_PLAIN_VALUE) |
|
75 |
public List<String> find(@RequestParam("q") final String xquery) throws DnetInformationServiceException { |
|
76 |
return isStore.quickSearchXML(xquery); |
|
77 |
} |
|
78 |
|
|
79 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/DnetInformationServiceApplication.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is; |
|
2 |
|
|
3 |
import org.springframework.boot.SpringApplication; |
|
4 |
import org.springframework.boot.autoconfigure.SpringBootApplication; |
|
5 |
|
|
6 |
@SpringBootApplication |
|
7 |
public class DnetInformationServiceApplication { |
|
8 |
|
|
9 |
public static void main(final String[] args) { |
|
10 |
SpringApplication.run(DnetInformationServiceApplication.class, args); |
|
11 |
} |
|
12 |
|
|
13 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/registry/ResourceKindResolver.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.registry; |
|
2 |
|
|
3 |
import javax.xml.xpath.XPathExpressionException; |
|
4 |
|
|
5 |
public interface ResourceKindResolver { |
|
6 |
String getNormalKindForType(final String resourceType) throws XPathExpressionException; |
|
7 |
String getPendingKindForType(final String resourceType) throws XPathExpressionException; |
|
8 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/registry/NameBasedResourceKindResolver.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.registry; |
|
2 |
|
|
3 |
import javax.xml.xpath.XPathExpressionException; |
|
4 |
|
|
5 |
/** |
|
6 |
* Uses current driver convention and tries to guess the correct resource kinds (both normal and pending) depending on |
|
7 |
* the resource type name. |
|
8 |
* |
|
9 |
* @author marko |
|
10 |
* |
|
11 |
*/ |
|
12 |
public class NameBasedResourceKindResolver implements ResourceKindResolver { |
|
13 |
|
|
14 |
/** |
|
15 |
* {@inheritDoc} |
|
16 |
* @see eu.dnetlib.enabling.is.registry.ResourceKindResolver#getNormalKindForType(java.lang.String) |
|
17 |
*/ |
|
18 |
@Override |
|
19 |
public String getNormalKindForType(final String resourceType) throws XPathExpressionException { |
|
20 |
if (isService(resourceType)) |
|
21 |
return "ServiceResources"; |
|
22 |
return generateKind(resourceType); |
|
23 |
} |
|
24 |
|
|
25 |
/** |
|
26 |
* {@inheritDoc} |
|
27 |
* @see eu.dnetlib.enabling.is.registry.ResourceKindResolver#getPendingKindForType(java.lang.String) |
|
28 |
*/ |
|
29 |
@Override |
|
30 |
public String getPendingKindForType(final String resourceType) throws XPathExpressionException { |
|
31 |
if (isService(resourceType)) |
|
32 |
return "PendingServiceResources"; |
|
33 |
return "PendingDSResources"; |
|
34 |
} |
|
35 |
|
|
36 |
private boolean isService(final String resourceType) { |
|
37 |
return resourceType.matches(".*ServiceResourceType$"); |
|
38 |
} |
|
39 |
|
|
40 |
private String generateKind(final String resourceType) { |
|
41 |
return resourceType.replaceAll("ResourceType$", "Resources"); |
|
42 |
} |
|
43 |
|
|
44 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/registry/schema/ProfileValidator.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.registry.schema; |
|
2 |
|
|
3 |
import java.io.StringReader; |
|
4 |
|
|
5 |
import javax.xml.XMLConstants; |
|
6 |
import javax.xml.transform.stream.StreamSource; |
|
7 |
import javax.xml.validation.Schema; |
|
8 |
import javax.xml.validation.SchemaFactory; |
|
9 |
import javax.xml.validation.Validator; |
|
10 |
|
|
11 |
import org.apache.commons.lang3.StringUtils; |
|
12 |
import org.apache.commons.logging.Log; |
|
13 |
import org.apache.commons.logging.LogFactory; |
|
14 |
import org.springframework.beans.factory.annotation.Autowired; |
|
15 |
import org.springframework.stereotype.Component; |
|
16 |
import org.xml.sax.SAXException; |
|
17 |
|
|
18 |
import eu.dnetlib.DnetConstants; |
|
19 |
import eu.dnetlib.enabling.is.DnetInformationServiceException; |
|
20 |
import eu.dnetlib.enabling.is.store.ISStore; |
|
21 |
import eu.dnetlib.enabling.tools.XQueryUtils; |
|
22 |
|
|
23 |
@Component |
|
24 |
public class ProfileValidator { |
|
25 |
|
|
26 |
private static final Log log = LogFactory.getLog(ProfileValidator.class); |
|
27 |
|
|
28 |
@Autowired |
|
29 |
private ISStore isStore; |
|
30 |
|
|
31 |
@Autowired |
|
32 |
private XQueryUtils xqueryUtils; |
|
33 |
|
|
34 |
public boolean isValid(final String type, final String profile) { |
|
35 |
|
|
36 |
final Schema schema = getSchema(type); |
|
37 |
|
|
38 |
if (schema == null) { return false; } |
|
39 |
|
|
40 |
final Validator validator = schema.newValidator(); |
|
41 |
|
|
42 |
try { |
|
43 |
// hack to workaround for #1500 |
|
44 |
validator.validate(new StreamSource(new StringReader(profile))); |
|
45 |
return true; |
|
46 |
} catch (final Throwable e) { |
|
47 |
log.warn("The profile is not valid"); |
|
48 |
return false; |
|
49 |
} |
|
50 |
} |
|
51 |
|
|
52 |
private Schema getSchema(final String resourceType) { |
|
53 |
try { |
|
54 |
if (StringUtils.isBlank(resourceType)) { throw new DnetInformationServiceException("Invalid resourceType"); } |
|
55 |
|
|
56 |
final String schemaSource = isStore.getXML(resourceType, xqueryUtils.getRootCollection() + DnetConstants.RESOURCE_TYPES); |
|
57 |
|
|
58 |
if ((schemaSource == null) || schemaSource.isEmpty()) { |
|
59 |
log.warn("cannot find resource type " + resourceType); |
|
60 |
return null; |
|
61 |
} |
|
62 |
return SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(new StringReader(schemaSource))); |
|
63 |
} catch (final DnetInformationServiceException e) { |
|
64 |
log.warn("cannot find resource type " + resourceType, e); |
|
65 |
return null; |
|
66 |
} catch (final SAXException e) { |
|
67 |
log.fatal("cannot parse resource type schema " + resourceType, e); |
|
68 |
return null; |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/registry/NoSuchPendingCategoryException.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.registry; |
|
2 |
|
|
3 |
public class NoSuchPendingCategoryException extends IllegalStateException { |
|
4 |
|
|
5 |
public NoSuchPendingCategoryException(String message) { |
|
6 |
super(message); |
|
7 |
} |
|
8 |
|
|
9 |
/** |
|
10 |
* |
|
11 |
*/ |
|
12 |
private static final long serialVersionUID = 6934748076388947172L; |
|
13 |
|
|
14 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/registry/ResourceKindResolverChain.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.registry; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import javax.xml.xpath.XPathExpressionException; |
|
6 |
|
|
7 |
import org.apache.commons.logging.Log; |
|
8 |
import org.apache.commons.logging.LogFactory; |
|
9 |
import org.springframework.beans.factory.annotation.Required; |
|
10 |
|
|
11 |
/** |
|
12 |
* Returns the first resource resolver |
|
13 |
* |
|
14 |
* @author marko |
|
15 |
* |
|
16 |
*/ |
|
17 |
public class ResourceKindResolverChain implements ResourceKindResolver { |
|
18 |
|
|
19 |
private static final Log log = LogFactory.getLog(ResourceKindResolverChain.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
20 |
|
|
21 |
/** |
|
22 |
* resolvers. |
|
23 |
*/ |
|
24 |
private List<ResourceKindResolver> resolvers; |
|
25 |
|
|
26 |
@Override |
|
27 |
public String getNormalKindForType(final String resourceType) throws XPathExpressionException { |
|
28 |
for (final ResourceKindResolver resolver : resolvers) { |
|
29 |
final String res = resolver.getNormalKindForType(resourceType); |
|
30 |
if (res != null) |
|
31 |
return res; |
|
32 |
} |
|
33 |
return null; |
|
34 |
|
|
35 |
} |
|
36 |
|
|
37 |
@Override |
|
38 |
public String getPendingKindForType(final String resourceType) throws XPathExpressionException { |
|
39 |
for (final ResourceKindResolver resolver : resolvers) { |
|
40 |
try { |
|
41 |
final String res = resolver.getPendingKindForType(resourceType); |
|
42 |
if (res != null) |
|
43 |
return res; |
|
44 |
} catch (NoSuchPendingCategoryException e) { |
|
45 |
log.debug("resolver didn't find pending category", e); |
|
46 |
continue; |
|
47 |
} |
|
48 |
} |
|
49 |
log.debug("cannot find pending category because no resolver found a pending category"); |
|
50 |
throw new NoSuchPendingCategoryException("no pending category for " + resourceType); |
|
51 |
} |
|
52 |
|
|
53 |
public List<ResourceKindResolver> getResolvers() { |
|
54 |
return resolvers; |
|
55 |
} |
|
56 |
|
|
57 |
@Required |
|
58 |
public void setResolvers(final List<ResourceKindResolver> resolvers) { |
|
59 |
this.resolvers = resolvers; |
|
60 |
} |
|
61 |
|
|
62 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/registry/ApplicationProfileResourceKindResolver.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.registry; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
|
|
5 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
6 |
import javax.xml.parsers.ParserConfigurationException; |
|
7 |
import javax.xml.xpath.XPath; |
|
8 |
import javax.xml.xpath.XPathExpressionException; |
|
9 |
import javax.xml.xpath.XPathFactory; |
|
10 |
|
|
11 |
import org.w3c.dom.Document; |
|
12 |
import org.xml.sax.SAXException; |
|
13 |
|
|
14 |
/** |
|
15 |
* Resolves resource kinds (pending and normal from the application profile xml file). |
|
16 |
* |
|
17 |
* @author marko |
|
18 |
* |
|
19 |
*/ |
|
20 |
public class ApplicationProfileResourceKindResolver implements ResourceKindResolver { |
|
21 |
|
|
22 |
/** |
|
23 |
* information space application profile. Contains, among others, mappings for the.. TODO: move to a DAO. |
|
24 |
* |
|
25 |
*/ |
|
26 |
private Document appProfile; |
|
27 |
|
|
28 |
public ApplicationProfileResourceKindResolver() { |
|
29 |
try { |
|
30 |
appProfile = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( |
|
31 |
getClass().getResourceAsStream("DRIVERInformationSpaceApplicationProfile.xml")); |
|
32 |
} catch (SAXException e) { |
|
33 |
throw new IllegalStateException("cannot parse information space application profile", e); |
|
34 |
} catch (IOException e) { |
|
35 |
throw new IllegalStateException("cannot parse information space application profile", e); |
|
36 |
} catch (ParserConfigurationException e) { |
|
37 |
throw new IllegalStateException("cannot parse information space application profile", e); |
|
38 |
} |
|
39 |
} |
|
40 |
|
|
41 |
/** |
|
42 |
* get the default resource kind associated with a given resource type. |
|
43 |
* |
|
44 |
* @param resourceType |
|
45 |
* resource type |
|
46 |
* @return resourceType |
|
47 |
* @throws XPathExpressionException |
|
48 |
* happens? |
|
49 |
*/ |
|
50 |
@Override |
|
51 |
public String getNormalKindForType(final String resourceType) throws XPathExpressionException { |
|
52 |
final XPath xpath = XPathFactory.newInstance().newXPath(); |
|
53 |
final String res = xpath.evaluate("//RESOURCE_TYPE[text() = '" + resourceType + "']/../../RESOURCE_KIND", appProfile); |
|
54 |
if (res.isEmpty()) |
|
55 |
return null; |
|
56 |
return res; |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* find the associated pending typology for a given resource type. |
|
61 |
* |
|
62 |
* @param resourceType |
|
63 |
* resource type |
|
64 |
* @return pending resource kind |
|
65 |
* @throws XPathExpressionException |
|
66 |
* shouldn't happen |
|
67 |
*/ |
|
68 |
@Override |
|
69 |
public String getPendingKindForType(final String resourceType) throws XPathExpressionException { |
|
70 |
final XPath xpath = XPathFactory.newInstance().newXPath(); |
|
71 |
final String res = xpath.evaluate("//RESOURCE_TYPE[text() = '" + resourceType + "']/../../PENDING_TYPOLOGY", appProfile); |
|
72 |
if (res.isEmpty()) |
|
73 |
throw new NoSuchPendingCategoryException("no pending category for " + resourceType); |
|
74 |
return res; |
|
75 |
} |
|
76 |
|
|
77 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/store/ISStore.java | ||
---|---|---|
1 |
package eu.dnetlib.enabling.is.store; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import eu.dnetlib.enabling.is.DnetInformationServiceException; |
|
6 |
|
|
7 |
public interface ISStore { |
|
8 |
|
|
9 |
boolean createFileColl(String fileColl) throws DnetInformationServiceException; |
|
10 |
|
|
11 |
boolean deleteFileColl(String fileColl) throws DnetInformationServiceException; |
|
12 |
|
|
13 |
boolean deleteXML(String fileName, String fileColl) throws DnetInformationServiceException; |
|
14 |
|
|
15 |
boolean executeXUpdate(String query) throws DnetInformationServiceException; |
|
16 |
|
|
17 |
List<String> getFileColls() throws DnetInformationServiceException; |
|
18 |
|
|
19 |
List<String> getFileNames(String fileColl) throws DnetInformationServiceException; |
|
20 |
|
|
21 |
String getXML(String fileName, String fileColl) throws DnetInformationServiceException; |
|
22 |
|
|
23 |
String getXMLbyQuery(String query) throws DnetInformationServiceException; |
|
24 |
|
|
25 |
boolean insertXML(String fileName, String fileColl, String file) throws DnetInformationServiceException; |
|
26 |
|
|
27 |
List<String> quickSearchXML(String query) throws DnetInformationServiceException; |
|
28 |
|
|
29 |
boolean updateXML(String fileName, String fileColl, String file) throws DnetInformationServiceException; |
|
30 |
|
|
31 |
String backup() throws DnetInformationServiceException; |
|
32 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/enabling/is/store/ISStoreImpl.java | ||
---|---|---|
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 org.apache.commons.logging.Log; |
|
8 |
import org.apache.commons.logging.LogFactory; |
|
9 |
import org.xmldb.api.base.XMLDBException; |
|
10 |
|
|
11 |
import eu.dnetlib.enabling.is.DnetInformationServiceException; |
|
12 |
import eu.dnetlib.xml.database.XMLDatabase; |
|
13 |
|
|
14 |
public class ISStoreImpl implements ISStore { |
|
15 |
|
|
16 |
private static final Log log = LogFactory.getLog(ISStoreImpl.class); // NOPMD by marko on 11/24/08 4:46 PM |
|
17 |
|
|
18 |
private XMLDatabase xmlDatabase; |
|
19 |
|
|
20 |
@Override |
|
21 |
public boolean createFileColl(final String fileColl) throws DnetInformationServiceException { |
|
22 |
try { |
|
23 |
xmlDatabase.createCollection(fileColl); |
|
24 |
} catch (final XMLDBException e) { |
|
25 |
throw new DnetInformationServiceException(e); |
|
26 |
} |
|
27 |
return true; |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public boolean deleteFileColl(final String fileColl) throws DnetInformationServiceException { |
|
32 |
try { |
|
33 |
xmlDatabase.removeCollection(fileColl); |
|
34 |
} catch (final XMLDBException e) { |
|
35 |
throw new DnetInformationServiceException(e); |
|
36 |
} |
|
37 |
return true; |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public boolean deleteXML(final String fileName, final String fileColl) throws DnetInformationServiceException { |
|
42 |
try { |
|
43 |
return xmlDatabase.remove(fileName, fileColl); |
|
44 |
} catch (final XMLDBException e) { |
|
45 |
throw new DnetInformationServiceException(e); |
|
46 |
} |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public boolean executeXUpdate(final String query) throws DnetInformationServiceException { |
|
51 |
try { |
|
52 |
xmlDatabase.xupdate(query); |
|
53 |
} catch (final XMLDBException e) { |
|
54 |
throw new DnetInformationServiceException("Error while executing xupdate " + query, e); |
|
55 |
} |
|
56 |
return true; |
|
57 |
} |
|
58 |
|
|
59 |
@Override |
|
60 |
public List<String> getFileColls() throws DnetInformationServiceException { |
|
61 |
try { |
|
62 |
return xmlDatabase.listChildCollections(xmlDatabase.getRootCollection()); |
|
63 |
} catch (final XMLDBException e) { |
|
64 |
throw new DnetInformationServiceException(e); |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public List<String> getFileNames(final String fileColl) throws DnetInformationServiceException { |
|
70 |
try { |
|
71 |
return xmlDatabase.list(fileColl); |
|
72 |
} catch (final XMLDBException e) { |
|
73 |
throw new DnetInformationServiceException(e); |
|
74 |
} |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
public String getXML(final String fileName, final String fileColl) throws DnetInformationServiceException { |
|
79 |
try { |
|
80 |
return xmlDatabase.read(fileName, fileColl); |
|
81 |
} catch (final XMLDBException e) { |
|
82 |
throw new DnetInformationServiceException(e); |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
@Override |
|
87 |
public String getXMLbyQuery(final String query) throws DnetInformationServiceException { |
|
88 |
log.debug(query); |
|
89 |
|
|
90 |
try { |
|
91 |
final Iterator<String> xquery = xmlDatabase.xquery(query); |
|
92 |
if ((xquery != null) && xquery.hasNext()) { return xquery.next(); } |
|
93 |
return null; |
|
94 |
} catch (final XMLDBException e) { |
|
95 |
throw new DnetInformationServiceException(e); |
|
96 |
} |
|
97 |
} |
|
98 |
|
|
99 |
@Override |
|
100 |
public boolean insertXML(final String fileName, final String fileColl, final String file) throws DnetInformationServiceException { |
|
101 |
try { |
|
102 |
xmlDatabase.create(fileName, fileColl, file); |
|
103 |
} catch (final XMLDBException e) { |
|
104 |
throw new DnetInformationServiceException(e); |
|
105 |
} |
|
106 |
return true; |
|
107 |
} |
|
108 |
|
|
109 |
@Override |
|
110 |
public List<String> quickSearchXML(final String query) throws DnetInformationServiceException { |
|
111 |
log.debug(query); |
|
112 |
try { |
|
113 |
final Iterator<String> res = xmlDatabase.xquery(query); |
|
114 |
if (res == null) { return new ArrayList<>(); } |
|
115 |
final ArrayList<String> ans = new ArrayList<>(); |
|
116 |
res.forEachRemaining(ans::add); |
|
117 |
return ans; |
|
118 |
} catch (final XMLDBException e) { |
|
119 |
log.fatal("searching", e); |
|
120 |
throw new DnetInformationServiceException(e); |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
@Override |
|
125 |
public boolean updateXML(final String fileName, final String fileColl, final String file) throws DnetInformationServiceException { |
|
126 |
try { |
|
127 |
xmlDatabase.update(fileName, fileColl, file); |
|
128 |
} catch (final XMLDBException e) { |
|
129 |
throw new DnetInformationServiceException(e); |
|
130 |
} |
|
131 |
return true; |
|
132 |
} |
|
133 |
|
|
134 |
@Override |
|
135 |
public String backup() throws DnetInformationServiceException { |
|
136 |
try { |
|
137 |
return xmlDatabase.backup(); |
|
138 |
} catch (final Exception e) { |
|
139 |
throw new DnetInformationServiceException(e); |
|
140 |
} |
|
141 |
} |
|
142 |
|
|
143 |
public XMLDatabase getXmlDatabase() { |
|
144 |
return xmlDatabase; |
|
145 |
} |
|
146 |
|
|
147 |
public void setXmlDatabase(final XMLDatabase xmlDatabase) { |
|
148 |
this.xmlDatabase = xmlDatabase; |
|
149 |
} |
|
150 |
|
|
151 |
} |
modules/dnet-is-application/trunk/src/main/java/eu/dnetlib/xml/database/ExistDatabase.java | ||
---|---|---|
1 |
package eu.dnetlib.xml.database; // NOPMD |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.FileNotFoundException; |
|
5 |
import java.io.FileOutputStream; |
|
6 |
import java.io.FileReader; |
|
7 |
import java.io.FileWriter; |
|
8 |
import java.io.IOException; |
|
9 |
import java.io.InputStream; |
|
10 |
import java.io.StringReader; |
|
11 |
import java.io.StringWriter; |
|
12 |
import java.text.SimpleDateFormat; |
|
13 |
import java.util.ArrayList; |
|
14 |
import java.util.Arrays; |
|
15 |
import java.util.Date; |
|
16 |
import java.util.Iterator; |
|
17 |
import java.util.List; |
|
18 |
import java.util.concurrent.locks.Lock; |
|
19 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
|
20 |
import java.util.zip.ZipEntry; |
|
21 |
import java.util.zip.ZipOutputStream; |
|
22 |
|
|
23 |
import org.apache.commons.io.IOUtils; |
|
24 |
import org.apache.commons.logging.Log; |
|
25 |
import org.apache.commons.logging.LogFactory; |
|
26 |
import org.exist.util.DatabaseConfigurationException; |
|
27 |
import org.exist.xmldb.DatabaseImpl; |
|
28 |
import org.exist.xmldb.DatabaseInstanceManager; |
|
29 |
import org.exist.xmldb.EXistResource; |
|
30 |
import org.exist.xmldb.XmldbURI; |
|
31 |
import org.springframework.beans.factory.annotation.Autowired; |
|
32 |
import org.springframework.beans.factory.annotation.Required; |
|
33 |
import org.springframework.context.Lifecycle; |
|
34 |
import org.xmldb.api.DatabaseManager; |
|
35 |
import org.xmldb.api.base.Collection; |
|
36 |
import org.xmldb.api.base.Database; |
|
37 |
import org.xmldb.api.base.Resource; |
|
38 |
import org.xmldb.api.base.ResourceIterator; |
|
39 |
import org.xmldb.api.base.ResourceSet; |
|
40 |
import org.xmldb.api.base.XMLDBException; |
|
41 |
import org.xmldb.api.modules.CollectionManagementService; |
|
42 |
import org.xmldb.api.modules.XPathQueryService; |
|
43 |
|
|
44 |
import eu.dnetlib.enabling.is.DnetInformationServiceException; |
|
45 |
import eu.dnetlib.enabling.tools.BulkResourceImporter; |
|
46 |
import eu.dnetlib.exceptions.DnetGenericRuntimeException; |
|
47 |
import eu.dnetlib.miscutils.datetime.DateUtils; |
|
48 |
|
|
49 |
/** |
|
50 |
* eXist database wrapper. |
|
51 |
* |
|
52 |
* @author marko |
|
53 |
* |
|
54 |
*/ |
|
55 |
public abstract class ExistDatabase implements XMLDatabase, Lifecycle { // NOPMD by marko |
|
56 |
|
|
57 |
private static final Log log = LogFactory.getLog(ExistDatabase.class); // NOPMD |
|
58 |
|
|
59 |
private static final String XML_RESOURCE = "XMLResource"; |
|
60 |
private static final String CONFIG_TEMPLATE_FILE = "default-exist-conf.xml"; |
|
61 |
|
|
62 |
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); |
|
63 |
private final Lock readLock = rwl.readLock(); |
|
64 |
private final Lock writeLock = rwl.writeLock(); |
|
65 |
|
|
66 |
private Database database; |
|
67 |
|
|
68 |
private Collection root; |
|
69 |
|
|
70 |
private DatabaseInstanceManager manager; |
|
71 |
|
|
72 |
private XPathQueryService queryService; |
|
73 |
|
|
74 |
private CollectionManagementService colman; |
|
75 |
|
|
76 |
private String configFile; |
|
77 |
|
|
78 |
private String backupDir; |
|
79 |
|
|
80 |
@Autowired |
|
81 |
private BulkResourceImporter bulkImporter; |
|
82 |
|
|
83 |
private transient File dbDirectory; |
|
84 |
|
|
85 |
private File existConfigFile; |
|
86 |
|
|
87 |
private boolean xQueryJavaEnabled; |
|
88 |
|
|
89 |
@Override |
|
90 |
public void start() { |
|
91 |
log.info("starting database"); |
|
92 |
|
|
93 |
try { |
|
94 |
dbDirectory = getDatabaseDirectory(); |
|
95 |
|
|
96 |
if (dbDirectory.exists()) { |
|
97 |
existConfigFile = new File(dbDirectory, "conf.xml"); |
|
98 |
enableJava(existConfigFile, isXQueryJavaEnabled()); |
|
99 |
setConfigFile(existConfigFile.getAbsolutePath()); |
|
100 |
bulkImporter.setInitialized(true); |
|
101 |
} else { |
|
102 |
createDatabase(); |
|
103 |
} |
|
104 |
|
|
105 |
if (getDatabase() == null) { |
|
106 |
setDatabase(new DatabaseImpl()); |
|
107 |
getDatabase().setProperty("configuration", getConfigFile()); |
|
108 |
getDatabase().setProperty("create-database", "true"); |
|
109 |
} |
|
110 |
|
|
111 |
DatabaseManager.registerDatabase(getDatabase()); |
|
112 |
|
|
113 |
setRoot(DatabaseManager.getCollection("xmldb:exist://" + getRootCollection(), "admin", "")); |
|
114 |
setManager((DatabaseInstanceManager) getRoot().getService("DatabaseInstanceManager", "1.0")); |
|
115 |
setQueryService((XPathQueryService) getRoot().getService("XPathQueryService", "1.0")); |
|
116 |
setColman((CollectionManagementService) getRoot().getService("CollectionManagementService", "1.0")); |
|
117 |
|
|
118 |
if (!bulkImporter.isInitialized()) { |
|
119 |
bulkImporter.startImport(); |
|
120 |
} |
|
121 |
|
|
122 |
} catch (final XMLDBException | DnetInformationServiceException e) { |
|
123 |
throw new DnetGenericRuntimeException("cannot open eXist database", e); |
|
124 |
} |
|
125 |
} |
|
126 |
|
|
127 |
abstract protected File getDatabaseDirectory(); |
|
128 |
|
|
129 |
protected void enableJava(final File conf, final boolean enabled) { |
|
130 |
final StringWriter buffer = new StringWriter(); |
|
131 |
if (conf.exists()) { |
|
132 |
try { |
|
133 |
IOUtils.copy(new FileReader(conf), buffer); |
|
134 |
final String newConf = patchConfigFileEnableJava(buffer.toString(), enabled); |
|
135 |
if (!newConf.equals(buffer.toString())) { |
|
136 |
final FileWriter writer = new FileWriter(conf); |
|
137 |
try { |
|
138 |
IOUtils.copy(new StringReader(newConf), writer); |
|
139 |
} finally { |
|
140 |
writer.close(); |
|
141 |
} |
|
142 |
} |
|
143 |
} catch (final FileNotFoundException e) { |
|
144 |
log.warn("cannot patch eXist conf file", e); |
|
145 |
} catch (final IOException e) { |
|
146 |
log.warn("cannot patch eXist conf file", e); |
|
147 |
} |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
protected String patchConfigFileEnableJava(final String conf, final boolean enabled) { |
|
152 |
return conf.replaceAll("enable-java-binding=\"[^\"]*\"", "enable-java-binding=\"" + (enabled ? "yes" : "no") + "\""); |
|
153 |
} |
|
154 |
|
|
155 |
protected Collection getCollection(final String collection) throws XMLDBException { |
|
156 |
readLock.lock(); |
|
157 |
try { |
|
158 |
if (!collection.startsWith("/db")) { throw new XMLDBException(0, "collection path should begin with /db"); } |
|
159 |
return database.getCollection("exist://" + collection, "admin", ""); |
|
160 |
} finally { |
|
161 |
readLock.unlock(); |
|
162 |
} |
|
163 |
} |
|
164 |
|
|
165 |
protected void createDatabase() { |
|
166 |
log.debug("creating database in " + dbDirectory.getAbsolutePath()); |
|
167 |
try { |
|
168 |
new File(dbDirectory, "data").mkdirs(); |
|
169 |
|
|
170 |
final InputStream defaultConf = getClass().getResourceAsStream(CONFIG_TEMPLATE_FILE); |
|
171 |
if (defaultConf == null) { throw new IOException("cannot find " + CONFIG_TEMPLATE_FILE); } |
|
172 |
|
|
173 |
existConfigFile = new File(dbDirectory, "conf.xml"); |
|
174 |
final FileOutputStream confOutput = new FileOutputStream(existConfigFile); |
|
175 |
|
|
176 |
try { |
|
177 |
IOUtils.copy(defaultConf, confOutput); |
|
178 |
} finally { |
|
179 |
confOutput.close(); |
|
180 |
} |
|
181 |
enableJava(existConfigFile, isXQueryJavaEnabled()); |
|
182 |
|
|
183 |
setConfigFile(existConfigFile.getAbsolutePath()); |
|
184 |
} catch (final IOException e) { |
|
185 |
log.fatal("Error creating database dir", e); |
|
186 |
throw new DnetGenericRuntimeException(e); |
|
187 |
} |
|
188 |
log.debug("database created"); |
|
189 |
} |
|
190 |
|
|
191 |
@Override |
|
192 |
public void create(final String name, final String collection, final String content) throws XMLDBException { |
|
193 |
writeLock.lock(); |
|
194 |
try { |
|
195 |
if ("".equals(name)) { throw new XMLDBException(0, "cannot create a xml file with an empty file name"); } |
|
196 |
|
|
197 |
Collection col = getCollection(collection); |
|
198 |
|
|
199 |
if (col == null) { |
|
200 |
// create parent collections |
|
201 |
createCollection(collection, true); |
|
202 |
col = getCollection(collection); |
|
203 |
} |
|
204 |
|
|
205 |
final Resource res = col.createResource(name, XML_RESOURCE); |
|
206 |
res.setContent(content); |
|
207 |
col.storeResource(res); |
|
208 |
|
|
209 |
((EXistResource) res).freeResources(); |
|
210 |
col.close(); |
|
211 |
} finally { |
|
212 |
writeLock.unlock(); |
|
213 |
} |
|
214 |
} |
|
215 |
|
|
216 |
@Override |
|
217 |
public boolean remove(final String name, final String collection) throws XMLDBException { |
|
218 |
writeLock.lock(); |
|
219 |
try { |
|
220 |
final Collection col = getCollection(collection); |
|
221 |
|
|
222 |
final Resource res = col.getResource(name); |
|
223 |
if (res == null) { return false; } |
|
224 |
|
|
225 |
col.removeResource(res); |
|
226 |
col.close(); |
|
227 |
return true; |
|
228 |
} finally { |
|
229 |
writeLock.unlock(); |
|
230 |
} |
|
231 |
} |
|
232 |
|
|
233 |
@Override |
|
234 |
public void update(final String name, final String collection, final String content) throws XMLDBException { |
|
235 |
writeLock.lock(); |
|
236 |
try { |
|
237 |
final Collection col = getCollection(collection); |
|
238 |
|
|
239 |
final Resource res = col.getResource(name); |
|
240 |
if (res == null) { throw new XMLDBException(0, "resource doesn't exist"); } |
|
241 |
res.setContent(content); |
|
242 |
col.storeResource(res); |
|
243 |
((EXistResource) res).freeResources(); |
|
244 |
col.close(); |
|
245 |
} finally { |
|
246 |
writeLock.unlock(); |
|
247 |
} |
|
248 |
|
|
249 |
} |
|
250 |
|
|
251 |
@Override |
|
252 |
public String read(final String name, final String collection) throws XMLDBException { |
|
253 |
readLock.lock(); |
|
254 |
try { |
|
255 |
Resource res = null; |
|
256 |
final Collection coll = getCollection(collection); |
|
257 |
try { |
|
258 |
if (coll == null) { return null; } |
|
259 |
res = coll.getResource(name); |
|
260 |
if (res != null) { return (String) res.getContent(); } |
|
261 |
return null; |
|
262 |
} finally { |
|
263 |
if (res != null) { |
|
264 |
((EXistResource) res).freeResources(); |
|
265 |
} |
|
266 |
coll.close(); |
|
267 |
} |
|
268 |
} finally { |
|
269 |
readLock.unlock(); |
|
270 |
} |
|
271 |
} |
|
272 |
|
|
273 |
@Override |
|
274 |
public Iterator<String> xquery(final String query) throws XMLDBException { |
|
275 |
readLock.lock(); |
|
276 |
try { |
|
277 |
final ResourceSet result = getQueryService().query(query); |
|
278 |
if (result == null) { return null; } |
|
279 |
final ResourceIterator iterator = result.getIterator(); |
|
280 |
return new Iterator<String>() { |
|
281 |
|
|
282 |
@Override |
|
283 |
public boolean hasNext() { |
|
284 |
try { |
|
285 |
return iterator.hasMoreResources(); |
|
286 |
} catch (final XMLDBException e) { |
|
287 |
throw new RuntimeException("Error while getting next element", e); |
|
288 |
} |
|
289 |
} |
|
290 |
|
|
291 |
@Override |
|
292 |
public String next() { |
|
293 |
Resource res = null; |
|
294 |
try { |
|
295 |
res = iterator.nextResource(); |
|
296 |
return (String) res.getContent(); |
|
297 |
} catch (final XMLDBException e) { |
|
298 |
throw new RuntimeException("Error while getting next element", e); |
|
299 |
} finally { |
|
300 |
if (res != null) { |
|
301 |
try { |
|
302 |
((EXistResource) res).freeResources(); |
|
303 |
} catch (final XMLDBException e) { |
|
304 |
log.error("error on free resource"); |
|
305 |
} |
|
306 |
} |
|
307 |
} |
|
308 |
} |
|
309 |
}; |
|
310 |
} finally { |
|
311 |
readLock.unlock(); |
|
312 |
} |
|
313 |
} |
|
314 |
|
|
315 |
@Override |
|
316 |
public void xupdate(final String query) throws XMLDBException { |
|
317 |
writeLock.lock(); |
|
318 |
try { |
|
319 |
getQueryService().query(query); |
|
320 |
} finally { |
|
321 |
writeLock.unlock(); |
|
322 |
} |
|
323 |
|
|
324 |
} |
|
325 |
|
|
326 |
@Override |
|
327 |
public void stop() { |
|
328 |
log.info("shutting down xmldb..."); |
|
329 |
try { |
|
330 |
getManager().shutdown(); |
|
331 |
DatabaseManager.deregisterDatabase(database); |
|
332 |
log.info("...xmldb closed"); |
|
333 |
} catch (final XMLDBException e) { |
|
334 |
log.fatal("...cannot close database", e); |
|
335 |
} |
|
336 |
} |
|
337 |
|
|
338 |
@Override |
|
339 |
public boolean collectionExists(final String collection) throws XMLDBException { |
|
340 |
Collection col = null; |
|
341 |
try { |
|
342 |
col = getCollection(collection); |
|
343 |
return col != null; |
|
344 |
} finally { |
|
345 |
if (col != null) { |
|
346 |
col.close(); |
|
347 |
} |
|
348 |
} |
|
349 |
|
|
350 |
} |
|
351 |
|
|
352 |
@Override |
|
353 |
public void createCollection(final String collection) throws XMLDBException { |
|
354 |
writeLock.lock(); |
|
355 |
try { |
|
356 |
createCollection(collection, false); |
|
357 |
} finally { |
|
358 |
writeLock.unlock(); |
|
359 |
} |
|
360 |
} |
|
361 |
|
|
362 |
private void createCollection(final String collection, final boolean recursive) throws XMLDBException { |
|
363 |
if (recursive) { |
|
364 |
final XmldbURI uri = XmldbURI.create(collection).removeLastSegment(); |
|
365 |
if (!collectionExists(uri.toString())) { |
|
366 |
createCollection(uri.toString(), true); |
|
367 |
} |
|
368 |
} |
|
369 |
getColman().createCollection(collection); |
|
370 |
} |
|
371 |
|
|
372 |
@Override |
|
373 |
public void removeCollection(final String collection) throws XMLDBException { |
|
374 |
writeLock.lock(); |
|
375 |
try { |
|
376 |
getColman().removeCollection(collection); |
|
377 |
} finally { |
|
378 |
writeLock.unlock(); |
|
379 |
} |
|
380 |
} |
|
381 |
|
|
382 |
public String getConfigFile() { |
|
383 |
return configFile; |
|
384 |
} |
|
385 |
|
|
386 |
public void setConfigFile(final String configFile) { |
|
387 |
this.configFile = configFile; |
|
388 |
} |
|
389 |
|
|
390 |
@Override |
|
391 |
public String getBackupDir() { |
|
392 |
return backupDir; |
|
393 |
} |
|
394 |
|
|
395 |
@Required |
|
396 |
public void setBackupDir(final String backupDir) { |
|
397 |
this.backupDir = backupDir; |
|
398 |
} |
|
399 |
|
|
400 |
@Override |
|
401 |
public boolean isRunning() { |
|
402 |
return false; |
|
403 |
} |
|
404 |
|
|
405 |
protected Database getDatabase() { |
|
406 |
return database; |
|
407 |
} |
|
408 |
|
|
409 |
protected void setDatabase(final Database database) { |
|
410 |
this.database = database; |
|
411 |
} |
|
412 |
|
|
413 |
protected Collection getRoot() { |
|
414 |
return root; |
|
415 |
} |
|
416 |
|
|
417 |
protected void setRoot(final Collection root) { |
|
418 |
this.root = root; |
|
419 |
} |
|
420 |
|
|
421 |
protected DatabaseInstanceManager getManager() { |
|
422 |
return manager; |
|
423 |
} |
|
424 |
|
|
425 |
protected void setManager(final DatabaseInstanceManager manager) { |
|
426 |
this.manager = manager; |
|
427 |
} |
|
428 |
|
|
429 |
protected XPathQueryService getQueryService() { |
|
430 |
return queryService; |
|
431 |
} |
|
432 |
|
|
433 |
protected void setQueryService(final XPathQueryService queryService) { |
|
434 |
this.queryService = queryService; |
|
435 |
} |
|
436 |
|
|
437 |
protected CollectionManagementService getColman() { |
|
438 |
return colman; |
|
439 |
} |
|
440 |
|
|
441 |
protected void setColman(final CollectionManagementService colman) { |
|
442 |
this.colman = colman; |
|
443 |
} |
|
444 |
|
|
445 |
@Override |
|
446 |
public String getRootCollection() { |
|
447 |
return "/db"; |
|
448 |
} |
|
449 |
|
|
450 |
@Override |
|
451 |
public List<String> listChildCollections(final String collection) throws XMLDBException { |
|
452 |
readLock.lock(); |
|
453 |
try { |
|
454 |
final Collection col = getCollection(collection); |
|
455 |
if (col == null) { return new ArrayList<>(); } |
|
456 |
return Arrays.asList(col.listChildCollections()); |
|
457 |
} finally { |
|
458 |
readLock.unlock(); |
|
459 |
} |
|
460 |
} |
|
461 |
|
|
462 |
/** |
|
463 |
* {@inheritDoc} |
|
464 |
* |
|
465 |
* @see eu.dnetlib.xml.database.XMLDatabase#list(java.lang.String) |
|
466 |
*/ |
|
467 |
@Override |
|
468 |
public List<String> list(final String collection) throws XMLDBException { |
|
469 |
readLock.lock(); |
|
470 |
try { |
|
471 |
final Collection col = getCollection(collection); |
|
472 |
if (col == null) { return new ArrayList(); } |
|
473 |
return Arrays.asList(col.listResources()); |
|
474 |
} finally { |
|
475 |
readLock.unlock(); |
|
476 |
} |
|
477 |
} |
|
478 |
|
|
479 |
@Override |
|
480 |
public String backup() throws XMLDBException, DatabaseConfigurationException { |
|
481 |
log.info("Starting backup..."); |
|
482 |
readLock.lock(); |
|
483 |
try { |
|
484 |
verifyBackupDir(); |
|
485 |
|
|
486 |
final String seq = (new SimpleDateFormat("yyyyMMdd-HHmm")).format(new Date()); |
|
487 |
|
|
488 |
final ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(backupDir + "/data-" + seq + ".zip")); |
|
489 |
|
|
490 |
final FileWriter logFile = new FileWriter(backupDir + "/report-" + seq + ".log"); |
|
491 |
logFile.write("Backup started at: " + DateUtils.now_ISO8601() + "\n\n"); |
|
492 |
|
|
493 |
backup(getRoot().getName(), zip, logFile); |
|
494 |
|
|
495 |
logFile.write("\nBackup finished at: " + DateUtils.now_ISO8601() + "\n"); |
|
496 |
|
|
497 |
logFile.flush(); |
|
498 |
logFile.close(); |
|
499 |
|
|
500 |
zip.flush(); |
|
501 |
zip.close(); |
|
502 |
|
|
503 |
log.info("Backup finished"); |
|
504 |
return backupDir; |
|
505 |
} catch (final Exception e) { |
|
506 |
log.error("Backup failed", e); |
|
507 |
throw new XMLDBException(0, "cannot backup", e); |
|
508 |
} finally { |
|
509 |
readLock.unlock(); |
|
510 |
} |
|
511 |
} |
|
512 |
|
|
513 |
private void verifyBackupDir() { |
|
514 |
final File d = new File(backupDir); |
|
515 |
if (!d.exists()) { |
|
516 |
d.mkdirs(); |
|
517 |
} |
|
518 |
} |
|
519 |
|
|
520 |
private void backup(final String coll, final ZipOutputStream zip, final FileWriter logFile) throws XMLDBException, IOException { |
|
521 |
readLock.lock(); |
|
522 |
logFile.write("COLLECTION: " + coll + "\n"); |
|
523 |
log.info("Backup of collection " + coll); |
|
524 |
try { |
|
525 |
for (final String file : list(coll)) { |
|
526 |
zip.putNextEntry(new ZipEntry(coll + "/" + file + ".xml")); |
|
527 |
final Resource resource = getCollection(coll).getResource(file); |
|
528 |
zip.write(resource.getContent().toString().getBytes()); |
|
529 |
zip.closeEntry(); |
Also available in: Unified diff