1 package org.roller.util; 2 3 import java.beans.IntrospectionException ; 4 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileNotFoundException ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.OutputStream ; 12 13 import java.lang.reflect.AccessibleObject ; 14 import java.lang.reflect.Field ; 15 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.List ; 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 ; 29 30 31 38 public class OldRollerConfig implements java.io.Serializable 39 { 40 static final long serialVersionUID = -6625873343838437510L; 41 42 private static Log mLogger = 43 LogFactory.getFactory().getInstance( OldRollerConfig.class ); 44 45 49 protected String mAbsoluteURL = null; 50 51 52 protected boolean mRssUseCache = false; 53 54 55 protected int mRssCacheTime = 3000; 56 57 58 protected boolean mNewUserAllowed = false; 59 60 61 protected List mAdminUsers = new ArrayList (); 62 63 64 protected String mNewUserData = "/templates"; 65 66 67 protected String mNewUserThemes = "/themes"; 68 69 70 protected List mEditorPages = new ArrayList (); 71 72 73 protected boolean mEnableAggregator = false; 74 75 76 protected boolean mUploadEnabled = false; 77 78 79 protected Float mUploadMaxDirMB = new Float ( "2" ); 80 81 82 protected Float mUploadMaxFileMB = new Float ( ".5" ); 83 84 88 protected List mUploadAllow = new ArrayList (); 89 90 94 protected List mUploadForbid = new ArrayList (); 95 96 102 protected String mUploadDir = ""; 103 104 108 protected String uploadPath = "/resources"; 109 protected boolean mMemDebug = false; 110 111 115 protected boolean mAutoformatComments = false; 116 117 118 protected boolean mEscapeCommentHtml = false; 119 120 121 protected boolean mEmailComments = false; 122 123 124 protected boolean mEnableLinkback = false; 125 126 127 protected String mSiteName = "Roller-based Site"; 128 129 130 protected String mSiteDescription = "Roller-based Site"; 131 132 133 protected String mEmailAddress = ""; 134 135 136 protected String mIndexDir = 137 "${user.home}" + File.separator + "roller-index"; 138 139 142 protected boolean mEncryptPasswords = false; 143 144 145 protected String 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 ( rConfig.getUploadMaxDirMB() 162 .doubleValue() ) ); 163 this.setUploadMaxFileMB( new Float ( 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 public String getAbsoluteURL() 186 { 187 return mAbsoluteURL; 188 } 189 190 public void setAbsoluteURL( String 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 getAdminUsers() 226 { 227 return mAdminUsers; 228 } 229 230 233 public void setAdminUsers( List _adminUsers ) 234 { 235 mAdminUsers = _adminUsers; 236 } 237 238 241 public void addAdminUsers( String ignore ) 242 { 243 mAdminUsers.add( ignore ); 244 } 245 246 public String getNewUserData() 247 { 248 return mNewUserData; 249 } 250 251 254 public void setNewUserData( String str ) 255 { 256 mNewUserData = str; 257 } 258 259 public String getNewUserThemes() 260 { 261 return mNewUserThemes; 262 } 263 264 267 public void setNewUserThemes( String str ) 268 { 269 mNewUserThemes = str; 270 } 271 272 public List getEditorPages() 273 { 274 return mEditorPages; 275 } 276 277 280 public void setEditorPages( List _editorPages ) 281 { 282 mEditorPages = _editorPages; 283 } 284 285 288 public void addEditorPages( String 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 getUploadMaxDirMB() 314 { 315 return mUploadMaxDirMB; 316 } 317 318 public void setUploadMaxDirMB( Float use ) 319 { 320 mUploadMaxDirMB = use; 321 } 322 323 public Float getUploadMaxFileMB() 324 { 325 return mUploadMaxFileMB; 326 } 327 328 public void setUploadMaxFileMB( Float use ) 329 { 330 mUploadMaxFileMB = use; 331 } 332 333 public List getUploadAllow() 334 { 335 return mUploadAllow; 336 } 337 338 341 public void setUploadAllow( List _uploadAllow ) 342 { 343 mUploadAllow = _uploadAllow; 344 } 345 346 349 public void addUploadAllow( String ignore ) 350 { 351 mUploadAllow.add( ignore ); 352 } 353 354 public List getUploadForbid() 355 { 356 return mUploadForbid; 357 } 358 359 362 public void setUploadForbid( List _uploadForbid ) 363 { 364 mUploadForbid = _uploadForbid; 365 } 366 367 370 public void addUploadForbid( String ignore ) 371 { 372 mUploadForbid.add( ignore ); 373 } 374 375 public String getUploadDir() 376 { 377 return mUploadDir; 378 } 379 380 383 public void setUploadDir( String str ) 384 { 385 mUploadDir = str; 386 } 387 388 public String getUploadPath() 389 { 390 return uploadPath; 391 } 392 393 396 public void setUploadPath( String str ) 397 { 398 uploadPath = str; 399 } 400 401 public boolean getMemDebug() 402 { 403 return mMemDebug; 404 } 405 406 411 public void setMemDebug( boolean memDebug ) 412 { 413 mMemDebug = memDebug; 414 } 415 416 public boolean getAutoformatComments() 417 { 418 return mAutoformatComments; 419 } 420 421 424 public void setAutoformatComments( boolean value ) 425 { 426 mAutoformatComments = value; 427 } 428 429 public boolean getEscapeCommentHtml() 430 { 431 return mEscapeCommentHtml; 432 } 433 434 437 public void setEscapeCommentHtml( boolean value ) 438 { 439 mEscapeCommentHtml = value; 440 } 441 442 445 public boolean getEmailComments() 446 { 447 return mEmailComments; 448 } 449 450 455 public void setEmailComments( boolean emailComments ) 456 { 457 this.mEmailComments = emailComments; 458 } 459 460 465 public boolean isEnableLinkback() 466 { 467 return mEnableLinkback; 468 } 469 470 475 public void setEnableLinkback( boolean b ) 476 { 477 mEnableLinkback = b; 478 } 479 480 483 public String getSiteDescription() 484 { 485 return mSiteDescription; 486 } 487 488 491 public String getSiteName() 492 { 493 return mSiteName; 494 } 495 496 499 public void setSiteDescription( String string ) 500 { 501 mSiteDescription = string; 502 } 503 504 507 public void setSiteName( String string ) 508 { 509 mSiteName = string; 510 } 511 512 515 public String getEmailAddress() 516 { 517 return mEmailAddress; 518 } 519 520 523 public void setEmailAddress( String emailAddress ) 524 { 525 mEmailAddress = emailAddress; 526 } 527 528 531 public String getIndexDir() 532 { 533 return mIndexDir; 534 } 535 536 539 public void setIndexDir( String 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 557 public String getAlgorithm() 558 { 559 return mAlgorithm; 560 } 561 562 565 public void setAlgorithm( String algorithm ) 566 { 567 mAlgorithm = algorithm; 568 } 569 570 572 577 public String [] adminUsersArray() 578 { 579 if ( mAdminUsers == null ) 580 { 581 mAdminUsers = new ArrayList (); 582 } 583 584 return (String []) mAdminUsers.toArray( new String [mAdminUsers.size()] ); 585 } 586 587 592 public String [] editorPagesArray() 593 { 594 if ( mEditorPages == null ) 595 { 596 mEditorPages = new ArrayList (); 597 } 598 599 return (String []) mEditorPages.toArray( new String [mEditorPages.size()] ); 600 } 601 602 607 public String [] uploadAllowArray() 608 { 609 if ( mUploadAllow == null ) 610 { 611 mUploadAllow = new ArrayList (); 612 } 613 614 return (String []) mUploadAllow.toArray( new String [mUploadAllow.size()] ); 615 } 616 617 622 public String [] uploadForbidArray() 623 { 624 if ( mUploadForbid == null ) 625 { 626 mUploadForbid = new ArrayList (); 627 } 628 629 return (String []) mUploadForbid.toArray( new String [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 668 public String toString() 669 { 670 StringBuffer buf = new StringBuffer (); 671 672 buf.append( "RollerConfig \n" ); 673 674 Class clazz = getClass(); 675 676 Field [] 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 e ) 689 { 690 } 692 693 return buf.toString(); 694 } 695 696 703 public static OldRollerConfig readConfig( String path ) 704 { 705 InputStream in = null; 706 707 try 708 { 709 in = new FileInputStream ( path ); 710 return OldRollerConfig.readConfig( in ); 711 } 712 catch ( Exception 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 ioe ) 728 { 729 System.err.println( "RollerConfig.writeConfig() unable to close InputStream" ); 730 } 731 } 732 733 return new OldRollerConfig(); 734 } 735 736 745 public static OldRollerConfig readConfig( InputStream 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 e ) 756 { 757 throw new RuntimeException ( "FATAL ERROR reading RollerConfig inputstream.", 758 e ); 759 } 760 761 catch ( SAXException e ) 762 { 763 throw new RuntimeException ( "FATAL ERROR parsing RollerConfig, file is corrupted?", 764 e ); 765 } 766 767 catch ( IntrospectionException e ) 768 { 769 throw new RuntimeException ( "FATAL ERROR introspecting RollerConfig bean.", 770 e ); 771 } 772 } 773 774 781 public void writeConfig( String path ) throws RollerException 782 { 783 FileOutputStream out = null; 784 785 try 786 { 787 out = new FileOutputStream ( path ); 788 writeConfig( out ); 789 } 790 catch ( FileNotFoundException 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 ioe ) 804 { 805 System.err.println( "RollerConfig.writeConfig() unable to close OutputStream" ); 806 } 807 } 808 } 809 810 817 public void writeConfig( OutputStream 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 e ) 829 { 830 throw new RollerException( "ERROR writing to roller-config.xml stream.", 831 e ); 832 } 833 catch ( SAXException e ) 834 { 835 throw new RollerException( "ERROR writing to roller-config.xml stream.", 836 e ); 837 } 838 catch ( IntrospectionException e ) 839 { 840 throw new RollerException( "ERROR introspecting RollerConfig bean.", 841 e ); 842 } 843 } 844 845 850 public static void main( String [] args ) 851 { 852 String basedir = System.getProperty( "basedir" ); 853 String path = "build/roller/WEB-INF/roller-config.xml"; 854 path = new java.io.File ( 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" ) ) { 861 path = "build/roller/WEB-INF/roller-config-test.xml"; 862 path = new java.io.File ( basedir, path ).getAbsolutePath(); 863 OldRollerConfig bean = new OldRollerConfig(); 864 865 try 866 { 867 bean.writeConfig( path ); 868 } 869 catch ( Exception e ) 870 { 871 mLogger.error( "Unexpected exception", e ); 872 } 873 } 874 else { 876 OldRollerConfig bean = OldRollerConfig.readConfig( path ); 877 path = "build/roller/WEB-INF/roller-config-test.xml"; 878 path = new java.io.File ( basedir, path ).getAbsolutePath(); 879 880 try 881 { 882 bean.writeConfig( path ); 883 } 884 catch ( Exception e ) 885 { 886 mLogger.error( "Unexpected exception", e ); 887 } 888 } 889 890 System.out.println( "RollerConfig.main completed" ); 891 } 892 } 893 | Popular Tags |