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

org.ow2.bonita.runtime.CommonClassLoader 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.runtime;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.ow2.bonita.definition.GlobalClassData;
import org.ow2.bonita.services.Repository;
import org.ow2.bonita.util.BonitaRuntimeException;
import org.ow2.bonita.util.EngineEnvTool;

public final class CommonClassLoader extends ClassLoader {

  private static final Logger LOG = Logger.getLogger(CommonClassLoader.class.getName());
  private static CommonClassLoader current = new CommonClassLoader();
  public static final Object LOCK = new Object();
  private boolean isEmpty = true;
  
  private CommonClassLoader() {
    super(CommonClassLoader.class.getClassLoader());
  }

  public static CommonClassLoader getCurrent() {
    return current;
  }

  public static void reset() {
    synchronized (LOCK) {
      current = new CommonClassLoader();
    }
  }

  public Class< ? > findClass(String name) {
    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine("Looking for class " + name + "... ");
    }
    Repository repository = EngineEnvTool.getRepository();
    GlobalClassData globalClassData = repository.getGlobalClassData(name);

    try {
      if (globalClassData != null) {
        byte[] classData = globalClassData.getClassData();
        Class< ? > clazz = defineClass(name, classData, 0, classData.length);
        isEmpty = false;
        return clazz;
      }
      if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("class " + name + " not found.");
      }
      return null;
    } catch (Throwable e) {
      throw new BonitaRuntimeException("Problem while loading class with name : " + name, e);
    }
  }

  protected synchronized Class< ? > loadClass(String name, boolean resolve) throws ClassNotFoundException {
    Class< ? > c = findLoadedClass(name);
    if (c == null) {
      // Check if the class is available but not yet loaded
      c = findClass(name);
    }
    if (c == null) {
      c = getParent().loadClass(name);
    }
    if (resolve) {
      resolveClass(c);
    }
    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine("loadClass: " + name + ", result: " + c);
    }
    return c;
  }

  public boolean isEmpty() {
    return isEmpty;
  }
}






© 2015 - 2025 Weber Informatics LLC | Privacy Policy