/* Copyright 2012-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.snaker.jfinal.plugin;

import javax.sql.DataSource;

import org.snaker.engine.SnakerEngine;
import org.snaker.engine.cfg.Configuration;

import com.jfinal.plugin.IPlugin;
import com.jfinal.plugin.activerecord.IDataSourceProvider;

import java.util.Properties;

/**
 * 基于Jfinal的Snaker插件
 * 通过调用getEngine方法获取引擎的服务入口
 * @author yuqs
 * @since 2.0
 */
public class SnakerPlugin implements IPlugin {
	private static boolean isStarted = false;
	private static DataSource dataSource;
    private static Properties properties;
	private static IDataSourceProvider dataSourceProvider;
	private static SnakerEngine engine;
	
	/**
	 * 根据DataSource构造插件
	 * @param dataSource 数据源
	 */
	public SnakerPlugin(DataSource dataSource) {
		SnakerPlugin.dataSource = dataSource;
	}

    /**
     * 根据DataSource构造插件
     * @param dataSource 数据源
     */
    public SnakerPlugin(DataSource dataSource, Properties properties) {
        SnakerPlugin.dataSource = dataSource;
        SnakerPlugin.properties = properties;
    }
	
	/**
	 * 根据数据源提供者构造插件
	 * @param dataSourceProvider 数据源提供接口
	 */
	public SnakerPlugin(IDataSourceProvider dataSourceProvider) {
		SnakerPlugin.dataSourceProvider = dataSourceProvider;
	}
	
	/**
	 * 启动插件
	 */
	public boolean start() {
		if (isStarted)
			return true;
		if (dataSourceProvider != null)
			dataSource = dataSourceProvider.getDataSource();
		if (dataSource == null)
			throw new RuntimeException("SnakerPlugin start error: SnakerPlugin need DataSource");
        Configuration config = new Configuration().initAccessDBObject(dataSource);
        if(properties != null) config.initProperties(properties);
		engine = config.buildSnakerEngine();
		isStarted = true;
		return true;
	}
	
	/**
	 * 获取snaker的流程引擎
	 * @return
	 */
	public static SnakerEngine getEngine() {
		return engine;
	}

	/**
	 * 停止插件
	 */
	public boolean stop() {
		isStarted = false;
		return true;
	}
}

@@ -1444,16 +1444,44 @@
 rce %E6%95%B0%E6%8D%AE%E6%BA%90%0A
+     * @param properties %E5%B1%9E%E6%80%A7%0A
      */%0A
@@ -1712,16 +1712,44 @@
 %E6%95%B0%E6%8D%AE%E6%BA%90%E6%8F%90%E4%BE%9B%E6%8E%A5%E5%8F%A3%0A
+     * @param properties %E5%B1%9E%E6%80%A7%0A
 %09 */%0A%09pu
@@ -1804,16 +1804,39 @@
 Provider
+, Properties properties
 ) %7B%0A%09%09Sn
@@ -1887,16 +1887,62 @@
 ovider;%0A
+        SnakerPlugin.properties = properties;%0A
 %09%7D%0A%09%0A%09/*
@@ -2479,16 +2479,29 @@
  @return
+ SnakerEngine
 %0A%09 */%0A%09p

support jfinal properties