001: /*
002: * Milin Radivoj
003: * rmilin@gmail.com
004: */
005:
006: package org.enhydra.snapper.business;
007:
008: import java.io.File;
009: import java.util.ArrayList;
010: import java.util.Iterator;
011:
012: import org.apache.xmlbeans.XmlOptions;
013: import org.enhydra.snapper.Snapper;
014: import org.enhydra.snapper.xml.configuration.DOCUMENTGROUPLISTDocument;
015: import org.enhydra.snapper.xml.configuration.SITELISTDocument;
016: import org.enhydra.snapper.xml.configuration.DOCUMENTGROUPLISTDocument.DOCUMENTGROUPLIST;
017: import org.enhydra.snapper.xml.configuration.PATHDocument.PATH;
018: import org.enhydra.snapper.xml.configuration.SITEDocument.SITE;
019: import org.enhydra.snapper.business.DocumentGroupImpl;
020: import org.enhydra.snapper.business.PathImpl;
021: import org.enhydra.snapper.business.SiteImpl;
022: import org.enhydra.snapper.spec.DocumentGroup;
023: import org.enhydra.snapper.spec.Site;
024: import org.enhydra.snapper.spec.SiteList;
025:
026: import com.lutris.logging.Logger;
027:
028: /**
029: * @author Milin Radivoj
030: */
031:
032: public class SiteListImpl implements SiteList {
033:
034: private static Site[] listOFSites = null;
035:
036: private static String confFilePath;
037:
038: private static String documentGroupConfFilePath;
039:
040: private static int forAllMAXSIZE = 99999;
041:
042: private static int forAllMAXAGE = 99999;
043:
044: private static String forAllLANGUAGE = "DE";
045:
046: private static boolean forAllSEARCH = true;
047:
048: private static boolean forDOWNLOAD = true;
049:
050: private static boolean forAllINDEXCONTENT = true;
051:
052: private static boolean forAllINDEXDIRECTORY = false;
053:
054: private static boolean forAllINDEXUNKNOWNFILETYPES = false;
055:
056: public static DocumentGroup forAllDocumentGroup = null;
057:
058: private static boolean fromFile = false;
059:
060: public static boolean stop = false;
061:
062: public Site[] getList() {
063: return listOFSites;
064: }
065:
066: public Site findSiteByName(String name) {
067:
068: if (name != null && listOFSites != null) {
069: for (int i = 0; i < listOFSites.length; i++) {
070: if (name != null
071: && name.equalsIgnoreCase(listOFSites[i]
072: .getNAME()))
073: return listOFSites[i];
074: }
075: }
076: return null;
077: }
078:
079: public DocumentGroup getDefaultDocumentGroup() {
080: return forAllDocumentGroup;
081: }
082:
083: public void init(String confFilePath,
084: String documentGroupConfFilePath) {
085:
086: if (confFilePath == null) {
087: Snapper.log
088: .write(Logger.ERROR,
089: " Missing path to configuration file, Aborting with configuration process !!!");
090: return;
091: }
092:
093: this .confFilePath = confFilePath;
094: this .documentGroupConfFilePath = documentGroupConfFilePath;
095:
096: SITELISTDocument listFromFile = null;
097: DocumentGroupImpl forAllDocumentGroupTemp = new DocumentGroupImpl();
098: try {
099:
100: XmlOptions m_validationOptions;
101: ArrayList validationErrors = new ArrayList();
102: m_validationOptions = new XmlOptions();
103: m_validationOptions.setErrorListener(validationErrors);
104: listFromFile = SITELISTDocument.Factory.parse(new File(
105: confFilePath));
106:
107: boolean isValid = listFromFile
108: .validate(m_validationOptions);
109:
110: if (!isValid) {
111:
112: Iterator iter = validationErrors.iterator();
113: while (iter.hasNext()) {
114: Snapper.log.write(Logger.ERROR, "" + iter.next());
115:
116: }
117: Snapper.log
118: .write(Logger.ERROR,
119: " Validation of document faild , aborting with configuration process !!!");
120:
121: //forAllDocumentGroup.setIsDefaultDocGroup(true);
122: try {
123: if (documentGroupConfFilePath != null) {
124: ((DocumentGroupImpl) forAllDocumentGroupTemp)
125: .init(DOCUMENTGROUPLISTDocument.Factory
126: .parse(
127: new File(
128: documentGroupConfFilePath))
129: .getDOCUMENTGROUPLIST());
130: } else
131: forAllDocumentGroupTemp = null;
132: } catch (Exception e) {
133: Snapper.log
134: .write(
135: Logger.ERROR,
136: " Problem ocured while reading conf file : "
137: + documentGroupConfFilePath
138: + " with message : "
139: + e.getMessage()
140: + " , aborting with configuration process !!!");
141: }
142: return;
143: }
144: } catch (Exception e) {
145: Snapper.log
146: .write(
147: Logger.ERROR,
148: " Problem ocured while reading conf file : "
149: + confFilePath
150: + " with message : "
151: + e.getMessage()
152: + " , aborting with configuration process !!!");
153: forAllDocumentGroupTemp = new DocumentGroupImpl();
154:
155: //forAllDocumentGroup.setIsDefaultDocGroup(true);
156: try {
157: if (documentGroupConfFilePath != null) {
158: ((DocumentGroupImpl) forAllDocumentGroupTemp)
159: .init(DOCUMENTGROUPLISTDocument.Factory
160: .parse(
161: new File(
162: documentGroupConfFilePath))
163: .getDOCUMENTGROUPLIST());
164: } else
165: forAllDocumentGroupTemp = null;
166: } catch (Exception exx) {
167: Snapper.log
168: .write(
169: Logger.ERROR,
170: " Problem ocured while reading conf file : "
171: + documentGroupConfFilePath
172: + " with message : "
173: + e.getMessage()
174: + " , aborting with configuration process !!!");
175: }
176: return;
177: }
178:
179: int size = listFromFile.getSITELIST().sizeOfSITEArray();
180:
181: if (size < 1) {
182: Snapper.log.write(Logger.INFO, "None Site is defined");
183: }
184:
185: Site[] listOFSitesTemp = new SiteImpl[size];
186:
187: // initialize root Document group
188:
189: //forAllDocumentGroup.setIsDefaultDocGroup(true);
190:
191: if (!listFromFile.getSITELIST().isSetDOCUMENTGROUPLIST()) {
192: fromFile = true;
193: //Snapper.log.write(Logger.INFO, " DOCUMENTGROUPLIST for all sites is not defined, using default list : " + documentGroupConfFilePath);
194: try {
195: if (documentGroupConfFilePath != null) {
196: ((DocumentGroupImpl) forAllDocumentGroupTemp)
197: .init(DOCUMENTGROUPLISTDocument.Factory
198: .parse(
199: new File(
200: documentGroupConfFilePath))
201: .getDOCUMENTGROUPLIST());
202: } else
203: forAllDocumentGroupTemp = null;
204: } catch (Exception e) {
205: Snapper.log
206: .write(
207: Logger.ERROR,
208: " Problem ocured while reading conf file : "
209: + documentGroupConfFilePath
210: + " with message : "
211: + e.getMessage()
212: + " , aborting with configuration process !!!");
213: }
214:
215: } else {
216: ((DocumentGroupImpl) forAllDocumentGroupTemp)
217: .init(listFromFile.getSITELIST()
218: .getDOCUMENTGROUPLIST());
219: }
220:
221: forAllDocumentGroup = forAllDocumentGroupTemp;
222:
223: // collect top default values from xml file
224: forAllMAXSIZE = listFromFile.getSITELIST().getMAXSIZE();
225: forAllMAXAGE = listFromFile.getSITELIST().getMAXAGE();
226: String temp = listFromFile.getSITELIST().getLANGUAGE()
227: .toString();
228: if (temp != null) {
229: forAllLANGUAGE = temp;
230: }
231: forAllSEARCH = listFromFile.getSITELIST().getSEARCH();
232: forDOWNLOAD = listFromFile.getSITELIST().getDOWNLOAD();
233: forAllINDEXCONTENT = listFromFile.getSITELIST()
234: .getINDEXCONTENT();
235: forAllINDEXDIRECTORY = listFromFile.getSITELIST()
236: .getINDEXDIRECTORY();
237: forAllINDEXUNKNOWNFILETYPES = listFromFile.getSITELIST()
238: .getINDEXUNKNOWNFILETYPES();
239:
240: for (int i = 0; i < size; i++) {
241:
242: SITE confFileSite = (SITE) listFromFile.getSITELIST()
243: .getSITEArray(i);
244: SiteImpl curentSite = new SiteImpl();
245: // initialize default values
246: curentSite.setMAXSIZE(forAllMAXSIZE);
247: curentSite.setMAXAGE(forAllMAXAGE);
248: curentSite.setLANGUAGE(forAllLANGUAGE);
249: curentSite.setSEARCH(forAllSEARCH);
250: curentSite.setDOWNLOAD(forDOWNLOAD);
251: curentSite.setINDEXCONTENT(forAllINDEXCONTENT);
252: curentSite.setINDEXDIRECTORY(forAllINDEXDIRECTORY);
253: curentSite
254: .setINDEXUNKNOWNFILETYPES(forAllINDEXUNKNOWNFILETYPES);
255:
256: if (confFileSite.getNAME() != null) {
257: boolean detected = false;
258: for (int k = 0; k < listOFSitesTemp.length; k++) {
259:
260: if (null != listOFSitesTemp[k]) {
261: String existingNameOfSite = listOFSitesTemp[k]
262: .getNAME();
263:
264: if (existingNameOfSite.equals(confFileSite
265: .getNAME())) {
266: Snapper.log
267: .write(
268: Logger.WARNING,
269: "Site 'NAME' parameter :"
270: + existingNameOfSite
271: + " already exists, this Site definition will be ignored");
272: detected = true;
273: break;
274: }
275: }
276:
277: }
278: if (!detected)
279: curentSite.setNAME(confFileSite.getNAME());
280: else
281: continue;
282: } else {
283: Snapper.log
284: .write(
285: Logger.WARNING,
286: "Site 'NAME' parameter was not properly initialized , this Site definition will be ignored");
287: continue;
288: }
289:
290: if (confFileSite.isSetMAXSIZE()) {
291: curentSite.setMAXSIZE(confFileSite.getMAXSIZE());
292: }
293:
294: if (confFileSite.isSetMAXAGE()) {
295: curentSite.setMAXAGE(confFileSite.getMAXAGE());
296: }
297:
298: if (confFileSite.isSetLANGUAGE()) {
299: curentSite.setLANGUAGE(confFileSite.getLANGUAGE()
300: .toString());
301: }
302:
303: if (confFileSite.isSetSEARCH()) {
304: curentSite.setSEARCH(confFileSite.getSEARCH());
305: }
306:
307: if (confFileSite.isSetDOWNLOAD()) {
308: curentSite.setDOWNLOAD(confFileSite.getDOWNLOAD());
309: }
310:
311: if (confFileSite.isSetINDEXCONTENT()) {
312: curentSite.setINDEXCONTENT(confFileSite
313: .getINDEXCONTENT());
314: }
315:
316: if (confFileSite.isSetINDEXDIRECTORY()) {
317: curentSite.setINDEXDIRECTORY(confFileSite
318: .getINDEXDIRECTORY());
319: }
320:
321: if (confFileSite.isSetINDEXUNKNOWNFILETYPES()) {
322: curentSite.setINDEXUNKNOWNFILETYPES(confFileSite
323: .getINDEXUNKNOWNFILETYPES());
324: }
325:
326: if (confFileSite.isSetINDEXDIR()) {
327: String path = confFileSite.getINDEXDIR();
328:
329: if (path != null)
330: curentSite.setINDEXDIR(path);
331: else {
332: String dir = Snapper.getIndexDir();
333:
334: if (dir.endsWith("\\") || dir.endsWith("/"))
335: dir = dir + confFileSite.getNAME();
336: else
337: dir = dir + File.separator
338: + confFileSite.getNAME();
339:
340: curentSite.setINDEXDIR(dir);
341: Snapper.log
342: .write(
343: Logger.INFO,
344: " For Site :"
345: + curentSite.getNAME()
346: + " 'INDEXDIR' parameter was not properly initialized, value '"
347: + dir + "' will be used");
348:
349: }
350:
351: } else {
352: String dir = Snapper.getIndexDir();
353:
354: if (dir.endsWith("\\") || dir.endsWith("/"))
355: dir = dir + confFileSite.getNAME();
356: else
357: dir = dir + File.separator + confFileSite.getNAME();
358:
359: Snapper.log
360: .write(
361: Logger.INFO,
362: " For Site :"
363: + curentSite.getNAME()
364: + " 'INDEXDIR' parameter is not defined, value '"
365: + dir + "' will be used");
366: curentSite.setINDEXDIR(dir);
367: }
368:
369: if (confFileSite.isSetFILTERDB()) {
370: curentSite.setFILTERDB(confFileSite.getFILTERDB()
371: .getNAME());
372: curentSite.setFILTERTABLE(confFileSite.getFILTERDB()
373: .getFILTERTABLE());
374: curentSite.setFILTERCOLUMN(confFileSite.getFILTERDB()
375: .getFILTERCOLUMN());
376: }
377:
378: if (confFileSite.isSetINCLUDEDB()) {
379: curentSite.setINCLUDEDB(confFileSite.getINCLUDEDB()
380: .getNAME());
381: curentSite.setINCLUDETABLE(confFileSite.getINCLUDEDB()
382: .getINCLUDETABLE());
383: curentSite.setINCLUDECOLUMN(confFileSite.getINCLUDEDB()
384: .getINCLUDECOLUMN());
385: curentSite.setINCLUDECOLUMNMODIFIED(confFileSite
386: .getINCLUDEDB().getINCLUDECOLUMNMODIFIED());
387: }
388:
389: if (confFileSite.isSetMETADB()) {
390: curentSite
391: .setMETADB(confFileSite.getMETADB().getNAME());
392: curentSite.setMETATABLE(confFileSite.getMETADB()
393: .getMETATABLE());
394: curentSite.setMETAFILE(confFileSite.getMETADB()
395: .getMETAFILECOLUMN());
396: curentSite.setMETAKEY(confFileSite.getMETADB()
397: .getMETAKEYCOLUMN());
398: curentSite.setMETAVALUE(confFileSite.getMETADB()
399: .getMETAVALUECOLUMN());
400: }
401:
402: int pathListSize = 0;
403:
404: try {
405: pathListSize = confFileSite.getPATHLIST()
406: .sizeOfPATHArray();
407: } catch (Exception e) {
408: pathListSize = 0;
409: }
410:
411: PathImpl[] pathList = null;
412:
413: if (pathListSize > 0) {
414: pathList = new PathImpl[pathListSize];
415:
416: for (int j = 0; j < pathListSize; j++) {
417: PATH zeusPath = (PATH) confFileSite.getPATHLIST()
418: .getPATHArray(j);
419: PathImpl pathImpl = new PathImpl();
420:
421: pathImpl.setType(zeusPath.getPATHTYPE().toString());
422: pathImpl.setRoot(zeusPath.getROOT());
423:
424: if (zeusPath.isSetHOST())
425: pathImpl.setHost(zeusPath.getHOST());
426: else
427: pathImpl.setHost("");
428:
429: // if (zeusPath.isSetPORT())
430: // pathImpl.setPort(zeusPath.getPORT());
431: // else
432: // pathImpl.setPort("");
433:
434: if (zeusPath.isSetLOGINNAME())
435: pathImpl.setUser(zeusPath.getLOGINNAME());
436: else
437: pathImpl.setUser("");
438:
439: if (zeusPath.isSetPASSWORD())
440: pathImpl.setPass(zeusPath.getPASSWORD());
441: else
442: pathImpl.setPass("");
443:
444: if (zeusPath.isSetMAPPINGROOT())
445: pathImpl.setMappingRoot(zeusPath
446: .getMAPPINGROOT());
447: else
448: pathImpl.setMappingRoot("");
449:
450: pathList[j] = pathImpl;
451:
452: }
453:
454: }
455:
456: if (pathList != null) {
457: curentSite.setPathList(pathList);
458: }
459:
460: DOCUMENTGROUPLIST siteDGL = confFileSite
461: .getDOCUMENTGROUPLIST();
462:
463: if (siteDGL == null) {
464: curentSite.setDocumentGroup(forAllDocumentGroupTemp);
465: } else {
466: DocumentGroupImpl current = new DocumentGroupImpl();
467: ((DocumentGroupImpl) current).init(siteDGL);
468: curentSite.setDocumentGroup(current);
469: }
470:
471: listOFSitesTemp[i] = curentSite;
472: }
473:
474: if (listOFSitesTemp != null) {
475: int sciped = 0;
476: Site[] listOFSitesFinall = listOFSitesTemp;
477:
478: for (int k = 0; k < listOFSitesTemp.length; k++) {
479: if (null == listOFSitesTemp[k])
480: sciped++;
481: }
482:
483: if (sciped > 0) {
484: listOFSitesFinall = new Site[listOFSitesTemp.length
485: - sciped];
486: int z = 0;
487: for (int k = 0; k < listOFSitesTemp.length; k++) {
488: if (null != listOFSitesTemp[k]) {
489: listOFSitesFinall[z] = listOFSitesTemp[k];
490: z++;
491: }
492: }
493:
494: }
495:
496: listOFSites = listOFSitesFinall;
497: }
498:
499: }
500:
501: public void run() {
502: while (!stop) {
503: long timeToWait = (new Long(Snapper
504: .getReReadConfigFilesEveryMinutes() * 1000 * 60))
505: .longValue();
506: try {
507: Thread.sleep(timeToWait);
508: init(confFilePath, documentGroupConfFilePath);
509: } catch (Exception e) {
510:
511: }
512:
513: }
514:
515: }
516:
517: public void stopThread() {
518: stop = true;
519: }
520: }
|