1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.openejb.server.derbynet; 18 19 import org.apache.derby.drda.NetworkServerControl; 20 import org.apache.log4j.Priority; 21 import org.apache.openejb.server.SelfManaging; 22 import org.apache.openejb.server.ServerService; 23 import org.apache.openejb.server.ServiceException; 24 import org.apache.openejb.util.Log4jPrintWriter; 25 import org.apache.openejb.loader.Options; 26 import org.apache.openejb.loader.SystemInstance; 27 28 import java.io.IOException; 29 import java.io.InputStream; 30 import java.io.OutputStream; 31 import java.net.InetAddress; 32 import java.net.Socket; 33 import java.util.Properties; 34 35 /** 36 * @version $Rev: 752255 $ $Date: 2009-03-10 13:45:35 -0700 (Tue, 10 Mar 2009) $ 37 */ 38 public class DerbyNetworkService implements ServerService, SelfManaging { 39 40 private NetworkServerControl serverControl; 41 private int port = 1527; 42 private int threads; 43 private boolean disabled; 44 private InetAddress host; 45 46 public String getIP() { 47 return host.getHostAddress(); 48 } 49 50 public String getName() { 51 return "derbynet"; 52 } 53 54 public int getPort() { 55 return port; 56 } 57 58 public void init(Properties properties) throws Exception { 59 Options options = new Options(properties); 60 61 this.threads = options.get("threads", 20); 62 this.port = options.get("port", 1527); 63 this.disabled = options.get("disabled", false); 64 65 host = InetAddress.getByName("0.0.0.0"); 66 System.setProperty("derby.system.home", SystemInstance.get().getBase().getDirectory().getAbsolutePath()); 67 } 68 69 public void service(InputStream inputStream, OutputStream outputStream) throws ServiceException, IOException { 70 } 71 72 public void service(Socket socket) throws ServiceException, IOException { 73 } 74 75 public void start() throws ServiceException { 76 if (disabled) return; 77 try { 78 serverControl = new NetworkServerControl(host, port); 79 //serverControl.setMaxThreads(threads); 80 81 serverControl.start(new Log4jPrintWriter("Derby", Priority.INFO)); 82 } catch (Exception e) { 83 throw new ServiceException(e); 84 } 85 } 86 87 public void stop() throws ServiceException { 88 if (serverControl == null) { 89 return; 90 } 91 try { 92 serverControl.shutdown(); 93 } catch (Exception e) { 94 throw new ServiceException(e); 95 } finally { 96 serverControl = null; 97 } 98 } 99 100 }