// 2012-10-19 johnpfeiffer requires NetworkAccountDAO, NetworkAccount, mysql-j-5.1+
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ListIterator;
import java.util.Properties;
public class NetworkAccountCLI
{
private static final String CORRECTUSAGE = "java -jar NetworkAccountCLI-" + NetworkAccount.CLASSVERSION + " host port user password";
private static final String NEWLINE = System.getProperty( "line.separator" );
private String dbms;
private String host;
private String port;
private String user;
private String password;
public static void main( String[] args )
{
if( args.length != 4)
{
System.err.println( "Incorrect number of parameters, correct usage: " + CORRECTUSAGE );
System.exit(1);
}
long startTime = System.currentTimeMillis();
Connection c = null;
NetworkAccountCLI main = new NetworkAccountCLI();
try
{
main.dbms = "mysql";
main.host = args[0];
main.port = args[1];
main.user = args[2];
main.password = args[3];
try
{
System.out.println( "DEBUG: Connecting ... " + main.dbms + " " + main.host + " " + main.port + " " + main.user + " " + main.password );
c = main.getConnection();
}catch( Exception e )
{
System.err.println( e.getMessage() );
System.exit( 1 );
}
NetworkAccountDAO networkAccountDAO = new NetworkAccountDAO( c );
ArrayList <NetworkAccount> networkAccountList = networkAccountDAO.getList();
System.out.println( "Accounts Found: " + networkAccountList.size() );
Collections.sort( networkAccountList , NetworkAccount.COMPARE_BY_CREATEDATETIME );
ListIterator <NetworkAccount> it = networkAccountList.listIterator();
NetworkAccount current = null;
StringBuilder response = new StringBuilder();
while( it.hasNext() )
{
current = it.next();
if( current.isExternalAuthenticationEnabled() )
{
response.append( current.getCreateDatetime() + "," + current.getName() + "," + current.loginUrl() + "," + current.getOid() + ",deleted="
+ current.isDeleted() + NEWLINE );
}
}
System.out.println( response.toString() );
c.close();
}catch( SQLException e )
{
System.err.println( "ERROR: SQLException: " + e.getMessage() );
System.exit( 1 );
}finally
{
try
{
if( c != null )
{
c.close();
}
}catch( Exception e )
{
}
}
System.out.println( "DEBUG: finished in " + ( System.currentTimeMillis() - startTime ) + " ms" );
} // end main
protected Connection getConnection() throws SQLException
{
DriverManager.registerDriver( new com.mysql.jdbc.Driver() );
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put( "user" , this.user );
connectionProps.put( "password" , this.password );
if( this.dbms.equals( "mysql" ) )
{
conn = DriverManager.getConnection( "jdbc:" + this.dbms + "://" + this.host + ":" + this.port + "/mysql" , connectionProps );
}else if( this.dbms.equals( "derby" ) )
{// conn = DriverManager.getConnection( "jdbc:" + this.dbms + ":" + this.dbName + ";create=true", connectionProps );
}
return conn;
}
} // end class