⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 definitionclassloader.java

📁 一个java工作流引擎
💻 JAVA
字号:
package org.jbpm.model.definition.impl;

import java.io.*;
import org.apache.commons.logging.*;
import org.jbpm.persistence.*;

public class DefinitionClassLoader extends ClassLoader {

  private static FileMgr fileMgr = null;
  private Long definitionId = null;
  
  public static void setFileMgr( FileMgr fm ) {
    fileMgr = fm;
  }

  public DefinitionClassLoader( Long definitionId ) {
    // This class's classloader is specified as the parent classloader 
    // in the super constructor, 
    // For all classes that are loaded through this classloader, first the parent 
    // classloader tries to load the class.  The parent classloader 'sees' the 
    // jbpm-application-level delegation classes.  That means the classes that are packed together
    // with the jbpm application.  It is obvious that for these classes no
    // process-level versioning can be applied.
    // The classes for a specific definition are loaded in the method
    // {@link # findClass(String)}.  That method is called when the parent classloader
    // cannot find the requested class.
    this( DefinitionClassLoader.class.getClassLoader(), definitionId );
  }

  public DefinitionClassLoader( ClassLoader parent, Long definitionId ) {
    super( parent );
    this.definitionId = definitionId;
  }
  
	public InputStream getResourceAsStream( String name ) {
    InputStream stream = getParent().getResourceAsStream( name );
    if ( stream == null ) {
      while ( name.startsWith( "/" ) ) {
        name = name.substring( 1 ); 
      }
      String fileName = "classes/" + name;
      stream = new ByteArrayInputStream( getFileBytes( definitionId, fileName ) );
    }
    return stream;
  }

  protected Class findClass(String name) throws ClassNotFoundException {
    Class clazz = null;
    
    log.debug( "searching class '" + name + "' in process definition '" + definitionId + "'" );
    
    if ( fileMgr != null ) {
      String fileName = "classes/" + name.replace( '.', '/' ) + ".class";
      byte[] classBytes = getFileBytes( definitionId, fileName );
      
      if ( classBytes != null ) {
        clazz = defineClass(name, classBytes, 0, classBytes.length);
      }
    }
    
    if ( clazz == null ) {
      throw new ClassNotFoundException( "class '" + name + "' is not available in process definition '" + definitionId + "'" );
    }

    return clazz;
  }
  
  /**
   * TODO : update this method for including native code in a process archive :-)
   */
	protected String findLibrary(String libname) {
		return super.findLibrary(libname);
	}
  
  private byte[] getFileBytes( Long definitionId, String fileName ) {
    byte[] bytes = null;
    if ( fileMgr != null ) {
      bytes = fileMgr.retrieveBytes( definitionId, fileName );
    }
    return bytes;
  }
  
  private static Log log = LogFactory.getLog(DefinitionClassLoader.class);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -