use of org.hsqldb.persist.HsqlProperties in project tomee by apache.
the class HsqlService method init.
@Override
public void init(final Properties p) throws Exception {
final Properties properties = new Properties();
for (final Map.Entry<Object, Object> entry : p.entrySet()) {
// Sometimes the properties object has non string values
if (!(entry.getKey() instanceof String))
continue;
if (!(entry.getValue() instanceof String))
continue;
final String property = (String) entry.getKey();
final String value = (String) entry.getValue();
if (property.startsWith(sc_key_dbname + ".") || property.startsWith(sc_key_database + ".")) {
throw new ServiceException("Databases cannot be declared in the hsql.properties. " + "Instead declare a database connection in the openejb.conf file");
}
if ("port".equals(property)) {
properties.setProperty(sc_key_port, value);
} else if ("bind".equals(property)) {
properties.setProperty(sc_key_address, value);
} else {
properties.setProperty(property, value);
}
}
properties.setProperty(sc_key_no_system_exit, "true");
final boolean disabled = Boolean.parseBoolean(properties.getProperty("disabled"));
final ContainerSystem containerSystem = SystemInstance.get().getComponent(ContainerSystem.class);
if (!disabled && containerSystem != null) {
final NamingEnumeration<Binding> bindings;
try {
bindings = containerSystem.getJNDIContext().listBindings("openejb/Resource/");
final Set<String> dbnames = new TreeSet<String>();
for (final Binding binding : Collections.list(bindings)) {
final Object value = binding.getObject();
if (value instanceof DataSource) {
final DataSource jdbc = (DataSource) value;
Connection connection = null;
String path = null;
try {
connection = jdbc.getConnection();
final DatabaseMetaData meta = connection.getMetaData();
path = getPath(meta.getDriverName(), meta.getURL());
} catch (Throwable t) {
continue;
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException sqlEx) {
// no-op
}
}
}
if (path != null) {
if (dbnames.size() > 9) {
throw new ServiceException("Hsql Server can only host 10 database instances");
}
String dbname = path.substring(path.lastIndexOf(':') + 1);
dbname = dbname.substring(dbname.lastIndexOf('/') + 1);
if (!dbnames.contains(dbname)) {
properties.put(sc_key_dbname + "." + dbnames.size(), dbname);
properties.put(sc_key_database + "." + dbnames.size(), path);
dbnames.add(dbname);
}
}
}
}
} catch (NameNotFoundException e) {
//Ignore
}
// create the server
server = new Server();
// add the silent property
properties.setProperty(sc_key_silent, "true");
// set the log and error writers
server.setLogWriter(new HsqlPrintWriter(false));
server.setErrWriter(new HsqlPrintWriter(true));
server.setProperties(new HsqlProperties(properties));
// get the port
port = server.getPort();
// get the Address
final String ipString = server.getAddress();
if (ipString != null && ipString.length() > 0) {
this.ip = ipString;
}
}
}
Aggregations