************************************************
* 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