Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [geclipse-dev] JSDL job description creation

This is the complete class I use an initialization

/*****************************
************************************************
 * Copyright (c) 2008 g-Eclipse Consortium
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Initial development of the original code was made for the
 * g-Eclipse project founded by European Union
 * project number: FP6-IST-034327  http://www.geclipse.eu/
 *
 * Contributors:
 *    Neophytos Theodorou (phytosth@xxxxxxxxxxxx)- UCY
 *****************************************************************************/

package eu.geclipse.benchmarking.model;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.emf.ecore.xmi.impl.XMLMapImpl;

import eu.geclipse.core.model.GridModel;
import eu.geclipse.core.model.GridModelException;
import eu.geclipse.core.model.IGridElement;
import eu.geclipse.core.model.IGridJobDescription;
import eu.geclipse.core.model.IGridJobID;
import eu.geclipse.core.model.IGridJobService;
import eu.geclipse.core.model.IVirtualOrganization;
import eu.geclipse.core.model.impl.ResourceGridContainer;
import eu.geclipse.core.reporting.ProblemException;
import eu.geclipse.gbdl.model.DocumentRoot;
import eu.geclipse.gbdl.model.GbdlPackage;
import eu.geclipse.gbdl.util.BenchmarkTypeWrapper;
import eu.geclipse.jsdl.JSDLJobDescription;
import eu.geclipse.jsdl.JSDLModelFacade;
import eu.geclipse.jsdl.model.base.DataStagingType;
import eu.geclipse.jsdl.model.base.SourceTargetType;

//TODO: Auto-generated Javadoc
/**
 * The Class GBDLBenchmarkDescription.
 */
public class GBDLBenchmarkDescription extends ResourceGridContainer
implements IGridBenchmarkDescription
{

  /** The file count. */
  private static int fileCount = 0;

  /** The gbdl package. */
  protected GbdlPackage gbdlPackage = GbdlPackage.eINSTANCE;

  /** The document root. */
  protected DocumentRoot documentRoot;
  //protected BenchmarkType benchmarkType;
  /** The benchmark. */
  protected BenchmarkTypeWrapper benchmark;

  /** The loaded. */
  private boolean loaded = false;


  /**
   * Creates a new instance of <code>GBDLBenchmarkDescription</code> based on the contents of the <code>IFile</code>
   * provided as argument.
   *
   * @param file An instance of <code>IFile</code> referring to a .gbdl benchmark description.
   */
  public GBDLBenchmarkDescription( final IFile file ) {
    super( file );
    try {
      //Read file contents
      InputStream fileContent = file.getContents();
      try {
        if( fileContent.read() != -1 ) {
          loadModel( file );
        }
      } catch( IOException e ) {
        System.out.println( e.getMessage() ); //TODO Neophytos - Error Reporting   
      } finally {
        try {
          fileContent.close();
        } catch( IOException exception ) {
          // ignore closing errors
        }
      }
    } catch( CoreException e ) {
      // TODO Neophytos - Error Reporting
    }
  }

  /**
   * Load the gbdl model form the contents of the .gbdl file supplied as argument. Assigns the
   * <code>BenchmarkType</code> object describing the model to benchmark attribute and the root
   * of the GBDL document to documentRoot attribute.
   *
   * @param file An instance of <code>IFile</code> referring to a .gbdl benchmark description.
   */
  public void loadModel( final IFile file ) {
    //Get filepath and resource URI
    String filepath = file.getFullPath().toString();
    URI uri = URI.createPlatformResourceURI( filepath, false );

    //Create the new Resource
    ResourceSet resourceSet = new ResourceSetImpl();
    Resource resourceA = resourceSet.createResource( uri );

    //Create an XML map to parse the gbdl XML
    XMLMapImpl xmlmap = new XMLMapImpl();
    xmlmap.setNoNamespacePackage( GbdlPackage.eINSTANCE );
    Map<String, Object> options = new HashMap<String, Object>();
    options.put( XMLResource.OPTION_ENCODING, "UTF8" ); //$NON-NLS-1$
    options.put( XMLResource.OPTION_XML_MAP, xmlmap );

    //Load the model
    try {
      resourceA.load( options );
      this.documentRoot = ( DocumentRoot )resourceA.getContents().get( 0 );
      this.benchmark = new BenchmarkTypeWrapper(this.documentRoot.getBenchmark());
    } catch( IOException ioEx ) {
      System.out.println( ioEx.toString() );// TODO Neophytos - error Reporting
    } catch( Exception e ) {
      e.printStackTrace();// TODO Neophytos - error Reporting
    }
    this.loaded = true;
  }

  /**
   * Checks if is loaded.
   *
   * @return true, if is loaded
   */

  public boolean isLoaded(){
    return this.loaded;
  }

  /**
   * Gets the jSDL job descriptions.
   *
   * @return the jSDL job descriptions
   */
  public  List<IGridJobDescription> getJSDLJobDescriptions(){
    IFolder tempFolder = createTemporaryJSDLFolder();
    IFile tempJSDLFile = createTemporaryJSDLFile(tempFolder);
    IGridElement element = GridModel.getRoot().findElement( tempJSDLFile );
    JSDLJobDescription basicJSDL = null;
    if( element instanceof JSDLJobDescription ) {
      basicJSDL = initializeJSDLJobDescription(( JSDLJobDescription )element);
      basicJSDL.save( tempJSDLFile );
    }
    ArrayList<IGridJobDescription> tmp = new ArrayList<IGridJobDescription>();
    tmp.add( basicJSDL );
    return tmp;
  }

  /**
   * Creates the temporary jsdl folder.
   *
   * @return the i folder
   */
  private IFolder createTemporaryJSDLFolder(){
    IProject currentProject = ResourcesPlugin.getWorkspace().getRoot().getProject(this.getProject().getName());
    IFolder tempJSDLFolder = currentProject.getFolder( "Benchmark Descriptions" ).getFolder( ".tmp_jsdl" ); //$NON-NLS-1$//$NON-NLS-2$
    if(!tempJSDLFolder.exists())
      try {
        tempJSDLFolder.create( false, true, null );
      } catch( CoreException e ) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      return tempJSDLFolder;
  }

  /**
   * Creates the temporary jsdl file.
   *
   * @param jsdlFolder the jsdl folder
   *
   * @return the i file
   */
  private IFile createTemporaryJSDLFile(final IFolder jsdlFolder){
    IFile newJSDL = jsdlFolder.getFile( "." + this.benchmark.getName().toLowerCase() + (fileCount++) + ".jsdl" ); //$NON-NLS-1$//$NON-NLS-2$
    if(!newJSDL.exists()){

      try {
        FileOutputStream tempOutStream= new FileOutputStream(newJSDL.getRawLocation().toOSString());
        tempOutStream.close();

        FileInputStream tempInStream = new FileInputStream(newJSDL.getRawLocation().toOSString());
        newJSDL.create( tempInStream, true, null );
        tempInStream.close();


      } catch( FileNotFoundException e ) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch( CoreException e1 ) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } catch( IOException e ) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
    return newJSDL;
  }

  /**
   * Initialize jsdl job description.
   *
   * @param newJSDL the new jsdl
   *
   * @return the jSDL job description
   */
  private JSDLJobDescription initializeJSDLJobDescription(final JSDLJobDescription newJSDL){
    //Initialize JSDL model
    newJSDL.createRoot();
    newJSDL.addJobDescription();
    newJSDL.addJobIdentification( "Benchmark: " + this.benchmark.getName(), this.benchmark.getDescription() ); //$NON-NLS-1$
    newJSDL.addApplication();

    //Add Benchmark Name
    newJSDL.setApplicationName( "Benchmark: " + this.benchmark.getName() ); //$NON-NLS-1$

    //Add executable

    //Add application, stage in  script and archive and stage out output
    newJSDL.addPOSIXApplicationDetails( this.benchmark.getName(),
                                        this.benchmark.getExecutableString(),
                                        null, //stdin
                                        null, //stdinName
                                        null, //stdout
                                        null, //outName
                                        null, //err
                                        null //errName
    );

    //Add StageIn
    List<String> stageInURIs = this.benchmark.getStageInURIs();
    if(stageInURIs != null)
      for(String URI : stageInURIs){
        DataStagingType d = getDataStageIn(URI);
        newJSDL.addDataStagingType( d );
      }

    //AddStageOut
    //TODO Add StageOut

    //Add Resources
    //TODO Rewrite to create multiple JSDLs
    newJSDL.addCandidateHosts( this.benchmark.getResourcesURLs() );

    return newJSDL;

  }

  private DataStagingType getDataStageIn(final String stageInURI){
    return getDataStaging(stageInURI, "in"); //$NON-NLS-1$
  }

  private DataStagingType getDataStaging(final String stageURI, final String direction){
    DataStagingType newData = JSDLModelFacade.getDataStagingType();
    newData.setFileName( stageURI );
    SourceTargetType sourceDataOut = JSDLModelFacade.getSourceTargetType();
    sourceDataOut.setURI( stageURI);
    if(direction.equals( "in" )) //$NON-NLS-1$
      newData.setSource( sourceDataOut );
    else
      newData.setTarget( sourceDataOut );
    return newData;
  }
  public void submitJSDL(final JSDLJobDescription jsdl){
    IGridJobID jobID = null;
    System.out.println("Submiting JSDL");
    try {
      IVirtualOrganization vo =  this.getProject().getVO();
      IGridJobService[] subServices = vo.getJobSubmissionServices( new NullProgressMonitor() );
      if ( subServices[0] != null && jsdl != null ) {
        jobID = subServices[0].submitJob( jsdl, new NullProgressMonitor() );
        System.out.println(jobID.getJobID());
       
        if ( jobID != null ) {
          System.out.println("Submision Successfull"); //$NON-NLS-1$
         
        }
      }
    } catch( GridModelException e ) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch( ProblemException e ) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public String getBenchmarkDescription() {
    return this.benchmark.getName();
  }

  public String getBenchmarkName() {
    return this.benchmark.getDescription();
  }
}


Hope it helps

Neophytos


On Wed, May 7, 2008 at 5:55 PM, reuillon <reuillon@xxxxxxxx> wrote:
Neophytos Theodorou a écrit :

Deer Romain. First of all check if the file is actually created in the local disk and in which folder. Another think is instead of trying to create a new JSDL file from the file instance
try getting it like this
IFolder tempFolder = createTemporaryJSDLFolder();
   IFile tempJSDLFile = createTemporaryJSDLFile(tempFolder);
How do you implement createTemporaryJSDLFile, because what I do at that step for the moment is creating an empty file?

   IGridElement element = GridModel.getRoot().findElement( tempJSDLFile );
If I do that on an empty file how could an instance of JSDLJobDescription be created?

   JSDLJobDescription basicJSDL = null;
   if( element instanceof JSDLJobDescription ) {
     basicJSDL = initializeJSDLJobDescription(( JSDLJobDescription )element);
     basicJSDL.save( tempJSDLFile );
   }

Also I suggest that you take a look at the code of GBDL Benchmark Description. At least I have tested it and it works so you would have a known path to achiece your goal. I'm not also sure on the call on creating a new project. Isn't there a way to use the current project?

Thaks Neophytos
Thanks, Romain.

On Wed, May 7, 2008 at 4:53 PM, reuillon <reuillon@xxxxxxxx <mailto:reuillon@xxxxxxxx>> wrote:

   Thank you for you answer but using your code I obtains the same
   exception.

   I think the problem is that the file is not being looked for at
   the right place:
   java.io.FileNotFoundException: /myproject/jsdl/myjsld.jsdl  (No
   such file or directory)

   When I create the IFile the getFullPath method gives me:
   /myproject/jsdl/myjsld.jsdl

   The getJSDLString method of the JSDLJobDescription class contains
   the following code:

     File jsdlFile = this.getPath().toFile();
     FileInputStream jsdlStream = null;
     jsdlStream = new FileInputStream( jsdlFile );

   this.getPath call getFullPath on the IFile. The creation of the
   input stream is done on the absolute path:
   /myproject/jsdl/myjsld.jsdl, that does'nt exist.

   Is my IFile is not created correctly or is it a bug?

   Cheers, Romain.

   Neophytos Theodorou a écrit :

       Had a simillar problem. it seems that you have to open an
       output stream to the file to actually create it. This what I
       have done

       private IFile createTemporaryJSDLFile(final IFolder jsdlFolder){
          IFile newJSDL = jsdlFolder.getFile( "." +
       this.benchmark.getName()
       .toLowerCase() + (fileCount++) + ".jsdl" );
       //$NON-NLS-1$//$NON-NLS-2$
          if(!newJSDL.exists()){

            try {
              FileOutputStream tempOutStream= new
       FileOutputStream(newJSDL.getRawLocation().toOSString());
              tempOutStream.close();

              FileInputStream tempInStream = new
       FileInputStream(newJSDL.getRawLocation().toOSString());
              newJSDL.create( tempInStream, true, null );
              tempInStream.close();


            } catch( FileNotFoundException e ) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            } catch( CoreException e1 ) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
            } catch( IOException e ) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
          return newJSDL;
        }

       Although It may seem reduntant it actually worked. You can
       find the rest of the code in
       eu.geclipse.benchmarking.model.GBDLBenchmarkDescription

       Neophytos Theodororou
       University of Cyprus


       On Tue, May 6, 2008 at 5:03 PM, reuillon <reuillon@xxxxxxxx
       <mailto:reuillon@xxxxxxxx> <mailto:reuillon@xxxxxxxx

       <mailto:reuillon@xxxxxxxx>>> wrote:

          Hi All,

          I am trying to create a simple jsdl job description with the
          following code:

                   IWorkspace ws = ResourcesPlugin.getWorkspace();
                         System.out.println(ws.getRoot().getLocationURI().toString());
                   IWorkspaceRoot swRoot = ws.getRoot();
                   IProject project = swRoot.getProject( "myproject" );

                   if ( ! project.exists() ) {
                       project.create( null );
                   }

                   project.open( null );

                   IFile file = project.getFile( "myjsld.jsdl" );

                   if(!file.exists()) {                  file.create(new
          InputStream(){

                           @Override
                           public int read() throws IOException {
                               return -1;
                           }
                                          }, IResource.REPLACE, null );
                   }

                   JSDLJobDescription jsdl = new JSDLJobDescription(
       file );
                   jsdl.create(new JSDLJobDescriptionCreator());
                   jsdl.createRoot();
                   jsdl.addJobDescription();
                   jsdl.addApplication();
                   jsdl.setApplicationName("/bin/date");
                   jsdl.save(file);

                   System.out.println(jsdl.getJSDLString());

          But I get the following exception:

          java.io.FileNotFoundException: /myproject/myjsld.jsdl (No such
          file or directory)
           at java.io.FileInputStream.open(Native Method)
           at java.io.FileInputStream.<init>(FileInputStream.java:106)
           at
                eu.geclipse.jsdl.JSDLJobDescription.getJSDLString(JSDLJobDescription.java:250)

           at
                fr.cemagref.simexplorer.ide.osgi.processors.JobLauncherProcessor.process(JobLauncherProcessor.java:209)

           at
                fr.cemagref.simexplorer.ide.core.processors.modular.ProcessorsList.process(ProcessorsList.java:54)

           at
                fr.cemagref.simexplorer.ide.core.IDEModularApplication.run(IDEModularApplication.java:41)

           at
                fr.cemagref.simexplorer.service.loader.EFSSimulationLoaderImpl.run(EFSSimulationLoaderImpl.java:26)

           at
                fr.cemagref.simexplorer.service.application.SimExplorer.start(SimExplorer.java:49)

           at
                org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:169)

           at
                org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:106)

           at
                org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:76)

           at
                org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:363)

           at
                org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:176)

           at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
           at
                sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

           at
                sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

           at java.lang.reflect.Method.invoke(Method.java:597)
           at
       org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:508)
           at org.eclipse.equinox.launcher.Main.basicRun(Main.java:447)
           at org.eclipse.equinox.launcher.Main.run(Main.java:1173)
           at org.eclipse.equinox.launcher.Main.main(Main.java:1148)

          At the end of the process the jsdl file has been written:

          <?xml version="1.0" encoding="UTF-8"?>
          <jsdl:JobDefinition
          xmlns:jsdl="http://schemas.ggf.org/jsdl/2005/11/jsdl">
          <jsdl:JobDescription>
           <jsdl:Application>
             <jsdl:ApplicationName>/bin/date</jsdl:ApplicationName>
           </jsdl:Application>
          </jsdl:JobDescription>
          </jsdl:JobDefinition>


          Did I miss something?


          Romain

          _______________________________________________
          geclipse-dev mailing list
          geclipse-dev@xxxxxxxxxxx <mailto:geclipse-dev@xxxxxxxxxxx>
       <mailto:geclipse-dev@xxxxxxxxxxx
       <mailto:geclipse-dev@xxxxxxxxxxx>>

          https://dev.eclipse.org/mailman/listinfo/geclipse-dev


       ------------------------------------------------------------------------



       _______________________________________________
       geclipse-dev mailing list
       geclipse-dev@xxxxxxxxxxx <mailto:geclipse-dev@xxxxxxxxxxx>
       https://dev.eclipse.org/mailman/listinfo/geclipse-dev
       

   _______________________________________________
   geclipse-dev mailing list
   geclipse-dev@xxxxxxxxxxx <mailto:geclipse-dev@xxxxxxxxxxx>
   https://dev.eclipse.org/mailman/listinfo/geclipse-dev


------------------------------------------------------------------------

_______________________________________________
geclipse-dev mailing list
geclipse-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/geclipse-dev
 

_______________________________________________
geclipse-dev mailing list
geclipse-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/geclipse-dev



Back to the top