All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.bonita.facade.APIInterceptor 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.facade;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ow2.bonita.env.Environment;
import org.ow2.bonita.env.EnvironmentFactory;
import org.ow2.bonita.env.GlobalEnvironmentFactory;
import org.ow2.bonita.env.InvalidEnvironmentException;
import org.ow2.bonita.facade.exception.BonitaInternalException;
import org.ow2.bonita.facade.exception.BonitaWrapperException;
import org.ow2.bonita.pvm.internal.cmd.CommandService;
import org.ow2.bonita.pvm.internal.env.Authentication;
import org.ow2.bonita.util.BonitaException;
import org.ow2.bonita.util.BonitaRuntimeException;
import org.ow2.bonita.util.Command;
import org.ow2.bonita.util.EnvTool;
import org.ow2.bonita.util.ExceptionManager;

public class APIInterceptor implements InvocationHandler, Serializable {

  private static final long serialVersionUID = 1L;

  protected static final Logger LOG = Logger.getLogger(APIInterceptor.class.getName());

  private final Object api;

  public APIInterceptor(final Object api) {
    this.api = api;
  }

  class APIInterceptorCommand implements Command {

    private static final long serialVersionUID = 6293853276365124717L;
    private final transient Method m;
    private final Object[] args;
    private final boolean isSecuritySet;

    public APIInterceptorCommand(final Method m, final Object[] args, final boolean isSecuritySet) {
      this.args = args;
      this.m = m;
      this.isSecuritySet = isSecuritySet;
      if (LOG.isLoggable(Level.FINE)) {
        StringBuffer sb = new StringBuffer();
        sb.append("Creating APIInterceptorCommand: " + this + ". Method: " + m + ", isSecuritySet:" + isSecuritySet);
        if (args != null) {
          for (Object arg : args) {
            sb.append(" - Arg: " + arg);
          }
        } else {
          sb.append(" Args: null.");  
        }
        LOG.fine(sb.toString());
      }
    }

    public Object execute(final Environment env) throws Exception {
      if (!this.isSecuritySet) {
        Authentication.setUserId(EnvTool.getBonitaSecurityContext().getUser());
      }
      try {
        if (LOG.isLoggable(Level.FINE)) {
          StringBuffer sb = new StringBuffer();
          sb.append("Executing APIInterceptorCommand: " + this + " on api: " + APIInterceptor.this.api + ". Env: " + env);
          LOG.fine(sb.toString());
        }
        return this.m.invoke(APIInterceptor.this.api, this.args);
      } catch (final InvocationTargetException e) {
        final Throwable t = e.getCause();
        if (t instanceof Exception) {
          throw (Exception) t;
        } else if (t instanceof Error) {
          throw (Error) t;
        } else {
        	String message = ExceptionManager.getInstance().getFullMessage("bai_APII_1", t);
          throw new BonitaInternalException(message, t);
        }
      }
    }
  }

  public Object invoke(final Object proxy, final Method method, final Object[] args) throws BonitaException {
    try {
      // If already in environment: use the same environment to execute the
      // command directly
      if (Environment.getCurrent() != null) {
        return new APIInterceptorCommand(method, args, true).execute(Environment.getCurrent());
      }
      // If no environment: use directly the command Service with interceptors
      final EnvironmentFactory envFactory = GlobalEnvironmentFactory.getEnvironmentFactory();
      return envFactory.get(CommandService.class).execute(new APIInterceptorCommand(method, args, false));
    } catch (final BonitaRuntimeException e) {
      final Throwable cause = e.getCause();
      if (cause instanceof BonitaException) {
        throw (BonitaException) cause;
      } else if (e instanceof InvalidEnvironmentException) {
    	  throw e;
      } else if (e instanceof BonitaWrapperException) {
    	  throw (RuntimeException) e.getCause();
      }
      throw e;
    } catch (final BonitaException e) {
      throw e;
    } catch (final RuntimeException e) {
      throw BonitaInternalException.build(e);
    } catch (final Exception t) {
    	String message = ExceptionManager.getInstance().getFullMessage("bai_APII_2", t);
      throw new BonitaInternalException(message, t);
    }
  }
}