KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > example > ConfigurationsExample


1 package example;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Properties JavaDoc;
5 import org.apache.commons.cli.CommandLine;
6 import org.apache.commons.cli.CommandLineParser;
7 import org.apache.commons.cli.Options;
8 import org.apache.commons.cli.ParseException;
9 import org.apache.commons.cli.PosixParser;
10
11 public class ConfigurationsExample {
12     
13     public static void main(String JavaDoc[] args) {
14         String JavaDoc jdbcPropToLoad = "prod.properties";
15         CommandLineParser parser = new PosixParser();
16         Options options = new Options();
17         options.addOption("d", "dev", false, "Dev tag to launch app in dev mode. Means that app will launch embedded mckoi db.");
18         try {
19             CommandLine line = parser.parse( options, args );
20             if(line.hasOption("d")) {
21                 System.err.println("App is in DEV mode");
22                 jdbcPropToLoad = "dev.properties";
23             }
24         }
25         catch( ParseException exp ) {
26             System.err.println( "Parsing failed. Reason: " + exp.getMessage() );
27         }
28         Properties JavaDoc p = new Properties JavaDoc();
29         try {
30             p.load(ConfigurationsExample.class.getResourceAsStream("/"+jdbcPropToLoad));
31         } catch (IOException JavaDoc e) {
32             System.err.println( "Properties loading failed. Reason: " + e.getMessage());
33         }
34         try {
35             String JavaDoc clazz = p.getProperty("driver.class");
36             Class.forName(clazz);
37             System.out.println(" Jdbc driver loaded :"+clazz);
38         } catch (ClassNotFoundException JavaDoc e) {
39             System.err.println( "Jdbc Driver class loading failed. Reason: " + e.getMessage());
40             e.printStackTrace();
41         }
42         
43     }
44 }
45
Popular Tags