Setup AppSetting like .NET in JAVA
Hufft… I felt really bothered when build an application in JAVA but I can’t have application configuration with global scope, customizable without recompiling code –___– when usually I got it easily when I build .NET application (which is app.config or web.config). Enough already!! :D So, I decided to create my own simple global configuration.. No more pain… no more sighhhhh hehehe :)) Created with XSD Schema and JAXB. Let’s go to the steps :
- if you are using IDE Netbeans 6.8, you need “XML Schema and WSDL” plugin setup first. you can do it following this.
- if you done with the first step, then create a new java application project, and add a XSD sample file to customize it as we need.
- now we need to code the XML file configuration (AppSetting) and XSD Schema.
now the XSD Schema that following the XML file structure :1: <?xml version="1.0" encoding="UTF-8"?>
2:
3: <appSetting>
4: <setting name="CodeCount" serializeAs="String">
5: <value>1000000</value>
6: </setting>
7: <setting name="CodeCountTotal" serializeAs="String">
8: <value>2000000</value>
9: </setting>
10: <setting name="LettersCount" serializeAs="Integer">
11: <value>1</value>
12: </setting>
13: <setting name="NumbersCount" serializeAs="Integer">
14: <value>1</value>
15: </setting>
16: <setting name="Mix" serializeAs="Boolean">
17: <value>true</value>
18: </setting>
19: </appSetting>
20:
1: <?xml version="1.0" encoding="UTF-8"?>
2:
3: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
4:
5: <xsd:annotation>
6: <xsd:documentation xml:lang="en">
7: AppSetting for Sample Application Setting.
8: Copyright 2010 CodeGear. All rights reserved.
9: </xsd:documentation>
10: </xsd:annotation>
11:
12: <xsd:element name="appSetting" type="ApplicationGlobalConfiguration"/>
13:
14: <xsd:complexType name="ApplicationGlobalConfiguration" >
15: <xsd:sequence>
16: <xsd:element ref="setting" minOccurs="0" maxOccurs="unbounded" />
17: </xsd:sequence>
18: </xsd:complexType>
19:
20: <xsd:element name="setting" type="SettingType"/>
21:
22: <xsd:complexType name="SettingType">
23: <xsd:sequence>
24: <xsd:element name="value" type="xsd:string" />
25: </xsd:sequence>
26: <xsd:attribute name="name" type="xsd:string" />
27: <xsd:attribute name="serializeAs" type="xsd:string" />
28: </xsd:complexType>
29:
30: </xsd:schema>
31:
- With JAXB we can generate the class persistence according the XSD Schema we have before. From window Project, add new JAXB Binding.

then It will create JAXB Binding in your project, right click > Regenerate java Code, to generate the java class persistence.
If you like, you can move the class persistence to your own package. - Now everything sets. To test it, I create simple code like this :
it will print out this to your console :1: package AppSetting;
2:
3: import com.conf.classpersistence.*;
4: import java.io.File;
5: import java.io.IOException;
6: import java.util.List;
7: import javax.xml.bind.JAXBContext;
8: import javax.xml.bind.JAXBElement;
9: import javax.xml.bind.JAXBException;
10: import javax.xml.bind.Unmarshaller;
11: /**12: *
13: * @author tuta87
14: */
15: public class Main {
16:
17: /**18: * @param args the command line arguments
19: */
20: public static void main(String[] args) throws JAXBException, IOException {
21: JAXBContext jaxbContext = JAXBContext.newInstance("com.conf.classpersistence");22: Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
23:
24: JAXBElement<ApplicationGlobalConfiguration> appSettingElement =
25: (JAXBElement<ApplicationGlobalConfiguration>)unmarshaller.unmarshal(
26: new File("src/com/conf/xml/AppSetting.xml"));
27:
28: ApplicationGlobalConfiguration appSetting = appSettingElement.getValue();
29: appSetting.setupHashtable();
30: System.out.println("Configuration count : " + appSetting.getSetting().size());31: List<SettingType> listOfAllSetting = appSetting.getSetting();
32: for (SettingType setting : listOfAllSetting){
33: System.out.print("Setting Name : " + setting.getName());34: System.out.print(", DataType : " + setting.getSerializeAs());35: System.out.println(", Value : " + setting.getValue());36: }
37: }
38:
39: }
40:
Configuration count : 5
Setting Name : CodeCount, DataType : String, Value : 1000000
Setting Name : CodeCountTotal, DataType : String, Value : 2000000
Setting Name : LettersCount, DataType : Integer, Value : 1
Setting Name : NumbersCount, DataType : Integer, Value : 1
Setting Name : Mix, DataType : Boolean, Value : true
BUILD SUCCESSFUL (total time: 1 second)as you can see from the sample code on point five, all configurations saved in List<SettingType>. Now to make it more easier, I want all configuration putted in Hashtable<K,V> object. So it will makes clear about the data type every setting we have.
- Open the ApplicationGlobalConfiguration.java, we need to add new Hashtable<K,V> property on it :
You need to pay attention on line :1: package com.conf.classpersistence;
2:
3: import java.util.ArrayList;
4: import java.util.Hashtable;
5: import java.util.List;
6: import javax.xml.bind.annotation.XmlAccessType;
7: import javax.xml.bind.annotation.XmlAccessorType;
8: import javax.xml.bind.annotation.XmlType;
9:
10: @XmlAccessorType(XmlAccessType.FIELD)
11: @XmlType(name = "ApplicationGlobalConfiguration", propOrder = {
12: "setting","hashSetting"
13: })
14: public class ApplicationGlobalConfiguration {
15:
16: protected List<SettingType> setting;
17: private Hashtable<String, Object> hashSetting;
18:
19: public List<SettingType> getSetting() {
20: if (setting == null) {
21: setting = new ArrayList<SettingType>();
22: }
23: return this.setting;
24: }
25:
26: public void setupHashtable(){
27: List<SettingType> listOfAllSetting = this.getSetting();
28: for (SettingType setting : listOfAllSetting){
29: if (setting.getSerializeAs().equals("String"))
30: this.getHashSetting().put(setting.getName(), setting.getValue());
31: else if (setting.getSerializeAs().equals("Integer"))
32: this.getHashSetting().put(setting.getName(), Integer.parseInt(setting.getValue()));
33: else if (setting.getSerializeAs().equals("Double"))
34: this.getHashSetting().put(setting.getName(), Double.parseDouble(setting.getValue()));
35: else if (setting.getSerializeAs().equals("Boolean"))
36: this.getHashSetting().put(setting.getName(), Boolean.parseBoolean(setting.getValue()));
37:
38: }
39: }
40:
41: public Object getSettingValueByName(String settingName){
42: return (this.getHashSetting().get(settingName));
43: }
44:
45: /**46: * @return the hashSetting
47: */
48: public Hashtable<String, Object> getHashSetting() {
49: if (this.hashSetting == null)
50: return (hashSetting = new Hashtable<String, Object>());
51: else
52: return hashSetting;
53: }
54: }
55:
Line 12 : add “hashSetting”, it according the property name on Line 17
Line 17 : add new property
Line 26 : a method used to setup Hashtable value and key
Line 41 : a method used to get a value by Setting Name
Line 48 : a method used to get the Hashtable object. - Now to test it you can do it with this sample code:
1: boolean bl = ((Boolean)appSetting.getHashSetting().get("Mix")).booleanValue();
2: String str = (String)appSetting.getHashSetting().get("CodeCountTotal");3: int i = (Integer)appSetting.getHashSetting().get("NumbersCount");
4:
5: System.out.print("Try to get Mix value : " + appSetting.getSettingValueByName("Mix"));
DONE! now i have my own AppSetting on my JAVA application. If you need the sample source of this post, you can take it from my DataBox. Enjoy… :)



0 comments:
Post a Comment
Post a Comment