001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.derby;
018    
019    import java.net.InetAddress;
020    import java.net.InetSocketAddress;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.apache.derby.drda.NetworkServerControl;
025    import org.apache.geronimo.gbean.GBeanInfo;
026    import org.apache.geronimo.gbean.GBeanInfoBuilder;
027    import org.apache.geronimo.gbean.GBeanLifecycle;
028    
029    /**
030     * A GBean that manages remote network access to the embedded Derby server.
031     *
032     * todo need to figure out how to configure this without using system properties
033     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
034     */
035    public class DerbyNetworkGBean implements GBeanLifecycle {
036        private static final Log log = LogFactory.getLog("DerbyNetwork");
037    
038        private NetworkServerControl network;
039        private String host = "localhost";
040        private int port = 1527;
041    
042        public DerbyNetworkGBean(DerbySystem system) {
043        }
044    
045        public String getHost() {
046            return host;
047        }
048    
049        public void setHost(String host) {
050            this.host = host;
051        }
052    
053        public int getPort() {
054            return port;
055        }
056    
057        public void setPort(int port) {
058            this.port = port;
059        }
060    
061        public InetSocketAddress getAddress() {
062            return new InetSocketAddress(getHost(), getPort());
063        }
064    
065        public void doStart() throws Exception {
066            InetAddress address = InetAddress.getByName(host);
067            network = new NetworkServerControl(address, port);
068            network.start(null); // todo work out how to add this to our log stream
069            log.debug("Started on host " + host + ':' + port);
070        }
071    
072        public void doStop() throws Exception {
073            if (network != null) {
074                try {
075                    network.shutdown();
076                } finally {
077                    network = null;
078                }
079            }
080            log.debug("Stopped");
081        }
082    
083        public void doFail() {
084        }
085    
086        public static final GBeanInfo GBEAN_INFO;
087    
088        public static GBeanInfo getGBeanInfo() {
089            return GBEAN_INFO;
090        }
091    
092        static {
093            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Derby Connector", DerbyNetworkGBean.class);
094            infoFactory.addAttribute("host", String.class, true, true);
095            infoFactory.addAttribute("port", Integer.TYPE, true, true);
096            infoFactory.addAttribute("address", InetSocketAddress.class, false);
097            infoFactory.addReference("derbySystem", DerbySystem.class, "GBean");
098            infoFactory.setConstructor(new String[]{"derbySystem"});
099            GBEAN_INFO = infoFactory.getBeanInfo();
100        }
101    }