KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > hsqldb > ServerImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.hsqldb;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20
21 import org.apache.avalon.framework.CascadingRuntimeException;
22 import org.apache.avalon.framework.activity.Startable;
23 import org.apache.avalon.framework.context.Context;
24 import org.apache.avalon.framework.context.ContextException;
25 import org.apache.avalon.framework.context.Contextualizable;
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.avalon.framework.parameters.Parameterizable;
28 import org.apache.avalon.framework.parameters.Parameters;
29 import org.apache.avalon.framework.parameters.ParameterException;
30 import org.apache.avalon.framework.service.ServiceException;
31 import org.apache.avalon.framework.service.ServiceManager;
32 import org.apache.avalon.framework.service.Serviceable;
33 import org.apache.avalon.framework.thread.ThreadSafe;
34
35 import org.apache.cocoon.Constants;
36 import org.apache.cocoon.components.thread.RunnableManager;
37
38 /**
39  * This class runs an instance of the HSQLDB HSQL protocol network database server.
40  *
41  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
42  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
43  * @version CVS $Id: ServerImpl.java 292270 2005-09-28 19:23:57Z vgritsenko $
44  */

45 public class ServerImpl extends AbstractLogEnabled
46                         implements Server, Parameterizable, Contextualizable,
47                                    ThreadSafe, Runnable JavaDoc, Serviceable, Startable {
48
49     private static final boolean DEFAULT_TRACE = false;
50     private static final boolean DEFAULT_SILENT = true;
51     private static final int DEFAULT_PORT = 9002;
52     private static final String JavaDoc CONTEXT_PROTOCOL = "context:/";
53     private static final String JavaDoc DEFAULT_DB_NAME = "cocoondb";
54     private static final String JavaDoc DEFAULT_DB_PATH = "context://WEB-INF/db";
55
56     /** Cocoon context **/
57     private org.apache.cocoon.environment.Context cocoonContext;
58
59     /** The HSQLDB HSQL protocol network database server instance **/
60     private org.hsqldb.Server hsqlServer = new org.hsqldb.Server();
61
62     /** The threadpool name to be used for daemon thread */
63     private String JavaDoc m_daemonThreadPoolName = "daemon";
64
65     /** The {@link ServiceManager} instance */
66     private ServiceManager m_serviceManager;
67
68     /** Contextualize this class */
69     public void contextualize(Context context) throws ContextException {
70         cocoonContext = (org.apache.cocoon.environment.Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
71     }
72
73     /**
74      * Initialize the ServerImpl.
75      * Posible options:
76      * <ul>
77      * <li>port = port where the server is listening</li>
78      * <li>silent = false => display all queries</li>
79      * <li>trace = print JDBC trace messages</li>
80      * <li>name = name of the HSQL-DB</li>
81      * <li>path = path to the database - context-protocol is resolved</li>
82      * </ul>
83      */

84     public void parameterize(Parameters params) throws ParameterException {
85         hsqlServer.setLogWriter(null); /* Remove console log */
86         hsqlServer.setErrWriter(null); /* Remove console log */
87         hsqlServer.setPort(params.getParameterAsInteger("port", DEFAULT_PORT));
88         hsqlServer.setSilent(params.getParameterAsBoolean("silent", DEFAULT_SILENT));
89         hsqlServer.setTrace(params.getParameterAsBoolean("trace", DEFAULT_TRACE));
90         hsqlServer.setNoSystemExit(true);
91         if (getLogger().isDebugEnabled()) {
92             getLogger().debug("Configure HSQLDB with port: " + hsqlServer.getPort() +
93                               ", silent: " + hsqlServer.isSilent() +
94                               ", trace: " + hsqlServer.isTrace());
95         }
96
97         m_daemonThreadPoolName = params.getParameter("thread-pool-name", m_daemonThreadPoolName);
98
99         final String JavaDoc dbCfgPath = params.getParameter("path", DEFAULT_DB_PATH);
100         String JavaDoc dbPath = dbCfgPath;
101         // Test if we are running inside a WAR file
102
if(dbPath.startsWith(ServerImpl.CONTEXT_PROTOCOL)) {
103             dbPath = this.cocoonContext.getRealPath(dbPath.substring(ServerImpl.CONTEXT_PROTOCOL.length()));
104         }
105         if (dbPath == null) {
106             throw new ParameterException("The hsqldb cannot be used inside an unexpanded WAR file. " +
107                                          "Real path for <" + dbCfgPath + "> is null.");
108         }
109
110         String JavaDoc dbName = params.getParameter("name", DEFAULT_DB_NAME);
111         try {
112             hsqlServer.setDatabasePath(0, new File JavaDoc(dbPath).getCanonicalPath() + File.separator + dbName);
113         } catch (IOException JavaDoc e) {
114             throw new ParameterException("Could not get database directory <" + dbPath + ">", e);
115         }
116
117         if (getLogger().isDebugEnabled()) {
118             getLogger().debug("Database path is <" + hsqlServer.getDatabasePath(0, true) + ">");
119         }
120     }
121
122     /**
123      * @param serviceManager The <@link ServiceManager} instance
124      * @throws ServiceException In case we cannot find a service needed
125      */

126     public void service(ServiceManager serviceManager) throws ServiceException {
127         m_serviceManager = serviceManager;
128     }
129
130     /** Start the server */
131     public void start() {
132         RunnableManager runnableManager = null;
133         try {
134             runnableManager = (RunnableManager) m_serviceManager.lookup(RunnableManager.ROLE);
135             runnableManager.execute(m_daemonThreadPoolName, this);
136         } catch(final ServiceException e) {
137             throw new CascadingRuntimeException("Cannot get RunnableManager", e);
138         } finally {
139             if (null != runnableManager) {
140                 m_serviceManager.release(runnableManager);
141             }
142         }
143     }
144
145     /** Stop the server */
146     public void stop() {
147         getLogger().debug("Shutting down HSQLDB");
148         hsqlServer.stop();
149         getLogger().debug("Shutting down HSQLDB: Done");
150     }
151
152     /** Run the server */
153     public void run() {
154         if (getLogger().isDebugEnabled()) {
155             getLogger().debug("Starting " + hsqlServer.getProductName() + " " + hsqlServer.getProductVersion() + " with parameters:");
156         }
157         this.hsqlServer.start();
158     }
159 }
160
Popular Tags