001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: package org.apache.roller.util;
019:
020: import java.beans.IntrospectionException;
021:
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileNotFoundException;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.OutputStream;
029:
030: import java.lang.reflect.AccessibleObject;
031: import java.lang.reflect.Field;
032:
033: import java.util.ArrayList;
034: import java.util.Arrays;
035: import java.util.List;
036:
037: import org.apache.commons.betwixt.io.BeanReader;
038: import org.apache.commons.betwixt.io.BeanWriter;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: import org.apache.roller.RollerException;
043: import org.apache.roller.pojos.RollerConfigData;
044:
045: import org.xml.sax.SAXException;
046:
047: /**
048: * Configuration object for Roller. Reads and writes roller-config.xml.
049: * This file is a relic of the old days, back when we used to store the Roller
050: * configuration in an XML file. In Roller 0.9.9 and later, this file only
051: * exists to allow use to read only roller-config.xml files on startup and
052: * copy them into the database.
053: */
054: public class OldRollerConfig implements java.io.Serializable {
055: static final long serialVersionUID = -6625873343838437510L;
056:
057: private static Log mLogger = LogFactory.getFactory().getInstance(
058: OldRollerConfig.class);
059:
060: /**
061: * Absolute URL for site, for cases where infered absolute URL doesn't
062: * work.
063: */
064: protected String mAbsoluteURL = null;
065:
066: /** Should Roller cache return RSS pages. */
067: protected boolean mRssUseCache = false;
068:
069: /** Duration to cache RSS pages (in seconds). */
070: protected int mRssCacheTime = 3000;
071:
072: /** Does Roller allow the creation of new users. */
073: protected boolean mNewUserAllowed = false;
074:
075: /** List of usernames with Admin priviledges. */
076: protected List mAdminUsers = new ArrayList();
077:
078: /** Where to get data for creating new users (new-user.xml). */
079: protected String mNewUserData = "/templates";
080:
081: /** Where to get Themes presented to new users. */
082: protected String mNewUserThemes = "/themes";
083:
084: /** List of "editor pages" for the Weblog entry editor. */
085: protected List mEditorPages = new ArrayList();
086:
087: /** Dis/enble RSS aggregation capabilities. */
088: protected boolean mEnableAggregator = false;
089:
090: /** Are file uploads enabled. */
091: protected boolean mUploadEnabled = false;
092:
093: /** The maximum size of each user's upload directory. */
094: protected Float mUploadMaxDirMB = new Float("2");
095:
096: /** The maximum size allowed per uploaded file. */
097: protected Float mUploadMaxFileMB = new Float(".5");
098:
099: /**
100: * List of permitted file extensions (not including the "dot"). This
101: * attribute is mutually exclusive with uploadForbid.
102: */
103: protected List mUploadAllow = new ArrayList();
104:
105: /**
106: * List of forbidden file extensions (not including the "dot"). This
107: * attribute is mutually exclusive with uploadAllow.
108: */
109: protected List mUploadForbid = new ArrayList();
110:
111: /**
112: * Directory where uploaded files will be stored. May end with a slash.
113: * Optional, this value will default to RollerContext.USER_RESOURCES. If
114: * specified, should be a full path on the system harddrive or relative to
115: * the WebApp.
116: */
117: protected String mUploadDir = "";
118:
119: /**
120: * The path from which the webserver will serve upload files. This values
121: * must not end in a slash.
122: */
123: protected String uploadPath = "/resources";
124: protected boolean mMemDebug = false;
125:
126: /**
127: * Determines if the Comment page will "autoformat" comments. That is,
128: * replace carriage-returns with <br />.
129: */
130: protected boolean mAutoformatComments = false;
131:
132: /** Determines if the Comment page will escape html in comments. */
133: protected boolean mEscapeCommentHtml = false;
134:
135: /** Determines if e-mailing comments is enabled. */
136: protected boolean mEmailComments = false;
137:
138: /** Enable linkback extraction. */
139: protected boolean mEnableLinkback = false;
140:
141: /** Name of this site */
142: protected String mSiteName = "Roller-based Site";
143:
144: /** Description of this site */
145: protected String mSiteDescription = "Roller-based Site";
146:
147: /** Site administrator's email address */
148: protected String mEmailAddress = "";
149:
150: /** Lucene index directory */
151: protected String mIndexDir = "${user.home}" + File.separator
152: + "roller-index";
153:
154: /**
155: * Flag for encrypting passwords
156: */
157: protected boolean mEncryptPasswords = false;
158:
159: /** Algorithm for encrypting passwords */
160: protected String mAlgorithm = "SHA";
161:
162: public OldRollerConfig() {
163: }
164:
165: public OldRollerConfig(RollerConfigData rConfig) {
166: this .setAbsoluteURL(rConfig.getAbsoluteURL());
167: this .setRssUseCache(rConfig.getRssUseCache().booleanValue());
168: this .setRssCacheTime(rConfig.getRssCacheTime().intValue());
169: this .setNewUserAllowed(rConfig.getNewUserAllowed()
170: .booleanValue());
171: this .setNewUserThemes(rConfig.getUserThemes());
172: this .setEditorPages(rConfig.getEditorPagesList());
173: this .setEnableAggregator(rConfig.getEnableAggregator()
174: .booleanValue());
175: this
176: .setUploadEnabled(rConfig.getUploadEnabled()
177: .booleanValue());
178: this .setUploadMaxDirMB(new Float(rConfig.getUploadMaxDirMB()
179: .doubleValue()));
180: this .setUploadMaxFileMB(new Float(rConfig.getUploadMaxFileMB()
181: .doubleValue()));
182: this .setUploadAllow(Arrays.asList(rConfig.uploadAllowArray()));
183: this
184: .setUploadForbid(Arrays.asList(rConfig
185: .uploadForbidArray()));
186: this .setUploadDir(rConfig.getUploadDir());
187: this .setUploadPath(rConfig.getUploadPath());
188: this .setMemDebug(rConfig.getMemDebug().booleanValue());
189: this .setAutoformatComments(rConfig.getAutoformatComments()
190: .booleanValue());
191: this .setEscapeCommentHtml(rConfig.getEscapeCommentHtml()
192: .booleanValue());
193: this
194: .setEmailComments(rConfig.getEmailComments()
195: .booleanValue());
196: this .setEnableLinkback(rConfig.getEnableLinkback()
197: .booleanValue());
198: this .setSiteName(rConfig.getSiteName());
199: this .setSiteDescription(rConfig.getSiteDescription());
200: this .setEmailAddress(rConfig.getEmailAddress());
201: this .setIndexDir(rConfig.getIndexDir());
202: this .setEncryptPasswords(rConfig.getEncryptPasswords()
203: .booleanValue());
204: this .setAlgorithm(rConfig.getAlgorithm());
205: }
206:
207: //-------------------------------------- begin requisite getters & setters
208: public String getAbsoluteURL() {
209: return mAbsoluteURL;
210: }
211:
212: public void setAbsoluteURL(String string) {
213: mAbsoluteURL = string;
214: }
215:
216: public boolean getRssUseCache() {
217: return mRssUseCache;
218: }
219:
220: public void setRssUseCache(boolean use) {
221: mRssUseCache = use;
222: }
223:
224: public int getRssCacheTime() {
225: return mRssCacheTime;
226: }
227:
228: public void setRssCacheTime(int cacheTime) {
229: mRssCacheTime = cacheTime;
230: }
231:
232: public boolean getNewUserAllowed() {
233: return mNewUserAllowed;
234: }
235:
236: public void setNewUserAllowed(boolean use) {
237: mNewUserAllowed = use;
238: }
239:
240: public List getAdminUsers() {
241: return mAdminUsers;
242: }
243:
244: /**
245: * @param _adminUsers
246: */
247: public void setAdminUsers(List _adminUsers) {
248: mAdminUsers = _adminUsers;
249: }
250:
251: /**
252: * @param ignore
253: */
254: public void addAdminUsers(String ignore) {
255: mAdminUsers.add(ignore);
256: }
257:
258: public String getNewUserData() {
259: return mNewUserData;
260: }
261:
262: /**
263: * @param str
264: */
265: public void setNewUserData(String str) {
266: mNewUserData = str;
267: }
268:
269: public String getNewUserThemes() {
270: return mNewUserThemes;
271: }
272:
273: /**
274: * @param str
275: */
276: public void setNewUserThemes(String str) {
277: mNewUserThemes = str;
278: }
279:
280: public List getEditorPages() {
281: return mEditorPages;
282: }
283:
284: /**
285: * @param _editorPages
286: */
287: public void setEditorPages(List _editorPages) {
288: mEditorPages = _editorPages;
289: }
290:
291: /**
292: * @param ignore
293: */
294: public void addEditorPages(String ignore) {
295: mEditorPages.add(ignore);
296: }
297:
298: public boolean getEnableAggregator() {
299: return mEnableAggregator;
300: }
301:
302: public void setEnableAggregator(boolean use) {
303: mEnableAggregator = use;
304: }
305:
306: public boolean getUploadEnabled() {
307: return mUploadEnabled;
308: }
309:
310: public void setUploadEnabled(boolean use) {
311: mUploadEnabled = use;
312: }
313:
314: public Float getUploadMaxDirMB() {
315: return mUploadMaxDirMB;
316: }
317:
318: public void setUploadMaxDirMB(Float use) {
319: mUploadMaxDirMB = use;
320: }
321:
322: public Float getUploadMaxFileMB() {
323: return mUploadMaxFileMB;
324: }
325:
326: public void setUploadMaxFileMB(Float use) {
327: mUploadMaxFileMB = use;
328: }
329:
330: public List getUploadAllow() {
331: return mUploadAllow;
332: }
333:
334: /**
335: * @param _uploadAllow
336: */
337: public void setUploadAllow(List _uploadAllow) {
338: mUploadAllow = _uploadAllow;
339: }
340:
341: /**
342: * @param ignore
343: */
344: public void addUploadAllow(String ignore) {
345: mUploadAllow.add(ignore);
346: }
347:
348: public List getUploadForbid() {
349: return mUploadForbid;
350: }
351:
352: /**
353: * @param _uploadForbid
354: */
355: public void setUploadForbid(List _uploadForbid) {
356: mUploadForbid = _uploadForbid;
357: }
358:
359: /**
360: * @param ignore
361: */
362: public void addUploadForbid(String ignore) {
363: mUploadForbid.add(ignore);
364: }
365:
366: public String getUploadDir() {
367: return mUploadDir;
368: }
369:
370: /**
371: * @param str
372: */
373: public void setUploadDir(String str) {
374: mUploadDir = str;
375: }
376:
377: public String getUploadPath() {
378: return uploadPath;
379: }
380:
381: /**
382: * @param str
383: */
384: public void setUploadPath(String str) {
385: uploadPath = str;
386: }
387:
388: public boolean getMemDebug() {
389: return mMemDebug;
390: }
391:
392: /**
393: * Set memory debugging on or off.
394: *
395: * @param memDebug The mMemDebug to set
396: */
397: public void setMemDebug(boolean memDebug) {
398: mMemDebug = memDebug;
399: }
400:
401: public boolean getAutoformatComments() {
402: return mAutoformatComments;
403: }
404:
405: /**
406: * @param value
407: */
408: public void setAutoformatComments(boolean value) {
409: mAutoformatComments = value;
410: }
411:
412: public boolean getEscapeCommentHtml() {
413: return mEscapeCommentHtml;
414: }
415:
416: /**
417: * @param value
418: */
419: public void setEscapeCommentHtml(boolean value) {
420: mEscapeCommentHtml = value;
421: }
422:
423: /**
424: * @return boolean
425: */
426: public boolean getEmailComments() {
427: return mEmailComments;
428: }
429:
430: /**
431: * Sets the emailComments.
432: *
433: * @param emailComments The emailComments to set
434: */
435: public void setEmailComments(boolean emailComments) {
436: this .mEmailComments = emailComments;
437: }
438:
439: /**
440: * Enable linkback.
441: *
442: * @return
443: */
444: public boolean isEnableLinkback() {
445: return mEnableLinkback;
446: }
447:
448: /**
449: * Enable linkback.
450: *
451: * @param b
452: */
453: public void setEnableLinkback(boolean b) {
454: mEnableLinkback = b;
455: }
456:
457: /**
458: * @return
459: */
460: public String getSiteDescription() {
461: return mSiteDescription;
462: }
463:
464: /**
465: * @return
466: */
467: public String getSiteName() {
468: return mSiteName;
469: }
470:
471: /**
472: * @param string
473: */
474: public void setSiteDescription(String string) {
475: mSiteDescription = string;
476: }
477:
478: /**
479: * @param string
480: */
481: public void setSiteName(String string) {
482: mSiteName = string;
483: }
484:
485: /**
486: * @return
487: */
488: public String getEmailAddress() {
489: return mEmailAddress;
490: }
491:
492: /**
493: * @param emailAddress
494: */
495: public void setEmailAddress(String emailAddress) {
496: mEmailAddress = emailAddress;
497: }
498:
499: /**
500: * @return the index directory
501: */
502: public String getIndexDir() {
503: return mIndexDir;
504: }
505:
506: /**
507: * @param indexDir new index directory
508: */
509: public void setIndexDir(String indexDir) {
510: mIndexDir = indexDir;
511: }
512:
513: public boolean getEncryptPasswords() {
514: return mEncryptPasswords;
515: }
516:
517: public void setEncryptPasswords(boolean use) {
518: mEncryptPasswords = use;
519: }
520:
521: /**
522: * @return the algorithm for encrypting passwords
523: */
524: public String getAlgorithm() {
525: return mAlgorithm;
526: }
527:
528: /**
529: * @param algorithm, the new algorithm
530: */
531: public void setAlgorithm(String algorithm) {
532: mAlgorithm = algorithm;
533: }
534:
535: //---------------------------------------- end requisite getters & setters
536:
537: /**
538: * Convenience method for getAdminUsers.
539: *
540: * @return
541: */
542: public String[] adminUsersArray() {
543: if (mAdminUsers == null) {
544: mAdminUsers = new ArrayList();
545: }
546:
547: return (String[]) mAdminUsers.toArray(new String[mAdminUsers
548: .size()]);
549: }
550:
551: /**
552: * Convenience method for getEditorPages.
553: *
554: * @return
555: */
556: public String[] editorPagesArray() {
557: if (mEditorPages == null) {
558: mEditorPages = new ArrayList();
559: }
560:
561: return (String[]) mEditorPages.toArray(new String[mEditorPages
562: .size()]);
563: }
564:
565: /**
566: * Convenience method for getUploadAllow.
567: *
568: * @return
569: */
570: public String[] uploadAllowArray() {
571: if (mUploadAllow == null) {
572: mUploadAllow = new ArrayList();
573: }
574:
575: return (String[]) mUploadAllow.toArray(new String[mUploadAllow
576: .size()]);
577: }
578:
579: /**
580: * Convenience method for getUploadForbid.
581: *
582: * @return
583: */
584: public String[] uploadForbidArray() {
585: if (mUploadForbid == null) {
586: mUploadForbid = new ArrayList();
587: }
588:
589: return (String[]) mUploadForbid
590: .toArray(new String[mUploadForbid.size()]);
591: }
592:
593: public void updateValues(OldRollerConfig child) {
594: this .mAbsoluteURL = child.getAbsoluteURL();
595: this .mRssUseCache = child.getRssUseCache();
596: this .mRssCacheTime = child.getRssCacheTime();
597: this .mNewUserAllowed = child.getNewUserAllowed();
598: this .mAdminUsers = child.getAdminUsers();
599: this .mNewUserData = child.getNewUserData();
600: this .mNewUserThemes = child.getNewUserThemes();
601: this .mEditorPages = child.getEditorPages();
602: this .mEnableAggregator = child.getEnableAggregator();
603: this .mUploadEnabled = child.getUploadEnabled();
604: this .mUploadMaxDirMB = child.getUploadMaxDirMB();
605: this .mUploadMaxFileMB = child.getUploadMaxFileMB();
606: this .mUploadAllow = child.getUploadAllow();
607: this .mUploadForbid = child.getUploadForbid();
608: this .mUploadDir = child.getUploadDir();
609: this .uploadPath = child.getUploadPath();
610: this .mMemDebug = child.getMemDebug();
611: this .mAutoformatComments = child.getAutoformatComments();
612: this .mEscapeCommentHtml = child.getEscapeCommentHtml();
613: this .mEmailComments = child.getEmailComments();
614: this .mEnableLinkback = child.isEnableLinkback();
615: this .mSiteName = child.getSiteName();
616: this .mSiteDescription = child.getSiteDescription();
617: this .mEmailAddress = child.getEmailAddress();
618: this .mIndexDir = child.getIndexDir();
619: this .mEncryptPasswords = child.getEncryptPasswords();
620: this .mAlgorithm = child.getAlgorithm();
621: }
622:
623: /**
624: * nice output for debugging
625: *
626: * @return
627: */
628: public String toString() {
629: StringBuffer buf = new StringBuffer();
630:
631: buf.append("RollerConfig \n");
632:
633: Class clazz = getClass();
634:
635: Field[] fields = clazz.getDeclaredFields();
636:
637: try {
638: AccessibleObject.setAccessible(fields, true);
639:
640: for (int i = 0; i < fields.length; i++) {
641: buf.append("\t[" + fields[i].getName() + "="
642: + fields[i].get(this ) + "], \n");
643: }
644: } catch (Exception e) {
645: // ignored!
646: }
647:
648: return buf.toString();
649: }
650:
651: /**
652: * Read the RollerConfig from a file, as specified by a String path.
653: *
654: * @param path
655: *
656: * @return
657: */
658: public static OldRollerConfig readConfig(String path) {
659: InputStream in = null;
660:
661: try {
662: in = new FileInputStream(path);
663: return OldRollerConfig.readConfig(in);
664: } catch (Exception e) {
665: System.out.println("Exception reading RollerConfig: "
666: + e.getMessage());
667: } finally {
668: try {
669: if (in != null) {
670: in.close();
671: }
672: }
673:
674: catch (java.io.IOException ioe) {
675: System.err
676: .println("RollerConfig.writeConfig() unable to close InputStream");
677: }
678: }
679:
680: return new OldRollerConfig();
681: }
682:
683: /**
684: * Read the RollerConfig from a file, as specified by an InputStream.
685: *
686: * @param in
687: *
688: * @return
689: *
690: * @throws RuntimeException
691: */
692: public static OldRollerConfig readConfig(InputStream in) {
693: try {
694: BeanReader reader = new BeanReader();
695: reader.setDebug(99);
696: reader.registerBeanClass(OldRollerConfig.class);
697: return (OldRollerConfig) reader.parse(in);
698: }
699:
700: catch (IOException e) {
701: throw new RuntimeException(
702: "FATAL ERROR reading RollerConfig inputstream.", e);
703: }
704:
705: catch (SAXException e) {
706: throw new RuntimeException(
707: "FATAL ERROR parsing RollerConfig, file is corrupted?",
708: e);
709: }
710:
711: catch (IntrospectionException e) {
712: throw new RuntimeException(
713: "FATAL ERROR introspecting RollerConfig bean.", e);
714: }
715: }
716:
717: /**
718: * Write RollerConfig to file, as specified by a String path.
719: *
720: * @param path
721: *
722: * @throws RollerException
723: */
724: public void writeConfig(String path) throws RollerException {
725: FileOutputStream out = null;
726:
727: try {
728: out = new FileOutputStream(path);
729: writeConfig(out);
730: } catch (FileNotFoundException e) {
731: throw new RollerException("ERROR file not found: " + path,
732: e);
733: } finally {
734: try {
735: if (out != null) {
736: out.close();
737: }
738: } catch (java.io.IOException ioe) {
739: System.err
740: .println("RollerConfig.writeConfig() unable to close OutputStream");
741: }
742: }
743: }
744:
745: /**
746: * Write RollerConfig to file, as specified by an OutputStream.
747: *
748: * @param out
749: *
750: * @throws RollerException
751: */
752: public void writeConfig(OutputStream out) throws RollerException {
753: BeanWriter writer = new BeanWriter(out);
754: writer.enablePrettyPrint();
755: writer.setIndent(" ");
756: writer.setWriteIDs(false);
757:
758: try {
759: writer.write(this );
760: } catch (IOException e) {
761: throw new RollerException(
762: "ERROR writing to roller-config.xml stream.", e);
763: } catch (SAXException e) {
764: throw new RollerException(
765: "ERROR writing to roller-config.xml stream.", e);
766: } catch (IntrospectionException e) {
767: throw new RollerException(
768: "ERROR introspecting RollerConfig bean.", e);
769: }
770: }
771:
772: /**
773: * test stuff
774: *
775: * @param args
776: */
777: public static void main(String[] args) {
778: String basedir = System.getProperty("basedir");
779: String path = "build/roller/WEB-INF/roller-config.xml";
780: path = new java.io.File(basedir, path).getAbsolutePath();
781: if ((args.length > 0) && args[0].equals("read")) {
782: OldRollerConfig.readConfig(path);
783: } else if ((args.length > 0) && args[0].equals("write")) // write
784: {
785: path = "build/roller/WEB-INF/roller-config-test.xml";
786: path = new java.io.File(basedir, path).getAbsolutePath();
787: OldRollerConfig bean = new OldRollerConfig();
788:
789: try {
790: bean.writeConfig(path);
791: } catch (Exception e) {
792: mLogger.error("Unexpected exception", e);
793: }
794: } else // both
795: {
796: OldRollerConfig bean = OldRollerConfig.readConfig(path);
797: path = "build/roller/WEB-INF/roller-config-test.xml";
798: path = new java.io.File(basedir, path).getAbsolutePath();
799:
800: try {
801: bean.writeConfig(path);
802: } catch (Exception e) {
803: mLogger.error("Unexpected exception", e);
804: }
805: }
806:
807: System.out.println("RollerConfig.main completed");
808: }
809: }
|