001: /*
002: * snapper
003: *
004: * Enhydra super-servlet
005: *
006: */
007:
008: package org.enhydra.snapper;
009:
010: import java.io.File;
011: import java.io.InputStream;
012: import java.util.Properties;
013:
014: import org.enhydra.snapper.spec.DocumentGroup;
015: import org.enhydra.snapper.spec.DocumentGroupFactory;
016: import org.enhydra.snapper.spec.SiteList;
017: import org.enhydra.snapper.spec.SiteListFactory;
018:
019: import com.lutris.appserver.server.ApplicationException;
020: import com.lutris.appserver.server.StandardApplication;
021: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
022: import com.lutris.logging.LogChannel;
023: import com.lutris.logging.Logger;
024: import com.lutris.util.Config;
025: import com.lutris.util.ConfigException;
026:
027: /**
028: * The application object.
029: *
030: * Application-wide data would go here.
031: */
032:
033: public class Snapper extends StandardApplication {
034:
035: protected static String version = "v1.1";
036: protected static String indexDir;
037: protected static String searcherClassName;
038: protected static String xslDirectory;
039: protected static String metaURL;
040: protected static int maxRange = 50;
041: protected static String relativeIndexPath = "false";
042: protected static String parseOnFly = "true";
043: protected static String fileSeparator = "system-dependent";
044: protected static String highlightFuzzyQuery = "true";
045: protected static String searchDatePattern = "yyyyMMdd";
046: protected static String resultDatePattern = "yyyy.MM.dd";
047:
048: protected static String googleClientKey = "";
049: protected static String googleServiceURL = "";
050:
051: protected static String simpleSearchType = "Default";
052:
053: protected static int googleResultLimit;
054:
055: public static LogChannel log;
056:
057: protected static String previewURL = null;
058:
059: protected static String siteConfFile = null;
060: protected static String documentGroupConfFile = null;
061: protected static String statisticDirectory = null;
062: protected static boolean toLogStatistic = false;
063: public Properties prop;
064: public SnapperManager snapperManager;
065:
066: protected static int reReadConfigFilesEveryMinutes;
067:
068: private Thread reader = null;
069:
070: /*
071: * A few methods you might want to add to.
072: * See StandardApplication for more details.
073: */
074: public void startup(Config appConfig) throws ApplicationException {
075: super .startup(appConfig);
076:
077: /* try {
078: Constructor c = getClass().getClassLoader().loadClass( appConfig.getString("LogClassName") ).getConstructor(new Class[] { Boolean.TYPE });
079: Logger logger = (Logger) c.newInstance(new Object[] { new Boolean(true) });
080: log = logger.getChannel("org.enhydra.snapper.SnapperSearcher");
081: } catch (Exception e) {
082: System.err.println("Logger is not initialized properly!");
083: } */
084:
085: log = this .getLogChannel();
086:
087: // Here is where you would read application-specific settings from
088: // your config file.
089: // configureFromJar();
090: Properties prop = new Properties();
091: try {
092: prop.put("SearcherFactoryClassName", appConfig
093: .getString("Snapper.SearcherFactoryClassName"));
094: prop.put("LoggingManagerClassName", appConfig
095: .getString("Snapper.LoggingManagerClassName"));
096: prop.put("LoggerName", "SnapperSearcher.SnapperManager");
097: prop
098: .put(
099: "GoogleSearcherFactoryClassName",
100: appConfig
101: .getString("Snapper.GoogleSearcherFactoryClassName"));
102: prop.put("GoogleSearcherURL", appConfig
103: .getString("Snapper.GoogleSearcherURL"));
104: prop.put("GoogleSearcherKey", appConfig
105: .getString("Snapper.GoogleSearcherKey"));
106:
107: } catch (ConfigException except) {
108: throw new ApplicationException(except);
109: }
110:
111: SnapperManager.getInstance().init(prop);
112:
113: try {
114: indexDir = appConfig.getString("Snapper.IndexDir");
115: } catch (ConfigException e) {
116: if (logChannel != null) {
117: logChannel
118: .write(Logger.INFO,
119: "Snapper.IndexDir application parameter isn't properly initialized!");
120:
121: }
122: }
123:
124: try {
125: xslDirectory = appConfig.getString("Snapper.Xsl.Directory");
126: File temp = new File(xslDirectory);
127:
128: if (!temp.isDirectory()) {
129: temp = new File(appConfig.getConfigFile().getFile()
130: .getParent()
131: + File.separator + xslDirectory);
132:
133: if (!temp.isDirectory()) {
134: /* if (logChannel != null)
135: logChannel.write(Logger.INFO, "Snapper.xsl.Directory application parameter isn't properly initialized!");
136: */
137: } else {
138: try {
139: xslDirectory = temp.getCanonicalPath();
140: } catch (Exception e) {
141: logChannel
142: .write(Logger.INFO,
143: "Snapper.xsl.Directory application parameter isn't properly initialized!");
144: }
145: }
146: }
147:
148: } catch (ConfigException e) {
149: if (logChannel != null) {
150: logChannel
151: .write(Logger.INFO,
152: "Snapper.xsl.Directory application parameter isn't properly initialized!");
153:
154: }
155: }
156:
157: try {
158: metaURL = appConfig.getString("Snapper.MetaURL");
159: } catch (ConfigException e) {
160: if (logChannel != null) {
161: logChannel
162: .write(Logger.INFO,
163: "Snapper.MetaURL application parameter isn't properly initialized!");
164: }
165: }
166:
167: try {
168: maxRange = appConfig.getInt("Snapper.MaxRange");
169: } catch (ConfigException e) {
170: if (logChannel != null) {
171: logChannel
172: .write(Logger.INFO,
173: "Snapper.IndexDir application parameter isn't properly initialized!");
174:
175: }
176: }
177:
178: try {
179: simpleSearchType = appConfig
180: .getString("Snapper.SimpleSearch.Type");
181: } catch (ConfigException e) {
182: //if (logChannel != null) {
183: //logChannel.write(Logger.INFO, "Snapper.IndexDir application parameter isn't properly initialized!");
184:
185: }
186: try {
187: relativeIndexPath = appConfig
188: .getString("Snapper.RelativeIndexPath");
189: } catch (ConfigException e) {
190: if (logChannel != null) {
191: logChannel
192: .write(Logger.DEBUG,
193: "Snapper.RelativeIndexPath application parameter isn't properly initialized!");
194:
195: }
196: }
197:
198: try {
199: parseOnFly = appConfig.getString("Snapper.ParseOnFly");
200: } catch (ConfigException e) {
201: if (logChannel != null) {
202: logChannel
203: .write(Logger.DEBUG,
204: "Snapper.ParseOnFly application parameter isn't properly initialized!");
205:
206: }
207: }
208:
209: try {
210: fileSeparator = appConfig
211: .getString("Snapper.FileSeparator");
212: } catch (ConfigException e) {
213: if (logChannel != null) {
214: logChannel
215: .write(
216: Logger.DEBUG,
217: "Snapper.FileSeparator application parameter isn't properly initialized! Default value is used!");
218:
219: }
220: }
221:
222: try {
223: highlightFuzzyQuery = appConfig
224: .getString("Snapper.HighlightFuzzyQuery");
225: } catch (ConfigException e) {
226: highlightFuzzyQuery = "true";
227: }
228:
229: /* try {
230: indexType = appConfig.getString("Snapper.IndexType", "FileSystem");
231: } catch (ConfigException e) {
232: indexType = "FileSystem";
233: }*/
234:
235: try {
236: searchDatePattern = appConfig.getString(
237: "Snapper.SearchDatePattern", "yyyyMMdd");
238: } catch (ConfigException e) {
239: searchDatePattern = "yyyyMMdd";
240: }
241:
242: try {
243: resultDatePattern = appConfig.getString(
244: "Snapper.ResultDatePattern", "yyyy.MM.dd");
245: } catch (ConfigException e) {
246: resultDatePattern = "yyyyMMdd";
247: }
248:
249: try {
250: previewURL = appConfig.getString("Snapper.PreviewURL");
251: previewURL = previewURL + "?parse=" + parseOnFly;
252: } catch (Exception e) {
253: previewURL = null;
254: }
255:
256: try {
257: googleClientKey = appConfig.getString(
258: "Snapper.GoogleClientKey", "");
259: } catch (ConfigException e) {
260: googleClientKey = "";
261: }
262:
263: try {
264: googleServiceURL = appConfig.getString(
265: "Snapper.GoogleServiceURL",
266: "http://api.google.com/search/beta2");
267: } catch (ConfigException e) {
268: googleServiceURL = "http://api.google.com/search/beta2";
269: }
270:
271: try {
272: googleResultLimit = appConfig.getInt(
273: "Snapper.GoogleResultLimit", 10);
274: } catch (ConfigException e) {
275: googleResultLimit = 10;
276: }
277:
278: try {
279: reReadConfigFilesEveryMinutes = appConfig.getInt(
280: "Snapper.ReReadConfigFilesEveryMinutes", 5);
281: } catch (ConfigException e) {
282: reReadConfigFilesEveryMinutes = 5;
283: }
284:
285: try {
286: siteConfFile = appConfig.getString("Snapper.SiteConfFile");
287: File temp = new File(siteConfFile);
288:
289: if (!(temp.isFile() && temp.exists()))
290: temp = new File(System.getProperty("catalina.base"),
291: siteConfFile);
292:
293: if (temp.isFile() && temp.exists()) {
294: siteConfFile = temp.getCanonicalPath();
295: } else {
296: log
297: .write(Logger.WARNING,
298: " 'Snapper.SiteConfFile' application parameter isn't properly initialized!");
299: siteConfFile = null;
300: }
301:
302: } catch (Exception e) {
303: log
304: .write(Logger.WARNING,
305: " 'Snapper.SiteConfFile' application parameter isn't properly initialized!");
306: siteConfFile = null;
307: }
308:
309: try {
310: documentGroupConfFile = appConfig
311: .getString("Snapper.DocumentGroupConfFile");
312:
313: File temp = new File(documentGroupConfFile);
314:
315: if (!(temp.isFile() && temp.exists()))
316: temp = new File(System.getProperty("catalina.base"),
317: documentGroupConfFile);
318:
319: if (temp.isFile() && temp.exists()) {
320: documentGroupConfFile = temp.getCanonicalPath();
321: } else {
322: log
323: .write(
324: Logger.WARNING,
325: " 'Snapper.DocumentGroupConfFile' application parameter isn't properly initialized!");
326: documentGroupConfFile = null;
327: }
328:
329: } catch (Exception e) {
330: log
331: .write(
332: Logger.WARNING,
333: " 'Snapper.DocumentGroupConfFile' application parameter isn't properly initialized! ");
334: documentGroupConfFile = null;
335: }
336:
337: try {
338: SiteList sl = SiteListFactory
339: .getSiteList("org.enhydra.snapper.business.SiteListImpl");
340:
341: sl.init(siteConfFile, documentGroupConfFile);
342: reader = new Thread(sl);
343: reader.start();
344:
345: } catch (Exception e1) {
346: log.write(Logger.ERROR,
347: " Problem in configuration section : "
348: + e1.getMessage());
349:
350: }
351:
352: try {
353: toLogStatistic = appConfig.getBoolean(
354: "Snapper.StatisticActive", false);
355: } catch (ConfigException e) {
356: toLogStatistic = false;
357: }
358:
359: try {
360: statisticDirectory = appConfig
361: .getString("Snapper.StatisticDirectory");
362:
363: File tempFile = new File(statisticDirectory);
364:
365: if (tempFile != null && tempFile.isDirectory()) {
366: if (!tempFile.exists())
367: tempFile.mkdir();
368:
369: statisticDirectory = tempFile.getPath();
370:
371: if (!(statisticDirectory.endsWith("/") || statisticDirectory
372: .endsWith("\\")))
373: statisticDirectory = statisticDirectory
374: + File.separator;
375:
376: } else {
377:
378: String temp = appConfig.getConfigFile().getFile()
379: .getParentFile().getParentFile()
380: .getParentFile().getParent()
381: + File.separator + statisticDirectory;
382:
383: tempFile = new File(temp);
384:
385: if (!tempFile.exists())
386: tempFile.mkdir();
387:
388: statisticDirectory = tempFile.getPath();
389:
390: if (!(statisticDirectory.endsWith("/") || statisticDirectory
391: .endsWith("\\")))
392: statisticDirectory = statisticDirectory
393: + File.separator;
394:
395: }
396:
397: } catch (Exception e) {
398: String temp = appConfig.getConfigFile().getFile()
399: .getParentFile().getParentFile().getParentFile()
400: .getParent()
401: + File.separator + statisticDirectory;
402:
403: File tempFile = new File(temp);
404:
405: if (!tempFile.exists())
406: tempFile.mkdir();
407:
408: statisticDirectory = tempFile.getPath();
409:
410: if (!(statisticDirectory.endsWith("/") || statisticDirectory
411: .endsWith("\\")))
412: statisticDirectory = statisticDirectory
413: + File.separator;
414: }
415:
416: try {
417: log.write(Logger.INFO, " Snapper " + version + " started");
418: } catch (Exception e) {
419: }
420:
421: }
422:
423: public boolean requestPreprocessor(HttpPresentationComms comms)
424: throws Exception {
425: return super .requestPreprocessor(comms);
426: }
427:
428: public synchronized void shutdown() {
429: try {
430: if (reader != null) {
431: SiteList sl = SiteListFactory
432: .getSiteList("org.enhydra.snapper.business.SiteListImpl");
433: sl.stopThread();
434: reader.interrupt();
435: reader = null;
436: }
437: } catch (Exception e) {
438: }
439: super .shutdown();
440: }
441:
442: public String toHtml() {
443: return "This is <I>snapper</I>";
444: }
445:
446: public static String getIndexDir() {
447: return indexDir;
448: }
449:
450: public static String getSearcherClassName() {
451: return searcherClassName;
452: }
453:
454: public static String getXslDirectory() {
455: return xslDirectory;
456: }
457:
458: public static String getMetaURL() {
459: return metaURL;
460: }
461:
462: public static String getSimpleSearchType() {
463: return simpleSearchType;
464: }
465:
466: public static int getMaxRange() {
467: return maxRange;
468: }
469:
470: public static String getRelativeIndexPath() {
471: return relativeIndexPath;
472: }
473:
474: public static String getFileSeparatorConvention() {
475: return fileSeparator;
476: }
477:
478: public static String getHighlightFuzzyQuery() {
479: return highlightFuzzyQuery;
480: }
481:
482: private void configureFromJar() {
483: //String rootDirectoryPath=System.getProperty("user.dir");
484: try {
485:
486: InputStream is = Snapper.class.getClassLoader()
487: .getResourceAsStream("snapperConf.forJar");
488:
489: prop = new Properties();
490: prop.load(is);
491:
492: } catch (Exception ex) {
493: ex.printStackTrace();
494: System.out
495: .println("Snapper needs to be configured properly - Can't read Snapper's default configuration from JAR!!!");
496: }
497: }
498:
499: public static LogChannel getLogChanel() {
500: return log;
501: }
502:
503: public static String getPreviewURL() {
504: return previewURL;
505: }
506:
507: public static void setPreviewURL(String value) {
508: previewURL = value;
509: }
510:
511: public static String getGoogleServiceURL() {
512: return googleServiceURL;
513: }
514:
515: public static String getGoogleClientKey() {
516: return googleClientKey;
517: }
518:
519: public static String getSearchDatePattern() {
520: return searchDatePattern;
521: }
522:
523: public static String getResultDatePattern() {
524: return resultDatePattern;
525: }
526:
527: public static int getGoogleResultLimit() {
528: return googleResultLimit;
529: }
530:
531: public static String getStatisticDirectory() {
532: return statisticDirectory;
533: }
534:
535: public static boolean getStatisticActive() {
536: return toLogStatistic;
537: }
538:
539: public static int getReReadConfigFilesEveryMinutes() {
540: return reReadConfigFilesEveryMinutes;
541: }
542:
543: public static String getParseOnFly() {
544: return parseOnFly;
545: }
546: }
|