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