/*
* 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.
*
* $Id: ManagedServer.java 511426 2007-02-25 03:25:02Z vgritsenko $
*/
package org.apache.xindice.server;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.Database;
import org.apache.xindice.util.Configuration;
import org.apache.xindice.util.XindiceException;
import org.apache.xindice.xml.dom.DOMParser;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* Creates an registers a database instance for use of the managed driver. This class
* may be used to create the database the managed driver uses (as is the case in the test
* cases) but it is not required. Any method may be used to create the database instance
* that the managed driver talks to. As long as the instance name exists when the driver
* fires up all will be good.
*
* @author <a href="mailto:kevin@rocketred.com.au">Kevin O'Neill</a>
* @version $Revision: 511426 $, $Date: 2007-02-24 22:25:02 -0500 (Sat, 24 Feb 2007) $
*/
public class ManagedServer {
private static final Log log = LogFactory.getLog(ManagedServer.class);
private Database db;
private boolean running;
/**
* (Optionally configure and) start the server
*/
public synchronized void start() throws Exception {
if (!running) {
configure();
running = true;
} else {
log.warn("Start called on a running server, ignored");
}
}
/**
* Stop the server, close database
*/
public synchronized void stop() throws Exception {
if (running) {
db.close();
db = null;
running = false;
} else {
log.warn("Stop called on a stopped server, ignored");
}
}
/**
* Configure and load database
*/
public synchronized void configure() throws IOException, XindiceException {
db = Database.getDatabase(loadConfiguration());
if (null == db) {
log.fatal("Unable to configure database");
throw new XindiceException("Unable to configure database");
}
if (log.isInfoEnabled()) {
log.info("Database name: '" + db.getName() + "'");
}
}
protected Configuration loadConfiguration() throws IOException, XindiceException {
Configuration config;
String configDir = null;
String configFile = System.getProperty(Xindice.PROP_XINDICE_CONFIGURATION);
if (configFile != null && !configFile.equals("")) {
if (log.isInfoEnabled()) {
log.info("Specified configuration file: '" + configFile + "'");
}
FileInputStream configXMLFile = new FileInputStream(new File(configFile));
config = new Configuration(DOMParser.toDocument(configXMLFile), false);
configDir = new File(configFile).getAbsoluteFile().getParent();
} else {
if (log.isInfoEnabled()) {
log.info("No configuration file specified, going with the default configuration");
}
config = new Configuration(DOMParser.toDocument(Xindice.DEFAULT_CONFIGURATION), false);
}
config = config.getChild("root-collection", false);
String dbRoot = config.getAttribute(Database.DBROOT, Database.DBROOT_DEFAULT);
if (!new File(dbRoot).isAbsolute()) {
// Let's see if the property was specified.
String home = System.getProperty(Xindice.PROP_XINDICE_DB_HOME);
if (home != null) {
dbRoot = new File(home + File.separator + dbRoot).getCanonicalPath();
} else if (configDir != null) {
dbRoot = configDir + File.separator + dbRoot;
} else {
dbRoot = new File("." + File.separator + dbRoot).getCanonicalPath();
log.warn("The database configuration file is not specified and there was no "
+ Xindice.PROP_XINDICE_DB_HOME + " property set, "
+ "so Xindice was unable to determine a database location. "
+ "Database will be created relative to the current directory.");
}
config.setAttribute(Database.DBROOT, dbRoot);
}
return config;
}
}