Revision 36813
Added by Michele Artini about 9 years ago
ContentInitializer.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.enabling.nodeManager; |
2 | 2 |
|
3 |
import java.io.IOException; |
|
3 | 4 |
import java.util.List; |
4 |
import java.util.Map; |
|
5 | 5 |
|
6 |
import org.apache.commons.io.IOUtils; |
|
6 | 7 |
import org.apache.commons.logging.Log; |
7 | 8 |
import org.apache.commons.logging.LogFactory; |
8 |
import org.dom4j.Document; |
|
9 |
import org.dom4j.io.SAXReader; |
|
10 | 9 |
import org.reflections.Reflections; |
11 | 10 |
import org.springframework.beans.factory.annotation.Required; |
12 | 11 |
import org.springframework.context.ResourceLoaderAware; |
... | ... | |
16 | 15 |
import org.springframework.core.io.support.ResourcePatternUtils; |
17 | 16 |
|
18 | 17 |
import com.google.common.collect.Lists; |
19 |
import com.google.common.collect.Maps;
|
|
18 |
import com.google.gson.Gson;
|
|
20 | 19 |
|
21 | 20 |
import eu.dnetlib.common.services.locators.DnetServiceLocator; |
22 | 21 |
import eu.dnetlib.enabling.annotations.DnetResource; |
23 | 22 |
import eu.dnetlib.enabling.annotations.DnetResourceHelper; |
23 |
import eu.dnetlib.enabling.datastructures.BaseResource; |
|
24 | 24 |
import eu.dnetlib.rmi.objects.is.DnetDataStructure; |
25 | 25 |
import eu.dnetlib.rmi.objects.is.DnetResourceType; |
26 | 26 |
import eu.dnetlib.rmi.soap.InformationService; |
... | ... | |
41 | 41 |
public void init() { |
42 | 42 |
try { |
43 | 43 |
log.info("Content initialization - START"); |
44 |
registerAnnotatedResouceTypes(basePackage); |
|
45 |
registerDatastructures(initDsPath, false); |
|
44 |
final List<Class<? extends BaseResource>> types = registerAnnotatedResouceTypes(basePackage); |
|
45 |
for (Class<? extends BaseResource> type : types) { |
|
46 |
registerDatastructures(initDsPath, type, false); |
|
47 |
} |
|
46 | 48 |
log.info("Content initialization - END"); |
47 | 49 |
} catch (Throwable e) { |
48 | 50 |
log.error("Content Initialization failed: " + e.getMessage(), e); |
49 | 51 |
} |
50 | 52 |
} |
51 | 53 |
|
52 |
public List<String> registerAnnotatedResouceTypes(final String pkg) throws InformationServiceException { |
|
54 |
@SuppressWarnings("unchecked") |
|
55 |
public List<Class<? extends BaseResource>> registerAnnotatedResouceTypes(final String pkg) throws InformationServiceException { |
|
53 | 56 |
log.info("importing types from package " + pkg); |
54 | 57 |
|
55 |
final List<String> res = Lists.newArrayList();
|
|
58 |
final List<Class<? extends BaseResource>> res = Lists.newArrayList();
|
|
56 | 59 |
final InformationService is = serviceLocator.getService(InformationService.class); |
57 | 60 |
final Reflections reflections = new Reflections(pkg); |
58 | 61 |
for (Class<?> cl : reflections.getTypesAnnotatedWith(DnetResource.class)) { |
59 | 62 |
log.info(" - Registering/updating resourceType " + cl.getName()); |
60 | 63 |
final DnetResourceType type = DnetResourceHelper.obtainResourceType(cl); |
61 |
|
|
62 | 64 |
is.addResourceType(type); |
63 |
res.add(type.getType()); |
|
65 |
if (BaseResource.class.isAssignableFrom(cl)) { |
|
66 |
res.add((Class<? extends BaseResource>) cl); |
|
67 |
} else { |
|
68 |
log.error("Class " + cl + " does not extends BaseResource"); |
|
69 |
throw new InformationServiceException("Class " + cl + " does not extends BaseResource"); |
|
70 |
} |
|
64 | 71 |
} |
65 | 72 |
return res; |
66 | 73 |
} |
67 | 74 |
|
68 |
public Map<String, Integer> registerDatastructures(final String path, final boolean override) throws InformationServiceException { |
|
75 |
public void registerDatastructures(final String basePath, final Class<? extends BaseResource> type, final boolean override) |
|
76 |
throws InformationServiceException { |
|
77 |
|
|
78 |
final DnetResource ann = type.getAnnotation(DnetResource.class); |
|
79 |
|
|
80 |
final String path; |
|
81 |
|
|
82 |
switch (ann.format()) { |
|
83 |
case JSON: |
|
84 |
path = basePath + "/" + ann.type() + "/**/*.json"; |
|
85 |
break; |
|
86 |
case XML: |
|
87 |
path = basePath + "/" + ann.type() + "/**/*.xml"; |
|
88 |
break; |
|
89 |
case TEXT: |
|
90 |
path = basePath + "/" + ann.type() + "/**/*.text"; |
|
91 |
break; |
|
92 |
default: |
|
93 |
path = basePath; |
|
94 |
} |
|
95 |
|
|
69 | 96 |
log.info("importing resources from " + path); |
70 | 97 |
|
71 |
final Map<String, Integer> res = Maps.newHashMap(); |
|
72 |
|
|
73 | 98 |
final InformationService is = serviceLocator.getService(InformationService.class); |
74 | 99 |
final ResourcePatternResolver resourceResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader); |
75 | 100 |
try { |
76 |
final SAXReader reader = new SAXReader(); |
|
77 | 101 |
for (Resource resource : resourceResolver.getResources(path)) { |
78 |
final Document doc = reader.read(resource.getInputStream()); |
|
79 |
final String code = doc.valueOf("/datastructure/@code").trim(); |
|
80 |
final String type = doc.valueOf("/datastructure/@type").trim(); |
|
81 |
final boolean alreadyRegisterd = is.isRegisteredDs(code, type); |
|
82 |
if (alreadyRegisterd || override) { |
|
83 |
final DnetDataStructure ds = alreadyRegisterd ? is.getDsByCode(code, type) : new DnetDataStructure(); |
|
84 |
ds.setCode(code); |
|
85 |
ds.setType(type); |
|
86 |
ds.setValid(!doc.valueOf("/datastructure/@valid").trim().equalsIgnoreCase("false")); |
|
87 |
ds.setName(doc.valueOf("/datastructure/name").trim()); |
|
88 |
ds.setDescription(doc.valueOf("/datastructure/description").trim()); |
|
89 |
ds.setContent(doc.valueOf("/datastructure/content").trim()); |
|
90 |
|
|
91 |
is.registerDs(ds); |
|
92 |
|
|
93 |
final Integer old = res.get(ds.getType()); |
|
94 |
res.put(ds.getType(), old == null ? 1 : old + 1); |
|
102 |
try { |
|
103 |
final BaseResource r; |
|
104 |
switch (ann.format()) { |
|
105 |
case JSON: |
|
106 |
r = new Gson().fromJson(IOUtils.toString(resource.getInputStream()), type); |
|
107 |
break; |
|
108 |
case XML: |
|
109 |
r = type.getConstructor(Resource.class).newInstance(resource); |
|
110 |
break; |
|
111 |
case TEXT: |
|
112 |
r = type.getConstructor(Resource.class).newInstance(resource); |
|
113 |
break; |
|
114 |
default: |
|
115 |
r = null; |
|
116 |
} |
|
117 |
final DnetDataStructure ds = r.asDnetDataStructure(); |
|
118 |
if (override || !is.isRegisteredDs(ds.getCode(), ds.getType())) { |
|
119 |
log.info(" - Registering/updating resource " + ds.getCode()); |
|
120 |
is.registerDs(ds); |
|
121 |
} |
|
122 |
} catch (Throwable e) { |
|
123 |
log.error("Failed registration of resource " + resource.getFilename(), e); |
|
124 |
throw new InformationServiceException("Failed registration of resource " + resource.getFilename(), e); |
|
95 | 125 |
} |
126 |
} |
|
127 |
} catch (IOException e) { |
|
128 |
log.error("Failed registration of resources in path " + path, e); |
|
129 |
throw new InformationServiceException("Failed registration of resources in path " + path, e); |
|
96 | 130 |
|
97 |
} |
|
98 |
return res; |
|
99 |
} catch (Exception e) { |
|
100 |
throw new InformationServiceException("DS initialization failed", e); |
|
101 | 131 |
} |
102 | 132 |
} |
103 | 133 |
|
Also available in: Unified diff
content initialization