Package org.jbpm.env.impl

Source Code of org.jbpm.env.impl.DefaultEnvironmentFactory

/*
* 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.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jbpm.env.Context;
import org.jbpm.env.Environment;
import org.jbpm.env.EnvironmentFactory;
import org.jbpm.env.xml.EnvironmentParser;
import org.jbpm.wire.WireContext;
import org.jbpm.wire.WireDefinition;

/**
* an environment factory that also is the application context.
*
* <p>This environment factory will produce environments with 2 contexts:
* the application context and the block context.
* </p>
*
* <p>An application context is build from two wire definitions: the application
* wire definition and the environment wire definition.
* </p>
*
* <p>The application context itself is build from the application wire definition.
* So all objects that are created in this context remain cached for the lifetime of
* this application context object.
* </p>
*
* <p>This application context is also a environment factory.  The produced environments
* contain 2 contexts: the application context itself and a new environment context,
* build from the environment wire definition.  For each created environment, a new
* environment context will be created from the same environment wire definition.
* Objects in the environment context will live for as long as the environment.
* </p>
* @author Tom Baeyens
*/
public class DefaultEnvironmentFactory extends EnvironmentFactory implements Context {

  private static final long serialVersionUID = 1L;
  private static final Logger log = Logger.getLogger(DefaultEnvironmentFactory.class.getName());
  protected static EnvironmentParser environmentParser = new EnvironmentParser();

  protected WireContext applicationWireContext = null;
  protected WireDefinition blockWireDefinition = null;
  protected List<Class<?>> hiddenExceptionTypes = null;
 
  public Environment openEnvironment() {
    DefaultEnvironment environment = new DefaultEnvironment(this);

    // set the classloader
    ClassLoader classLoader = applicationWireContext.getClassLoader();
    if (classLoader!=null) {
      environment.setClassLoader(classLoader);
    }

    // add the application context
    environment.addContext(applicationWireContext);

    // add the environment block context
    WireContext blockContext = new WireContext(blockWireDefinition, DefaultEnvironment.CONTEXTNAME_BLOCK, environment, true);
    // add the environment block context to the environment
    environment.addContext(blockContext);
    // finish the creation of the environment wire context
    blockContext.create();

    // fire an open environment event
    applicationWireContext.fire(DefaultEnvironment.EVENT_OPENENVIRONMENT, environment);
   
    // if all went well, only then push the created environment
    push(environment);

    return environment;
  }

  public void setEnvironment(Environment environment) {
    // this object is the environment factory and the application context, so
    // it doesn't need to do anythings when the application context is added
    // to the environment
  }

  public void close() {
    applicationWireContext.fire(WireContext.EVENT_CLOSE, null);
  }

  public void handleException(DefaultEnvironment defaultEnvironment, Throwable exception) {
    if (mustBeLogged(exception)) {
      log.log(Level.SEVERE, "exception in environment block:"+exception.getMessage(), exception);
    }

    WireContext wireContext = (WireContext) defaultEnvironment.getBlockContext();
    wireContext.fire(DefaultEnvironment.EVENT_EXCEPTION, this);
  }

  protected boolean mustBeLogged(Throwable exception) {
    if (hiddenExceptionTypes==null) {
      return true;
    }
    for (Class<?> hiddenExceptionType: hiddenExceptionTypes) {
      if (hiddenExceptionType.isAssignableFrom(exception.getClass())) {
        return true;
      }
    }
    return false;
  }

  // application context delegation methods ///////////////////////////////////
 
  public Object get(String key) {
    return applicationWireContext.get(key);
  }

  public <T> T get(Class<T> type) {
    return applicationWireContext.get(type);
  }

  public String getName() {
    return applicationWireContext.getName();
  }

  public boolean has(String key) {
    return applicationWireContext.has(key);
  }

  public Set<String> keys() {
    return applicationWireContext.keys();
  }

  public Object set(String key, Object value) {
    return applicationWireContext.set(key, value);
  }

  // getters and setters //////////////////////////////////////////////////////
 
  public WireDefinition getBlockContextDefinition() {
    return blockWireDefinition;
  }
  public void setBlockWireDefinition(WireDefinition blockWireDefinition) {
    this.blockWireDefinition = blockWireDefinition;
  }
  public List<Class< ? >> getHiddenExceptionTypes() {
    return hiddenExceptionTypes;
  }
  public void setHiddenExceptionTypes(List<Class< ? >> hiddenExceptionTypes) {
    this.hiddenExceptionTypes = hiddenExceptionTypes;
  }
  public WireContext getApplicationWireContext() {
    return applicationWireContext;
  }
  public void setApplicationWireContext(WireContext applicationWireContext) {
    this.applicationWireContext = applicationWireContext;
  }
  public WireDefinition getBlockWireDefinition() {
    return blockWireDefinition;
  }
}
TOP

Related Classes of org.jbpm.env.impl.DefaultEnvironmentFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.