
org.ow2.bonita.services.util.ServiceEnvTool Maven / Gradle / Ivy
The newest version!
/**
* 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.services.util;
import org.ow2.bonita.pvm.env.Environment;
import org.ow2.bonita.services.Archiver;
import org.ow2.bonita.services.Querier;
import org.ow2.bonita.services.Recorder;
import org.ow2.bonita.services.UUIDGenerator;
import org.ow2.bonita.services.handlers.FinishedInstanceHandler;
import org.ow2.bonita.services.handlers.UndeployedProcessHandler;
import org.ow2.bonita.services.impl.QuerierListAccessor;
import org.ow2.bonita.util.BonitaRuntimeException;
import org.ow2.bonita.util.EnvConstants;
import org.ow2.bonita.util.ExceptionManager;
import org.ow2.bonita.util.Misc;
/**
* This class holds the {@link EnvironmentFactory} singleton.
*
* The {@link EnvironmentFactory} returned by {@link #getEnvironmentFactory()} comes from the
* parsing of the resource defined by the property
* {@link #ENVIRONMENT_PROPERTY}. If this property has not been set, the
* {@link #DEFAULT_ENVIRONMENT} resource is used and parsed.
*
* @author Marc Blachon, Guillaume Porcher, Charles Souillard, Miguel Valdes, Pierre Vigneras
*/
public class ServiceEnvTool {
protected ServiceEnvTool() { }
protected static Environment getEnv() {
final Environment environment = Environment.getCurrent();
Misc.badStateIfNull(environment, "Environment is null!");
return environment;
}
@SuppressWarnings("unchecked")
protected static T getEnvObject(final Class clazz, final String name) {
final Environment environment = getEnv();
Misc.badStateIfNull(environment, "Environment is null!");
final Object object = environment.get(name);
if (object == null) {
String message = ExceptionManager.getInstance().getMessage("bs_SET_1", name);
throw new BonitaRuntimeException(message);
} else if (!clazz.isInstance(object)) {
String message = ExceptionManager.getInstance().getMessage(
"bs_SET_2", object, name, clazz.getName());
throw new BonitaRuntimeException(message);
}
return (T) object;
}
public static Recorder getRecorder() {
return getEnvObject(Recorder.class, Recorder.DEFAULT_KEY);
}
public static Archiver getArchiver() {
return getEnvObject(Archiver.class, Archiver.DEFAULT_KEY);
}
//// TODO: remove the 3 methods
public static Querier getAllQueriers() {
return getQuerierListAccessor(Querier.DEFAULT_KEY).getAllQueriers();
}
public static Querier getJournalQueriers() {
return getQuerierListAccessor(Querier.DEFAULT_KEY).getJournals();
}
public static Querier getHistoryQueriers() {
return getQuerierListAccessor(Querier.DEFAULT_KEY).getHistories();
}
//// End of remove
private static QuerierListAccessor getQuerierListAccessor(final String queryList) {
return getEnvObject(QuerierListAccessor.class, queryList);
}
public static Querier getAllQueriers(final String queryList) {
return getQuerierListAccessor(queryList).getAllQueriers();
}
public static Querier getJournalQueriers(final String queryList) {
return getQuerierListAccessor(queryList).getJournals();
}
public static Querier getHistoryQueriers(final String queryList) {
return getQuerierListAccessor(queryList).getHistories();
}
public static UUIDGenerator getUUIDGenerator() {
return getEnvObject(UUIDGenerator.class, EnvConstants.UUID_GENERATOR_NAME);
}
public static FinishedInstanceHandler getFinishedInstanceHandler() {
return getEnvObject(FinishedInstanceHandler.class, FinishedInstanceHandler.DEFAULT_KEY);
}
public static UndeployedProcessHandler getUndeployedProcessHandler() {
return getEnvObject(UndeployedProcessHandler.class, UndeployedProcessHandler.DEFAULT_KEY);
}
public static String getUserId() {
final String userId = getEnv().getUserId();
if (userId == null) {
String message = ExceptionManager.getInstance().getMessage("bs_SET_3");
throw new BonitaRuntimeException(message);
}
return userId;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy