0

I am planing to Maven develop a module that actually needs to read configuration data something like below, where tag <path> will be repeated multiple times with different path name and can be same <pathtype> and different URL and parameters and then attempt to access those URLs and paths in Java classes.

In Java I should be able to read all <pathurl> tags and its parameters for given type. then I will process them in Java one by one.

What is best way to configure this data and read in Java? Should it be normal properties file loading process by Java or there is any new best practice using spring or any other utility.

Basically I want to know is there any way of configuring data other than in this XML format and then parsing it? Since the data is static in that XML, my client don't want to use database.

<path>
  <pathname>mypath</pathname>
  <pathtype>httpfile</pathtype>
  <pathurl>http://acdsds:8380/gis/</pathurl>
  <params>
    <user>sad</user>
    <password>spwd</password>
    <httprescd>100</httprescd>
    <mexist>DROD_MEF.gif</mexist>
  </params>
</path>
halfer
  • 19,824
  • 17
  • 99
  • 186
mahesh
  • 1,523
  • 4
  • 23
  • 39
  • believe me, your question is not clear for 2 reasons. 1) you need `sax`, `dom` and for more java interoperable xml, `jaxb` for xml processing in java. 2) you should consider spring for a `framework` for different things like lightweight `dependency control`. As a complement, maven looks for xmls in `src/main/resources` normally, and in some, in `src/main/resources/META-INF` folder – Siva Tumma Jan 31 '14 at 03:41

3 Answers3

1

When it comes to configuring software using certain properties such as URL's, passwords and unique keys in spring PropertyPlaceHolderConfigurer is your best bet.

From spring document:

You use the PropertyPlaceholderConfigurer to externalize property values from a bean definition in a separate file using the standard Java Properties format. Doing so enables the person deploying an application to customize environment-specific properties such as database URLs and passwords, without the complexity or risk of modifying the main XML definition file or files for the container.

What you will do is, put all your configuration data in a properties file:

##JDBC related properties start here##
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseURL=jdbc:mysql://localhost:3306/databaseName
jdbc.userName=root
jdbc.password=root  
##JDBC related properties end here##
## path Configuration start here##
path.name=mypath
path.type=httpfile
path.url=http://acdsds:8380/gis/
## path Configuration ends here##

Then, configure spring to access external properties file(assuming your properties file is named settings.properties):

<!--settings for accessing external property files--> 
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>                  
                <value>/yourPathToPropertiesFile/settings.properties</value>                   
            </list>
        </property>
    </bean>  

After you have configured PropertyPlaceholderConfigurer, access your properties by simple using @value annotation, wherever you want.

@Value("${path.name}")
String pathName;

You can even use properties file to configure you data source and many other stuff:

<bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.databaseURL}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>
Sudarshan_SMD
  • 2,549
  • 33
  • 23
0

You should probably start by reading some basic tutorials on XML processing in Java. I'm biased, but my standard recommendation is still the material at IBM's DeveloperWorks XML site

keshlam
  • 7,931
  • 2
  • 19
  • 33
  • Thanks for your response. Basically I want to know is there any way of configuring data other than in this xml format and then parsing it? since the data is static in that xml, my client dont want to use database. – mahesh Jan 31 '14 at 04:11
  • There are an infinite number of ways of configuring data, so I'm not sure what you're asking. – keshlam Jan 31 '14 at 04:14
  • can you please just give me one other way other than xml and database or properties file? – mahesh Jan 31 '14 at 04:26
  • One other way: JSON. One other way: System properties. One other way: Any representation which is convenient for your program. – keshlam Jan 31 '14 at 13:50
0

You can use json configuration file and Google's Gson(https://code.google.com/p/google-gson/) to parse the json file. Refer to this thread Parse a nested JSON using gson on how to use Gson library & sample json format.

Community
  • 1
  • 1
Arun Avanathan
  • 982
  • 4
  • 11
  • 26