/*
 * Copyright 2010-2014 Orient Technologies LTD (info(at)orientechnologies.com)
 *
 * 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 com.orientechnologies.orient.core.metadata.schema.clusterselection;

import java.lang.reflect.Field;

import com.orientechnologies.common.factory.OConfigurableStatefulFactory;
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;

/**
 * Factory to get the cluster selection strategy.
 * 
 * @author Luca Garulli (l.garulli--at--orientechnologies.com)
 */
public class OClusterSelectionFactory extends OConfigurableStatefulFactory<String, OClusterSelectionStrategy> {
  public OClusterSelectionFactory() {
    setDefaultClass(ORoundRobinClusterSelectionStrategy.class);

    register(ODefaultClusterSelectionStrategy.NAME, ODefaultClusterSelectionStrategy.class);
    register(ORoundRobinClusterSelectionStrategy.NAME, ORoundRobinClusterSelectionStrategy.class);
    register(OBalancedClusterSelectionStrategy.NAME, OBalancedClusterSelectionStrategy.class);
    this.registerCustomizedStrategy();
  }

  private void registerCustomizedStrategy() {
    final OGlobalConfiguration cfg = OGlobalConfiguration.findByKey("db.customized.cluster.selection");
    if(cfg != null) {
      String clzName = (String)cfg.getValue();
      try {
        Class clz = Class.forName(clzName);
        Field field = clz.getField("NAME");
        if(field != null) {
          String fieldValue = (String)field.get(clz.newInstance());
          //OLogManager.instance().info(this, "register key : " + fieldValue + " value : " + clz.getName());
          register(fieldValue, clz);
        } else {
          OLogManager.instance().error(this, "NAME field missing");
        }
      }catch(Exception ex) {
        OLogManager.instance().error(this, "failed to register class - " + clzName);
      }        
    }
  }
  
  public OClusterSelectionStrategy getStrategy(final String iStrategy) {
    return newInstance(iStrategy);
  }
}

@@ -719,41 +719,8 @@
 n;%0A%0A
-import java.lang.reflect.Field;%0A%0A
 impo
@@ -792,131 +792,8 @@
 ory;
-%0Aimport com.orientechnologies.common.log.OLogManager;%0Aimport com.orientechnologies.orient.core.config.OGlobalConfiguration;
 %0A%0A/*
@@ -1423,848 +1423,10 @@
 ;%0A  
-  this.registerCustomizedStrategy();%0A  %7D%0A%0A  private void registerCustomizedStrategy() %7B%0A    final OGlobalConfiguration cfg = OGlobalConfiguration.findByKey(%22db.customized.cluster.selection%22);%0A    if(cfg != null) %7B%0A      String clzName = (String)cfg.getValue();%0A      try %7B%0A        Class clz = Class.forName(clzName);%0A        Field field = clz.getField(%22NAME%22);%0A        if(field != null) %7B%0A          String fieldValue = (String)field.get(clz.newInstance());%0A          //OLogManager.instance().info(this, %22register key : %22 + fieldValue + %22 value : %22 + clz.getName());%0A          register(fieldValue, clz);%0A        %7D else %7B%0A          OLogManager.instance().error(this, %22NAME field missing%22);%0A        %7D%0A      %7Dcatch(Exception ex) %7B%0A        OLogManager.instance().error(this, %22failed to register class - %22 + clzName);%0A      %7D        %0A    %7D%0A  %7D%0A  
+%7D%0A
 %0A  p

rollback customized cluster selection register