Home » openejb-3.1.2-src » org.apache » openejb » server » hsql » [javadoc | source]

    1   /**
    2    *
    3    * Licensed to the Apache Software Foundation (ASF) under one or more
    4    * contributor license agreements.  See the NOTICE file distributed with
    5    * this work for additional information regarding copyright ownership.
    6    * The ASF licenses this file to You under the Apache License, Version 2.0
    7    * (the "License"); you may not use this file except in compliance with
    8    * the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   package org.apache.openejb.server.hsql;
   19   
   20   import org.apache.commons.dbcp.BasicDataSource;
   21   import org.apache.openejb.loader.SystemInstance;
   22   import org.apache.openejb.resource.jdbc.HsqldbDataSourcePlugin;
   23   import org.apache.openejb.server.SelfManaging;
   24   import org.apache.openejb.server.ServerService;
   25   import org.apache.openejb.server.ServiceException;
   26   import org.apache.openejb.spi.ContainerSystem;
   27   import org.hsqldb.Database;
   28   import org.hsqldb.DatabaseManager;
   29   import org.hsqldb.Server;
   30   import org.hsqldb.ServerConfiguration;
   31   import org.hsqldb.ServerConstants;
   32   import org.hsqldb.jdbcDriver;
   33   import org.hsqldb.persist.HsqlProperties;
   34   
   35   import javax.naming.Binding;
   36   import javax.naming.NameNotFoundException;
   37   import javax.naming.NamingEnumeration;
   38   import java.io.IOException;
   39   import java.io.InputStream;
   40   import java.io.OutputStream;
   41   import java.net.Socket;
   42   import java.util.Collections;
   43   import java.util.Map;
   44   import java.util.Properties;
   45   import java.util.Set;
   46   import java.util.TreeSet;
   47   
   48   /**
   49    * @version $Rev: 791660 $ $Date: 2009-07-06 17:31:49 -0700 (Mon, 06 Jul 2009) $
   50    */
   51   public class HsqlService implements ServerService, SelfManaging {
   52       private int port = ServerConfiguration.getDefaultPort(ServerConstants.SC_PROTOCOL_HSQL, false);
   53       private String ip = ServerConstants.SC_DEFAULT_ADDRESS;
   54       private Server server;
   55   
   56       public String getName() {
   57           return "hsql";
   58       }
   59   
   60       public int getPort() {
   61           return port;
   62       }
   63   
   64       public String getIP() {
   65           return ip;
   66       }
   67   
   68   
   69       public void init(Properties p) throws Exception {
   70           Properties properties = new Properties();
   71           for (Map.Entry<Object, Object> entry : p.entrySet()) {
   72               // Somtimes the properties object has non string values
   73               if (!(entry.getKey() instanceof String)) continue;
   74               if (!(entry.getValue() instanceof String)) continue;
   75   
   76               String property = (String) entry.getKey();
   77               String value = (String) entry.getValue();
   78   
   79               if (property.startsWith(ServerConstants.SC_KEY_DBNAME + ".") ||
   80                       property.startsWith(ServerConstants.SC_KEY_DATABASE + ".")) {
   81   
   82                   throw new ServiceException("Databases cannot be declared in the hsql.properties.  " +
   83                           "Instead declare a database connection in the openejb.conf file");
   84               }
   85   
   86               if ("port".equals(property)) {
   87                   properties.setProperty(ServerConstants.SC_KEY_PORT, value);
   88               } else if ("bind".equals(property)) {
   89                   properties.setProperty(ServerConstants.SC_KEY_ADDRESS, value);
   90               } else {
   91                   properties.setProperty(property, value);
   92               }
   93           }
   94           properties.setProperty(ServerConstants.SC_KEY_NO_SYSTEM_EXIT, "true");
   95   
   96           boolean disabled = Boolean.parseBoolean(properties.getProperty("disabled"));
   97           ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
   98           if (!disabled && containerSystem != null) {
   99               NamingEnumeration<Binding> bindings = null;
  100               try {
  101                   bindings = containerSystem.getJNDIContext().listBindings("openejb/Resource/");
  102                   Set<String> dbnames = new TreeSet<String>();
  103                   for (Binding binding : Collections.list(bindings)) {
  104                       Object value = binding.getObject();
  105                       if (value instanceof BasicDataSource) {
  106                           BasicDataSource jdbc = (BasicDataSource) value;
  107                           String path = getPath(jdbc);
  108                           if (path != null) {
  109                               if (dbnames.size() > 9) {
  110                                   throw new ServiceException("Hsql Server can only host 10 database instances");
  111                               }
  112                               String dbname = path.substring(path.lastIndexOf(':') + 1);
  113                               dbname = dbname.substring(dbname.lastIndexOf('/') + 1);
  114                               if (!dbnames.contains(dbname)) {
  115                                   properties.put(ServerConstants.SC_KEY_DBNAME + "." + dbnames.size(), dbname);
  116                                   properties.put(ServerConstants.SC_KEY_DATABASE + "." + dbnames.size(), path);
  117                                   dbnames.add(dbname);
  118                               }
  119                           }
  120                       }
  121                   }
  122               } catch (NameNotFoundException e) {
  123               }
  124   
  125               // create the server
  126               server = new Server();
  127               // add the silent property
  128               properties.setProperty(ServerConstants.SC_KEY_SILENT, "true");
  129               // set the log and error writers
  130               server.setLogWriter(new HsqlPrintWriter(false));
  131               server.setErrWriter(new HsqlPrintWriter(true));
  132               server.setProperties(new HsqlProperties(properties));
  133   
  134               // get the port
  135               port = server.getPort();
  136   
  137               // get the Address
  138               String ipString = server.getAddress();
  139               if (ipString != null && ipString.length() > 0) {
  140                   this.ip = ipString;
  141               }
  142           }
  143       }
  144   
  145       private String getPath(BasicDataSource jdbc) {
  146           // is this connectoion using the hsql driver?
  147           if (!jdbcDriver.class.getName().equals(jdbc.getDriverClassName())) {
  148               return null;
  149           }
  150   
  151           String url = jdbc.getUrl();
  152   
  153           // is this a hsql url?
  154           if (url == null || !url.startsWith("jdbc:hsqldb:")) {
  155               return null;
  156           }
  157   
  158           // resolve the relative path
  159           url = HsqldbDataSourcePlugin.toAbsolutePath(url);
  160   
  161           // hack off the jdbc:hsqldb stuff
  162           String path = url.substring("jdbc:hsqldb:".length());
  163   
  164           // is this a connection to a local file, mem, or res database?
  165           if (!path.startsWith("file:") && !path.startsWith("mem:") && path.startsWith("res:")) {
  166               return null;
  167           }
  168   
  169           return path;
  170       }
  171   
  172       public void service(InputStream inputStream, OutputStream outputStream) throws ServiceException, IOException {
  173           throw new UnsupportedOperationException("Method not implemented: service(InputStream in, OutputStream out)");
  174       }
  175   
  176       public void service(Socket socket) throws ServiceException, IOException {
  177           throw new UnsupportedOperationException("Method not implemented: service(Socket socket)");
  178       }
  179   
  180       public void start() throws ServiceException {
  181           if (server == null) return;
  182           server.start();
  183       }
  184   
  185       public void stop() throws ServiceException {
  186           if (server == null) return;
  187           try {
  188               server.stop();
  189           } finally {
  190               server = null;
  191               DatabaseManager.closeDatabases(Database.CLOSEMODE_COMPACT);
  192           }
  193       }
  194   }

Home » openejb-3.1.2-src » org.apache » openejb » server » hsql » [javadoc | source]