]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
07deb97a11bd3709073b403adaa846737ed10d80
[tmp/jakarta-migration.git] /
1 /**
2  * Copyright 2009 University of California at Berkeley
3  */
4 package org.collectionspace.services.common;
5
6 import javax.servlet.ServletContext;
7 import javax.servlet.ServletContextEvent;
8 import javax.servlet.ServletContextListener;
9
10 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
11
12 /**
13  * CollectionSpaceServiceContextListener is a ServletContextListener that helps initialize
14  * the services layer at deployment and undeployment times
15  */
16 public class CollectionSpaceServiceContextListener implements ServletContextListener {
17
18     @Override
19     public void contextInitialized(ServletContextEvent event) {
20         try {            
21             //
22             // Initialize/start the Nuxeo EP server instance and create/retrieve the service workspaces
23             //
24             ServletContext servletContext = event.getServletContext();
25             ServiceMain svcMain = ServiceMain.getInstance(servletContext);
26             
27             svcMain.retrieveAllWorkspaceIds();
28             
29             // Create required indexes (aka indices) in tables not associated
30             // with any specific tenant.
31             svcMain.createRequiredIndices();
32             //
33             // Invoke all post-initialization handlers, passing in a DataSource instance of the Nuxeo db.
34             // Typically, these handlers modify column types and add indexes to the Nuxeo db schema.
35             //
36             svcMain.firePostInitHandlers();
37                         
38         } catch (Throwable e) {
39             e.printStackTrace();
40             //fail here
41             System.err.println("[ERROR] ***");
42             System.err.println("[ERROR] The CollectionSpace Services could not initialize.  Please see the log files for details.");
43             System.err.println("[ERROR] ***");
44 //            throw new RuntimeException(e);
45         }
46     }
47
48     @Override
49     public void contextDestroyed(ServletContextEvent event) {
50         ServiceMain instance = null;
51         
52         try {
53                 ServiceMain.getInstance();
54         } catch (Throwable t) {
55                 // Do nothing.  Error already logged by the Services layer
56         } finally {
57                 if (instance != null) {
58                         instance.release();
59                 } else {
60                         System.err.println("ERROR: The CollectionSpace Services layer failed to startup successfully.  Look in the tomcat logs and cspace-services logs for details.");
61                 }
62                 JpaStorageUtils.releaseEntityManagerFactories();
63         }
64     }
65 }