1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
|
/*
* PropertyDatabase.java
*
* Copyright (C) 2001,,2003 2002 Matt Albrecht
* groboclown@users.sourceforge.net
* http://groboutils.sourceforge.net
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package net.sourceforge.groboutils.util.io.v1;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ResourceBundle;
import java.util.Locale;
import java.util.Properties;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* A database of property files. Internally, it uses a
* ResourceBundle, so that locale specific properties can be used.
* The format is for the application defined property files to be defined
* as "read-only", and for a user defined property file to be the
* readable/writeable properties (i.e. modifications or additions or
* removals of the default properties). User settings override the
* read-only settings. The user property file is not localized, and is
* stored at <tt>$home/.<i>app-name</i>/user.properties</tt> (or the filename
* may be specified).
* <P>
* By default, the properties are loaded from the Resource streams,
* although this can be changed.
* <P>
* Before using this class, you must initialize the user property file
* by either {@link #setApplicationName( String )} or
* {@link #setUserPropertyFile( String )}.
* <P>
* The stored data is only of type String, and multiple identical key
* entries are not possible - only the first one is allowed.
*
* @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
* @since January 7, 2001
* @version $Date: 2003/02/10 22:52:45 $
*/
public class PropertyDatabase
{
//---------------------------------------------------------------------
// Public Static Fields
//---------------------------------------------------------------------
// Protected Static Fields
//---------------------------------------------------------------------
// Private Static Fields
private static final String USER_FILE_NAME = "user.properties";
private static final String USER_REMOVED = "<<REMOVED>>";
//---------------------------------------------------------------------
// Public Fields
//---------------------------------------------------------------------
// Protected Fields
//---------------------------------------------------------------------
// Private Fields
private Hashtable resourceValues = new Hashtable();
private Properties userValues = null;
private boolean doAutosave = false;
private File userProps = null;
private Locale locale = Locale.getDefault();
private PrintWriter trace = null;
private File appDirectory = null;
//---------------------------------------------------------------------
// Constructors
/**
* Default Constructor
*/
public PropertyDatabase()
{
// do nothing
}
/**
* Specify the Locale to load the properties from.
*/
public PropertyDatabase( Locale l )
{
this.locale = l;
}
//---------------------------------------------------------------------
// Public Methods
/**
* Set the application name and thus the corresponding directory that will
* store the user properties. The user properties are located
* at <tt>$home/.<i>app-name</i>/user.properties</tt>. If the user property
* file is already set, then an IllegalStateException is thrown.
*
* @param name name of the application.
*/
public void setApplicationName( String name )
throws IOException
{
if (this.userProps != null)
{
throw new IllegalStateException(
"property file already in use" );
}
if (name == null)
{
throw new IllegalArgumentException(
"no null args" );
}
// find the home directory.
File home = new File( System.getProperty("user.home") );
if (home == null || !home.isDirectory() || !home.exists())
{
throw new FileNotFoundException(
"user.home property not valid" );
}
// for hidden unix home path
name = "." + name;
this.appDirectory = new File( home, name );
if (!this.appDirectory.exists())
{
// need to create the appDir directory
this.appDirectory.mkdir();
}
else if (!this.appDirectory.isDirectory())
{
// error - app is not a directory
throw new FileNotFoundException(
"application directory "+this.appDirectory.toString()+
" is not a directory" );
}
// get the user file
this.userProps = new File( this.appDirectory, USER_FILE_NAME );
// if the file doesn't exist, create it
this.userProps.createNewFile();
loadUserProperties();
}
/**
* Returns the directory where the user properties are stored for the
* current application, or <tt>null</tt> if there is no application set.
*/
public File getApplicationDirectory()
{
return this.appDirectory;
}
/**
* Set the user property file name exactly. If the user property file
* is already set, then an IllegalStateException is thrown.
*/
public void setUserPropertyFile( String name )
throws IOException
{
if (this.userProps != null)
{
throw new IllegalStateException(
"property file already in use" );
}
if (name == null)
{
throw new IllegalArgumentException(
"no null args" );
}
File f = new File( name );
if (f.isDirectory())
{
throw new IOException("user property file "+f+" is a directory");
}
// if the file doesn't exist, create it
f.createNewFile();
this.userProps = f;
loadUserProperties();
}
/**
* Saves the current user properties.
*/
public void saveUserProperties()
throws IOException
{
if (this.userValues == null || this.userProps == null)
{
throw new IllegalStateException("database not initialized");
}
synchronized( this )
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream( this.userProps );
this.userValues.store( fos, "User values" );
}
finally
{
if (fos != null) fos.close();
fos = null;
}
}
}
/**
* Adds a resource bundle of the given name to the database, from the
* specified locale. Note that
* if a user property already exists with a given key, then the
* user property overrides the resource property.
*/
public void addResourceBundle( String resourceName )
{
ResourceBundle rb = ResourceBundle.getBundle( resourceName,
this.locale );
if (rb == null)
{
return;
}
Hashtable rv = this.resourceValues;
Enumeration enum = rb.getKeys();
String key, val;
synchronized( rv )
{
while (enum.hasMoreElements())
{
key = (String)enum.nextElement();
if (rv.contains( key ) && this.trace != null)
{
this.trace.println("Resource "+resourceName+
" contains a duplicate key '"+key+"'.");
}
val = rb.getString( key );
if (val == null)
{
if (this.trace != null)
{
this.trace.println("Resource "+resourceName+
" contains a null key '"+key+"'." );
}
if (rv.contains( key ))
{
rv.remove( key );
}
}
else
{
rv.put( key, val );
}
}
}
}
/**
* Retrieves the auto-save setting.
*/
public boolean isAutosaveOn()
{
return this.doAutosave;
}
/**
* Sets the current autosave setting.
*/
public void setAutosaveOn( boolean yes )
{
this.doAutosave = yes;
}
/**
* Removes a value from the properties. If the property is defined
* by a resource, then the user list must specify that it is
* removed.
*
* @param key the key to remove
* @return the value the key was assigned to, or <tt>null</tt> if nothing
* was removed.
*/
public String removeValue( String key )
{
String val = null;
val = this.userValues.getProperty( key );
if (val != null)
{
if (val.equals( USER_REMOVED ))
{
return null;
}
if (this.resourceValues.contains( key ))
{
// remove from the user list
this.userValues.setProperty( key, USER_REMOVED );
}
else
{
this.userValues.remove( key );
}
autoSave();
return val;
}
val = (String)this.resourceValues.get( key );
if (val != null)
{
// we know the user values doesn't have this key
this.userValues.setProperty( key, USER_REMOVED );
autoSave();
return val;
}
// no one had this key.
return null;
}
/**
* Sets the given value to the user properties.
*
* @param key the key to assign the value to
* @param value the value to be assigned to the key
*/
public void setValue( String key, String value )
{
if (value == null)
{
removeValue( key );
return;
}
this.userValues.setProperty( key, value );
autoSave();
}
/**
* Retrieves the value associated with the given key.
*
* @param key the key to pull the value out of
*/
public String getValue( String key )
{
String val = this.userValues.getProperty( key );
if (val != null)
{
if (val.equals( USER_REMOVED ))
{
return null;
}
return val;
}
return (String)this.resourceValues.get( key );
}
/**
* Resets the user property to the resource bundle's default value.
*
* @param key the key to be reset.
* @return the default value for the key.
*/
public String setValueToDefault( String key )
{
boolean needSave = this.userValues.contains( key );
this.userValues.remove( key );
if (needSave) autoSave();
return (String)this.resourceValues.get( key );
}
//---------------------------------------------------
// Convenience functions
/**
* Convenience function to convert a property to an int value.
*
* @return the given key converted to an integer, or Integer.MIN_VALUE
* if there was a parse error.
*/
public int getIntValue( String key )
{
try
{
return Integer.parseInt( getValue( key ) );
}
catch (NumberFormatException nfe)
{
return Integer.MIN_VALUE;
}
catch (NullPointerException nfe)
{
return Integer.MIN_VALUE;
}
}
/**
* Convenience function to convert an int value to a String property.
*/
public void setIntValue( String key, int value )
{
setValue( key, Integer.toString( value ) );
}
/**
* Convenience function to convert a property to a boolean value.
*
* @return the given key converted to a boolean, or false
* if there was a parse error.
*/
public boolean getBooleanValue( String key )
{
try
{
return Boolean.getBoolean( getValue( key ) );
}
catch (NullPointerException nfe)
{
return false;
}
}
/**
* Convenience function to convert a boolean value to a String property.
*/
public void setBooleanValue( String key, boolean value )
{
setValue( key,
(value ? Boolean.TRUE.toString() : Boolean.FALSE.toString() ) );
}
/**
* Convenience function to convert a property to a byte value.
*
* @return the given key converted to a byte, or Byte.MIN_VALUE
* if there was a parse error.
*/
public byte getByteValue( String key )
{
try
{
return Byte.parseByte( getValue( key ) );
}
catch (NumberFormatException nfe)
{
return Byte.MIN_VALUE;
}
catch (NullPointerException nfe)
{
return Byte.MIN_VALUE;
}
}
/**
* Convenience function to convert a byte value to a String property.
*/
public void setByteValue( String key, byte value )
{
setValue( key, Byte.toString( value ) );
}
/**
* Convenience function to convert a property to a char value.
*
* @return the given key converted to a char, or Character.MIN_VALUE
* if there was a parse error.
*/
public char getCharValue( String key )
{
String val = getValue( key );
if (val == null || val.length() <= 0) return Character.MIN_VALUE;
return val.charAt(0);
}
/**
* Convenience function to convert a char value to a String property.
*/
public void setCharValue( String key, char value )
{
setValue( key, ""+value );
}
/**
* Convenience function to convert a property to a double value.
*
* @return the given key converted to a double, or Double.MIN_VALUE
* if there was a parse error.
*/
public double getDoubleValue( String key )
{
try
{
return Double.parseDouble( getValue( key ) );
}
catch (NumberFormatException nfe)
{
return Double.MIN_VALUE;
}
catch (NullPointerException nfe)
{
return Double.MIN_VALUE;
}
}
/**
* Convenience function to convert a byte value to a String property.
*/
public void setDoubleValue( String key, double value )
{
setValue( key, Double.toString( value ) );
}
/**
* Convenience function to convert a property to a float value.
*
* @return the given key converted to a float, or Float.MIN_VALUE
* if there was a parse error.
*/
public float getFloatValue( String key )
{
try
{
return Float.parseFloat( getValue( key ) );
}
catch (NumberFormatException nfe)
{
return Float.MIN_VALUE;
}
catch (NullPointerException nfe)
{
return Float.MIN_VALUE;
}
}
/**
* Convenience function to convert a float value to a String property.
*/
public void setFloatValue( String key, float value )
{
setValue( key, Float.toString( value ) );
}
/**
* Convenience function to convert a property to a long value.
*
* @return the given key converted to a long, or Long.MIN_VALUE
* if there was a parse error.
*/
public long getLongValue( String key )
{
try
{
return Long.parseLong( getValue( key ) );
}
catch (NumberFormatException nfe)
{
return Long.MIN_VALUE;
}
catch (NullPointerException nfe)
{
return Long.MIN_VALUE;
}
}
/**
* Convenience function to convert a long value to a String property.
*/
public void setLongValue( String key, long value )
{
setValue( key, Long.toString( value ) );
}
/**
* Convenience function to convert a property to a short value.
*
* @return the given key converted to a short, or Short.MIN_VALUE
* if there was a parse error.
*/
public short getShortValue( String key )
{
try
{
return Short.parseShort( getValue( key ) );
}
catch (NumberFormatException nfe)
{
return Short.MIN_VALUE;
}
catch (NullPointerException nfe)
{
return Short.MIN_VALUE;
}
}
/**
* Convenience function to convert a short value to a String property.
*/
public void setShortValue( String key, short value )
{
setValue( key, Short.toString( value ) );
}
//--------------------------------------------------
// Debug aid function
/**
* Sets the trace stream. If you set this to non-null, then warnings,
* such as ResourceBundles containing duplicate keys, will be reported
* to the stream. Errors will still be thrown as exceptions. Autosave
* will send any exceptions to this trace.
*/
public void setTrace( PrintWriter tracer )
{
this.trace = tracer;
}
//---------------------------------------------------------------------
// Protected Methods
/**
*
*/
protected void loadUserProperties()
throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream( this.userProps );
prop.load( fis );
fis.close();
// if no exception was thrown...
synchronized( this )
{
this.userValues = prop;
}
}
/**
*
*/
protected void autoSave()
{
if (isAutosaveOn())
{
try
{
// attempt to save
saveUserProperties();
}
catch (IOException ioe)
{
if (this.trace != null)
{
ioe.printStackTrace( this.trace );
}
}
}
}
//---------------------------------------------------------------------
// Private Methods
}
|