1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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.geronimo.derby;
18
19 import java.net.InetAddress;
20 import java.net.InetSocketAddress;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.derby.drda.NetworkServerControl;
25 import org.apache.geronimo.gbean.GBeanInfo;
26 import org.apache.geronimo.gbean.GBeanInfoBuilder;
27 import org.apache.geronimo.gbean.GBeanLifecycle;
28
29 /**
30 * A GBean that manages remote network access to the embedded Derby server.
31 *
32 * todo need to figure out how to configure this without using system properties
33 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
34 */
35 public class DerbyNetworkGBean implements GBeanLifecycle {
36 private static final Log log = LogFactory.getLog("DerbyNetwork");
37
38 private NetworkServerControl network;
39 private String host = "localhost";
40 private int port = 1527;
41
42 public DerbyNetworkGBean(DerbySystem system) {
43 }
44
45 public String getHost() {
46 return host;
47 }
48
49 public void setHost(String host) {
50 this.host = host;
51 }
52
53 public int getPort() {
54 return port;
55 }
56
57 public void setPort(int port) {
58 this.port = port;
59 }
60
61 public InetSocketAddress getAddress() {
62 return new InetSocketAddress(getHost(), getPort());
63 }
64
65 public void doStart() throws Exception {
66 InetAddress address = InetAddress.getByName(host);
67 network = new NetworkServerControl(address, port);
68 network.start(null);
69 log.debug("Started on host " + host + ':' + port);
70 }
71
72 public void doStop() throws Exception {
73 if (network != null) {
74 try {
75 network.shutdown();
76 } finally {
77 network = null;
78 }
79 }
80 log.debug("Stopped");
81 }
82
83 public void doFail() {
84 }
85
86 public static final GBeanInfo GBEAN_INFO;
87
88 public static GBeanInfo getGBeanInfo() {
89 return GBEAN_INFO;
90 }
91
92 static {
93 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Derby Connector", DerbyNetworkGBean.class);
94 infoFactory.addAttribute("host", String.class, true, true);
95 infoFactory.addAttribute("port", Integer.TYPE, true, true);
96 infoFactory.addAttribute("address", InetSocketAddress.class, false);
97 infoFactory.addReference("derbySystem", DerbySystem.class, "GBean");
98 infoFactory.setConstructor(new String[]{"derbySystem"});
99 GBEAN_INFO = infoFactory.getBeanInfo();
100 }
101 }