Code Search for Developers
 
 
  

LiteStorage.java from GridBlocks at Krugle


Show LiteStorage.java syntax highlighted

/*
 * Copyright (c) 2005
 * Helsinki Institute of Physics
 * see LICENSE file for details
 *
 * LiteStorage.java
 * Created on Sep 24, 2004
 */
package fi.hip.gb.midlet.core;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStoreException;

import fi.hip.gb.midlet.util.StorageUtils;

/**
 * Stores the description, status and results objects of one job.
 * 
 * @author Juho Karppinen
 */
public class LiteStorage {
    /** description of the job */
    private LiteDescription wds;
    
    /** status of the job */
    private LiteStatus ws;
    
    /** <code>LiteResult</code> objects storing results and their meta-data */
    private Vector results = new Vector();
	
	/** own record ID */
	private int recordID = -1;
	
	/** name of the record store for storages */
	public final static String STORAGE_NAME = "storage";
	    
	/** just some object transfered along this object */
	public Object payload;
	
	/**
	 * Creates a new empty container.
	 * 
	 * @param jobID id of the job
	 */
	public LiteStorage(Long jobID) {
	    this.ws = new LiteStatus();
	    this.wds = new LiteDescription(jobID);
	}

	/**
	 * Creates new container for job with results. Job status
	 * will be generated according to result size.
	 * 
	 * @param jobID id of the job
	 * @param result result available
	 */
    public LiteStorage(Long jobID, LiteResult result) {
        this(jobID);
		this.ws.setExecModel(1,1);
		this.ws.setTransferModel(result.getSize(), result.getSize());
		results.addElement(result);
    }
    
	/**
	 * Creates a new container for the job from existing data.
	 * 
	 * @param description description for the job
	 * @param status status for the job
	 * @param resultVector <code>LiteResult</code> objects
	 */
    public LiteStorage(LiteDescription description, LiteStatus status, Vector resultVector) {
        this.wds = description;
        this.ws = status;
        addResults(resultVector);
    }
    
	/**
	 * Loads the object from the memory
	 * @param recID record ID
	 * @throws Exception if record cannot be read
	 */
	public LiteStorage(int recID) throws Exception {
	    this(StorageUtils.read(STORAGE_NAME, recID));
	    
		this.recordID = recID;
	}
	
	public LiteStorage(DataInputStream dis) throws IOException {
	    System.out.println("Parsing storage from stream");
	    int size = dis.readInt();
	    System.out.println("subcount " + size);
	    
		int id[] = new int[size];
		for(int i=0; i < id.length; i++) {
			id[i] = dis.readInt();
		}
		dis.close();
		
		System.out.println("reading status from " + id[0]);
		ws = new LiteStatus(id[0]);
		
        /*
		this.results = new Vector(size-1);
		for(int i=0; i < size - 1 ; i++) {
			System.out.println("reading result " + i + " from " +  id[i+1]);
			this.results.addElement(new LiteResult(id[i+1]));
		}
        */

		//deSerialize(dis);
		/*
		for(int i=0; i < resultCount; i++) {
			int id = dis.readInt();
			results.setElementAt(new LiteResult(dis), i);
		}
		dis.close();
		*/
	}
	
    /**
     * Gets the description of the job
     * @return description of the job
     */
    public LiteDescription getDescription() {
        return this.wds;
    }
    
    /**
     * Sets the description of the job
     * @param description the description of the job
     */
    public void setDescription(LiteDescription description) {
        this.wds = description;
    }
    
    /**
     * Gets the status of the job
     * @return status information
     */
    public LiteStatus getStatus() {
        return this.ws;
    }
    
    /**
     * Sets the status of the job
     * @param status status information
     */
    public void setStatus(LiteStatus status) {
        this.ws = status;
    }
    
    /**
     * Gets available results and their metadatas
     * @return <code>LiteResult</code> objects
     */
    public Vector getResult() {
        return this.results;
    }
    
	/**
	 * Adds a result file to the container. If result file
	 * already exists it will be replaced.
	 * @param res result object
	 */
	public void addResult(LiteResult res) {
		for(int i=0; i < results.size(); i++) {
			if(((LiteResult)results.elementAt(i)).getName().equals(res.getName())) {
			    LiteResult oldItem = (LiteResult) this.results.elementAt(i);
			    
			    // update the size on status container
			    int[] oldSize = this.ws.getTransferModel();
			    this.ws.setTransferModel(oldSize[0] + res.getSize() - oldItem.getSize(), oldSize[1]);
			    
			    // update the result file
			    this.results.setElementAt(res, i);
				return;
			}
		}
		this.results.addElement(res);
	}
	
	/**
	 * Adds multiple results to the container.
	 * @param newResults <code>LiteResult</code> objects
	 */
	public void addResults(Vector newResults) {
	    for (Enumeration e = newResults.elements(); e.hasMoreElements();) {
            LiteResult r = (LiteResult) e.nextElement();
            
            System.out.println("rfile " + r.getName());
            if(r.getName().endsWith(".txt")) {
                // skip the metadata file
                System.out.println("metadata file " + r.getName());
            } else {
                // find the possible metadata file
                for (Enumeration e2 = newResults.elements(); e2.hasMoreElements();) {
                    LiteResult m = (LiteResult) e2.nextElement();
                    if(m.getName().equals(r.getName() + ".txt")) {
                        r.setDescription(m.readContent());
                        System.out.println("metadata found for " + r.getName() 
                                + " : " + r.getDescription());
                    }
                }
                addResult(r);
            }
        }
	}

	/**
	 * Serialize the container into outputstream with
	 * all result data
	 * @param dos stream to write to
	 * @throws Exception if fields cannot be serialized
	 */
	public void store(DataOutputStream dos) throws Exception {
		ws.serialize(dos);
		for(int i=0; i<results.size(); i++) {
			((LiteResult)results.elementAt(i)).serialize(dos);
		}
	}
	
	/**
	 * Stores the status into record store without actual results.
	 * Only the result record ids are stored.
	 * 
	 * @return record ID of the status on the record store
	 * @throws Exception if result cannot be save
	 */ 
	public int store() throws Exception {
	    // save the record ID's here, first item is status and the rest are results
	    int id[] = new int[1 /*+ this.results.size()*/];
	    
		System.out.println("saving status:");
		System.out.println(ws.toString());
		id[0] = this.ws.store();
		System.out.println("status saved to " + id[0]);
		
		// write result files
		for(int i=0; i<this.results.size(); i++) {
			//LiteResult res = (LiteResult)this.results.elementAt(i);
			//System.out.println("saving " + res.toString());
			//id[i+1] = res.store();
			//System.out.println("res " + res.getName() + " saved to " + id[i+1]);
		}

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
        System.out.println("writing storage len " + id.length);
		dos.write(id.length);
		for(int i=0; i < id.length; i++) {
			dos.writeInt(id[i]);
		}
		dos.close();
		this.recordID = StorageUtils.store(baos.toByteArray(), STORAGE_NAME);
		System.out.println("storage stored to " + recordID);
		return this.recordID;
	}
	
	/**
	 * Gets all elements stored on the persistant storage.
	 * @return <code>Vector</code> containing <code>LiteStorage</code> objects
	 */
	public static Vector iterate() {
	    Vector elements = new Vector();
	    
		StorageUtils storageUtils = null;
		try {
		    storageUtils = new StorageUtils();
		    storageUtils.openRecordStore(LiteStorage.STORAGE_NAME);
			RecordEnumeration re = storageUtils.enumerate();
			while(re.hasNextElement()) {
				int nextID = re.nextRecordId();
				System.out.println("adding id " + nextID);
				try {
					LiteStorage storage = new LiteStorage(nextID);
					System.out.println("read jobname " + storage.getDescription().getJobname());
					elements.addElement(storage);
				} catch(Exception e) {
					e.printStackTrace();
					System.out.println("Deleting corrupted result index " +  nextID + " " + e.getMessage());
					StorageUtils.deleteRecord(STORAGE_NAME, nextID);
				}
			}
		} catch(RecordStoreException rse) {
			rse.printStackTrace();
		} finally {
			try {
			    storageUtils.closeRecordStore();
			} catch (RecordStoreException rse) {
				rse.printStackTrace();
			}
		}

		return elements;
	}
	
	/**
	 * Gets the record ID
	 * @return positive id if status is saved on the memory
	 */
	public int getRecordID() {
		return this.recordID;
	}
}




See more files for this project here

GridBlocks

GridBlocks builds a grid application framework via easy-to-use building blocks in distributed environment. The framework offers components for Grid security, distributed storage, computing, and Portlet web interfaces.

Project homepage: http://sourceforge.net/projects/gridblocks
Programming language(s): Java,JSP,XML
License: other

  LiteDescription.java
  LiteResult.java
  LiteStatus.java
  LiteStorage.java