View Javadoc

1   package org.astrogrid.xdbserver;
2   
3   import javax.servlet.*;
4   import javax.servlet.http.*;
5   
6   import org.astrogrid.cfg.ConfigReader;
7   import org.astrogrid.xmldb.client.XMLDBFactory;
8   import org.exist.xmldb.DatabaseInstanceManager;
9   import org.xmldb.api.DatabaseManager;
10  import org.xmldb.api.base.Collection;
11  import org.xmldb.api.base.Database;
12  import org.xmldb.api.base.XMLDBException;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  
17  /***
18   * Class ExistDatabaseManager
19   * Purpose: Small servlet class used for registering xmldb databases.  It is a little more specefic to eXist only for shutdown
20   * purposes.   This servlet should be defined in a servlet containers web.xml and load-on-startup should be set to
21   * make sure it is loaded.
22   * @author Kevin Benson
23   *
24   */
25  public class ExistDatabaseManager extends HttpServlet {
26     
27     //Constant variable to deterimen if this is an internal eXist db being ran.
28     private static final String DEFAULT_EXIST_DRIVER_URI = "xmldb:exist://";
29  
30     //instantiate the Logging for the class.
31     private static final Log log = LogFactory.getLog(ExistDatabaseManager.class);
32  
33     //xmldb database driver to be registered
34     private static String dbDriver = null;
35  
36     //xmldb uri to the location of the database for using the XMLDB:API
37     private static String xmldbURI = null;
38     
39     //Standard configuration class for obtaining entries in JNDI or property files.
40     public static ConfigReader conf = null;
41     
42     private static String pluginType = null;
43     
44  
45     static {
46        if(conf == null) {
47           conf = org.astrogrid.cfg.ConfigFactory.getCommonConfig();
48           pluginType = conf.getString("datacenter.querier.plugin",null);
49           if(isFitsPlugin()) {
50               xmldbURI = conf.getString("xmldb.uri");
51               dbDriver = conf.getString("xmldb.driver","org.exist.xmldb.DatabaseImpl");
52           }//if
53        }//if
54     }
55     
56     private static boolean isFitsPlugin() {
57         if(pluginType != null && pluginType.indexOf("FitsQuerierPlugin") != -1) {
58             return true;
59         }
60         return false;
61     }
62     
63     /***
64      * Method: registerDB
65      * Purpose: register a XMLDB database.  And to create the database if necessary on internal
66      * db instances.
67      * @param confFile - a configuration file for internal database normally used on eXist xmldb.
68      * @throws IllegalAccessException cannot access the class of the database driver.
69      * @throws ClassNotFoundException cannot find the class of the database driver.
70      * @throws InstantiationException cannot instantiate the class of the database driver
71      * @throws XMLDBException cannot register the database driver.
72      */
73     public void registerDB(String confFile)  throws IllegalAccessException,
74                                             ClassNotFoundException,
75                                             InstantiationException,
76                                             XMLDBException  {
77         log.info("Try registering database");
78             Class cl = Class.forName( dbDriver );
79             Database database = (Database) cl.newInstance();
80             //ClassLoader loader = this.getClass().getClassLoader();
81             //return loader.getResourceAsStream(DEFAULT_DATABASE_XSL);
82      
83             if(runningInternalEXist()) {
84                 database.setProperty("create-database", "true" );
85                 log.info("set config file for reading = " + confFile);
86                 database.setProperty("configuration",confFile);
87             }
88             DatabaseManager.registerDatabase( database );
89             XMLDBFactory.setDBInitialized(true);
90             log.info("database registered");
91     }
92  
93     /***
94      * Method: shutdownDB
95      * Purpose: Only for internal eXist xmldb use.  Shuts down the eXist database on the closing down of the servlet container.
96      * @throws XMLDBException cannot shutdown the database or obtain the root collection.
97      */
98     public void shutdownDB() throws XMLDBException {
99         if(runningInternalEXist()) {
100            log.info("try shutting down database");
101            XMLDBFactory xmldbFactory = new XMLDBFactory();
102            Collection coll = xmldbFactory.openAdminCollection();
103            //Collection coll = DatabaseManager.getCollection( xmldbURI + "/db", "admin", "" );
104            DatabaseInstanceManager mgr = (DatabaseInstanceManager)coll.getService("DatabaseInstanceManager", "1.0");
105            mgr.shutdown();
106            log.info("database shutdown");
107        }
108    }
109 
110    /***
111     * Method: runningInternalEXist
112     * Purpose: check if were running the eXist XML database internally.
113     * @return boolean if the database is eXist and running internally.
114     */
115    private boolean runningInternalEXist() {
116        if(DEFAULT_EXIST_DRIVER_URI.equals(xmldbURI)) {
117            return true;
118        }
119        return false;
120    }
121 
122    /***
123     * Method: destroy
124     * Purpose: Common Servlet API destroy method called when servlet container goes down.  Currently shuts down the database.
125     */
126    public void destroy() {
127        super.destroy();
128        if(!isFitsPlugin()) {
129            return;
130        }
131        try {
132            shutdownDB();
133        }catch(XMLDBException xdbe) {
134            log.error("COULD NOT SHUTDOWN THE DATABASE");
135            log.error(xdbe);
136            log.error(xdbe.toString());
137            log.error(xdbe.getMessage());
138            xdbe.printStackTrace();
139        }
140    }
141 
142    /***
143     * Method init
144     * Purpose: Common Servlet API initialization method to initialize a servlet.  Currently registers the database and starts up the database if necessary.
145     */
146    public void init(ServletConfig config)
147                 throws ServletException
148    {
149        super.init(config);
150        //System.out.println("inside init of EXistDatabaseManager try registering databae");
151        log.info("inside init of EXistDatabaseManager try registering databae");
152        if(!isFitsPlugin()) {
153            return;
154        }
155        try {
156            String dbConfig = conf.getString("reg.custom.exist.configuration",null);
157            String configuration = null;
158            if(dbConfig == null || dbConfig.trim().length() <= 0) {
159                dbConfig = config.getInitParameter("basedir");
160                configuration = config.getInitParameter("configuration");
161                if(configuration == null || configuration.trim().length() <= 0)
162                    configuration = "conf.xml";
163                dbConfig = (dbConfig == null) ? config.getServletContext().getRealPath(
164                 ".") : config.getServletContext().getRealPath(dbConfig);
165                 if(dbConfig.endsWith("/"))
166                     dbConfig += configuration;
167                 else
168                     dbConfig += "/" + configuration;
169            }
170            registerDB(dbConfig);
171        }catch(IllegalAccessException iae) {
172            log.error(iae);
173        }catch(ClassNotFoundException cnfe) {
174            log.error(cnfe);
175        }catch(InstantiationException ie) {
176            log.error(ie);
177        }catch(XMLDBException xdbe) {
178            log.error(xdbe);
179        }
180    }
181    
182 }