View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.core.setup;
16  
17  import org.figure8.join.util.LogUtil;
18  
19  import org.apache.commons.logging.Log;
20  
21  import java.io.InputStream;
22  import java.util.Properties;
23  /**
24   * Wrapper for common database properties.
25   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
26   * @version $Revision: 1.3 $
27   */
28  public class DatabaseDetails{
29     
30     // Static -------------------------------------------------------------------
31     
32     /** Get a commons logger. */
33     protected static Log log = LogUtil.getLog(DatabaseDetails.class);
34     
35     /** Constant for other, no officially supported database. */
36     public static final String OTHER_DB = "other";
37     /** Array of supported database names. */
38     public static final String[] SUPPORTED_DB = new String[] {"hsqldb", "mysql", "oracle"};
39     
40     
41     // Attributes ---------------------------------------------------------------
42     
43     /** Name of user for connecting db. */
44     private String username = null;
45     /** Password of user for connecting db. */
46     private String password = null;
47     /** URL for connecting db. */
48     private String databaseUrl = null;
49     /** Classname of the JDBC driver to use. */
50     private String driverClassname = null;
51     
52     /** Maximum size of the connection pool (if managed by application). */
53     private int poolSize = 0;
54     /** SQL dialect to use for querying db. */
55     private String dialect = null;
56     
57     
58     // Constructors -------------------------------------------------------------
59     
60     /** Creates a new instance of DatabaseDetails */
61     public DatabaseDetails(){
62     }
63     
64     
65     // Public -------------------------------------------------------------------
66     
67     /** @param user Name of user for connecting db */
68     public void setUsername(String user){
69        this.username = user;
70     }
71     /** @return Get the user for connecting db */
72     public String getUsername(){
73        return username;
74     }
75     
76     /** @param pwd Password for connecting db */
77     public void setPassword(String pwd){
78        this.password = pwd;
79     }
80     /** @return Get the password for connecting db */
81     public String getPassword(){
82        return password;
83     }
84     
85     /** @param url URL for connecting to db */
86     public void setDatabaseUrl(String url){
87        this.databaseUrl = url;
88     }
89     /** @return Get the URL for connecting to db */
90     public String getDatabaseUrl(){
91        return databaseUrl;
92     }
93     
94     /** @param driver JDBC driver classname for connecting db */
95     public void setDriverClassname(String driver){
96        this.driverClassname = driver;
97     }
98     /** @return Get the classname of JDBC driver for connecting */
99     public String getDriverClassname(){
100       return driverClassname;
101    }
102    
103    /** @param size Size of connection pool (if handled by application) */
104    public void setPoolSize(int size){
105       this.poolSize = size;
106    }
107    /** @return Get the size of the connection pool */
108    public int getPoolSize(){
109       return poolSize;
110    }
111    
112    /** @param dialect SQL dialect for querying db */
113    public void setDialect(String dialect){
114       this.dialect = dialect;
115    }
116    /** @return Get the SQL dialect for querying db */
117    public String getDialect(){
118       return dialect;
119    }
120    
121    /**
122     * Get a default details wrapping object for <b>database</b> name.
123     * @param database Name of the database to get defaults for
124     * @return An instance of DatabaseDetails
125     */
126    public static DatabaseDetails getDefault(String database){
127       // Build empty details.
128       DatabaseDetails details = new DatabaseDetails();
129       
130       // Return empty if not supported.
131       if (!isSupportedDatabase(database)){
132          log.warn(database + " is not supported. Returning empty details.");
133          return details;
134       }
135       
136       // Get default properties.
137       Properties props = getDefaultProperties(database);
138       details.setUsername(props.getProperty("username"));
139       details.setPassword(props.getProperty("password"));
140       details.setDatabaseUrl(props.getProperty("databaseUrl"));
141       details.setDriverClassname(props.getProperty("driverClassname"));
142       details.setDialect(props.getProperty("dialect"));
143       
144       try {details.setPoolSize(Integer.parseInt(props.getProperty("poolSize")));}
145       catch (NumberFormatException e){
146          log.warn("Bad number within poolSize field in " + database);
147          log.warn("Setting this property to the default value: 5");
148          details.setPoolSize(5);
149       }
150       return details;
151    }
152    
153    /**
154     * Tell if the given database name is officialy supported by Join.
155     * @param database Name of the database to test (ex: "mysql")
156     * @return true if supportd, false otherwise
157     */
158    public static boolean isSupportedDatabase(String database){
159       // Browse supported db.
160       for (int i=0; i<SUPPORTED_DB.length; i++)
161          if (SUPPORTED_DB[i].equals(database.toLowerCase()))
162             return true;
163       return false;
164    }
165    
166    
167    // Protected ----------------------------------------------------------------
168    
169    /**
170     * Loading default properties for <b>database</b> from properties file in classpath
171     * @param database Name of database to load properties for
172     */
173    protected static Properties getDefaultProperties(String database){
174       log.info("Try loading " + database + ".properties from classpath...");
175       // Try loading properties for this db.
176       InputStream is = DatabaseDetails.class.getResourceAsStream(database + ".properties");
177       if (is != null){
178          // Loading props.
179          Properties props = new Properties();
180          try{
181             props.load(is);
182             is.close();
183          }
184          catch (Exception e) {log.info("Could not close InputStream: " + e);}
185          
186          return props;
187       }
188       else{
189          // Log and return null.
190          log.warn(database + ".properties not found from classpath. Returning null.");
191          return null;
192       }
193    }
194    
195    
196    // Override of Object -------------------------------------------------------
197    
198    /** @return Get the string representation of this details */
199    public String toString(){
200       StringBuffer buffer = new StringBuffer(driverClassname + "\n");
201       buffer.append(databaseUrl + "\n");
202       buffer.append(dialect + "\n");
203       buffer.append(username + "\\" + password);
204       return buffer.toString();
205    }
206 }