1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 package org.astrogrid.community.server.service ;
37
38 import org.apache.commons.logging.Log ;
39 import org.apache.commons.logging.LogFactory ;
40
41 import org.exolab.castor.jdo.Database;
42 import org.exolab.castor.jdo.PersistenceException ;
43 import org.exolab.castor.jdo.DatabaseNotFoundException ;
44
45 import org.astrogrid.community.common.service.CommunityService ;
46 import org.astrogrid.community.common.service.data.ServiceStatusData ;
47
48 import org.astrogrid.community.server.database.configuration.DatabaseConfiguration ;
49 import org.astrogrid.community.server.database.configuration.DatabaseConfigurationFactory ;
50
51 /***
52 * A base class for our server classes.
53 * Handles the service health checks.
54 * Handles the database connection settings.
55 *
56 */
57 public class CommunityServiceImpl
58 implements CommunityService
59 {
60 /***
61 * Our debug logger.
62 *
63 */
64 private static Log log = LogFactory.getLog(CommunityServiceImpl.class);
65
66 /***
67 * Our default database name, 'org.astrogrid.community.database'.
68 *
69 */
70 protected static final String DEFAULT_DATABASE_NAME = "org.astrogrid.community.database" ;
71
72 /***
73 * Our default database description, 'astrogrid-community-database.xml'.
74 *
75 */
76 protected static final String DEFAULT_DATABASE_XML = "astrogrid-community-database.xml" ;
77
78 /***
79 * Our default database SQL script, 'astrogrid-community-database.sql'.
80 *
81 */
82 protected static final String DEFAULT_DATABASE_SQL = "astrogrid-community-database.sql" ;
83
84 /***
85 * Public constructor, using default database configuration.
86 *
87 */
88 public CommunityServiceImpl()
89 {
90 log.debug("") ;
91 log.debug("----\"----") ;
92 log.debug("CommunityServiceImpl()") ;
93 log.debug(" Class : " + this.getClass()) ;
94
95
96 DatabaseConfigurationFactory factory = new DatabaseConfigurationFactory() ;
97
98
99 try {
100 this.setDatabaseConfiguration(
101 factory.loadDatabaseConfiguration(
102 DEFAULT_DATABASE_NAME,
103 DEFAULT_DATABASE_XML,
104 DEFAULT_DATABASE_SQL
105 )
106 ) ;
107 }
108
109
110 catch (Exception ouch)
111 {
112 this.logException(ouch, "Initialising default database configuration") ;
113 }
114 }
115
116 /***
117 * Public constructor, using specific database configuration.
118 *
119 */
120 public CommunityServiceImpl(DatabaseConfiguration config)
121 {
122 log.debug("") ;
123 log.debug("----\"----") ;
124 log.debug("CommunityServiceImpl()") ;
125 log.debug(" Class : " + this.getClass()) ;
126 log.debug(" Config : " + config) ;
127
128
129 this.setDatabaseConfiguration(config) ;
130 }
131
132 /***
133 * Public constructor, using parent service.
134 *
135 */
136 public CommunityServiceImpl(CommunityServiceImpl parent)
137 {
138 log.debug("") ;
139 log.debug("----\"----") ;
140 log.debug("CommunityServiceImpl()") ;
141 log.debug(" Class : " + this.getClass()) ;
142 log.debug(" Parent : " + parent.getClass()) ;
143
144
145 if (null != parent)
146 {
147 this.setDatabaseConfiguration(
148 parent.getDatabaseConfiguration()
149 ) ;
150 }
151 }
152
153 /***
154 * Service health check.
155 *
156 */
157 public ServiceStatusData getServiceStatus()
158 {
159 log.debug("") ;
160 log.debug("----\"----") ;
161 log.debug("CommunityServer.getServiceStatus()") ;
162
163 ServiceStatusData status = new ServiceStatusData() ;
164
165
166 Runtime runtime = Runtime.getRuntime() ;
167 status.setFreeMemory(
168 runtime.freeMemory()
169 ) ;
170 status.setTotalMemory(
171 runtime.totalMemory()
172 ) ;
173 log.debug(" Server free memory : " + String.valueOf(status.getFreeMemory())) ;
174 log.debug(" Server total memory : " + String.valueOf(status.getTotalMemory())) ;
175
176
177 log.debug(" Database config : " + databaseConfiguration) ;
178 if (null != databaseConfiguration)
179 {
180 log.debug(" Database name : " + databaseConfiguration.getDatabaseName()) ;
181 status.setDatabaseName(databaseConfiguration.getDatabaseName()) ;
182 }
183
184 log.debug("----\"----") ;
185 return status ;
186 }
187
188 /***
189 * Our database configuration.
190 *
191 */
192 private DatabaseConfiguration databaseConfiguration ;
193
194 /***
195 * Set our database configuration.
196 * This makes it easier to run JUnit tests with a different database configurations.
197 *
198 */
199 public void setDatabaseConfiguration(DatabaseConfiguration config)
200 {
201 this.databaseConfiguration = config ;
202 }
203
204 /***
205 * Acces to our database configuration.
206 *
207 */
208 public DatabaseConfiguration getDatabaseConfiguration()
209 {
210 return this.databaseConfiguration ;
211 }
212
213 /***
214 * Create a new database connection.
215 *
216 */
217 public Database getDatabase()
218 throws DatabaseNotFoundException, PersistenceException
219 {
220 if (null != databaseConfiguration)
221 {
222 return databaseConfiguration.getDatabase() ;
223 }
224 throw new DatabaseNotFoundException("Database configuration not set") ;
225 }
226
227 /***
228 * Close a database connection.
229 * @todo Handle java.lang.IllegalStateException
230 *
231 */
232 public void closeConnection(Database database)
233 {
234
235
236 if (null != database)
237 {
238
239
240 try {
241 database.close() ;
242 }
243 catch (Exception ouch)
244 {
245 logException(ouch, "CommunityManagerBase.closeConnection()") ;
246 }
247 }
248 }
249
250 /***
251 * Rollback a database transaction.
252 * @todo Handle java.lang.IllegalStateException
253 *
254 */
255 public void rollbackTransaction(Database database)
256 {
257
258
259 if (null != database)
260 {
261
262
263 try {
264 database.rollback() ;
265 }
266 catch (Exception ouch)
267 {
268 logException(ouch, "CommunityManagerBase.rollbackTransaction()") ;
269 }
270 }
271 }
272
273 /***
274 * Generic log reporting for an un-expected exception.
275 * Use this to log exceptions that you don;t expect to happen, and cause a failure.
276 *
277 */
278 public void logException(Throwable ouch, String location)
279 {
280 log.debug("") ;
281 log.debug(" ----") ;
282 log.debug(" WARNING - Exception caught") ;
283 log.debug(" Location : " + location) ;
284 log.debug(" Exception : " + ouch) ;
285 log.debug(" Message : " + ouch.getMessage()) ;
286 log.debug(" ----") ;
287 }
288
289 /***
290 * Generic log reporting for an expected exception.
291 * Use this to log exceptions that you expect to happen, and can recover from.
292 * This is for dubug only, and should not output anything on a release system.
293 *
294 */
295 public void logExpectedException(Throwable ouch, String location)
296 {
297 log.debug("") ;
298 log.debug(" ----") ;
299 log.debug(" DEBUG - Expected Exception handled") ;
300 log.debug(" Location : " + location) ;
301 log.debug(" Exception : " + ouch) ;
302 log.debug(" Message : " + ouch.getMessage()) ;
303 log.debug(" ----") ;
304 }
305 }