View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/community/server/src/java/org/astrogrid/community/server/service/CommunityServiceImpl.java,v $</cvs:source>
3    * <cvs:author>$Author: dave $</cvs:author>
4    * <cvs:date>$Date: 2004/09/16 23:18:08 $</cvs:date>
5    * <cvs:version>$Revision: 1.8 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: CommunityServiceImpl.java,v $
9    *   Revision 1.8  2004/09/16 23:18:08  dave
10   *   Replaced debug logging in Community.
11   *   Added stream close() to FileStore.
12   *
13   *   Revision 1.7.8.1  2004/09/16 09:58:48  dave
14   *   Replaced debug with commons logging ....
15   *
16   *   Revision 1.7  2004/09/09 01:19:50  dave
17   *   Updated MIME type handling in MySpace.
18   *   Extended test coverage for MIME types in FileStore and MySpace.
19   *   Added VM memory data to community ServiceStatusData.
20   *
21   *   Revision 1.6.74.2  2004/09/07 04:38:26  dave
22   *   Debug print out of server memory ...
23   *
24   *   Revision 1.6.74.1  2004/09/07 04:01:47  dave
25   *   Added memory stats ...
26   *
27   *   Revision 1.6  2004/06/18 13:45:20  dave
28   *   Merged development branch, dave-dev-200406081614, into HEAD
29   *
30   *   Revision 1.5.14.1  2004/06/17 13:38:59  dave
31   *   Tidied up old CVS log entries
32   *
33   * </cvs:log>
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          // Create our database configuration factory.
96          DatabaseConfigurationFactory factory = new DatabaseConfigurationFactory() ;
97          //
98          // Load our default database configuration.
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         // Catch anything that went BANG.
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         // Set our database configuration.
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         // Use our parent's database configuration 
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 		// Get the current runtime data ...
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 		// Get the database config settings.
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         // If the database connection is not null.
236         if (null != database)
237             {
238             //
239             // Try to close the connection.
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         // If the database connection is not null.
259         if (null != database)
260             {
261             //
262             // Try to rollback the current transaction.
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     }