/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.geronimo.derby;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.derby.drda.NetworkServerControl;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
import org.apache.geronimo.gbean.GBeanLifecycle;
/**
* A GBean that manages remote network access to the embedded Derby server.
*
* todo need to figure out how to configure this without using system properties
* @version $Rev: 1033400 $ $Date: 2010-11-10 17:13:38 +0800 (Wed, 10 Nov 2010) $
*/
public class DerbyNetworkGBean implements GBeanLifecycle {
private static final Logger log = LoggerFactory.getLogger(DerbyNetworkGBean.class);
private NetworkServerControl network;
private String host = "localhost";
private int port = 1527;
private String userName="dbadmin";
private String userPassword="manager";
public DerbyNetworkGBean(DerbySystem system) {
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public InetSocketAddress getAddress() {
return new InetSocketAddress(getHost(), getPort());
}
public void doStart() throws Exception {
InetAddress address = InetAddress.getByName(host);
// set system property to enable Derby user authentication
if ("false".equals(System.getProperty("derby.connection.requireAuthentication"))){
network = new NetworkServerControl(address, port);
} else {
network = new NetworkServerControl(address, port, userName, userPassword);
}
network.start(null); // todo work out how to add this to our log stream
log.debug("Started on host " + host + ':' + port);
}
public void doStop() throws Exception {
if (network != null) {
try {
network.shutdown();
} finally {
network = null;
}
}
log.debug("Stopped");
}
public void doFail() {
}
public static final GBeanInfo GBEAN_INFO;
public static GBeanInfo getGBeanInfo() {
return GBEAN_INFO;
}
static {
GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Derby Connector", DerbyNetworkGBean.class);
infoFactory.addAttribute("host", String.class, true, true);
infoFactory.addAttribute("port", Integer.TYPE, true, true);
infoFactory.addAttribute("userName", String.class, true, true);
infoFactory.addAttribute("userPassword", String.class, true, true);
infoFactory.addAttribute("address", InetSocketAddress.class, false);
infoFactory.addReference("derbySystem", DerbySystem.class, "GBean");
infoFactory.setConstructor(new String[]{"derbySystem"});
GBEAN_INFO = infoFactory.getBeanInfo();
}
}