package org.pentaho.mongo.wrapper;

import java.lang.reflect.Proxy;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.LogChannelInterface;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.trans.steps.mongodb.MongoDbMeta;

public class MongoClientWrapperFactory {
  public static MongoClientWrapper createMongoClientWrapper( MongoDbMeta meta, VariableSpace vars, LogChannelInterface log )
    throws KettleException {
    if ( meta.getUseKerberosAuthentication() ) {
      KerberosMongoClientWrapper wrapper = new KerberosMongoClientWrapper( meta, vars, log );
      return (MongoClientWrapper) Proxy.newProxyInstance( wrapper.getClass().getClassLoader(),
          new Class<?>[] { MongoClientWrapper.class }, new KerberosInvocationHandler( wrapper.getAuthContext(), wrapper ) );
    } else if ( !Const.isEmpty( meta.getAuthenticationUser() ) || !Const.isEmpty( meta.getAuthenticationPassword() ) ) {
      return new UsernamePasswordMongoClientWrapper( meta, vars, log );
    } else {
      return new NoAuthMongoClientWrapper( meta, vars, log );
    }
  }
}

@@ -887,118 +887,238 @@
 lse 
-if ( !Const.isEmpty( meta.getAuthenticationUser() ) %7C%7C !Const.isEmpty( meta.getAuthenticationPassword() ) 
+%7B%0A      String user = vars.environmentSubstitute(meta.getAuthenticationUser());%0A      String password = vars.environmentSubstitute(meta.getAuthenticationPassword());%0A      if (!Const.isEmpty(user) %7C%7C !Const.isEmpty(password)
 ) %7B%0A
+  
     
@@ -1157,33 +1157,32 @@
 goClientWrapper(
- 
 meta, vars, log 
@@ -1180,24 +1180,25 @@
 ars, log
- 
 );%0A    
+ 
+ 
 %7D else %7B
@@ -1200,24 +1200,26 @@
 lse %7B%0A      
+ 
+ 
 return new N
@@ -1234,33 +1234,32 @@
 goClientWrapper(
- 
 meta, vars, log 
@@ -1253,28 +1253,35 @@
 a, vars, log
- 
 );%0A
+      %7D%0A
     %7D%0A  %7D%0A%7D%0A

fix empty login/password