This discussion is archived
7 Replies Latest reply: Oct 13, 2013 11:55 AM by meandmycode RSS

Exporting properties

meandmycode Newbie
Currently Being Moderated

Hi,

 

We set some parameters in one java application ( jar one ) and then we in another application (jar two)  we use some of these properties for running it.

We have an immutable class called AppAProperties.java where all properties are declared and we provide getters here. As you probably already know I don't want a dependency from one application to the other.

I have a dependency from AppA to AppB ( but I don't want it the other way around). So yes I can send the AppAProperties object into my AppB. But how can I nicely access the properties from AppAProperties.java without the need to for long names like:

AppB.getAppProperties.getOneProperty();

 

Any ideas or readings that I can do about this?

 

br,

 

//mike

  • 1. Re: Exporting properties
    rp0428 Guru
    Currently Being Moderated

    The Java Preferences API can be used to store those settings.

     

    See The Java Tutorials

    http://docs.oracle.com/javase/tutorial/essential/environment/other.html

    Other Configuration Utilities

    Here is a summary of some other configuration utilities.

    The Preferences API allows applications to store and retrieve configuration data in an implementation-dependent backing store. Asynchronous updates are supported, and the same set of preferences can be safely updated by multiple threads and even multiple applications. For more information, refer to the Preferences API Guide.

    Here is an IBM developerWorks article that shows how to use Preferences

    http://www.ibm.com/developerworks/library/j-prefapi/index.html

    Store objects using the Preferences API

     

    If your data can be expressed as simple objects, the API can be useful storage

     

    The Preferences API -- a lightweight, cross-platform persistence API introduced in JDK 1.4 -- is designed to store small amounts of data (string, simple byte arrays, and so on.) and was not intended to be an interface to a traditional database. It can, however, be effective as a storage device if your data can be expressed as simple objects. This article offers an introduction to the API, explains how objects are stored, demonstrates the process in action, and provides a code library to do the work.

  • 2. Re: Exporting properties
    baftos Expert
    Currently Being Moderated

    Ok, so you have two applications that are related by the fact that one stores (let's call it the myapp.Configurator) and one that reads some properties (the myapp.Executor) and uses them somehow. In order to keep them in sync, you need a class that is shared by both, call it myapp.Properties. It knows how to store and retrieve the configuration. Why exactly this cannot be used in both applications? There is no 'dependency from one application to the other', just a dependency on a class, similar to the class java.util.Properties or java.util.prefs.Preferences.

  • 3. Re: Exporting properties
    meandmycode Newbie
    Currently Being Moderated

    Hi,

     

    Thanks for reply. Ok I agree I can have a shared class but where should it be located? If have project A that depends on project B and the properties needs to be in project A. How do I make the class shared? So how do I access the properties from project B.

     

    br,

     

    //mike

  • 4. Re: Exporting properties
    rp0428 Guru
    Currently Being Moderated
    So how do I access the properties from project B.

     

    The links I provided above show exactly how to do that. What is it about using Preferences that you don't understand?

  • 5. Re: Exporting properties
    meandmycode Newbie
    Currently Being Moderated

    Hi,

     

    Thanks for quick repply ( fantastic!). Yes I found that you provided me with this:

     

    Preferences API Overview

     

    Really good and it provides some good examples at the bottom. AFAIK  I can acess the preference from B using:

     

    Preferences prefs = Preferences.userRoot().node(ourNodeName);

     

    Really nice. I will do some trials tomorrow at the office.

     

    Again Thanks!

     

    //mike

  • 6. Re: Exporting properties
    meandmycode Newbie
    Currently Being Moderated

    Hi again!

     

    One thing poped up in my mind. Is it possible to override Preferences using the commmand line vm args?

     

    //mike

  • 7. Re: Exporting properties
    rp0428 Guru
    Currently Being Moderated
    One thing poped up in my mind. Is it possible to override Preferences using the commmand line vm args?

     

    Your question doesn't make any sense.

     

    Preferences don't get created or used unless YOUR code creates or uses them.

     

    It is YOUR code that determines what is stored in a preference and it is YOUR code that either reads/uses those preferences or uses args or something else instead.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points