001: /*
002: * Enhydra super-servlet
003: */
004:
005: package org.enhydra.dm;
006:
007: import java.lang.reflect.Array;
008: import java.util.HashMap;
009: import java.util.Map;
010: import java.util.Properties;
011:
012: import org.enhydra.dm.api.DocumentManager;
013: import org.enhydra.dm.api.DocumentStore;
014: import org.enhydra.dm.api.FoDocumentManager;
015: import org.enhydra.dm.api.UserManager;
016: import org.enhydra.dm.api.loggers.Log;
017: import org.enhydra.dm.api.util.DesEncription;
018: import org.enhydra.dm.business.DocumentManagerImpl;
019: import org.enhydra.dm.business.DocumentStoreImpl;
020: import org.enhydra.dm.business.FoDocumentManagerImpl;
021: import org.enhydra.dm.business.UserManagerImpl;
022: import org.enhydra.dm.util.Numerator;
023: import org.enhydra.dm.util.TransformUtility;
024: import org.enhydra.dm.util.filesys.PropertyChangeListenerImpl;
025:
026: import com.lutris.appserver.server.Application;
027: import com.lutris.appserver.server.ApplicationException;
028: import com.lutris.appserver.server.StandardApplication;
029: import com.lutris.appserver.server.httpPresentation.HttpPresentationComms;
030: import com.lutris.util.Config;
031: import com.lutris.util.ConfigException;
032:
033: /**
034: * The application object. Application-wide data would go here.
035: */
036: public class EnhydraDM extends StandardApplication {
037:
038: public static String userTransactionJNDIPath;
039:
040: public static final String defaultUserTransactionJNDIPath = "java:comp/env/UserTransaction";
041:
042: public static final String defaultLogImpl = "org.enhydra.dm.api.loggers.DefaultLog";
043:
044: public static final String defaultDocumentManagerImpl = DocumentManagerImpl.class
045: .getName();
046:
047: public static final String defaultDocumentStoreImpl = DocumentStoreImpl.class
048: .getName();
049:
050: public static final String defaultUserManagerImpl = UserManagerImpl.class
051: .getName();
052:
053: public static String allowedExtensionsForUpload;
054:
055: public static int allowedFileSizeForUpload;
056:
057: public static String previewerURL;
058:
059: public static String previewerXSL;
060:
061: public static final String defaultDesEncriptionImpl = null;
062:
063: public static Log logger;
064:
065: /**
066: * Descriptor / Encriptor
067: */
068: public static DesEncription desenc = null;
069:
070: public static Application myself = null;
071:
072: /**
073: * Document Manager
074: */
075: private DocumentManager documentManager = null;
076:
077: /**
078: * Document Manager
079: */
080: private DocumentStore documentStore = null;
081:
082: /**
083: * App Users Manager
084: */
085: private UserManager userManager = null;
086:
087: /**
088: * Label properties files path
089: */
090: private String propFilesPath = null;
091:
092: /**
093: * xsl and properties file refresh period set up in seconds.
094: */
095: private long fileRefreshPeriod = 0;
096:
097: /**
098: * Key is properties file name, value is appropriate
099: * <code>PropetiesChangeListener</code>
100: */
101: private Map propsMap = new HashMap();
102:
103: /**
104: * FoDocument Manager
105: */
106: private FoDocumentManager foDocumentManager = null;
107:
108: /*
109: * A few methods you might want to add to. See StandardApplication for more details.
110: */
111: public void startup(Config appConfig) throws ApplicationException {
112: super .startup(appConfig);
113: myself = this ;
114: // Calculating default document store path
115: Properties props = null;
116:
117: try {
118: props = readConfig(config);
119: initLogger(props);
120: userTransactionJNDIPath = appConfig
121: .getString(
122: "DatabaseManager.defaults.XATransactionManagerLookupName",
123: defaultUserTransactionJNDIPath);
124:
125: } catch (ConfigException e) {
126: userTransactionJNDIPath = defaultUserTransactionJNDIPath;
127: logger = Log.getInstance();
128:
129: }
130: if (null != logger) {
131: logger
132: .log(Log.INFORMATION,
133: "Logger for EnhydraDM is successfully initialized!");
134: }
135:
136: initDesEncriptor(props);
137: initUserManager(props);
138: initDocumentManager(props);
139: initDocumentStore(props);
140: setFileRefreshPeriod(appConfig);
141: initTransformUtility(props);
142: initPropFiles(appConfig);
143: initFoDocumentManager(appConfig);
144: setAllowedExtensionsForUpload(appConfig);
145: setAllowedFileSizeForUpload(appConfig);
146: setPreviewerURL(appConfig);
147: setPreviewerXSL(appConfig);
148: initNumerator(props);
149:
150: }
151:
152: private void setAllowedExtensionsForUpload(Config appConfig) {
153: try {
154: String temp = appConfig.getString("Upload.Extensions");
155: if (temp != null) {
156: allowedExtensionsForUpload = temp;
157: }
158: } catch (Exception ex) {
159: }
160: if (allowedExtensionsForUpload == null) {
161: if (null != logger) {
162: logger
163: .log(Log.WARNING,
164: "Allowed extensions for upload setup failed. Default value is set.");
165: }
166: allowedExtensionsForUpload = "doc, docx, xls, xslx, ppt, pptx, pps, ppsx, jpeg, msg, pdf, zip, gif, bmp, txt, tif, png";
167: }
168: }
169:
170: private void setAllowedFileSizeForUpload(Config appConfig) {
171: try {
172: int temp = appConfig.getInt("Upload.FileSize");
173: if (temp > 0) {
174: allowedFileSizeForUpload = temp;
175: }
176: } catch (Exception ex) {
177: }
178: if (allowedFileSizeForUpload <= 0) {
179: if (null != logger) {
180: logger
181: .log(Log.WARNING,
182: "Allowed file size for upload setup failed. Default value is set.");
183: }
184: allowedFileSizeForUpload = 2000000;
185: }
186:
187: }
188:
189: private void setPreviewerURL(Config appConfig) {
190: try {
191: String temp = appConfig.getString("Previewer.URL");
192: if (temp != null && !temp.equals("")) {
193: previewerURL = temp;
194: }
195: } catch (Exception ex) {
196: }
197: if (previewerURL == null) {
198: if (null != logger) {
199: logger
200: .log(Log.INFORMATION,
201: "No previewer URL is set. Using default value.");
202: }
203: previewerURL = "";
204: }
205: }
206:
207: private void setPreviewerXSL(Config appConfig) {
208: try {
209: String temp = appConfig.getString("Previewer.XSL");
210: if (temp != null && !temp.equals("")) {
211: previewerXSL = temp;
212: }
213: } catch (Exception ex) {
214: }
215: if (previewerXSL == null) {
216: if (null != logger) {
217: logger
218: .log(Log.INFORMATION,
219: "No previewer XSL is set. Using default value.");
220: }
221: previewerXSL = "preview";
222: }
223: }
224:
225: private void initLogger(Properties props)
226: throws ApplicationException {
227: String logProviderName = Log.class.getName();
228: try {
229: String logProvider = props.getProperty(logProviderName,
230: defaultLogImpl);
231: try {
232: logger = (Log) Class.forName(logProvider).newInstance();
233: Log.setLogger(logger);
234: } catch (Exception ex) {
235: logger = Log.getInstance();
236: }
237: } catch (Exception e) {
238: throw new ApplicationException(
239: "Init DesEncriptor exception!", e);
240: }
241: }
242:
243: private void initUserManager(Properties props)
244: throws ApplicationException {
245: try {
246: String userManagerImplClassName = props
247: .getProperty(UserManager.class.getName(),
248: defaultUserManagerImpl);
249: userManager = (UserManager) Class.forName(
250: userManagerImplClassName).newInstance();
251: if (null != logger) {
252: userManager.setLogger(logger);
253: }
254: } catch (Exception e) {
255: throw new ApplicationException(
256: "Init UserManager exception", e);
257: }
258: }
259:
260: private void initDocumentManager(Properties props)
261: throws ApplicationException {
262:
263: try {
264:
265: String documentManagerImplClassName = props.getProperty(
266: DocumentManager.class.getName(),
267: defaultDocumentManagerImpl);
268: documentManager = (DocumentManager) Class.forName(
269: documentManagerImplClassName).newInstance();
270: if (null != logger) {
271: documentManager.setLogger(logger);
272: }
273: documentManager.configure(props);
274:
275: } catch (Exception e) {
276: throw new ApplicationException(
277: "Init DocumentaManager exception", e);
278: }
279: }
280:
281: private void initDocumentStore(Properties props)
282: throws ApplicationException {
283:
284: try {
285: String documentStoreImplClassName = props.getProperty(
286: DocumentStore.class.getName(),
287: defaultDocumentStoreImpl);
288: documentStore = (DocumentStore) Class.forName(
289: documentStoreImplClassName).newInstance();
290: if (null != logger) {
291: documentStore.setLogger(logger);
292: }
293: documentStore.configure(props);
294: } catch (Exception e) {
295: throw new ApplicationException(
296: "Init DocumentaManager exception", e);
297: }
298: }
299:
300: private void initDesEncriptor(Properties props)
301: throws ApplicationException {
302: try {
303:
304: String desEncriptionImplClass = props.getProperty(
305: DesEncription.class.getName(),
306: defaultDesEncriptionImpl);
307: if (null != desEncriptionImplClass) {
308: desenc = (DesEncription) Class.forName(
309: desEncriptionImplClass).newInstance();
310: if (null != desenc) {
311: desenc.setLogger(logger);
312: desenc.configure(props);
313: } else {
314: if (null != logger) {
315: logger
316: .log(Log.WARNING,
317: "DesEncription implementation can't be found!");
318: }
319: }
320: }
321:
322: } catch (Exception ex) {
323: throw new ApplicationException(
324: "Init DesEncriptor exception!", ex);
325: }
326: }
327:
328: private void initNumerator(Properties props)
329: throws ApplicationException {
330: try {
331: Numerator
332: .setUserTransactionJNDIPath(userTransactionJNDIPath);
333: Numerator.configure(props);
334: } catch (Exception ex) {
335: throw new ApplicationException("Init Numerator exception!",
336: ex);
337: }
338:
339: }
340:
341: public boolean requestPreprocessor(HttpPresentationComms comms)
342: throws Exception {
343: return super .requestPreprocessor(comms);
344: }
345:
346: /**
347: * This is an optional function, used only by the Multiserver's graphical
348: * administration. This bit of HTML appears in the status page for this application.
349: * You could add extra status info, for example a list of currently logged in users.
350: *
351: * @return HTML that is displayed in the status page of the Multiserver.
352: */
353: public String toHtml() {
354: return "This is <I>EnhydraDM</I> sample application!";
355: }
356:
357: public DocumentManager getDocumentManager() {
358: return documentManager;
359: }
360:
361: public void setDocumentManager(DocumentManager documentManager) {
362: this .documentManager = documentManager;
363: }
364:
365: public DocumentStore getDocumentStore() {
366: return documentStore;
367: }
368:
369: public void setDocumentStore(DocumentStore documentStore) {
370: this .documentStore = documentStore;
371: }
372:
373: private Properties readConfig(Config config) throws ConfigException {
374: Properties props = new Properties();
375: try {
376: String[] leafKeys = config.leafKeys();
377: int size = leafKeys.length;
378:
379: for (int i = 0; i < size; i++) {
380: String leafKey = leafKeys[i];
381: Object value = config.get(leafKeys[i]);
382: if (value.getClass().isArray()) {
383: int len = Array.getLength(value);
384: String valueArray = Array.get(value, 0).toString();
385: for (int j = 1; j < len; j++) {
386: valueArray = valueArray + ","
387: + Array.get(value, j).toString();
388: }
389: props.put(leafKey + "[]", valueArray);
390: } else {
391: if (value instanceof javax.sql.DataSource) {
392: props.put(leafKey, value.toString());
393: } else {
394: props.put(leafKey, value);
395: }
396: }
397:
398: }
399:
400: } catch (Exception ex) {
401: throw new ConfigException(ex);
402: }
403: return props;
404: }
405:
406: /**
407: * Method load fileRefreshPeriod from <code>web.xml</code> file. If
408: * <code>fileRefreshPeriod</code> is 0 or negative number or it isn't set up
409: * application won't reload *.properties and *.xsl files at runtime.
410: *
411: * @param appConfig
412: */
413: private void setFileRefreshPeriod(Config appConfig) {
414: try {
415: int temp = appConfig
416: .getInt("Application.FileRefreshPeriod");
417: if (temp < 0) {
418: if (null != logger) {
419: logger.log(Log.INFORMATION,
420: "File refresh period setup failed. Negative period value:"
421: + temp);
422: }
423: fileRefreshPeriod = 0;
424: } else {
425: fileRefreshPeriod = temp * 1000;
426: }
427: } catch (Exception ex) {
428: if (null != logger) {
429: logger.log(Log.INFORMATION,
430: "File refresh period setup failed.", ex);
431: }
432: }
433: }
434:
435: /**
436: * Method finds fiels and folders on the file sistem and make transformers, necessary
437: * for making pdf file from editor content.
438: *
439: * @param properties
440: * @throws ApplicationException
441: */
442: private void initTransformUtility(Properties properties)
443: throws ApplicationException {
444: try {
445: TransformUtility.configure(properties);
446: } catch (Exception ex) {
447: throw new ApplicationException(
448: "Init TransformUtility exception!", ex);
449: }
450: }
451:
452: /**
453: * Method finds fodler with proerties files on the file sistem.
454: *
455: * @param appConfig
456: * @throws ApplicationException
457: */
458: private void initPropFiles(Config appConfig)
459: throws ApplicationException {
460: try {
461: propFilesPath = appConfig.getString("Application.PropPath");
462: } catch (ConfigException e) {
463: throw new ApplicationException(e);
464: }
465:
466: }
467:
468: /**
469: * In <code>propsMap</code> map, method try to find appropriate
470: * <code>propertyChangeListener</code> for proceeded file name. If it doesn't exist,
471: * method try to make new <code>propertyChangeListener</code>. If properties file
472: * for proceeded file name doesn't exist, it repeat this procedure for proceeded
473: * default properties file name. Using <code>propertyChangeListener</code>, metod
474: * get appropriate properties.
475: *
476: * @param fileName - proerties file name
477: * @param defaultFile - default proerties file name
478: * @return <code>Properties</code> object
479: */
480: public Properties getProperties(String fileName, String defaultFile) {
481: PropertyChangeListenerImpl propertyChangeListener = null;
482: propertyChangeListener = (PropertyChangeListenerImpl) propsMap
483: .get(fileName);
484:
485: String propFilePath = null;
486: if (propertyChangeListener == null) {
487: propFilePath = propFilesPath + fileName + ".properties";
488: try {
489: propertyChangeListener = new PropertyChangeListenerImpl(
490: propFilePath, fileRefreshPeriod, logger);
491: propsMap.put(fileName, propertyChangeListener);
492: } catch (Exception e) {
493: propertyChangeListener = (PropertyChangeListenerImpl) propsMap
494: .get(defaultFile);
495:
496: if (propertyChangeListener != null) {
497: return propertyChangeListener.getProperties();
498: }
499:
500: propFilePath = propFilesPath + defaultFile
501: + ".properties";
502: try {
503: propertyChangeListener = new PropertyChangeListenerImpl(
504: propFilePath, fileRefreshPeriod, logger);
505: propsMap.put(defaultFile, propertyChangeListener);
506: } catch (Exception ex) {
507: propertyChangeListener = null;
508: }
509: }
510: }
511:
512: if (propertyChangeListener == null)
513: return null;
514: return propertyChangeListener.getProperties();
515: }
516:
517: private void initFoDocumentManager(Config appConfig)
518: throws ApplicationException {
519:
520: try {
521: String foDocumentManagerImplClassName = config.getString(
522: FoDocumentManager.class.getName(),
523: FoDocumentManagerImpl.class.getName());
524: foDocumentManager = (FoDocumentManager) Class.forName(
525: foDocumentManagerImplClassName).newInstance();
526: if (null != logger) {
527: foDocumentManager.setLogger(logger);
528: }
529: } catch (Exception e) {
530: throw new ApplicationException(
531: "Init FoDocumentaManager exception", e);
532: }
533: }
534:
535: public FoDocumentManager getFoDocumentManager() {
536: return foDocumentManager;
537: }
538:
539: public void setFoDocumentManager(FoDocumentManager foDocumentManager) {
540: this .foDocumentManager = foDocumentManager;
541: }
542:
543: public UserManager getUserManager() {
544: return userManager;
545: }
546:
547: public void setUserManager(UserManager userManager) {
548: this.userManager = userManager;
549: }
550: }
|