[Index]

Getting started/Tutorial




Model

JHPropertiesTyped consists of five elements
  • Property [class]
    • Contains name of the property
    • Links to PropertypeType
    • Links to PropertyIO
  • PropertyType [class] - (Read more)
    • Contains type conversion from and to string to type.
      • DerivedPropertyType
        • An abstract and readonly PropertyType, where the resulting type is dependent on other Property values. One or many, will be initialize last.
        • DerivedFromTypePropertyType
          • An abstract DerivedPropertyType, where the resulting type is dependent on one other Property, implemented in TypeFromGeneric<K,T>. (Read more)
            • FileDerivedPropertyType - An DerivedFromTypePropertyType, where the resulting type is dependent on a Property with returns a File, implemented in TypeFromGeneric<File,T>. (Read more)
            • URLDerivedPropertyType - An DerivedFromTypePropertyType, where the resulting type is dependent on a Property with returns a URL, implemented in TypeFromGeneric<URL,T>. (Read more)
  • PropertyIO [interface] - (Read more)
    • Read and writes to the IO.
    • Cache for loaded strings and converted types.
    • Timed cache for converted types.
  • PropertyCollection [interface] - (Read more)
    • Responsible for finding, setting,validating of PropertyIO on all Property's
    • List of Properties
    • Collection validation
    • Collection reloading
  • PropertyValidationHandler [interface] - (Read more)
    • Handles message and action for valid/invalid Property's and the PropertyIO
  • PropertyChangeListener [interface] - (Read more)
    • Handles actions for when a Property is changed either successfully or not.

Example of usages


Property definitions interface

A property definition looks like this :

Property<Type> NAME = new Property<Type>("property.name",new PropertyType());

Example:
public interface DemoPropertiesDefinitions {
    public final static Property<Integer> P_INTEGER = new Property<Integer>("p.int" , new IntegerPropertyType(0,20000));
    public final static Property<String> P_STRING = new Property<String>("p.string" , new StringPropertyType(80));
    public final static Property<Color> P_COLOR = new Property<Color>("p.color" , new ColorPropertyType());
    public final static Property<Month> P_MONTH = new Property<Month>("p.month" , new MonthPropertyType());
    public final static Property<File> P_FILE = new Property<File>("p.file" , new FilePropertyType());
}


Property file "c:/app/resources/properties/demoproperties.properties"


p.int=1973
p.string=This is a message
p.color=RED
p.month=APRIL
p.file=${java.io.tmpdir}${file.separator}mytextfile.txt


Property class, static collection initialization

public class DemoProperties extends PropertyStaticCollection implements DemoPropertiesDefinitions {
        
    private DemoProperties() {
        super(new FilePropertyIO(new File("c:/app/resources/properties/demoproperties.properties"),false,true);
    }
    static {
        new DemoPropeties();
    }

Usages

We need to initialize the collection ONCE. Examples:
  • A singleton implementation
  • A static initializer (not recommened)
  • In servlet context intializer

Values can be read values in several ways :
int intValue = DemoPropeties.P_INTEGER.getTypedValue();
int intValue = DemoPropeties.getInstance().P_INTEGER.getTypedValue();
int intValue = DemoPropertiesDefinitions.P_INTEGER.getTypedValue();
Color color = DemoPropeties.P_COLOR.getTypedValue();
Month month = DemoPropeties.P_MONTH.getTypedValue();
File mytextfile = DemoPropeties.P_FILE.getTypedValue();
Access collection :
//For reloading
DemoProperties.getInstance().getIo().reload();

//For getting backup
DemoProperties.getInstance().getIo().backup();

//Getting property validation list
List<PropertyValidationDTO> list = DemoProperties.getInstance().validation();

You can also set values:
DemoPropeties.P_INTEGER.setValue("437");
DemoPropeties.P_INTEGER.setTypedValue(437);
To generic view and update values for any collection see the demo web application "JHPropertiesTypedWebDemo".
This can easily imported and used in your own project, with your on look and feel.

Important note:
When reading a value from a PropertyIO, the value is pre parsed before validation.
You can use ${name} in the value, where the name references to System properties names, which will be replace in the value, before validation takes place.

Converting your properties

To getting started with your own properties. You can use a tool that will try to convert your properties to java definitions.
Click here to read more about available Tools.
The class PropertiesJavaDefinitionPrinter has several ways of doing this, but there are 6 input parameters, where only one requried.
  • Input [Requried] : File, PropertyIO, Properties, Text.
  • suggestType [Optional] : Should the generator, suggest a type property properly will be or just return a dummy type.
  • writeClassComments [Optional] : Taking all the lines starting with '##' before the first property and writes as Class Javadoc. Ignoring all lines that starts with '##!'.
  • writePropertyJavaComments [Optional] : Taking all the lines starting with '#' or '!' before a property and writes as it as Javadoc for the Property . Ignoring all lines that starts with '#!'.
  • writePropertyDescription [Optional] : Taking all the lines starting with '#' or '!' before a property and writes as it as description string for the Property. Ignoring all lines that starts with '#!'.
  • writePropertyDescriptionInSeperateInterface [Optional] : As in 'Write Property Description',but the description will be put in a seperate interface to avoid the Property definition line to get exceedingly long.
There several ways to do this.

Code

public static void main(String[] args) {
	BasicConfigurator.configure();
	PropertyFrameworkGlobals.setLogFileValidations(false);
	PropertyFrameworkGlobals.setAutoEncryptPasswords(false);
	List<String> lines = PropertiesJavaDefinitionPrinter.generateJavaDefinitionsAsList(new File("c:\temp\myapp.properties"),true,true,true,true,true);
	PrintLines.print(lines);
}

Call a main class from prompt:
java -cp jhpropertiestyped-<version>.jar;commons-logging-1.1.2.jar dk.heick.properties.collections.PropertiesJavaDefinitionPrinter c:\temp\myapp.properties 
OR
java -cp jhpropertiestyped-<version>.jar;commons-logging-1.1.2.jar dk.heick.properties.collections.PropertiesJavaDefinitionPrinter c:\temp\myapp.properties true true true true true
OR
Deploy the JHPropertiesTypeWebDemo war file to a web server, and use the page "http://localhost:8080/jhpropertiestypedwebdemo-2.0/generate.jsp".


Example of usages - System Properties


System Properties Collection using JHPropertiesTyped

public class SystemPropertiesCollection extends PropertyStaticCollection {
	
	private static final long serialVersionUID = -6162322703648692824L;
	
	public final static Property<Class<?>>          AWT_TOOLKIT = new Property<Class<?>>("awt.toolkit",new ClassnamePropertyType(),true);	
	
	public final static Property<Charset>           FILE_ENCODING = new Property<Charset>("file.encoding",new CharsetPropertyType(),true);	
	public final static Property<String>            FILE_ENCODING_PKG = new Property<String>("file.encoding.pkg",new StringPropertyType(),true);
	public final static Property<String>            FILE_SEPARATOR = new Property<String>("file.separator",new StringPropertyType());	
	
	public final static Property<String>     	PATH_SEPERATOR = new Property<String>("path.separator",new StringPropertyType());		
	
	public final static Property<Class<?>> 		JAVA_AWT_GRAPHICSENV = new Property<Class<?>>("java.awt.graphicsenv",new ClassnamePropertyType(),true);		
	public final static Property<List<File>> 	JAVA_CLASS_PATH = new Property<List<File>>("java.class.path",new ListPropertyType<File>(new FilePropertyType(new DefaultFileValidator(AcceptFileType.BOTH)),System.getProperty("path.separator")));
	public final static Property<Double> 		JAVA_CLASS_VERSION = new Property<Double>("java.class.version",new DoublePropertyType());
	public final static Property<List<File>> 	JAVA_EXT_DIRS = new Property<List<File>>("java.ext.dirs",new ListPropertyType<File>(new DirectoryPropertyType(new DefaultDirectoryValidator(false,true)),System.getProperty("path.separator")));
	public final static Property<File> 		JAVA_HOME = new Property<File>("java.home",new DirectoryPropertyType());
	public final static Property<File> 		JAVA_IO_TMPDIR = new Property<File>("java.io.tmpdir",new DirectoryPropertyType());
	public final static Property<List<File>> 	JAVA_LIBRARY_PATH = new Property<List<File>>("java.library.path",new ListPropertyType<File>(new DirectoryPropertyType(new DefaultDirectoryValidator(false,true)),System.getProperty("path.separator")));
		
	public final static Property<String> 		JAVA_RUNTIME_NAME = new Property<String>("java.runtime.name",new StringPropertyType(),true);	   
	public final static Property<String> 		JAVA_RUNTIME_VERSION = new Property<String>("java.runtime.version",new StringPropertyType(),true);
	public final static Property<String>     	JAVA_SPECIFICATION_NAME = new Property<String>("java.specification.name",new StringPropertyType(),true); 
	public final static Property<String>     	JAVA_SPECIFICATION_VENDOR = new Property<String>("java.specification.vendor",new StringPropertyType(),true); 
	public final static Property<Double>     	JAVA_SPECIFICATION_VERSION = new Property<Double>("java.specification.version",new DoublePropertyType(),true);
	public final static Property<String>     	JAVA_VERSION = new Property<String>("java.version",new StringPropertyType());
	public final static Property<String>     	JAVA_VENDOR = new Property<String>("java.vendor",new StringPropertyType(),true); 
	public final static Property<URL> 		JAVA_VENDOR_URL = new Property<URL>("java.vendor.url",new URLPropertyType(),true);
	public final static Property<String> 		JAVA_VM_INFO = new Property<String>("java.vm.info",new StringPropertyType(),true);//
	public final static Property<String>    	JAVA_VM_NAME = new Property<String>("java.vm.name",new StringPropertyType(),true);	
	public final static Property<String>     	JAVA_VM_SPECIFICATION_NAME = new Property<String>("java.vm.specification.name",new StringPropertyType(),true); 
	public final static Property<String>     	JAVA_VM_SPECIFICATION_VENDOR = new Property<String>("java.vm.specification.vendor",new StringPropertyType(),true); 
	public final static Property<Double> 		JAVA_VM_SPECIFICATION_VERSION = new Property<Double>("java.vm.specification.version",new DoublePropertyType(),true);
	public final static Property<String>    	JAVA_VM_VENDOR = new Property<String>("java.vm.vendor",new StringPropertyType(),true); 
	public final static Property<String>     	JAVA_VM_VERSION = new Property<String>("java.vm.version",new StringPropertyType(),true);
	
	public final static Property<String>     	LINE_SEPERATOR = new Property<String>("line.separator",new StringPropertyType());
	
	public final static Property<String>     	OS_NAME = new Property<String>("os.name",new StringPropertyType(),true); 
	public final static Property<String>     	OS_ARCH = new Property<String>("os.arch",new StringPropertyType(),true); 
	public final static Property<Double>     	OS_VERSION = new Property<Double>("os.version",new DoublePropertyType(),true);
	
	public final static Property<List<File>> 	SUN_BOTH_CLASS_PATH = new Property<List<File>>("sun.boot.class.path",new ListPropertyType<File>(new FilePropertyType(new DefaultFileValidator(false,false,false,AcceptFileType.BOTH)),System.getProperty("path.separator")));		
	public final static Property<Charset> 		SUN_IO_UNICODE_ENCODING = new Property<Charset>("sun.io.unicode.encoding",new CharsetPropertyType());

	public final static Property<Charset> 		USER_COUNTRY = new Property<Charset>("user.country",new CharsetPropertyType(),true);
	public final static Property<String> 		USER_COUNTRY_FORMAT = new Property<String>("user.country.format",new StringPropertyType(),true);
	public final static Property<File> 		USER_DIR = new Property<File>("user.dir",new DirectoryPropertyType(),true);   
	public final static Property<File> 		USER_HOME = new Property<File>("user.home",new DirectoryPropertyType(),true);   
	public final static Property<Locale>    	USER_LANGUAGE = new Property<Locale>("user.language",new LocalePropertyType());
	public final static Property<Locale> 		USER_LANGUAGE_FORMAT = new Property<Locale>("user.language.format",new LocalePropertyType(),true);
	public final static Property<String>     	USER_NAME = new Property<String>("user.name",new StringPropertyType(),true);        	
	public final static Property<TimeZone> 		USER_TIMEZONE = new Property<TimeZone>("user.timezone",new TimeZonePropertyType(),true);	   
	public final static Property<String>		USER_VARIANT = new Property<String>("user.variant",new StringPropertyType(),true);


	static {
		//Static Initializer
		getInstance();
	}
	 
	private SystemPropertiesCollection() {		
		super(new MemoryPropertyIO(System.getProperties(),true));         
	}	
	
	private static SystemPropertiesCollection instance=null;
	
	public static SystemPropertiesCollection getInstance() {
		if (instance==null) {
			instance = new SystemPropertiesCollection();
		}
		return instance;
	}
}