/*
 * The Gemma project
 * 
 * Copyright (c) 2007 University of British Columbia
 * 
 * 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 ubic.gemma.javaspaces.gigaspaces;

import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springmodules.javaspaces.gigaspaces.GigaSpacesTemplate;

import ubic.gemma.util.AbstractSpringAwareCLI;

/**
 * @author keshav
 * @version $Id$
 */
public class ExpressionExperimentMasterCLI extends AbstractSpringAwareCLI {

    private static Log log = LogFactory.getLog( ExpressionExperimentMasterCLI.class );

    private GigaSpacesTemplate template;

    /*
     * (non-Javadoc)
     * 
     * @see ubic.gemma.util.AbstractCLI#buildOptions()
     */
    @Override
    protected void buildOptions() {
        // TODO Auto-generated method stub

    }

    /**
     * @param args
     */
    public static void main( String[] args ) {
        log.info( "Running GigaSpaces Master ... \n" );
        ExpressionExperimentMasterCLI p = new ExpressionExperimentMasterCLI();
        try {
            Exception ex = p.doWork( args );
            if ( ex != null ) {
                ex.printStackTrace();
            }
        } catch ( Exception e ) {
            throw new RuntimeException( e );
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see ubic.gemma.util.AbstractCLI#doWork(java.lang.String[])
     */
    @Override
    protected Exception doWork( String[] args ) {
        Exception err = processCommandLine( this.getClass().getName(), args );
        try {
            init();
            start();
        } catch ( Exception e ) {
            log.error( "Transformation error..." + e.getMessage() );
            e.printStackTrace();
        }

        return err;
    }

    /**
     * @throws Exception
     */
    protected void init() throws Exception {
        template = ( GigaSpacesTemplate ) this.getBean( "gigaspacesTemplate" );
    }

    /**
     * 
     *
     */
    protected void start() {
        ExpressionExperimentTask proxy = ( ExpressionExperimentTask ) this.getBean( "proxy" );
        for ( int i = 0; i < 2; i++ ) {
            StopWatch stopwatch = new StopWatch();
            stopwatch.start();

            // TODO remove me - using this test expression experiment
            // ExpressionExperiment ee = ExpressionExperiment.Factory.newInstance();
            // String name = "_" + RandomUtils.nextLong();
            // ee.setName( "Expression Experiment - " + name + "from " + this.getClass().getName() );
            // ee.setShortName( name );
            // Result res = proxy.execute( ee );

            // TODO read values from command line
            Result res = proxy.execute( "GSE3434", false, false );
            stopwatch.stop();
            long wt = stopwatch.getTime();
            log.info( "Submitted Job " + res.getTaskID() + " in " + wt + " ms.  Result expression experiment id is "
                    + res.getAnswer() + "." );

        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see ubic.gemma.util.AbstractSpringAwareCLI#processOptions()
     */
    @Override
    protected void processOptions() {
        super.processOptions();
    }
}

@@ -2776,415 +2776,8 @@
 ();%0A
-%0A            // TODO remove me - using this test expression experiment%0A            // ExpressionExperiment ee = ExpressionExperiment.Factory.newInstance();%0A            // String name = %22_%22 + RandomUtils.nextLong();%0A            // ee.setName( %22Expression Experiment - %22 + name + %22from %22 + this.getClass().getName() );%0A            // ee.setShortName( name );%0A            // Result res = proxy.execute( ee );%0A%0A
     
@@ -2885,24 +2885,25 @@
 e, false );%0A
+%0A
             
@@ -3080,16 +3080,16 @@
 id is %22%0A
-
         
@@ -3124,24 +3124,258 @@
 ) + %22.%22 );%0A%0A
+            /*%0A             * Terminate the VM after you get the result. This is needed because of the timeout millis that is set in%0A             * the spring context.%0A             */%0A            if ( res != null ) System.exit( 0 );%0A%0A
         %7D%0A  

terminate the VM after you get the result - not waiting for timeout millis