Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

ggrandes/standalone-preferences

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Standalone-Preferences

Java Preferences API Implementation on Filesystem and HTTP-Client. Open Source Java project under Apache License v2.0

Current Stable Version is 2.0.1


Features

  • Small Footprint
  • Allow eval of get with System Properties and Back reference with ${tagname}
  • Allow disable eval on get (see configuration).
  • Config file per package.
  • System Preferences are supported, YES.
  • User Preferences are NOT supported (intentionally).
  • Allow System properties in org.javastack.preferences.sourcedir
  • Allow ClassLoaderMap properties in org.javastack.preferences.sourcedir
  • Allow URL in org.javastack.preferences.sourcedir (http/https)

DOC

Configuration: System Properties

  • Standard Java Preferences API for select the factory: java.util.prefs.PreferencesFactory
    • Example: -Djava.util.prefs.PreferencesFactory=org.javastack.preferences.StandalonePreferencesFactory
  • Select source of System Preferences: org.javastack.preferences.sourcedir=directoryName (allow {placeholder} evaluation)
    • Example Local: -Dorg.javastack.preferences.sourcedir={user.home}/myprefs/
    • Example Remote: -Dorg.javastack.preferences.sourcedir=http://config.acme.com/{user.name}/
    • Default value: {user.home}/sysprefs/
  • For disable Eval of get (Global): org.javastack.preferences.evalget.disabled=true
  • For autoexpire cache of preferences (Global): org.javastack.preferences.stale.millis=180000
    • Default value: 0 (no expire)

Configuration: Local Properties

  • For disable Eval of get (Preferences Node): preferences.evalget.disabled=true

Usage Example (basic)

package org.javastack.preferences.example;

import java.util.prefs.Preferences;

public class Example {
	/**
	 * Simple Test
	 */
	public static void main(final String[] args) throws Throwable {
		final Preferences config = Preferences.systemNodeForPackage(Example.class);
		System.out.println(config.get("mykey", "my-default-value1"));
		System.out.println(config.get("mykey2", "my-default-value2"));
		System.out.println(config.get("other.key", "my-default-value3"));
		System.out.println(config.get("urlbase", "my-default-value4"));
		System.out.println(config.get("urlsearch", "my-default-value5"));
	}
}

Usage Example (enum based)

package org.javastack.preferences.example;

import java.util.prefs.Preferences;

public enum ExampleEnum {
	/**
	 * My Test Key
	 */
	MYKEY("my-default-value1"),
	/**
	 * Other Test Key
	 */
	OTHER_KEY("my-default-value3"),
	//
	;
	//
	private static final Preferences conf;
	private final String keyName;
	private final String defaultValue;

	static {
		conf = Preferences.systemNodeForPackage(ExampleEnum.class);
	}

	ExampleEnum(final String defaultValue) {
		this.keyName = name().toLowerCase().replace('_', '.');
		this.defaultValue = defaultValue;
	}

	public String get() {
		return conf.get(keyName, defaultValue);
	}

	public int getInt() {
		return Integer.parseInt(get());
	}

	public long getLong() {
		return Long.parseLong(get());
	}

	public boolean getBoolean() {
		return Boolean.parseBoolean(get());
	}

	/**
	 * Simple Test
	 */
	public static void main(final String[] args) throws Throwable {
		System.out.println(ExampleEnum.MYKEY.get());
		System.out.println(ExampleEnum.OTHER_KEY.get());
	}
}

Example Config

{user.home}/sysprefs/org.javastack.preferences.example.properties
mykey=my-config-value
other.key=my-config-value-for-other
urlbase=https://www.acme.com
urlsearch=${urlbase}/search?user=${user.name}
# For disable Eval uncomment this line
#preferences.evalget.disabled=true

Running standalone

java -cp standalone-preferences-X.X.X.jar org.javastack.preferences.example.Example

Sample Output

my-config-value
my-default-value2
my-config-value-for-other
https://www.acme.com
https://www.acme.com/search?user=developer

Running in Tomcat Standalone

Copy standalone-preferences-X.X.X.jar to ${CATALINA_HOME}/lib/
You can set your CATALINA_OPTS="-Dorg.javastack.preferences.sourcedir=\${CATALINA_BASE}/appconf/ -Djava.util.prefs.PreferencesFactory=org.javastack.preferences.StandalonePreferencesFactory"
Note: Don't copy standalone-preferences-X.X.X.jar to your WEB-INF/lib/ (classloader problems)

Running in Tomcat in Eclipse

-Dorg.javastack.preferences.sourcedir=${workspace_loc:/Servers}/appconf/{servlet.ContextName:noname}
-Djava.util.prefs.PreferencesFactory=org.javastack.preferences.StandalonePreferencesFactory
Note: If you want use servlet.ContextName you need ClassLoaderMap

Running Inside Spring

<!-- Config -->
<bean id="preferencePlaceHolder" 
        class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
    <property name="systemTreePath" value="com.acme.foobar.package.name" />
</bean>
<!-- Example -->
<bean id="dataSource" 
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

MAVEN

Add the dependency to your pom.xml:

<dependency>
    <groupId>org.javastack</groupId>
    <artifactId>standalone-preferences</artifactId>
    <version>2.0.1</version>
    <scope>provided</scope>
</dependency>

Inspired in Java Preferences API and Apache Commons Configuration, this code is Java-minimalistic version.

About

Java Preferences API Implementation on Filesystem

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages