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

📄 jbpmconfiguration.java

📁 一个java工作流引擎
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  }

  public JbpmConfiguration( Properties jbpmProperties ) {
    // start with an empty properties collection
    properties = new Properties();
    // then put all default configurations in it
    properties.putAll( defaults );
    // then add (overwrite!) with the given properties
    properties.putAll( jbpmProperties );

    configureLogging();
    initializeFactory();
    logConfigurations();
    configureFileMgr();
    configureActionExecutions();
    checkDataSourceConfiguration();
  }

  // this constructor is added for testing purposes, thats why it is package private
  /* package private */ JbpmConfiguration( Properties jbpmProperties, Map factoryClassNames ) {
    // start with an empty properties collection
    properties = new Properties();
    // then put all default configurations in it
    properties.putAll( defaults );
    // then add (overwrite!) with the given properties
    properties.putAll( jbpmProperties );

    this.factoryCache = new HashMap();
		this.factoryClassNames = factoryClassNames;
		initializeFactoryClassName( "jbpm.file.mgr", fileMgrAbbreviatedClassNames );
		initializeFactoryClassName( "jbpm.id.generator", idGeneratorAbbreviatedClassNames );
		initializeFactoryClassName( "jbpm.persistence.session.factory", persistenceSessionFactoryClassNames );
  }
  
	private void initializeFactory() {
    // create an empty instance cache
    factoryCache = new HashMap();

    // initialize the factory-class-names, based upon the given properties
    factoryClassNames = new HashMap();
    initializeFactoryClassName( "jbpm.file.mgr", fileMgrAbbreviatedClassNames );
    initializeFactoryClassName( "jbpm.id.generator", idGeneratorAbbreviatedClassNames );
    initializeFactoryClassName( "jbpm.persistence.session.factory", persistenceSessionFactoryClassNames );
  }
  
  private void initializeFactoryClassName(String key, Map abbreviatedClassNames) {
    String value = properties.getProperty( key );
    if ( value != null ) {
      if ( abbreviatedClassNames.containsKey( value ) ) {
        factoryClassNames.put( key, abbreviatedClassNames.get( value ) );
      } else {
        factoryClassNames.put( key, value );
      }
    }
	}

  private void configureFileMgr() {
    DefinitionClassLoader.setFileMgr( (FileMgr) instantiate( "jbpm.file.mgr", FileMgr.class ) );
  }
  
  private void configureActionExecutions() {
    ActionImpl.setExecuteActionsFlag(getBoolean("jbpm.execute.actions"));
  }

  private void checkDataSourceConfiguration() {
		if ( properties.containsKey( "hibernate.connection.datasource" ) ) {
      properties.remove( "hibernate.connection.username" );
      properties.remove( "hibernate.connection.url" );
      properties.remove( "hibernate.connection.password" );
    }
	}

  private void configureLogging() {
    String logStdOut = properties.getProperty( "jbpm.log.stdout" );
    if ( getBoolean( "jbpm.log.stdout" ) ) {
      JbpmLogFactory.initJbpmLogging( properties );
    }
	}

  private void logConfigurations() {
    Log log = LogFactory.getLog(JbpmConfiguration.class);
    if ( log.isDebugEnabled() ) {
      log.debug( "jBpm configuration: " );
      Map sortedProps = new TreeMap( properties );
      for ( Iterator iter = sortedProps.entrySet().iterator(); iter.hasNext(); ) {
        Map.Entry entry = (Map.Entry) iter.next();
        if ( factoryClassNames.containsKey( entry.getKey() ) ) {
          log.debug( "  [" + entry.getKey() + "] " + entry.getValue() + " --> " + factoryClassNames.get( entry.getKey() ) );
        } else {
          log.debug( "  [" + entry.getKey() + "] " + entry.getValue() );
        }
      }
    }
  }

  /**
   * provides access to all the jbpm configuration properties.
   */
  public Properties getProperties() {
    return properties;
  }

  /**
   * gets one configuration property.
   */
  public String getProperty( String key ) {
    return properties.getProperty( key ); 
  }

  /**
   * checks if a key is present.
   */
  public boolean containsKey( String key ) {
    return properties.containsKey( key ); 
  }
  
  public boolean getBoolean( String key ) {
    boolean result = false;
    
    String booleanText = properties.getProperty( key );
    if ( booleanText == null ) {
      throw new ConfigurationException( "key '" + key + "' is not configured and has a required value of {true|yes|enable|on|false|no|disable|off}" );
    }

    if ( ( "true".equalsIgnoreCase(booleanText ))
         || ( "yes".equalsIgnoreCase(booleanText) )
         || ( "enable".equalsIgnoreCase(booleanText) )
         || ( "on".equalsIgnoreCase(booleanText) ) ) {
      result = true;

    } else if ( ( ! "false".equalsIgnoreCase(booleanText ))
                && ( ! "no".equalsIgnoreCase(booleanText) )
                && ( ! "disable".equalsIgnoreCase(booleanText) )
                && ( ! "off".equalsIgnoreCase(booleanText) ) ) {
      throw new ConfigurationException( "key '" + key + "' has an invalid value of '" + booleanText + "', allowed values are {true|yes|enable|on|false|no|disable|off}" );
    }
    
    return result;
  }

  public long getLong( String key ) {
    long value = 0;
    
    String valueText = properties.getProperty( key );
    if ( valueText != null ) {
      try {
				value = Long.parseLong( valueText );
			} catch (NumberFormatException e) {
				throw new ConfigurationException( "jbpm configuration property '" + key + "' is not parsable to a long : '" + valueText + "'" );
			}  
    }
    
    return value;
  }
    
  
  /**
   * instantiates an implementation of which the class-name is configured in the configuration.
   * First, this method searches for the constructor that takes a JbpmConfiguration as a parameter.
   * If that is found, the configured jBpm properties are provided in the constructor.  Otherwise
   * the default constructor is used.
   * The classloader of this class is used to load the class.
   * Instantiated objects are cached.
   * @param key is the key under which the class-name can be looked up in the configuration.
   * @throws ConfigurationException if for some reason or another, the instantiator cannot produce
   *   an object of the expectedClass.
   */
  public Object instantiate( String key, Class expectedClass ) {
    
    // first see if the requested object is in the cache
    List cacheKey = Arrays.asList( new Object[] { key, expectedClass } );
    Object object = factoryCache.get( cacheKey );
    String className = (String) factoryClassNames.get(key);
    
    if ( className == null ) {
      throw new ConfigurationException( "no class-name configured for key '" + key + "'" );
    }
    
    // if it's not in the cache
    if ( object == null ) {
      try {
        Class persistenceFactoryClass = JbpmConfiguration.class.getClassLoader().loadClass(className);
        Constructor constructor = null;
        try {
          // if the implementation has a constructor with a Properties parameter, use that one
          constructor = persistenceFactoryClass.getConstructor(new Class[] { JbpmConfiguration.class });
          object = constructor.newInstance( new Object[]{ this } );
        } catch (NoSuchMethodException e) {
          // otherwise use the default constructor
          constructor = persistenceFactoryClass.getConstructor(null);
          object = constructor.newInstance(null);
        }

      } catch (NoSuchMethodException e) {
        throw new ConfigurationException("couldn't instantiate class " + className + " (as configured in key " + key + ") : class does not have an appropriate constructor : default-constructor or a constructor that takes a Properties as an argument", e);
      } catch (NullPointerException e) {
        throw new ConfigurationException("no class specified for key " + key + " (expected the classname of an implementation of " + expectedClass.getName() + ")", e);
      } catch (ClassNotFoundException e) {
        throw new ConfigurationException("couldn't find " + className + " (as configured in key " + key + ")", e);
      } catch (Throwable t) {
        throw new ConfigurationException("couldn't instantiate class " + className + " (as configured in key " + key + ") : see stacktrace for more details", t);
      }

      // check if the instantiated class is of the expected type
      if ( ! expectedClass.isInstance(object) ) {
        throw new ConfigurationException("class configured in key " + key + " (" + className + ") is not castable to a " + expectedClass.getName());
      }

      // put the instantiated object into the cache
      factoryCache.put( cacheKey, object );
    }

    return object;
  }

}

⌨️ 快捷键说明

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