1 | package eu.clarin.cmdi.mdservice.action; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileNotFoundException; |
---|
5 | import java.io.FileOutputStream; |
---|
6 | import java.io.IOException; |
---|
7 | import java.io.InputStream; |
---|
8 | import java.io.OutputStream; |
---|
9 | import java.util.Properties; |
---|
10 | |
---|
11 | import javax.xml.parsers.DocumentBuilder; |
---|
12 | import javax.xml.parsers.DocumentBuilderFactory; |
---|
13 | import javax.xml.parsers.ParserConfigurationException; |
---|
14 | |
---|
15 | import org.w3c.dom.Document; |
---|
16 | import org.xml.sax.SAXException; |
---|
17 | |
---|
18 | import net.sf.json.JSON; |
---|
19 | import net.sf.json.JSONSerializer; |
---|
20 | import net.sf.json.xml.XMLSerializer; |
---|
21 | |
---|
22 | |
---|
23 | import eu.clarin.cmdi.mdservice.model.Query; |
---|
24 | import org.apache.log4j.Logger; |
---|
25 | import org.apache.log4j.xml.DOMConfigurator; |
---|
26 | |
---|
27 | /** |
---|
28 | * Helper class, main-class to run as command-line java-app for testing |
---|
29 | * |
---|
30 | * @author vronk |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | public class Admin { |
---|
35 | |
---|
36 | |
---|
37 | private static String config_path = "mdservice.properties"; |
---|
38 | private static Properties config; |
---|
39 | public static Logger log = Logger.getLogger(Admin.class); |
---|
40 | |
---|
41 | public static String FATAL = "fatal"; |
---|
42 | public static String ERROR = "error"; |
---|
43 | public static String WARN = "warn"; |
---|
44 | public static String INFO = "info"; |
---|
45 | public static String DEBUG = "debug"; |
---|
46 | public static String TRACE = "trace"; |
---|
47 | |
---|
48 | |
---|
49 | public static void main(String[] args) { |
---|
50 | |
---|
51 | //log = Logger.getLogger(Admin.class); |
---|
52 | //DOMConfigurator.configure("log4j.xml"); |
---|
53 | //log.debug("WELCOME LOG4J"); |
---|
54 | |
---|
55 | //String dirpath = "C:/apps/ApacheTomcat/webapps/mdservice/schemas"; |
---|
56 | |
---|
57 | //CMDRegistryProxy cmdreg = new CMDRegistryProxy (); |
---|
58 | |
---|
59 | //cmdreg.readCompRegistryFromDir(dirpath); |
---|
60 | |
---|
61 | //System.out.println(cmdreg.listComponents()); |
---|
62 | |
---|
63 | testJSON(); |
---|
64 | |
---|
65 | //loadConfig(config_path); |
---|
66 | |
---|
67 | /* |
---|
68 | //Query q = new Query("dc.title=dino or dinosaur", Query.RECORDSET); |
---|
69 | //Query q = new Query("( ( Date any 2005 ) or ( Date any 2001 ) ) and ( ( Description any part ) )", Query.RECORDSET); |
---|
70 | Query q = new Query("( ( Date any 2005 ) or ( Country = \"United States\" ) ) and ( ( Description any part ) )", Query.RECORDSET); |
---|
71 | //System.out.println(q.toXCQL()); |
---|
72 | System.out.println(q.toXPath()); |
---|
73 | */ |
---|
74 | //Query q = new Query("CMD", "cmdIndex"); |
---|
75 | |
---|
76 | //try { |
---|
77 | |
---|
78 | /* |
---|
79 | |
---|
80 | Query q = new Query("Title=the", Query.RECORDSET, "olac" ); |
---|
81 | q.execute(); |
---|
82 | |
---|
83 | InputStream inputStream = q.getResult().getResultStream("htmltable"); |
---|
84 | */ |
---|
85 | //MDTransformer.getMDTransformer(q.getResult().getResultStream("xml"),"model2html",); |
---|
86 | /* |
---|
87 | MDRepoProxyAction mrpa = new MDRepoProxyAction(); |
---|
88 | // crp.setCompname("cmdi-corpus-aac2"); |
---|
89 | mrpa.setQ("Title any the"); |
---|
90 | mrpa.setCollection("olac"); |
---|
91 | mrpa.setActionkey("recordset"); |
---|
92 | mrpa.setFormat("xml"); |
---|
93 | mrpa.execute(); |
---|
94 | InputStream inputStream = mrpa.getInputStream(); |
---|
95 | |
---|
96 | Cache.getCache().putInCache("test", inputStream); |
---|
97 | */ |
---|
98 | |
---|
99 | /* |
---|
100 | DCRProxyAction dcr = new DCRProxyAction(); |
---|
101 | dcr.setDc("1253"); |
---|
102 | InputStream inputStream = MDTransformer.getMDTransformer().transformXML(dcr.getSourceStream(), "dcif2htmldiv"); |
---|
103 | |
---|
104 | */ |
---|
105 | /* |
---|
106 | CompRegistryProxyAction crp = new CompRegistryProxyAction (); |
---|
107 | // crp.setCompname("cmdi-corpus-aac2"); |
---|
108 | crp.setFormat("htmllist"); |
---|
109 | crp.execute(); |
---|
110 | InputStream inputStream = crp.getInputStream(); |
---|
111 | */ |
---|
112 | |
---|
113 | // InputStream inputStream = MDRepositoryProxy.getMDRepo().getCollections(null); |
---|
114 | |
---|
115 | //copyStreams(inputStream,System.out); |
---|
116 | /* |
---|
117 | } catch (IOException e1) { |
---|
118 | // TODO Auto-generated catch block |
---|
119 | e1.printStackTrace(); |
---|
120 | } catch (InterruptedException e1) { |
---|
121 | // TODO Auto-generated catch block |
---|
122 | e1.printStackTrace(); |
---|
123 | } catch (Exception e) { |
---|
124 | Admin.notifyUser(e.getMessage()); |
---|
125 | } |
---|
126 | */ |
---|
127 | |
---|
128 | //System.out.println(res.xmlString()); |
---|
129 | //System.out.println(res.toFormat("MODEL2VIEW")); |
---|
130 | |
---|
131 | |
---|
132 | //System.out.println(res.xmlString()); |
---|
133 | |
---|
134 | // MDRepositoryProxy.getMDRepo().queryModel("CMD", System.out); |
---|
135 | |
---|
136 | //testParse(); |
---|
137 | |
---|
138 | /* |
---|
139 | * CMDComponent testcomp = new CMDComponent("C:/apps/ApacheTomcat/webapps/mdservice/schemas/component-olac.xml"); |
---|
140 | |
---|
141 | System.out.println(testcomp.name()); |
---|
142 | System.out.println("components:"); |
---|
143 | for (Iterator it = testcomp.components().iterator(); |
---|
144 | it.hasNext();) { |
---|
145 | CMDComponentType comp = (CMDComponentType)it.next(); |
---|
146 | System.out.println(comp.getName() + " " + comp.getConceptLink() + "\n"); |
---|
147 | |
---|
148 | } |
---|
149 | |
---|
150 | System.out.println("elements:"); |
---|
151 | for (Iterator it = testcomp.elements().iterator(); |
---|
152 | it.hasNext();) { |
---|
153 | CMDElementType comp = (CMDElementType)it.next(); |
---|
154 | System.out.println(comp.getName() + " " + comp.getConceptLink() + "\n"); |
---|
155 | |
---|
156 | } |
---|
157 | */ |
---|
158 | } |
---|
159 | |
---|
160 | /* |
---|
161 | public static void testCache (InputStream instream) { |
---|
162 | String myKey = "myKey2"; |
---|
163 | String myValue; |
---|
164 | int myRefreshPeriod = 1000; |
---|
165 | GeneralCacheAdministrator admin = new GeneralCacheAdministrator(); |
---|
166 | System.out.print(admin.isMemoryCaching()); |
---|
167 | System.out.print(admin.getProperty("cache.path")); |
---|
168 | |
---|
169 | try { |
---|
170 | // Get from the cache |
---|
171 | myValue = (String) admin.getFromCache(myKey, myRefreshPeriod); |
---|
172 | System.out.println("value from cache:" + myValue); |
---|
173 | } catch (NeedsRefreshException nre) { |
---|
174 | try { |
---|
175 | // Get the value (probably from the database) |
---|
176 | // myValue = "This is the content retrieved."; |
---|
177 | // Store in the cache |
---|
178 | admin.putInCache(myKey, instream.toString()); |
---|
179 | System.out.println("freshfromcache: " ); |
---|
180 | System.out.println(admin.getFromCache(myKey, myRefreshPeriod)); |
---|
181 | //copyStreams((InputStream) admin.getFromCache(myKey, myRefreshPeriod),System.out); |
---|
182 | } catch (Exception ex) { |
---|
183 | // We have the current content if we want fail-over. |
---|
184 | // myValue = (String) nre.getCacheContent(); |
---|
185 | // It is essential that cancelUpdate is called if the |
---|
186 | // cached content is not rebuilt |
---|
187 | admin.cancelUpdate(myKey); |
---|
188 | } |
---|
189 | //System.out.println(myValue); |
---|
190 | } |
---|
191 | */ |
---|
192 | public static void loadConfig() { |
---|
193 | loadConfig(config_path); |
---|
194 | } |
---|
195 | |
---|
196 | public static void loadConfig(String configPath) { |
---|
197 | |
---|
198 | System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); |
---|
199 | InputStream config_file; |
---|
200 | |
---|
201 | try { |
---|
202 | config_file = Admin.class.getClassLoader().getResourceAsStream(configPath); |
---|
203 | |
---|
204 | // System.out.println(config_file); |
---|
205 | if (config_file == null) { |
---|
206 | Admin.notifyUser("CONFIGURATION ERROR: Properties file not found!"); |
---|
207 | } else { |
---|
208 | |
---|
209 | System.out.println("Reading configuration from: " + Admin.class.getClassLoader().getResource(configPath)); |
---|
210 | //config_file = new FileInputStream(configPath); |
---|
211 | config = new Properties(); |
---|
212 | config.load(config_file); |
---|
213 | } |
---|
214 | |
---|
215 | } catch (FileNotFoundException e) { |
---|
216 | // TODO Auto-generated catch block |
---|
217 | e.printStackTrace(); |
---|
218 | } catch (IOException e) { |
---|
219 | // TODO Auto-generated catch block |
---|
220 | e.printStackTrace(); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | public static Properties getConfig() { |
---|
226 | if (config==null) { |
---|
227 | loadConfig(config_path); |
---|
228 | } |
---|
229 | return config; |
---|
230 | } |
---|
231 | |
---|
232 | public static void notifyUser(String msg ) { |
---|
233 | notifyUser(msg, Admin.DEBUG); |
---|
234 | } |
---|
235 | public static void notifyUser(String msg, String type ) { |
---|
236 | //Logger log = Logger.getLogger("Admin.class"); |
---|
237 | if (msg.length() == 0){ |
---|
238 | return; |
---|
239 | } |
---|
240 | if (type.equals(Admin.FATAL)) { |
---|
241 | log.fatal(msg); |
---|
242 | } |
---|
243 | if (type.equals(Admin.ERROR)) { |
---|
244 | log.error(msg); |
---|
245 | } |
---|
246 | if (type.equals(Admin.WARN)) { |
---|
247 | log.warn(msg); |
---|
248 | } |
---|
249 | if (type.equals(Admin.INFO)) { |
---|
250 | log.info(msg); |
---|
251 | } |
---|
252 | if (type.equals(Admin.DEBUG)) { |
---|
253 | log.debug(msg); |
---|
254 | } |
---|
255 | if (type.equals(Admin.TRACE)) { |
---|
256 | log.trace(msg); |
---|
257 | } |
---|
258 | //System.out.println(msg); |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | public static InputStream loadFile(String path) { |
---|
263 | |
---|
264 | InputStream file=null; |
---|
265 | |
---|
266 | try { |
---|
267 | file = Admin.class.getClassLoader().getResourceAsStream(path); |
---|
268 | |
---|
269 | // System.out.println(config_file); |
---|
270 | if (file == null) { |
---|
271 | Admin.notifyUser("File not found!: " + path); |
---|
272 | } else { |
---|
273 | |
---|
274 | System.out.println("Reading in: " + Admin.class.getClassLoader().getResource(path)); |
---|
275 | //config_file = new FileInputStream(configPath); |
---|
276 | // Properties test = new Properties(); |
---|
277 | // test.load(file); |
---|
278 | } |
---|
279 | /* |
---|
280 | } catch (FileNotFoundException e) { |
---|
281 | // TODO Auto-generated catch block |
---|
282 | e.printStackTrace(); |
---|
283 | */ |
---|
284 | } catch (Exception e) { |
---|
285 | // TODO Auto-generated catch block |
---|
286 | e.printStackTrace(); |
---|
287 | } |
---|
288 | |
---|
289 | return file; |
---|
290 | } |
---|
291 | |
---|
292 | |
---|
293 | public static Document getXMLDocument(String path) { |
---|
294 | Document retdoc=null; |
---|
295 | try { |
---|
296 | |
---|
297 | DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); |
---|
298 | DocumentBuilder docBuilder; |
---|
299 | try { |
---|
300 | docBuilder = docFactory.newDocumentBuilder(); |
---|
301 | try { |
---|
302 | Admin.notifyUser("trying to read from:" + path); |
---|
303 | InputStream xmlstream = loadFile(path); |
---|
304 | //retdoc= docBuilder.parse(fname); |
---|
305 | retdoc= docBuilder.parse(xmlstream); |
---|
306 | Admin.notifyUser("read in:" + retdoc); |
---|
307 | } catch (SAXException e) { |
---|
308 | // TODO Auto-generated catch block |
---|
309 | Admin.notifyUser(e.getMessage()); |
---|
310 | e.printStackTrace(); |
---|
311 | } |
---|
312 | } catch (ParserConfigurationException e) { |
---|
313 | // TODO Auto-generated catch block |
---|
314 | Admin.notifyUser(e.getMessage()); |
---|
315 | e.printStackTrace(); |
---|
316 | } |
---|
317 | |
---|
318 | |
---|
319 | } catch (IOException ex){ |
---|
320 | //Admin.notifyUser("initCacheCounter:" + ex.toString()); |
---|
321 | ex.printStackTrace(); |
---|
322 | } catch (Exception e) { |
---|
323 | Admin.notifyUser("Admin.getXMLDocument():" + e.getMessage()); |
---|
324 | } |
---|
325 | return retdoc; |
---|
326 | } |
---|
327 | |
---|
328 | |
---|
329 | |
---|
330 | public static void copyStreams (InputStream in, OutputStream out) throws IOException, InterruptedException { |
---|
331 | |
---|
332 | byte[] buffer = new byte[1024]; |
---|
333 | int len = in.read(buffer); |
---|
334 | while (len != -1) { |
---|
335 | out.write(buffer, 0, len); |
---|
336 | len = in.read(buffer); |
---|
337 | if (Thread.interrupted()) { |
---|
338 | throw new InterruptedException(); |
---|
339 | } |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | public static void writeToFile (String path, InputStream in) { |
---|
344 | try |
---|
345 | { |
---|
346 | File f=new File(path); |
---|
347 | OutputStream out=new FileOutputStream(f); |
---|
348 | copyStreams(in,out); |
---|
349 | out.close(); |
---|
350 | } |
---|
351 | catch (Exception e){ |
---|
352 | Admin.notifyUser("ERROR writeToFile"); |
---|
353 | e.printStackTrace(); |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | public static void testJSON() { |
---|
358 | Admin.notifyUser("JSON TEST"); |
---|
359 | String str = "{'name':'JSON','integer':1,'double':2.0,'boolean':true,'nested':{'id':42},'array':[1,2,3]}"; |
---|
360 | Query q = new Query("dc.title=dino or dinosaur", Query.RECORDSET); |
---|
361 | JSON json = JSONSerializer.toJSON( str); |
---|
362 | XMLSerializer xmlSerializer = new XMLSerializer(); |
---|
363 | String xml = xmlSerializer.write( json ); |
---|
364 | Admin.notifyUser(xml); |
---|
365 | } |
---|
366 | |
---|
367 | } |
---|
368 | |
---|
369 | |
---|