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.openejb;
018    
019    import java.net.InetSocketAddress;
020    import java.util.Properties;
021    
022    import org.apache.geronimo.gbean.GBeanInfo;
023    import org.apache.geronimo.gbean.GBeanInfoBuilder;
024    import org.apache.geronimo.gbean.GBeanLifecycle;
025    import org.apache.geronimo.management.geronimo.NetworkConnector;
026    import org.apache.openejb.server.ServiceManager;
027    import org.apache.openejb.loader.SystemInstance;
028    
029    /**
030     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
031     */
032    public class EjbDaemonGBean implements NetworkConnector, GBeanLifecycle {
033        private String host;
034        private int port;
035        private int threads;
036        private ServiceManager serviceManager;
037    
038        public EjbDaemonGBean() {
039            System.setProperty("openejb.nobanner","true");
040            serviceManager = new ServiceManager();
041        }
042    
043        public String getProtocol() {
044            return "ejbd";
045        }
046    
047        public String getHost() {
048            return host;
049        }
050    
051        public void setHost(String host) {
052            this.host = host;
053        }
054    
055        public int getPort() {
056            return port;
057        }
058    
059        public void setPort(int port) {
060            this.port = port;
061        }
062    
063        public int getThreads() {
064            return threads;
065        }
066    
067        public void setThreads(int threads) {
068            this.threads = threads;
069        }
070    
071        public InetSocketAddress getListenAddress() {
072            return new InetSocketAddress(host, port);
073        }
074    
075        public void doStart() throws Exception {
076            Properties properties = SystemInstance.get().getProperties();
077            properties.setProperty("ejbd.bind", host);
078            properties.setProperty("ejbd.port", Integer.toString(port));
079            if (threads > 0) {
080                properties.setProperty("ejbd.threads", Integer.toString(threads));
081            }
082    
083            serviceManager.init();
084            serviceManager.start(false);
085    
086        }
087    
088        public void doStop() throws Exception {
089            serviceManager.stop();
090        }
091    
092        public void doFail() {
093        }
094    
095        public static final GBeanInfo GBEAN_INFO;
096    
097        static {
098            GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic("OpenEJB Daemon", EjbDaemonGBean.class);
099            infoBuilder.addAttribute("host", String.class, true);
100            infoBuilder.addAttribute("port", int.class, true);
101            infoBuilder.addAttribute("threads", int.class, true);
102    
103            GBEAN_INFO = infoBuilder.getBeanInfo();
104        }
105    
106        public static GBeanInfo getGBeanInfo() {
107            return GBEAN_INFO;
108        }
109    }