1 16 package org.apache.cocoon.components.hsqldb; 17 18 import java.io.File ; 19 import java.io.IOException ; 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 45 public class ServerImpl extends AbstractLogEnabled 46 implements Server, Parameterizable, Contextualizable, 47 ThreadSafe, Runnable , 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 CONTEXT_PROTOCOL = "context:/"; 53 private static final String DEFAULT_DB_NAME = "cocoondb"; 54 private static final String DEFAULT_DB_PATH = "context://WEB-INF/db"; 55 56 57 private org.apache.cocoon.environment.Context cocoonContext; 58 59 60 private org.hsqldb.Server hsqlServer = new org.hsqldb.Server(); 61 62 63 private String m_daemonThreadPoolName = "daemon"; 64 65 66 private ServiceManager m_serviceManager; 67 68 69 public void contextualize(Context context) throws ContextException { 70 cocoonContext = (org.apache.cocoon.environment.Context) context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT); 71 } 72 73 84 public void parameterize(Parameters params) throws ParameterException { 85 hsqlServer.setLogWriter(null); 86 hsqlServer.setErrWriter(null); 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 dbCfgPath = params.getParameter("path", DEFAULT_DB_PATH); 100 String dbPath = dbCfgPath; 101 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 dbName = params.getParameter("name", DEFAULT_DB_NAME); 111 try { 112 hsqlServer.setDatabasePath(0, new File (dbPath).getCanonicalPath() + File.separator + dbName); 113 } catch (IOException 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 126 public void service(ServiceManager serviceManager) throws ServiceException { 127 m_serviceManager = serviceManager; 128 } 129 130 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 146 public void stop() { 147 getLogger().debug("Shutting down HSQLDB"); 148 hsqlServer.stop(); 149 getLogger().debug("Shutting down HSQLDB: Done"); 150 } 151 152 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 |