KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > util > OldRollerConfig


1 package org.roller.util;
2
3 import java.beans.IntrospectionException JavaDoc;
4
5 import java.io.File JavaDoc;
6 import java.io.FileInputStream JavaDoc;
7 import java.io.FileNotFoundException JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12
13 import java.lang.reflect.AccessibleObject JavaDoc;
14 import java.lang.reflect.Field JavaDoc;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.apache.commons.betwixt.io.BeanReader;
21 import org.apache.commons.betwixt.io.BeanWriter;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import org.roller.RollerException;
26 import org.roller.pojos.RollerConfigData;
27
28 import org.xml.sax.SAXException JavaDoc;
29
30
31 /**
32  * Configuration object for Roller. Reads and writes roller-config.xml.
33  * This file is a relic of the old days, back when we used to store the Roller
34  * configuration in an XML file. In Roller 0.9.9 and later, this file only
35  * exists to allow use to read only roller-config.xml files on startup and
36  * copy them into the database.
37  */

38 public class OldRollerConfig implements java.io.Serializable JavaDoc
39 {
40     static final long serialVersionUID = -6625873343838437510L;
41     
42     private static Log mLogger =
43         LogFactory.getFactory().getInstance( OldRollerConfig.class );
44
45     /**
46      * Absolute URL for site, for cases where infered absolute URL doesn't
47      * work.
48      */

49     protected String JavaDoc mAbsoluteURL = null;
50
51     /** Should Roller cache return RSS pages. */
52     protected boolean mRssUseCache = false;
53
54     /** Duration to cache RSS pages (in seconds). */
55     protected int mRssCacheTime = 3000;
56
57     /** Does Roller allow the creation of new users. */
58     protected boolean mNewUserAllowed = false;
59
60     /** List of usernames with Admin priviledges. */
61     protected List JavaDoc mAdminUsers = new ArrayList JavaDoc();
62
63     /** Where to get data for creating new users (new-user.xml). */
64     protected String JavaDoc mNewUserData = "/templates";
65
66     /** Where to get Themes presented to new users. */
67     protected String JavaDoc mNewUserThemes = "/themes";
68
69     /** List of "editor pages" for the Weblog entry editor. */
70     protected List JavaDoc mEditorPages = new ArrayList JavaDoc();
71
72     /** Dis/enble RSS aggregation capabilities. */
73     protected boolean mEnableAggregator = false;
74
75     /** Are file uploads enabled. */
76     protected boolean mUploadEnabled = false;
77
78     /** The maximum size of each user's upload directory. */
79     protected Float JavaDoc mUploadMaxDirMB = new Float JavaDoc( "2" );
80
81     /** The maximum size allowed per uploaded file. */
82     protected Float JavaDoc mUploadMaxFileMB = new Float JavaDoc( ".5" );
83
84     /**
85      * List of permitted file extensions (not including the "dot"). This
86      * attribute is mutually exclusive with uploadForbid.
87      */

88     protected List JavaDoc mUploadAllow = new ArrayList JavaDoc();
89
90     /**
91      * List of forbidden file extensions (not including the "dot"). This
92      * attribute is mutually exclusive with uploadAllow.
93      */

94     protected List JavaDoc mUploadForbid = new ArrayList JavaDoc();
95
96     /**
97      * Directory where uploaded files will be stored. May end with a slash.
98      * Optional, this value will default to RollerContext.USER_RESOURCES. If
99      * specified, should be a full path on the system harddrive or relative to
100      * the WebApp.
101      */

102     protected String JavaDoc mUploadDir = "";
103
104     /**
105      * The path from which the webserver will serve upload files. This values
106      * must not end in a slash.
107      */

108     protected String JavaDoc uploadPath = "/resources";
109     protected boolean mMemDebug = false;
110
111     /**
112      * Determines if the Comment page will "autoformat" comments. That is,
113      * replace carriage-returns with <br />.
114      */

115     protected boolean mAutoformatComments = false;
116
117     /** Determines if the Comment page will escape html in comments. */
118     protected boolean mEscapeCommentHtml = false;
119
120     /** Determines if e-mailing comments is enabled. */
121     protected boolean mEmailComments = false;
122
123     /** Enable linkback extraction. */
124     protected boolean mEnableLinkback = false;
125
126     /** Name of this site */
127     protected String JavaDoc mSiteName = "Roller-based Site";
128
129     /** Description of this site */
130     protected String JavaDoc mSiteDescription = "Roller-based Site";
131
132     /** Site administrator's email address */
133     protected String JavaDoc mEmailAddress = "";
134
135     /** Lucene index directory */
136     protected String JavaDoc mIndexDir =
137         "${user.home}" + File.separator + "roller-index";
138
139     /**
140      * Flag for encrypting passwords
141      */

142     protected boolean mEncryptPasswords = false;
143     
144     /** Algorithm for encrypting passwords */
145     protected String JavaDoc mAlgorithm = "SHA";
146     
147     public OldRollerConfig()
148     {
149     }
150
151     public OldRollerConfig( RollerConfigData rConfig )
152     {
153         this.setAbsoluteURL( rConfig.getAbsoluteURL() );
154         this.setRssUseCache( rConfig.getRssUseCache().booleanValue() );
155         this.setRssCacheTime( rConfig.getRssCacheTime().intValue() );
156         this.setNewUserAllowed( rConfig.getNewUserAllowed().booleanValue() );
157         this.setNewUserThemes( rConfig.getUserThemes() );
158         this.setEditorPages( rConfig.getEditorPagesList() );
159         this.setEnableAggregator( rConfig.getEnableAggregator().booleanValue() );
160         this.setUploadEnabled( rConfig.getUploadEnabled().booleanValue() );
161         this.setUploadMaxDirMB( new Float JavaDoc( rConfig.getUploadMaxDirMB()
162                                                   .doubleValue() ) );
163         this.setUploadMaxFileMB( new Float JavaDoc( rConfig.getUploadMaxFileMB()
164                                                    .doubleValue() ) );
165         this.setUploadAllow( Arrays.asList( rConfig.uploadAllowArray() ) );
166         this.setUploadForbid( Arrays.asList( rConfig.uploadForbidArray() ) );
167         this.setUploadDir( rConfig.getUploadDir() );
168         this.setUploadPath( rConfig.getUploadPath() );
169         this.setMemDebug( rConfig.getMemDebug().booleanValue() );
170         this.setAutoformatComments( rConfig.getAutoformatComments()
171                                            .booleanValue() );
172         this.setEscapeCommentHtml( rConfig.getEscapeCommentHtml()
173                                           .booleanValue() );
174         this.setEmailComments( rConfig.getEmailComments().booleanValue() );
175         this.setEnableLinkback( rConfig.getEnableLinkback().booleanValue() );
176         this.setSiteName( rConfig.getSiteName() );
177         this.setSiteDescription( rConfig.getSiteDescription() );
178         this.setEmailAddress( rConfig.getEmailAddress() );
179         this.setIndexDir( rConfig.getIndexDir() );
180         this.setEncryptPasswords( rConfig.getEncryptPasswords().booleanValue() );
181         this.setAlgorithm( rConfig.getAlgorithm() );
182     }
183
184     //-------------------------------------- begin requisite getters & setters
185
public String JavaDoc getAbsoluteURL()
186     {
187         return mAbsoluteURL;
188     }
189
190     public void setAbsoluteURL( String JavaDoc string )
191     {
192         mAbsoluteURL = string;
193     }
194
195     public boolean getRssUseCache()
196     {
197         return mRssUseCache;
198     }
199
200     public void setRssUseCache( boolean use )
201     {
202         mRssUseCache = use;
203     }
204
205     public int getRssCacheTime()
206     {
207         return mRssCacheTime;
208     }
209
210     public void setRssCacheTime( int cacheTime )
211     {
212         mRssCacheTime = cacheTime;
213     }
214
215     public boolean getNewUserAllowed()
216     {
217         return mNewUserAllowed;
218     }
219
220     public void setNewUserAllowed( boolean use )
221     {
222         mNewUserAllowed = use;
223     }
224
225     public List JavaDoc getAdminUsers()
226     {
227         return mAdminUsers;
228     }
229
230     /**
231      * @param _adminUsers
232      */

233     public void setAdminUsers( List JavaDoc _adminUsers )
234     {
235         mAdminUsers = _adminUsers;
236     }
237
238     /**
239      * @param ignore
240      */

241     public void addAdminUsers( String JavaDoc ignore )
242     {
243         mAdminUsers.add( ignore );
244     }
245
246     public String JavaDoc getNewUserData()
247     {
248         return mNewUserData;
249     }
250
251     /**
252      * @param str
253      */

254     public void setNewUserData( String JavaDoc str )
255     {
256         mNewUserData = str;
257     }
258
259     public String JavaDoc getNewUserThemes()
260     {
261         return mNewUserThemes;
262     }
263
264     /**
265      * @param str
266      */

267     public void setNewUserThemes( String JavaDoc str )
268     {
269         mNewUserThemes = str;
270     }
271
272     public List JavaDoc getEditorPages()
273     {
274         return mEditorPages;
275     }
276
277     /**
278      * @param _editorPages
279      */

280     public void setEditorPages( List JavaDoc _editorPages )
281     {
282         mEditorPages = _editorPages;
283     }
284
285     /**
286      * @param ignore
287      */

288     public void addEditorPages( String JavaDoc ignore )
289     {
290         mEditorPages.add( ignore );
291     }
292
293     public boolean getEnableAggregator()
294     {
295         return mEnableAggregator;
296     }
297
298     public void setEnableAggregator( boolean use )
299     {
300         mEnableAggregator = use;
301     }
302
303     public boolean getUploadEnabled()
304     {
305         return mUploadEnabled;
306     }
307
308     public void setUploadEnabled( boolean use )
309     {
310         mUploadEnabled = use;
311     }
312
313     public Float JavaDoc getUploadMaxDirMB()
314     {
315         return mUploadMaxDirMB;
316     }
317
318     public void setUploadMaxDirMB( Float JavaDoc use )
319     {
320         mUploadMaxDirMB = use;
321     }
322
323     public Float JavaDoc getUploadMaxFileMB()
324     {
325         return mUploadMaxFileMB;
326     }
327
328     public void setUploadMaxFileMB( Float JavaDoc use )
329     {
330         mUploadMaxFileMB = use;
331     }
332
333     public List JavaDoc getUploadAllow()
334     {
335         return mUploadAllow;
336     }
337
338     /**
339      * @param _uploadAllow
340      */

341     public void setUploadAllow( List JavaDoc _uploadAllow )
342     {
343         mUploadAllow = _uploadAllow;
344     }
345
346     /**
347      * @param ignore
348      */

349     public void addUploadAllow( String JavaDoc ignore )
350     {
351         mUploadAllow.add( ignore );
352     }
353
354     public List JavaDoc getUploadForbid()
355     {
356         return mUploadForbid;
357     }
358
359     /**
360      * @param _uploadForbid
361      */

362     public void setUploadForbid( List JavaDoc _uploadForbid )
363     {
364         mUploadForbid = _uploadForbid;
365     }
366
367     /**
368      * @param ignore
369      */

370     public void addUploadForbid( String JavaDoc ignore )
371     {
372         mUploadForbid.add( ignore );
373     }
374
375     public String JavaDoc getUploadDir()
376     {
377         return mUploadDir;
378     }
379
380     /**
381      * @param str
382      */

383     public void setUploadDir( String JavaDoc str )
384     {
385         mUploadDir = str;
386     }
387
388     public String JavaDoc getUploadPath()
389     {
390         return uploadPath;
391     }
392
393     /**
394      * @param str
395      */

396     public void setUploadPath( String JavaDoc str )
397     {
398         uploadPath = str;
399     }
400
401     public boolean getMemDebug()
402     {
403         return mMemDebug;
404     }
405
406     /**
407      * Set memory debugging on or off.
408      *
409      * @param memDebug The mMemDebug to set
410      */

411     public void setMemDebug( boolean memDebug )
412     {
413         mMemDebug = memDebug;
414     }
415
416     public boolean getAutoformatComments()
417     {
418         return mAutoformatComments;
419     }
420
421     /**
422      * @param value
423      */

424     public void setAutoformatComments( boolean value )
425     {
426         mAutoformatComments = value;
427     }
428
429     public boolean getEscapeCommentHtml()
430     {
431         return mEscapeCommentHtml;
432     }
433
434     /**
435      * @param value
436      */

437     public void setEscapeCommentHtml( boolean value )
438     {
439         mEscapeCommentHtml = value;
440     }
441
442     /**
443      * @return boolean
444      */

445     public boolean getEmailComments()
446     {
447         return mEmailComments;
448     }
449
450     /**
451      * Sets the emailComments.
452      *
453      * @param emailComments The emailComments to set
454      */

455     public void setEmailComments( boolean emailComments )
456     {
457         this.mEmailComments = emailComments;
458     }
459
460     /**
461      * Enable linkback.
462      *
463      * @return
464      */

465     public boolean isEnableLinkback()
466     {
467         return mEnableLinkback;
468     }
469
470     /**
471      * Enable linkback.
472      *
473      * @param b
474      */

475     public void setEnableLinkback( boolean b )
476     {
477         mEnableLinkback = b;
478     }
479
480     /**
481      * @return
482      */

483     public String JavaDoc getSiteDescription()
484     {
485         return mSiteDescription;
486     }
487
488     /**
489      * @return
490      */

491     public String JavaDoc getSiteName()
492     {
493         return mSiteName;
494     }
495
496     /**
497      * @param string
498      */

499     public void setSiteDescription( String JavaDoc string )
500     {
501         mSiteDescription = string;
502     }
503
504     /**
505      * @param string
506      */

507     public void setSiteName( String JavaDoc string )
508     {
509         mSiteName = string;
510     }
511
512     /**
513      * @return
514      */

515     public String JavaDoc getEmailAddress()
516     {
517         return mEmailAddress;
518     }
519
520     /**
521      * @param emailAddress
522      */

523     public void setEmailAddress( String JavaDoc emailAddress )
524     {
525         mEmailAddress = emailAddress;
526     }
527
528     /**
529      * @return the index directory
530      */

531     public String JavaDoc getIndexDir()
532     {
533         return mIndexDir;
534     }
535
536     /**
537      * @param indexDir new index directory
538      */

539     public void setIndexDir( String JavaDoc indexDir )
540     {
541         mIndexDir = indexDir;
542     }
543
544     public boolean getEncryptPasswords()
545     {
546         return mEncryptPasswords;
547     }
548
549     public void setEncryptPasswords( boolean use )
550     {
551         mEncryptPasswords = use;
552     }
553     
554     /**
555      * @return the algorithm for encrypting passwords
556      */

557     public String JavaDoc getAlgorithm()
558     {
559         return mAlgorithm;
560     }
561
562     /**
563      * @param algorithm, the new algorithm
564      */

565     public void setAlgorithm( String JavaDoc algorithm )
566     {
567         mAlgorithm = algorithm;
568     }
569     
570     //---------------------------------------- end requisite getters & setters
571

572     /**
573      * Convenience method for getAdminUsers.
574      *
575      * @return
576      */

577     public String JavaDoc[] adminUsersArray()
578     {
579         if ( mAdminUsers == null )
580         {
581             mAdminUsers = new ArrayList JavaDoc();
582         }
583
584         return (String JavaDoc[]) mAdminUsers.toArray( new String JavaDoc[mAdminUsers.size()] );
585     }
586
587     /**
588      * Convenience method for getEditorPages.
589      *
590      * @return
591      */

592     public String JavaDoc[] editorPagesArray()
593     {
594         if ( mEditorPages == null )
595         {
596             mEditorPages = new ArrayList JavaDoc();
597         }
598
599         return (String JavaDoc[]) mEditorPages.toArray( new String JavaDoc[mEditorPages.size()] );
600     }
601
602     /**
603      * Convenience method for getUploadAllow.
604      *
605      * @return
606      */

607     public String JavaDoc[] uploadAllowArray()
608     {
609         if ( mUploadAllow == null )
610         {
611             mUploadAllow = new ArrayList JavaDoc();
612         }
613
614         return (String JavaDoc[]) mUploadAllow.toArray( new String JavaDoc[mUploadAllow.size()] );
615     }
616
617     /**
618      * Convenience method for getUploadForbid.
619      *
620      * @return
621      */

622     public String JavaDoc[] uploadForbidArray()
623     {
624         if ( mUploadForbid == null )
625         {
626             mUploadForbid = new ArrayList JavaDoc();
627         }
628
629         return (String JavaDoc[]) mUploadForbid.toArray( new String JavaDoc[mUploadForbid.size()] );
630     }
631
632     public void updateValues( OldRollerConfig child )
633     {
634         this.mAbsoluteURL = child.getAbsoluteURL();
635         this.mRssUseCache = child.getRssUseCache();
636         this.mRssCacheTime = child.getRssCacheTime();
637         this.mNewUserAllowed = child.getNewUserAllowed();
638         this.mAdminUsers = child.getAdminUsers();
639         this.mNewUserData = child.getNewUserData();
640         this.mNewUserThemes = child.getNewUserThemes();
641         this.mEditorPages = child.getEditorPages();
642         this.mEnableAggregator = child.getEnableAggregator();
643         this.mUploadEnabled = child.getUploadEnabled();
644         this.mUploadMaxDirMB = child.getUploadMaxDirMB();
645         this.mUploadMaxFileMB = child.getUploadMaxFileMB();
646         this.mUploadAllow = child.getUploadAllow();
647         this.mUploadForbid = child.getUploadForbid();
648         this.mUploadDir = child.getUploadDir();
649         this.uploadPath = child.getUploadPath();
650         this.mMemDebug = child.getMemDebug();
651         this.mAutoformatComments = child.getAutoformatComments();
652         this.mEscapeCommentHtml = child.getEscapeCommentHtml();
653         this.mEmailComments = child.getEmailComments();
654         this.mEnableLinkback = child.isEnableLinkback();
655         this.mSiteName = child.getSiteName();
656         this.mSiteDescription = child.getSiteDescription();
657         this.mEmailAddress = child.getEmailAddress();
658         this.mIndexDir = child.getIndexDir();
659         this.mEncryptPasswords = child.getEncryptPasswords();
660         this.mAlgorithm = child.getAlgorithm();
661     }
662
663     /**
664      * nice output for debugging
665      *
666      * @return
667      */

668     public String JavaDoc toString()
669     {
670         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
671
672         buf.append( "RollerConfig \n" );
673
674         Class JavaDoc clazz = getClass();
675
676         Field JavaDoc[] fields = clazz.getDeclaredFields();
677
678         try
679         {
680             AccessibleObject.setAccessible( fields, true );
681
682             for ( int i = 0; i < fields.length; i++ )
683             {
684                 buf.append( "\t[" + fields[i].getName() + "=" +
685                             fields[i].get( this ) + "], \n" );
686             }
687         }
688         catch ( Exception JavaDoc e )
689         {
690             // ignored!
691
}
692
693         return buf.toString();
694     }
695
696     /**
697      * Read the RollerConfig from a file, as specified by a String path.
698      *
699      * @param path
700      *
701      * @return
702      */

703     public static OldRollerConfig readConfig( String JavaDoc path )
704     {
705         InputStream JavaDoc in = null;
706
707         try
708         {
709             in = new FileInputStream JavaDoc( path );
710             return OldRollerConfig.readConfig( in );
711         }
712         catch ( Exception JavaDoc e )
713         {
714             System.out.println( "Exception reading RollerConfig: " +
715                                 e.getMessage() );
716         }
717         finally
718         {
719             try
720             {
721                 if ( in != null )
722                 {
723                     in.close();
724                 }
725             }
726
727             catch ( java.io.IOException JavaDoc ioe )
728             {
729                 System.err.println( "RollerConfig.writeConfig() unable to close InputStream" );
730             }
731         }
732
733         return new OldRollerConfig();
734     }
735
736     /**
737      * Read the RollerConfig from a file, as specified by an InputStream.
738      *
739      * @param in
740      *
741      * @return
742      *
743      * @throws RuntimeException
744      */

745     public static OldRollerConfig readConfig( InputStream JavaDoc in )
746     {
747         try
748         {
749             BeanReader reader = new BeanReader();
750             reader.setDebug(99);
751             reader.registerBeanClass( OldRollerConfig.class );
752             return (OldRollerConfig) reader.parse( in );
753         }
754
755         catch ( IOException JavaDoc e )
756         {
757             throw new RuntimeException JavaDoc( "FATAL ERROR reading RollerConfig inputstream.",
758                                         e );
759         }
760
761         catch ( SAXException JavaDoc e )
762         {
763             throw new RuntimeException JavaDoc( "FATAL ERROR parsing RollerConfig, file is corrupted?",
764                                         e );
765         }
766
767         catch ( IntrospectionException JavaDoc e )
768         {
769             throw new RuntimeException JavaDoc( "FATAL ERROR introspecting RollerConfig bean.",
770                                         e );
771         }
772     }
773
774     /**
775      * Write RollerConfig to file, as specified by a String path.
776      *
777      * @param path
778      *
779      * @throws RollerException
780      */

781     public void writeConfig( String JavaDoc path ) throws RollerException
782     {
783         FileOutputStream JavaDoc out = null;
784
785         try
786         {
787             out = new FileOutputStream JavaDoc( path );
788             writeConfig( out );
789         }
790         catch ( FileNotFoundException JavaDoc e )
791         {
792             throw new RollerException( "ERROR file not found: " + path, e );
793         }
794         finally
795         {
796             try
797             {
798                 if ( out != null )
799                 {
800                     out.close();
801                 }
802             }
803             catch ( java.io.IOException JavaDoc ioe )
804             {
805                 System.err.println( "RollerConfig.writeConfig() unable to close OutputStream" );
806             }
807         }
808     }
809
810     /**
811      * Write RollerConfig to file, as specified by an OutputStream.
812      *
813      * @param out
814      *
815      * @throws RollerException
816      */

817     public void writeConfig( OutputStream JavaDoc out ) throws RollerException
818     {
819         BeanWriter writer = new BeanWriter( out );
820         writer.enablePrettyPrint();
821         writer.setIndent( " " );
822         writer.setWriteIDs( false );
823
824         try
825         {
826             writer.write( this );
827         }
828         catch ( IOException JavaDoc e )
829         {
830             throw new RollerException( "ERROR writing to roller-config.xml stream.",
831                                        e );
832         }
833         catch ( SAXException JavaDoc e )
834         {
835             throw new RollerException( "ERROR writing to roller-config.xml stream.",
836                                        e );
837         }
838         catch ( IntrospectionException JavaDoc e )
839         {
840             throw new RollerException( "ERROR introspecting RollerConfig bean.",
841                                        e );
842         }
843     }
844
845     /**
846      * test stuff
847      *
848      * @param args
849      */

850     public static void main( String JavaDoc[] args )
851     {
852         String JavaDoc basedir = System.getProperty( "basedir" );
853         String JavaDoc path = "build/roller/WEB-INF/roller-config.xml";
854         path = new java.io.File JavaDoc( basedir, path ).getAbsolutePath();
855         if ( ( args.length > 0 ) && args[0].equals( "read" ) )
856         {
857             OldRollerConfig.readConfig( path );
858         }
859         else if ( ( args.length > 0 ) && args[0].equals( "write" ) ) // write
860
{
861             path = "build/roller/WEB-INF/roller-config-test.xml";
862             path = new java.io.File JavaDoc( basedir, path ).getAbsolutePath();
863             OldRollerConfig bean = new OldRollerConfig();
864
865             try
866             {
867                 bean.writeConfig( path );
868             }
869             catch ( Exception JavaDoc e )
870             {
871                 mLogger.error( "Unexpected exception", e );
872             }
873         }
874         else // both
875
{
876             OldRollerConfig bean = OldRollerConfig.readConfig( path );
877             path = "build/roller/WEB-INF/roller-config-test.xml";
878             path = new java.io.File JavaDoc( basedir, path ).getAbsolutePath();
879
880             try
881             {
882                 bean.writeConfig( path );
883             }
884             catch ( Exception JavaDoc e )
885             {
886                 mLogger.error( "Unexpected exception", e );
887             }
888         }
889
890         System.out.println( "RollerConfig.main completed" );
891     }
892 }
893
Popular Tags