/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This 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; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.env.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import org.jbpm.PvmException;
import org.jbpm.env.Context;
import org.jbpm.env.Environment;
import org.jbpm.env.EnvironmentFactory;
import org.jbpm.tx.Transaction;
import org.jbpm.util.Closable;
/**
* @author Tom Baeyens
*/
public class DefaultEnvironment extends Environment {
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(DefaultEnvironment.class.getName());
/** key of the application context in the environment */
public static final String CONTEXTNAME_APPLICATION = "application";
/** key of the block context in the environment */
public static final String CONTEXTNAME_BLOCK = "block";
/** is fired for each exception reported in an environment block with
* {@link Environment#setException(Throwable)}. */
public static final String EVENT_EXCEPTION = "exception";
/** is fired when a environment created by this factory is being closed. The
* info object is the environment that was just opened. */
public static final String EVENT_CLOSEENVIRONMENT = "close-environment";
/** is fired when a new environment is being opened with this environment factory. The
* info object is the environment that was just opened. */
public static final String EVENT_OPENENVIRONMENT = "open-environment";
protected String userId;
protected DefaultEnvironmentFactory defaultEnvironmentFactory;
protected Map<String, Context> contexts;
protected ArrayList<String> defaultSearchOrderList;
protected String[] defaultSearchOrder;
protected ClassLoader classLoader;
protected Throwable exception;
public DefaultEnvironment(DefaultEnvironmentFactory defaultEnvironmentFactory) {
this.defaultEnvironmentFactory = defaultEnvironmentFactory;
contexts = new HashMap<String, Context>();
defaultSearchOrderList = new ArrayList<String>();
defaultSearchOrder = null;
}
// context methods ////////////////////////////////////////////////////////////
public Context getContext(String contextName) {
return contexts.get(contextName);
}
public void addContext(Context context) {
String key = context.getName();
contexts.put(key, context);
context.setEnvironment(this);
defaultSearchOrderList.add(key);
defaultSearchOrder = null;
}
public void removeContext(String contextName) {
Context removedContext = contexts.remove(contextName);
if (removedContext!=null) {
defaultSearchOrderList.remove(contextName);
defaultSearchOrder = null;
}
}
public Context getApplicationContext() {
return getContext(CONTEXTNAME_APPLICATION);
}
public Context getBlockContext() {
return getContext(CONTEXTNAME_BLOCK);
}
public EnvironmentFactory getEnvironmentFactory() {
return defaultEnvironmentFactory;
}
// userId methods ///////////////////////////////////////////////////////////
public String getUserId() {
// if the authenticated user was explicitely set
if (userId!=null) {
// return that one
return userId;
}
// if an Authentication was specified
Authentication authentication = get(Authentication.class);
if (authentication!=null) {
// let the authentication do the work
return authentication.getUserId();
}
return null;
}
public void setUserId(String userId) {
this.userId = userId;
}
// classloader methods //////////////////////////////////////////////////////
public ClassLoader getClassLoader() {
if (classLoader!=null) {
return classLoader;
}
return Thread.currentThread().getContextClassLoader();
}
public void setClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
// search methods ///////////////////////////////////////////////////////////
public Object get(String name) {
return get(name, null);
}
public Object get(String name, String[] searchOrder) {
if (searchOrder==null) {
searchOrder = getDefaultSearchOrder();
}
for (int i=0; i<searchOrder.length; i++){
Context context = contexts.get(searchOrder[i]);
if (context.has(name)) {
return context.get(name);
}
}
return null;
}
public <T> T get(Class<T> type) {
return find(type, null);
}
public <T> T find(Class<T> type, String[] searchOrder) {
if (searchOrder==null) {
searchOrder = getDefaultSearchOrder();
}
for (int i=0; i<searchOrder.length; i++){
Context context = contexts.get(searchOrder[i]);
T o = context.get(type);
if (o!=null) {
return o;
}
}
return null;
}
public Transaction getTransaction() {
// optimisation: search the block context first, only if that fails, use the default search order
Context blockContext = contexts.get(CONTEXTNAME_BLOCK);
if (blockContext!=null) {
Transaction transaction = blockContext.get(Transaction.class);
if (transaction!=null) {
return transaction;
}
}
return get(Transaction.class);
}
// exception ////////////////////////////////////////////////////////////////
public void setException(Throwable exception) {
this.exception = exception;
defaultEnvironmentFactory.handleException(this, exception);
// if an exception was thrown
if (exception!=null) {
rethrow(exception);
}
}
public Throwable getException() {
return exception;
}
// close ////////////////////////////////////////////////////////////////////
public void close() {
defaultEnvironmentFactory.applicationWireContext.fire(DefaultEnvironment.EVENT_CLOSEENVIRONMENT, this);
Environment popped = pop();
if (this!=popped) {
throw new PvmException("environment nesting problem");
}
Context context = getBlockContext();
if (context instanceof Closable) {
((Closable)context).close();
}
}
protected static void rethrow(Throwable exception) throws Error {
if (exception instanceof Error) {
throw (Error) exception;
}
if (exception instanceof RuntimeException) {
throw (RuntimeException) exception;
}
throw new PvmException(exception);
}
// private methods //////////////////////////////////////////////////////////
private String[] getDefaultSearchOrder() {
if (defaultSearchOrder==null) {
int size = defaultSearchOrderList.size();
defaultSearchOrder = (String[]) new String[size];
for (int i=0; i<size; i++) {
defaultSearchOrder[i] = defaultSearchOrderList.get(size-1-i);
}
}
return defaultSearchOrder;
}
}