1 18 package org.apache.roller.util; 19 20 import java.beans.IntrospectionException ; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 30 import java.lang.reflect.AccessibleObject ; 31 import java.lang.reflect.Field ; 32 33 import java.util.ArrayList ; 34 import java.util.Arrays ; 35 import java.util.List ; 36 37 import org.apache.commons.betwixt.io.BeanReader; 38 import org.apache.commons.betwixt.io.BeanWriter; 39 import org.apache.commons.logging.Log; 40 import org.apache.commons.logging.LogFactory; 41 42 import org.apache.roller.RollerException; 43 import org.apache.roller.pojos.RollerConfigData; 44 45 import org.xml.sax.SAXException ; 46 47 48 55 public class OldRollerConfig implements java.io.Serializable 56 { 57 static final long serialVersionUID = -6625873343838437510L; 58 59 private static Log mLogger = 60 LogFactory.getFactory().getInstance( OldRollerConfig.class ); 61 62 66 protected String mAbsoluteURL = null; 67 68 69 protected boolean mRssUseCache = false; 70 71 72 protected int mRssCacheTime = 3000; 73 74 75 protected boolean mNewUserAllowed = false; 76 77 78 protected List mAdminUsers = new ArrayList (); 79 80 81 protected String mNewUserData = "/templates"; 82 83 84 protected String mNewUserThemes = "/themes"; 85 86 87 protected List mEditorPages = new ArrayList (); 88 89 90 protected boolean mEnableAggregator = false; 91 92 93 protected boolean mUploadEnabled = false; 94 95 96 protected Float mUploadMaxDirMB = new Float ( "2" ); 97 98 99 protected Float mUploadMaxFileMB = new Float ( ".5" ); 100 101 105 protected List mUploadAllow = new ArrayList (); 106 107 111 protected List mUploadForbid = new ArrayList (); 112 113 119 protected String mUploadDir = ""; 120 121 125 protected String uploadPath = "/resources"; 126 protected boolean mMemDebug = false; 127 128 132 protected boolean mAutoformatComments = false; 133 134 135 protected boolean mEscapeCommentHtml = false; 136 137 138 protected boolean mEmailComments = false; 139 140 141 protected boolean mEnableLinkback = false; 142 143 144 protected String mSiteName = "Roller-based Site"; 145 146 147 protected String mSiteDescription = "Roller-based Site"; 148 149 150 protected String mEmailAddress = ""; 151 152 153 protected String mIndexDir = 154 "${user.home}" + File.separator + "roller-index"; 155 156 159 protected boolean mEncryptPasswords = false; 160 161 162 protected String mAlgorithm = "SHA"; 163 164 public OldRollerConfig() 165 { 166 } 167 168 public OldRollerConfig( RollerConfigData rConfig ) 169 { 170 this.setAbsoluteURL( rConfig.getAbsoluteURL() ); 171 this.setRssUseCache( rConfig.getRssUseCache().booleanValue() ); 172 this.setRssCacheTime( rConfig.getRssCacheTime().intValue() ); 173 this.setNewUserAllowed( rConfig.getNewUserAllowed().booleanValue() ); 174 this.setNewUserThemes( rConfig.getUserThemes() ); 175 this.setEditorPages( rConfig.getEditorPagesList() ); 176 this.setEnableAggregator( rConfig.getEnableAggregator().booleanValue() ); 177 this.setUploadEnabled( rConfig.getUploadEnabled().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.setUploadForbid( Arrays.asList( rConfig.uploadForbidArray() ) ); 184 this.setUploadDir( rConfig.getUploadDir() ); 185 this.setUploadPath( rConfig.getUploadPath() ); 186 this.setMemDebug( rConfig.getMemDebug().booleanValue() ); 187 this.setAutoformatComments( rConfig.getAutoformatComments() 188 .booleanValue() ); 189 this.setEscapeCommentHtml( rConfig.getEscapeCommentHtml() 190 .booleanValue() ); 191 this.setEmailComments( rConfig.getEmailComments().booleanValue() ); 192 this.setEnableLinkback( rConfig.getEnableLinkback().booleanValue() ); 193 this.setSiteName( rConfig.getSiteName() ); 194 this.setSiteDescription( rConfig.getSiteDescription() ); 195 this.setEmailAddress( rConfig.getEmailAddress() ); 196 this.setIndexDir( rConfig.getIndexDir() ); 197 this.setEncryptPasswords( rConfig.getEncryptPasswords().booleanValue() ); 198 this.setAlgorithm( rConfig.getAlgorithm() ); 199 } 200 201 public String getAbsoluteURL() 203 { 204 return mAbsoluteURL; 205 } 206 207 public void setAbsoluteURL( String string ) 208 { 209 mAbsoluteURL = string; 210 } 211 212 public boolean getRssUseCache() 213 { 214 return mRssUseCache; 215 } 216 217 public void setRssUseCache( boolean use ) 218 { 219 mRssUseCache = use; 220 } 221 222 public int getRssCacheTime() 223 { 224 return mRssCacheTime; 225 } 226 227 public void setRssCacheTime( int cacheTime ) 228 { 229 mRssCacheTime = cacheTime; 230 } 231 232 public boolean getNewUserAllowed() 233 { 234 return mNewUserAllowed; 235 } 236 237 public void setNewUserAllowed( boolean use ) 238 { 239 mNewUserAllowed = use; 240 } 241 242 public List getAdminUsers() 243 { 244 return mAdminUsers; 245 } 246 247 250 public void setAdminUsers( List _adminUsers ) 251 { 252 mAdminUsers = _adminUsers; 253 } 254 255 258 public void addAdminUsers( String ignore ) 259 { 260 mAdminUsers.add( ignore ); 261 } 262 263 public String getNewUserData() 264 { 265 return mNewUserData; 266 } 267 268 271 public void setNewUserData( String str ) 272 { 273 mNewUserData = str; 274 } 275 276 public String getNewUserThemes() 277 { 278 return mNewUserThemes; 279 } 280 281 284 public void setNewUserThemes( String str ) 285 { 286 mNewUserThemes = str; 287 } 288 289 public List getEditorPages() 290 { 291 return mEditorPages; 292 } 293 294 297 public void setEditorPages( List _editorPages ) 298 { 299 mEditorPages = _editorPages; 300 } 301 302 305 public void addEditorPages( String ignore ) 306 { 307 mEditorPages.add( ignore ); 308 } 309 310 public boolean getEnableAggregator() 311 { 312 return mEnableAggregator; 313 } 314 315 public void setEnableAggregator( boolean use ) 316 { 317 mEnableAggregator = use; 318 } 319 320 public boolean getUploadEnabled() 321 { 322 return mUploadEnabled; 323 } 324 325 public void setUploadEnabled( boolean use ) 326 { 327 mUploadEnabled = use; 328 } 329 330 public Float getUploadMaxDirMB() 331 { 332 return mUploadMaxDirMB; 333 } 334 335 public void setUploadMaxDirMB( Float use ) 336 { 337 mUploadMaxDirMB = use; 338 } 339 340 public Float getUploadMaxFileMB() 341 { 342 return mUploadMaxFileMB; 343 } 344 345 public void setUploadMaxFileMB( Float use ) 346 { 347 mUploadMaxFileMB = use; 348 } 349 350 public List getUploadAllow() 351 { 352 return mUploadAllow; 353 } 354 355 358 public void setUploadAllow( List _uploadAllow ) 359 { 360 mUploadAllow = _uploadAllow; 361 } 362 363 366 public void addUploadAllow( String ignore ) 367 { 368 mUploadAllow.add( ignore ); 369 } 370 371 public List getUploadForbid() 372 { 373 return mUploadForbid; 374 } 375 376 379 public void setUploadForbid( List _uploadForbid ) 380 { 381 mUploadForbid = _uploadForbid; 382 } 383 384 387 public void addUploadForbid( String ignore ) 388 { 389 mUploadForbid.add( ignore ); 390 } 391 392 public String getUploadDir() 393 { 394 return mUploadDir; 395 } 396 397 400 public void setUploadDir( String str ) 401 { 402 mUploadDir = str; 403 } 404 405 public String getUploadPath() 406 { 407 return uploadPath; 408 } 409 410 413 public void setUploadPath( String str ) 414 { 415 uploadPath = str; 416 } 417 418 public boolean getMemDebug() 419 { 420 return mMemDebug; 421 } 422 423 428 public void setMemDebug( boolean memDebug ) 429 { 430 mMemDebug = memDebug; 431 } 432 433 public boolean getAutoformatComments() 434 { 435 return mAutoformatComments; 436 } 437 438 441 public void setAutoformatComments( boolean value ) 442 { 443 mAutoformatComments = value; 444 } 445 446 public boolean getEscapeCommentHtml() 447 { 448 return mEscapeCommentHtml; 449 } 450 451 454 public void setEscapeCommentHtml( boolean value ) 455 { 456 mEscapeCommentHtml = value; 457 } 458 459 462 public boolean getEmailComments() 463 { 464 return mEmailComments; 465 } 466 467 472 public void setEmailComments( boolean emailComments ) 473 { 474 this.mEmailComments = emailComments; 475 } 476 477 482 public boolean isEnableLinkback() 483 { 484 return mEnableLinkback; 485 } 486 487 492 public void setEnableLinkback( boolean b ) 493 { 494 mEnableLinkback = b; 495 } 496 497 500 public String getSiteDescription() 501 { 502 return mSiteDescription; 503 } 504 505 508 public String getSiteName() 509 { 510 return mSiteName; 511 } 512 513 516 public void setSiteDescription( String string ) 517 { 518 mSiteDescription = string; 519 } 520 521 524 public void setSiteName( String string ) 525 { 526 mSiteName = string; 527 } 528 529 532 public String getEmailAddress() 533 { 534 return mEmailAddress; 535 } 536 537 540 public void setEmailAddress( String emailAddress ) 541 { 542 mEmailAddress = emailAddress; 543 } 544 545 548 public String getIndexDir() 549 { 550 return mIndexDir; 551 } 552 553 556 public void setIndexDir( String indexDir ) 557 { 558 mIndexDir = indexDir; 559 } 560 561 public boolean getEncryptPasswords() 562 { 563 return mEncryptPasswords; 564 } 565 566 public void setEncryptPasswords( boolean use ) 567 { 568 mEncryptPasswords = use; 569 } 570 571 574 public String getAlgorithm() 575 { 576 return mAlgorithm; 577 } 578 579 582 public void setAlgorithm( String algorithm ) 583 { 584 mAlgorithm = algorithm; 585 } 586 587 589 594 public String [] adminUsersArray() 595 { 596 if ( mAdminUsers == null ) 597 { 598 mAdminUsers = new ArrayList (); 599 } 600 601 return (String []) mAdminUsers.toArray( new String [mAdminUsers.size()] ); 602 } 603 604 609 public String [] editorPagesArray() 610 { 611 if ( mEditorPages == null ) 612 { 613 mEditorPages = new ArrayList (); 614 } 615 616 return (String []) mEditorPages.toArray( new String [mEditorPages.size()] ); 617 } 618 619 624 public String [] uploadAllowArray() 625 { 626 if ( mUploadAllow == null ) 627 { 628 mUploadAllow = new ArrayList (); 629 } 630 631 return (String []) mUploadAllow.toArray( new String [mUploadAllow.size()] ); 632 } 633 634 639 public String [] uploadForbidArray() 640 { 641 if ( mUploadForbid == null ) 642 { 643 mUploadForbid = new ArrayList (); 644 } 645 646 return (String []) mUploadForbid.toArray( new String [mUploadForbid.size()] ); 647 } 648 649 public void updateValues( OldRollerConfig child ) 650 { 651 this.mAbsoluteURL = child.getAbsoluteURL(); 652 this.mRssUseCache = child.getRssUseCache(); 653 this.mRssCacheTime = child.getRssCacheTime(); 654 this.mNewUserAllowed = child.getNewUserAllowed(); 655 this.mAdminUsers = child.getAdminUsers(); 656 this.mNewUserData = child.getNewUserData(); 657 this.mNewUserThemes = child.getNewUserThemes(); 658 this.mEditorPages = child.getEditorPages(); 659 this.mEnableAggregator = child.getEnableAggregator(); 660 this.mUploadEnabled = child.getUploadEnabled(); 661 this.mUploadMaxDirMB = child.getUploadMaxDirMB(); 662 this.mUploadMaxFileMB = child.getUploadMaxFileMB(); 663 this.mUploadAllow = child.getUploadAllow(); 664 this.mUploadForbid = child.getUploadForbid(); 665 this.mUploadDir = child.getUploadDir(); 666 this.uploadPath = child.getUploadPath(); 667 this.mMemDebug = child.getMemDebug(); 668 this.mAutoformatComments = child.getAutoformatComments(); 669 this.mEscapeCommentHtml = child.getEscapeCommentHtml(); 670 this.mEmailComments = child.getEmailComments(); 671 this.mEnableLinkback = child.isEnableLinkback(); 672 this.mSiteName = child.getSiteName(); 673 this.mSiteDescription = child.getSiteDescription(); 674 this.mEmailAddress = child.getEmailAddress(); 675 this.mIndexDir = child.getIndexDir(); 676 this.mEncryptPasswords = child.getEncryptPasswords(); 677 this.mAlgorithm = child.getAlgorithm(); 678 } 679 680 685 public String toString() 686 { 687 StringBuffer buf = new StringBuffer (); 688 689 buf.append( "RollerConfig \n" ); 690 691 Class clazz = getClass(); 692 693 Field [] fields = clazz.getDeclaredFields(); 694 695 try 696 { 697 AccessibleObject.setAccessible( fields, true ); 698 699 for ( int i = 0; i < fields.length; i++ ) 700 { 701 buf.append( "\t[" + fields[i].getName() + "=" + 702 fields[i].get( this ) + "], \n" ); 703 } 704 } 705 catch ( Exception e ) 706 { 707 } 709 710 return buf.toString(); 711 } 712 713 720 public static OldRollerConfig readConfig( String path ) 721 { 722 InputStream in = null; 723 724 try 725 { 726 in = new FileInputStream ( path ); 727 return OldRollerConfig.readConfig( in ); 728 } 729 catch ( Exception e ) 730 { 731 System.out.println( "Exception reading RollerConfig: " + 732 e.getMessage() ); 733 } 734 finally 735 { 736 try 737 { 738 if ( in != null ) 739 { 740 in.close(); 741 } 742 } 743 744 catch ( java.io.IOException ioe ) 745 { 746 System.err.println( "RollerConfig.writeConfig() unable to close InputStream" ); 747 } 748 } 749 750 return new OldRollerConfig(); 751 } 752 753 762 public static OldRollerConfig readConfig( InputStream in ) 763 { 764 try 765 { 766 BeanReader reader = new BeanReader(); 767 reader.setDebug(99); 768 reader.registerBeanClass( OldRollerConfig.class ); 769 return (OldRollerConfig) reader.parse( in ); 770 } 771 772 catch ( IOException e ) 773 { 774 throw new RuntimeException ( "FATAL ERROR reading RollerConfig inputstream.", 775 e ); 776 } 777 778 catch ( SAXException e ) 779 { 780 throw new RuntimeException ( "FATAL ERROR parsing RollerConfig, file is corrupted?", 781 e ); 782 } 783 784 catch ( IntrospectionException e ) 785 { 786 throw new RuntimeException ( "FATAL ERROR introspecting RollerConfig bean.", 787 e ); 788 } 789 } 790 791 798 public void writeConfig( String path ) throws RollerException 799 { 800 FileOutputStream out = null; 801 802 try 803 { 804 out = new FileOutputStream ( path ); 805 writeConfig( out ); 806 } 807 catch ( FileNotFoundException e ) 808 { 809 throw new RollerException( "ERROR file not found: " + path, e ); 810 } 811 finally 812 { 813 try 814 { 815 if ( out != null ) 816 { 817 out.close(); 818 } 819 } 820 catch ( java.io.IOException ioe ) 821 { 822 System.err.println( "RollerConfig.writeConfig() unable to close OutputStream" ); 823 } 824 } 825 } 826 827 834 public void writeConfig( OutputStream out ) throws RollerException 835 { 836 BeanWriter writer = new BeanWriter( out ); 837 writer.enablePrettyPrint(); 838 writer.setIndent( " " ); 839 writer.setWriteIDs( false ); 840 841 try 842 { 843 writer.write( this ); 844 } 845 catch ( IOException e ) 846 { 847 throw new RollerException( "ERROR writing to roller-config.xml stream.", 848 e ); 849 } 850 catch ( SAXException e ) 851 { 852 throw new RollerException( "ERROR writing to roller-config.xml stream.", 853 e ); 854 } 855 catch ( IntrospectionException e ) 856 { 857 throw new RollerException( "ERROR introspecting RollerConfig bean.", 858 e ); 859 } 860 } 861 862 867 public static void main( String [] args ) 868 { 869 String basedir = System.getProperty( "basedir" ); 870 String path = "build/roller/WEB-INF/roller-config.xml"; 871 path = new java.io.File ( basedir, path ).getAbsolutePath(); 872 if ( ( args.length > 0 ) && args[0].equals( "read" ) ) 873 { 874 OldRollerConfig.readConfig( path ); 875 } 876 else if ( ( args.length > 0 ) && args[0].equals( "write" ) ) { 878 path = "build/roller/WEB-INF/roller-config-test.xml"; 879 path = new java.io.File ( basedir, path ).getAbsolutePath(); 880 OldRollerConfig bean = new OldRollerConfig(); 881 882 try 883 { 884 bean.writeConfig( path ); 885 } 886 catch ( Exception e ) 887 { 888 mLogger.error( "Unexpected exception", e ); 889 } 890 } 891 else { 893 OldRollerConfig bean = OldRollerConfig.readConfig( path ); 894 path = "build/roller/WEB-INF/roller-config-test.xml"; 895 path = new java.io.File ( basedir, path ).getAbsolutePath(); 896 897 try 898 { 899 bean.writeConfig( path ); 900 } 901 catch ( Exception e ) 902 { 903 mLogger.error( "Unexpected exception", e ); 904 } 905 } 906 907 System.out.println( "RollerConfig.main completed" ); 908 } 909 } 910 | Popular Tags |