View Javadoc

1   package org.devaki.nextobjects.util;
2   /*
3   
4   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
5   
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by the
8   Free Software Foundation; either version 2 of the License, or (at your
9   option) any later version.
10  
11  This program is distributed in the hope that it will
12  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc., 59
18  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  
20  */
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.util.Properties;
25  import java.util.Vector;
26  import java.awt.Dimension;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.devaki.nextobjects.NextObjects;
30  import org.devaki.nextobjects.workspace.models.Database;
31  import org.devaki.nextobjects.workspace.models.columns.ColumnType;
32  /***
33   * This class containts all properties load-dump plus some usefull functions
34   * @author eflorent
35   */
36  public class NOPreferences
37  {
38      /*** Directory of nextObjects */
39      public static String APP_HOME =
40          System.getProperty("user.home") + File.separator + "devaki-nextobjects";
41      /*** Location of the preferences file */
42      private static String fNoPrefFile =
43          new StringBuffer()
44              .append(APP_HOME)
45              .append(File.separator)
46              .append("devaki.properties")
47              .toString();
48      /*** Properties of nextObjects */
49      private static Properties appProperties = new Properties();
50      /*** List of supported database engines */
51      public static final Vector TORQUE_DB = new Vector(14);
52      /*** List of supported datatypes */
53      public static final Vector TORQUE_TYPES = new Vector(28);
54      /***
55       * the logger
56       */
57      private static Log logger = LogFactory.getLog(NOXMLFactory.class.getName());
58      /***
59       * Construct a 'NOPreference' object.
60       * Initialized with default hardcoded value it create the property file.
61       * Then it load the file at each start.
62       * This class is also responsible for saving the preferences and also 
63       * fill in vectors TORQUE_DB and TORQUE_TYPES
64       */
65      public NOPreferences()
66      {
67          // init by overwriting variables from properties
68          loadProperties();
69          loadTorqueType();
70      }
71      /***
72       * Load torque types.
73       * @TODO use defaulttype.properties
74       */
75      public void loadTorqueType()
76      {
77          // Database Type
78          TORQUE_DB.addElement(
79              new Database(
80                  "Postgres",
81                  "postgresql",
82                  "org.postgresql.Driver",
83                  "No known problems"));
84          TORQUE_DB.addElement(
85              new Database(
86                  "Oracle",
87                  "oracle",
88                  "oracle.jdbc.driver.OracleDriver",
89                  "No known problems"));
90          TORQUE_DB.addElement(
91              new Database(
92                  "MySQL",
93                  "mysql",
94                  "org.gjt.mm.mysql.Driver",
95                  "No known problems"));
96          TORQUE_DB.addElement(
97              new Database(
98                  "DB2",
99                  "db2",
100                 "COM.ibm.db2.jdbc.{app|net}.DB2Driver",
101                 "Untested"));
102         TORQUE_DB.addElement(
103             new Database(
104                 "DB2/AS400",
105                 "db2400",
106                 "com.ibm.as400.access.AS400JDBCDriver",
107                 "Possible case-insensitivity issues"));
108         TORQUE_DB.addElement(
109             new Database(
110                 "MS SQL",
111                 "mssql",
112                 "com.microsoft.jdbc.sqlserver.SQLServerDriver",
113                 "Untested"));
114         TORQUE_DB.addElement(
115             new Database(
116                 "SapDB",
117                 "sapdb",
118                 "com.sap.dbtech.jdbc.DriverSapDB",
119                 "Untested"));
120         TORQUE_DB.addElement(
121             new Database(
122                 "Cloudscape",
123                 "cloudscape",
124                 "COM.cloudscape.core.JDBCDriver",
125                 "Untested"));
126         TORQUE_DB.addElement(
127             new Database(
128                 "Hypersonic",
129                 "hypersonic",
130                 "org.hsql.jdbcDriver",
131                 "Untested"));
132         TORQUE_DB.addElement(
133             new Database(
134                 "Interbase",
135                 "interbase",
136                 "interbase.interclient.Driver",
137                 "Untested"));
138         TORQUE_DB.addElement(
139             new Database(
140                 "Sybase",
141                 "sybase",
142                 "com.sybase.jdbc2.jdbc.SybDriver",
143                 "Untested"));
144         TORQUE_DB.addElement(
145             new Database(
146                 "Axion",
147                 "axion",
148                 "org.axiondb.jdbc.AxionDriver",
149                 "alpha"));
150         //TORQUE_DB.addElement(new Database("MS Access", "msaccess", "sun.jdbc.odbc.JdbcOdbcDriver", ""));
151         //TORQUE_DB.addElement(new Database("Instant DB", "instantdb", "org.enhydra.instantdb.jdbc.idbDriver", ""));
152         //TORQUE_DB.addElement(new Database("Informix", "informix", "com.informix.jdbc.IfxDriver", ""));
153         //TORQUE_DB.addElement(new Database("Weblogic", "weblogic", "weblogic.jdbc.pool.Driver", ""));
154         // Field Type
155         TORQUE_TYPES.addElement(new ColumnType("VARCHAR", "255", ""));
156         TORQUE_TYPES.addElement(new ColumnType("INTEGER", "", ""));
157         TORQUE_TYPES.addElement(new ColumnType("NUMERIC", "4", ""));
158         TORQUE_TYPES.addElement(new ColumnType("DATE", "", ""));
159         TORQUE_TYPES.addElement(new ColumnType("FLOAT", "2", ""));
160         TORQUE_TYPES.addElement(new ColumnType("CHAR", "", ""));
161         TORQUE_TYPES.addElement(new ColumnType("BLOB", "", ""));
162         TORQUE_TYPES.addElement(new ColumnType("CLOB", "", ""));
163         TORQUE_TYPES.addElement(new ColumnType("BIGINT", "", ""));
164         TORQUE_TYPES.addElement(new ColumnType("BINARY", "", ""));
165         TORQUE_TYPES.addElement(new ColumnType("BIT", "", ""));
166         TORQUE_TYPES.addElement(new ColumnType("BOOLEANINT", "1", ""));
167         TORQUE_TYPES.addElement(new ColumnType("BOOLEANCHAR", "1", ""));
168         TORQUE_TYPES.addElement(new ColumnType("DECIMAL", "2", ""));
169         TORQUE_TYPES.addElement(new ColumnType("LONGVARCHAR", "8192", ""));
170         TORQUE_TYPES.addElement(new ColumnType("REAL", "8", ""));
171         TORQUE_TYPES.addElement(new ColumnType("SMALLINT", "", ""));
172         TORQUE_TYPES.addElement(new ColumnType("TINYINT", "2", ""));
173         TORQUE_TYPES.addElement(new ColumnType("TIME", "4", ""));
174         TORQUE_TYPES.addElement(new ColumnType("TIMESTAMP", "4", ""));
175         TORQUE_TYPES.addElement(new ColumnType("VARBINARY", "1024", ""));
176         // thie following type have been removed because they cause
177         // sql-generator to crash (torque 3.0)
178         /*
179         TORQUE_TYPES.addElement(new ColumnType("DISTINCT", "", ""));
180         TORQUE_TYPES.addElement(new ColumnType("JAVA_OBJECT", "", ""));
181         TORQUE_TYPES.addElement(new ColumnType("NULL", "", ""));
182         TORQUE_TYPES.addElement(new ColumnType("ARRAY", "", ""));
183         TORQUE_TYPES.addElement(new ColumnType("OTHER", "", ""));
184         TORQUE_TYPES.addElement(new ColumnType("REF", "", ""));
185         TORQUE_TYPES.addElement(new ColumnType("STRUC", "", ""));
186         */
187     }
188     /***
189      * Load preferences from the 'NO_PREFS' properties file
190      */
191     public void loadProperties()
192     {
193         // Try loading the preferences
194         Properties tmpProps = new Properties();
195         try
196         {
197             tmpProps.load(new FileInputStream(fNoPrefFile));
198             appProperties.putAll(tmpProps);
199         }
200         catch (Exception e)
201         {
202             System.err.println("Error loading properties : " + fNoPrefFile);
203             System.out.println("I give up.");
204             e.printStackTrace();
205             System.exit(1);
206         }
207         NORecentFile.load();
208     }
209     /***
210      * Save preferences to the 'NO_PREFS' properties file
211      */
212     public static void saveProperties()
213     {
214         // Retrieve actual dimensions of the application and save them
215         Dimension dim = NextObjects.getReference().getSize();
216         appProperties.setProperty(
217             "nextobjects.screen.width",
218             Integer.toString(dim.width));
219         appProperties.setProperty(
220             "nextobjects.screen.height",
221             Integer.toString(dim.height));
222         // Try writing the properties file
223         try
224         {
225             logger.info("Writing properties");
226             FileOutputStream file = new FileOutputStream(fNoPrefFile);
227             appProperties.store(file, "devaki-nextobjects preferences");
228             file.flush(); //verbose
229             file.close();
230         }
231         catch (Exception exc)
232         {
233             logger.error(exc.toString());
234         }
235     }
236     /***
237      * Give a reference to the app Properties.
238      * @return the java.util.properties for this app
239      */
240     public static String getProperty(String pKey)
241     {
242         return appProperties.getProperty(pKey, "");
243     }
244 
245     /***
246      * Get all the properties
247      * @return the properties
248      */
249     public static Properties getAppProperties()
250     {
251         return appProperties;
252     }
253 
254     /***
255      * Set property
256      * @param pKey the key
257      * @param pValue the value
258      */
259     public static void setProperty(String pKey, String pValue)
260     {
261         appProperties.setProperty(pKey,pValue);
262     }
263 
264 }