
org.ow2.bonita.env.GlobalEnvironmentFactory Maven / Gradle / Ivy
/**
* Copyright (C) 2006 Bull S. A. S.
* Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.ow2.bonita.env;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ow2.bonita.pvm.env.EnvironmentFactory;
import org.ow2.bonita.util.BonitaConstants;
import org.ow2.bonita.util.Misc;
public final class GlobalEnvironmentFactory {
private GlobalEnvironmentFactory() { }
public static final String DEFAULT_ENVIRONMENT = "bonita-environment.xml";
public static final String DEFAULT_EMBEDDED_ENVIRONMENT = "bonita-default-environment.xml";
private static final Logger LOG = Logger.getLogger(GlobalEnvironmentFactory.class.getName());
private static EnvironmentFactory environmentFactory;
/**
* Property key used to define the environment XML file to parse for the
* holding of the {@link EnvironmentFactory} and returned by {@link #getEnvironmentFactory()}.
*
* This key is set to: org.ow2.bonita.environment
*/
private static final Object LOCK = new Object();
static class BonitaShutdownHook extends Thread {
public BonitaShutdownHook() {
super();
}
@Override
public void run() {
try {
if (environmentFactory != null) {
environmentFactory.close();
environmentFactory = null;
}
} catch (final Exception ee) {
LOG.severe(Misc.getStackTraceFrom(ee));
}
}
}
public static EnvironmentFactory getEnvironmentFactory() {
synchronized (LOCK) {
if (environmentFactory == null) {
final String environmentResource = System.getProperty(BonitaConstants.ENVIRONMENT_PROPERTY, DEFAULT_ENVIRONMENT);
// search in this order: URL, FILE, RESOURCE
URL url = null;
// check if url is a valid url.
// a valid url must contain the protocol.
try {
url = new URL(environmentResource);
} catch (final MalformedURLException e) {
// url is not valid, try from file
final File file = new File(environmentResource);
if (file.exists()) {
try {
// Call toURI.toURL because toURL does not escape special characters.
// see File.toURL() javadoc.
url = file.toURI().toURL();
} catch (final MalformedURLException e1) {
Misc.unreachableStatement();
}
}
}
// if environmentResource is not a url, not a file
// search in resources.
if (url == null) {
url = Thread.currentThread().getContextClassLoader().getResource(environmentResource);
}
if (url == null) {
url = Thread.currentThread().getContextClassLoader().getResource(DEFAULT_EMBEDDED_ENVIRONMENT);
if (url == null) {
throw new EnvironmentNotFoundException(environmentResource);
}
LOG.warning("No environment found, taking default environment from: " + url);
}
if (LOG.isLoggable(Level.CONFIG)) {
LOG.config("Reading environment configuration from: " + url);
}
final byte[] content;
try {
content = Misc.getAllContentFrom(url);
} catch (final IOException e) {
throw new InvalidEnvironmentException("An IO error occurs while reading environment from: " + url
+ " Do you have read access to this ressource?", e);
}
final String environment = new String(content);
if (LOG.isLoggable(Level.CONFIG)) {
LOG.config("The environment resource " + url + " contains: " + Misc.LINE_SEPARATOR
+ environment);
}
final EnvironmentFactory factory = XpdlEnvironmentParser.parseEnvironmentFactoryFromXmlString(environment);
setEnvironmentFactory(factory);
Runtime.getRuntime().addShutdownHook(new BonitaShutdownHook());
}
if (environmentFactory == null) {
throw new InvalidEnvironmentException("environmentFactory is null.");
}
return environmentFactory;
}
}
public static void setEnvironmentFactory(final EnvironmentFactory envFactory) {
synchronized (LOCK) {
environmentFactory = envFactory;
}
}
public static boolean isInitialized() {
return environmentFactory != null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy