View Javadoc

1   /*
2    * Copyright (c) 2003 by Atlassian Software Systems Pty. Ltd.
3    * All rights reserved.
4    */
5   package com.atlassian.config.db;
6   
7   import com.atlassian.config.ConfigurationException;
8   import com.atlassian.core.util.PropertyUtils;
9   import org.apache.commons.lang.StringUtils;
10  import org.apache.log4j.Logger;
11  
12  import java.util.ArrayList;
13  import java.util.Enumeration;
14  import java.util.List;
15  import java.util.Properties;
16  
17  public class DatabaseDetails
18  {
19      private static final Logger log = Logger.getLogger(DatabaseDetails.class);
20  
21      private String driverClassName;
22      private String databaseUrl;
23      private String userName;
24      private String password;
25      private int poolSize;
26      private String dialect;
27      private Properties configProps;
28      private List dbNotes = new ArrayList();
29      private Properties extraHibernateProperties = new Properties();
30  
31      public String getDatabaseUrl()
32      {
33          return databaseUrl;
34      }
35  
36      public void setDatabaseUrl(String databaseUrl)
37      {
38          this.databaseUrl = databaseUrl;
39      }
40  
41      public int getPoolSize()
42      {
43          return poolSize;
44      }
45  
46      public void setPoolSize(int poolSize)
47      {
48          this.poolSize = poolSize;
49      }
50  
51      public String getDriverClassName()
52      {
53          return driverClassName;
54      }
55  
56      public void setDriverClassName(String driverClassName)
57      {
58          this.driverClassName = driverClassName;
59      }
60  
61      public String getUserName()
62      {
63          return userName;
64      }
65  
66      public void setUserName(String userName)
67      {
68          this.userName = userName;
69      }
70  
71      public String getPassword()
72      {
73          return password;
74      }
75  
76      public void setPassword(String password)
77      {
78          this.password = password;
79      }
80  
81      public String getDialect()
82      {
83          return dialect;
84      }
85  
86      public void setDialect(String dialect)
87      {
88          this.dialect = dialect;
89      }
90  
91      public List getDbNotes()
92      {
93          return dbNotes;
94      }
95  
96      public void setDbNotes(List dbNotes)
97      {
98          this.dbNotes = dbNotes;
99      }
100 
101     public String toString()
102     {
103         StringBuffer str = new StringBuffer();
104         str = str.append(getDriverClassName()).append("\n");
105         str = str.append(getDatabaseUrl()).append("\n");
106         str = str.append(getDialect()).append("\n");
107         str = str.append(getUserName()).append("\n");
108         str = str.append(getPassword()).append("\n");
109 
110         return str.toString();
111     }
112 
113     public Properties getConfigProps()
114     {
115         return configProps;
116     }
117 
118     /**
119      * This method will set the dialect, pool size, and configuration properties based on a given database name.
120      */
121     public void setupForDatabase(String database)
122     {
123         int poolSizeToSet = 10;
124 
125         if (database.equals("other"))
126         {
127             setPoolSize(poolSizeToSet);
128             return;
129         }
130 
131         Properties props = getConfigProperties(database);
132         setDialect(props.getProperty("dialect"));
133 
134         try
135         {
136             poolSizeToSet = Integer.parseInt(props.getProperty("poolSize"));
137         }
138         catch (NumberFormatException e)
139         {
140             log.error("Could find a property for poolSize; nonetheless, defaulting to 10.");
141         }
142 
143         setPoolSize(poolSizeToSet);
144 
145         this.configProps = props;
146 
147         storeHibernateProperties(props);
148 
149     }
150 
151     /**
152      * Looks for properties prefixed with "hibernate." in a given properties file
153      * and bundles them into exextraHibernateProperties, for use in the hibernate connection.
154      * @param props
155      */
156     private void storeHibernateProperties(Properties props)
157     {
158         //now look for errata hibernate properties, which might have been passed within the properties file
159         Enumeration enu = props.keys();
160 
161         while (enu.hasMoreElements())
162         {
163             String key = (String) enu.nextElement();
164 
165             if (key.matches("hibernate.*") && props.getProperty(key) != null)
166             {
167                 extraHibernateProperties.put(key, props.getProperty(key));
168             }
169             else if (props.getProperty(key) == null) //it's helpful to output any nulls, which might be problematic when
170             {                                       //playing with a connection at the back end
171                 log.warn("database hibernate property present but set to null: [" + key + "] = [" + props.getProperty(key) + "]. Setting this property anyway." );
172                 extraHibernateProperties.put(key, props.getProperty(key));
173             }
174         }
175     }
176 
177     static Properties getConfigProperties(String databaseName)
178     {
179         return PropertyUtils.getProperties("database-defaults/" + databaseName.toLowerCase() + ".properties", DatabaseDetails.class);
180     }
181 
182     /**
183      * Constructs a default db config instance based upon the contents of a properties file,
184      * expected to look something like:
185      * <p/>
186      * driverClassName=oracle.jdbc.OracleDriver
187      * databaseUrl=jdbc:oracle:thin:@localhost:1521:SID
188      * userName=
189      * password=
190      * poolSize= 10
191      * dialect=net.sf.hibernate.dialect.HSQLDialect
192      * <p/>
193      * and be within the pwd/actions dir..
194      *
195      * @param databaseName
196      */
197     public static DatabaseDetails getDefaults(String databaseName) throws ConfigurationException
198     {
199         DatabaseDetails defaults = new DatabaseDetails();
200 
201         if ("other".equals(databaseName.toLowerCase()))
202             return defaults;
203 
204         Properties props = getConfigProperties(databaseName);
205         if (props == null)
206         {
207             throw new ConfigurationException("The default values for '" + databaseName + "' not found. Check that properties file exists in your database-defaults directory");
208         }
209 
210         defaults.setDriverClassName(props.getProperty("driverClassName"));
211         defaults.setDatabaseUrl(props.getProperty("databaseUrl"));
212         defaults.setUserName(props.getProperty("userName"));
213         defaults.setPassword(props.getProperty("password"));
214 
215         defaults.storeHibernateProperties(props);
216 
217         // load notes
218         List dbNotes = new ArrayList();
219         int i = 1;
220 
221         while (StringUtils.isNotEmpty(props.getProperty("note" + i)))
222         {
223             dbNotes.add(props.getProperty("note" + i));
224             i++;
225         }
226 
227         defaults.setDbNotes(dbNotes);
228 
229         try
230         {
231             defaults.setPoolSize(Integer.parseInt(props.getProperty("poolSize")));
232         }
233         catch (NumberFormatException e)
234         {
235             log.error("Bad number within poolSize field in " + databaseName + ".");
236             throw new ConfigurationException(e.getMessage(), e);
237         }
238 
239         return defaults;
240     }
241 
242     public Properties getExtraHibernateProperties()
243     {
244         return extraHibernateProperties;
245     }
246 
247     /**
248      *
249      * @return boolean - whether driver exists in class path.
250      */
251     public boolean checkDriver()
252     {
253         try
254         {
255             Class.forName(getDriverClassName());
256             return true;
257         }
258         catch (ClassNotFoundException e)
259         {
260             return false;
261         }
262     }
263 }