View Javadoc

1   //$Id: Main.java,v 1.6 2007/10/19 10:43:36 kameleono Exp $
2   
3   package net.sourceforge.sql2java;
4   
5   import java.io.FileInputStream;
6   import java.util.ArrayList;
7   import java.util.Map;
8   import java.util.Properties;
9   import java.util.StringTokenizer;
10  
11  public class Main {
12  
13      /**
14       * properties given in the command line
15       */
16      private static Properties prop;
17  
18      /**
19       * main entry point
20       */
21      public static void main(String argv[])
22      {
23          main(argv, null);
24      }
25  
26      /**
27       * main entry point
28       */
29      public static void main(String argv[], Map overideFileProperties)
30      {
31          // Check for required argument
32          if(argv == null || argv.length < 1) {
33              System.err.println("Usage: java net.sourceforge.sql2java.Main <properties filename>");
34              System.exit(1);
35          }
36  
37          //properties already here ?
38          prop = new Properties();
39          try
40          {
41              prop.load(new FileInputStream(argv[0]));
42              Database db = new Database();
43              db.setDriver(getProperty("jdbc.driver"));
44              db.setUrl(getProperty("jdbc.url"));
45              db.setUsername(getProperty("jdbc.username"));
46              db.setPassword(getProperty("jdbc.password"));
47              db.setCatalog(getProperty("jdbc.catalog"));
48              db.setSchema(getProperty("jdbc.schema"));
49              db.setTableNamePattern(getProperty("jdbc.tablenamepattern"));
50              db.setActiveConnections(getProperty("jdbc.active", "10"));
51              db.setIdleConnections(getProperty("jdbc.idle", "5"));
52              db.setMaxWait(getProperty("jdbc.maxwait", "120000"));
53  
54              CodeWriter writer = new CodeWriter(db, prop);
55  
56              if (overideFileProperties != null)
57                  prop.putAll(overideFileProperties);
58  
59              if ("false".equalsIgnoreCase(getProperty("jdbc.oracle.retrieve.remarks")))
60                  db.setOracleRetrieveRemarks(false);
61              else
62                  db.setOracleRetrieveRemarks(true);
63  
64              String tt = getProperty("jdbc.tabletypes", "TABLE");
65              StringTokenizer st = new StringTokenizer(tt, ",; \t");
66              ArrayList al = new ArrayList();
67  
68              while(st.hasMoreTokens()) {
69                  al.add(((String)st.nextToken()).trim());
70              }
71  
72              db.setTableTypes((String[])al.toArray(new String[al.size()]));
73  
74              db.load();
75              
76              // override destdir if given
77              if (argv.length > 1)
78                  writer.setDestinationFolder(argv[1]);
79              writer.process();
80          }
81          catch(Exception e)
82          {
83              e.printStackTrace();
84              System.exit(1);
85          }
86      }
87  
88      public static String getProperty(String property)
89      {
90          String res = ConfigHelper.getGlobalProperty(property);
91          if (res != null)
92              return res.trim();
93          String s = prop.getProperty(property);
94          return s!=null?s.trim():s;
95      }
96  
97      /**
98       * helper method with default values
99       */
100     public static String getProperty(String property, String default_val)
101     {
102         String s = getProperty(property);
103         if (s == null)
104             return default_val;
105         return s;
106     }
107 
108     /**
109      * is the given code in the string array ?
110      */
111     public static boolean isInArray(String[] ar, String code)
112     {
113         if (ar == null)
114             return false;
115         for (int i = 0; i < ar.length; i ++)
116         {
117             if (code.equalsIgnoreCase(ar[i]))
118                 return true;
119         }
120         return false;
121     }
122 
123 }