package com.senseidb.plugin;

import java.util.LinkedHashMap;
import java.util.Map;

class PluginHolder {
  private final SenseiPluginRegistry senseiPluginRegistry;
  String pluginCLass;
  String pluginName;
  String fullPrefix;
  Object instance;
  private Object factoryCreatedInstance;
  Map<String, String> properties = new LinkedHashMap<String, String>();

  public PluginHolder(SenseiPluginRegistry senseiPluginRegistry,
                      String pluginCLass,
                      String pluginName,
                      String fullPrefix) {
    this.senseiPluginRegistry = senseiPluginRegistry;
    this.pluginCLass = pluginCLass;
    this.pluginName = pluginName;
    this.fullPrefix = fullPrefix;
  }

  public PluginHolder(SenseiPluginRegistry senseiPluginRegistry,
                      Object instance,
                      String pluginName,
                      String fullPrefix) {
    this.senseiPluginRegistry = senseiPluginRegistry;
    this.instance = instance;
    this.pluginName = pluginName;
    this.fullPrefix = fullPrefix;
  }

  public Object getInstance() {
    if (instance == null) {
      synchronized (this) {
        try {
          instance = Class.forName(pluginCLass).newInstance();
        } catch (Exception ex) {
          throw new RuntimeException(ex);
        }
      }
    }
    if (instance instanceof SenseiPlugin) {
      ((SenseiPlugin) instance).init(properties, senseiPluginRegistry);
      //((SenseiPlugin) instance).start();
    }
    if (instance instanceof SenseiPluginFactory) {
      if (factoryCreatedInstance == null) {
        synchronized (instance) {
          factoryCreatedInstance = ((SenseiPluginFactory) instance).getBean(properties, fullPrefix,
              this.senseiPluginRegistry);

        }
      }
      return factoryCreatedInstance;
    }
    return instance;
  }

}

@@ -1229,101 +1229,8 @@
     
-  %7D catch (Exception ex) %7B%0A          throw new RuntimeException(ex);%0A        %7D%0A      %7D%0A    %7D%0A
     
@@ -1265,24 +1265,30 @@
 eiPlugin) %7B%0A
+      
       ((Sens
@@ -1347,16 +1347,22 @@
 istry);%0A
+      
       //
@@ -1392,24 +1392,129 @@
 e).start();%0A
+          %7D%0A        %7D catch (Exception ex) %7B%0A          throw new RuntimeException(ex);%0A        %7D%0A      %7D%0A
     %7D%0A    if
@@ -1670,16 +1670,28 @@
 stance =
+%0A           
  ((Sense
@@ -1739,21 +1739,113 @@
 ies,
- fullPrefix,%0A
+%0A                                                     fullPrefix,%0A                                       
     
@@ -1882,17 +1882,16 @@
 istry);%0A
-%0A
         

Make sure that SenseiPlugin is initialized only once.