KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4odoc > f1 > servlets > Db4oServletContextListener


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com */
2
3 package com.db4odoc.f1.servlets;
4
5 import com.db4o.Db4o;
6 import com.db4o.ObjectServer;
7 import javax.servlet.*;
8
9 public class Db4oServletContextListener implements ServletContextListener {
10     public static final String JavaDoc KEY_DB4O_FILE_NAME = "db4oFileName";
11
12     public static final String JavaDoc KEY_DB4O_SERVER = "db4oServer";
13
14     private ObjectServer server = null;
15
16     public void contextInitialized(ServletContextEvent event) {
17         close();
18         ServletContext context = event.getServletContext();
19         String JavaDoc filePath = context.getRealPath("WEB-INF/db/"
20                 + context.getInitParameter(KEY_DB4O_FILE_NAME));
21         server = Db4o.openServer(filePath, 0);
22         context.setAttribute(KEY_DB4O_SERVER, server);
23         context.log("db4o startup on " + filePath);
24     }
25
26     public void contextDestroyed(ServletContextEvent event) {
27         ServletContext context = event.getServletContext();
28         context.removeAttribute(KEY_DB4O_SERVER);
29         close();
30         context.log("db4o shutdown");
31     }
32
33     private void close() {
34         if (server != null) {
35             server.close();
36         }
37         server = null;
38     }
39 }
40
Popular Tags