Use jfig to manage configuration files

Source: Internet
Author: User

Many applications use a variety of configuration files. Configuration plays an important role in application development. In Java applications, various configuration files, such as attribute files and XML files, are used.

Jfig is a powerful and easy-to-use configuration tool for Java applications. It allows developers to manage more configuration files.

List 1 shows a simple configuration file of jfig named:Host Name

. Config. xml. The "Host Name" here is the hostname. This is because the default configuration file searched by jfig is<Hostname>

. Config. xml. However, you can also use custom jfiglocator to use other file names.
List 1: jfig configuration file:Hostname

. Config. xml
<Configuration>
<Section name = "webproject">
<Entry key = "instance" value = "base"/>
<Entry key = "httphost" value = "entserver"/>
</Section>

<Section name = "authentication">
<Entry key = "userid" value = "admin"/>
<Entry key = "pword" value = "x98oln1wq"/>
<Entry key = "role" value = "Administration"/>
<Entry key = "rights" value = "all"/>
</Section>

<Section name = "notification">
<Entry key = "icationicationtype" value = "general"/>
<Entry key = "receiveraddress" value = "admin@mycomp.com"/>
<Entry key = "senderaddress" value = "webproj_dev@mycomp.com"/>
</Section>
</Configuration>

Jfig supports "extension" and can include other new configuration files. The new configuration file will "inherit" the old configuration content. If the new configuration content is repeated in the old configuration file, override )".

 

Listing 2:
Jfig configuration fileDevelop

. Config. xml

<Configuration>

<! -- Extending parent configuration -->
<Include name ="Hostname. Config. xml "/>

<! -- Overriding all entry values of a section in parent configuration -->
<Section name = "authentication">
<Entry key = "userid" value = "admin"/>
<Entry key = "pword" value = "x98oln1wq"/>
<Entry key = "role" value = "Administration"/>
<Entry key = "rights" value = "all"/>
</Section>

<! -- Overriding only select entry values of a section in base
Configuration also possible -->
<Section name = "notification">
<Entry key = "senderaddress" value =. webproj_dev@mycomp.com./>
</Section>

</Configuration>
The jfiglocator container contains a component named jfiglocator.
A component named in the jfig container can be expanded to automatically load local configuration files not in the classpath. Search for files according to user definitions. Jfig providesSystem InformationSyntax:
Value = "$ system_propert_name $ ". Example: <Entry key = "class_path" value = "$ classpath $"/>
In addition, jfig supports. ini files. This is useful for Windows-based applications.
Install jfig: Install jfig is a very simple process, first download the relevant jar package (https://sourceforge.net/project/showfiles.php? Group_id = 85731 ). Jfig provides mavenfig and antfig classes to support Maven and ant. The jfig homepage (http://jfig.sourceforge.net/) lists various techniques that integrate log4j, WebLogic, and other tools.
After installing jfig and necessary configuration files, write an application code to see how to use it.
List 3: jfig initialization code
Try {
Jfig. initialize ();

} Catch (jfigexception e ){
E. getmessage ();
}
The initialize () of this jfig class will initialize one jfig's and only one instance (jfig is based on a single instance ).
After initialization, the configuration value can use the following jfig method in the application:
  • Getvalue (
    StringSection_name
    , StringKey_name
    )
    :

    Returns the value of a given key_name in the given section_name. Otherwise, a jfigexception is thrown.

    String uid = JFig.getInstance().getValue("authentication","userid");


     
     

  • Getvaue (
    StringSection_name
    , StringKey_name
    , StringDefaultvalue
    )
    : Return the value of a given key_name in the given section_name. If the value does not exist, it returns a default value.
    String uid = jfig. getinstance (). getvalue ("authentication", "userid", "guest ");
  • Setconfigurationvalue (
    StringSection_name
    , StringKey_name
    , StringValue
    )
    : Sets the configuration value; in other words, the value of the given key_name in the given section_name with the given value. Rarely used
    , Because configuration values are usually set during the initial parsing of configuration files.
    Usage:

    JFig.setConfigurationValue(("authentication", "userid","developer");
  • Reprocessconfiguration ():
    This method allows you to modify it when the system is running.
    Configuration file, and then re-process these files. This method can be easily called through GUI or other mechanisms in the running system. Jfig allows you to implement a jfiglistener to receive
    Notification of jfig being changed. In most cases, it is sufficient to directly call jfig when the attribute value is required after the configuration file is reprocessed, and listener is not required. In some cases, some may only
    To initialize a file object once, if you want to re-initialize the file object when the jfiglistener attribute value is changed, use jfiglistener.
    Jfig supports multiple methods to set system attributes. The setsectionasproperties (sectionname) method allows you to set the section of jfig to system properties or other properties objects.
    Another way is to define a section named properties in the jfig configuration file, and the attributes in this section will be set to system attributes. (Possible translation failure
    Accurate. Add an English section
    On: reprocesses configuration on a running application and creates a new configuration directory containing all jfig values. used when a change in the configuration values occurs)
    JFig.getInstance().reprocessConfiguration();
  • Printconfigurationdictionary ():
    Print out all jfig values of a jfig instance on the console. Used for monitoring purposes.

    JFig.getInstance().printConfigurationDictionary();

The following are some jfig methods that may be helpful.

  • Getintegervalue (
    StringSectionname
    , StringKeyname
    , StringDefaultvalue
    )
  • Getfloatvalue (
    StringSectionname
    , StringKeyname
    , StringDefaultvalue
    )
  • Getbooleanvalue (
    StringSectionname
    , StringKeyname
    , StringDefaultvalue
    )
  • Getarrayvalue (
    StringSectionname
    , StringKeyname
    )
  • Getentriesstartingwith (
    StringSectionname
    , StringKeyname
    )
  • Getvaluesstartingwith (
    StringSectionname
    , StringKeyValue
    )
  • Getsection (
    StringSectionname
    )
  • Getsectionasproperties (
    StringSectionname
    )
  • Addconfigeventlistener (
    JfiglistenerListener
    )

For more information about the jfig method, see its javadoc documentation.

The following is a small example:
--------------------------------------------------

Package com. myifig;

Import org. igfay. jfig. jfiglocator;
Import org. igfay. jfig. jfigif;
Import org. igfay. jfig. jfig;

Import java. util. properties;
Import java. util. enumeration;

Public class mymain {
Private Static final string dao_config_file = "com/myifig/Dao. xml ";

Public static void main (string [] ARGs ){
Jfiglocator = new jfiglocator (dao_config_file );

Jfigif daoconfig = jfig. getinstance (jfiglocator );

Properties prop = daoconfig. getsectionasproperties ("Dao ");

Enumeration enumsection = prop. Keys ();
While (enumsection. hasmoreelements ()){
String daoiface = (string) enumsection. nextelement ();
String daoimpl = prop. getproperty (daoiface );
System. Out. println ("*****" + daoiface + "" + daoimpl );
}
System. Out. println (">>> ");
}
}
Dao2.xml

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Section name = "Dao">
<Entry key = "com. myfig. mymain2" value = "mymain9"> </entry>
</Section>
</Configuration>
Dependent package:

File directory structure:

Run:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.