2 * Copyright 2009 University of California at Berkeley
4 package org.collectionspace.services.common;
6 import javax.servlet.ServletContext;
7 import javax.servlet.ServletContextEvent;
8 import javax.servlet.ServletContextListener;
10 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
13 * CollectionSpaceServiceContextListener is a ServletContextListener that helps initialize
14 * the services layer at deployment and undeployment times
16 public class CollectionSpaceServiceContextListener implements ServletContextListener {
19 public void contextInitialized(ServletContextEvent event) {
22 // Initialize/start the Nuxeo EP server instance and create/retrieve the service workspaces
24 ServletContext servletContext = event.getServletContext();
25 ServiceMain svcMain = ServiceMain.getInstance(servletContext);
27 svcMain.retrieveAllWorkspaceIds();
29 // Upgrade database schema
30 svcMain.upgradeDatabase();
32 // Create required indexes (aka indices) in tables not associated
33 // with any specific tenant.
34 svcMain.createRequiredIndices();
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.
39 svcMain.firePostInitHandlers();
41 } catch (Throwable e) {
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);
52 public void contextDestroyed(ServletContextEvent event) {
53 ServiceMain instance = null;
56 ServiceMain.getInstance();
57 } catch (Throwable t) {
58 // Do nothing. Error already logged by the Services layer
60 if (instance != null) {
63 System.err.println("ERROR: The CollectionSpace Services layer failed to startup successfully. Look in the tomcat logs and cspace-services logs for details.");
65 JpaStorageUtils.releaseEntityManagerFactories();