]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
527bc06cf65317da310087b65b1c8b1a6b375119
[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             // Upgrade database schema
30             svcMain.upgradeDatabase();
31
32             // Create required indexes (aka indices) in tables not associated
33             // with any specific tenant.
34             svcMain.createRequiredIndices();
35             //
36             // Invoke all post-initialization handlers, passing in a DataSource instance of the Nuxeo db.
37             // Typically, these handlers modify column types and add indexes to the Nuxeo db schema.
38             //
39             svcMain.firePostInitHandlers();
40
41         } catch (Throwable e) {
42             e.printStackTrace();
43             //fail here
44             System.err.println("[ERROR] ***");
45             System.err.println("[ERROR] The CollectionSpace Services could not initialize.  Please see the log files for details.");
46             System.err.println("[ERROR] ***");
47 //            throw new RuntimeException(e);
48         }
49     }
50
51     @Override
52     public void contextDestroyed(ServletContextEvent event) {
53         ServiceMain instance = null;
54
55         try {
56                 ServiceMain.getInstance();
57         } catch (Throwable t) {
58                 // Do nothing.  Error already logged by the Services layer
59         } finally {
60                 if (instance != null) {
61                         instance.release();
62                 } else {
63                         System.err.println("ERROR: The CollectionSpace Services layer failed to startup successfully.  Look in the tomcat logs and cspace-services logs for details.");
64                 }
65                 JpaStorageUtils.releaseEntityManagerFactories();
66         }
67     }
68 }