Code Search for Developers
 
 
  

DispatchForm.java from GridBlocks at Krugle


Show DispatchForm.java syntax highlighted

/*
 * Copyright (c) 2005
 * Helsinki Institute of Physics
 * see LICENSE file for details
 *
 * DispatchForm.java
 * Created on Nov 11, 2003
 */

package fi.hip.gb.client;

import java.io.DataInputStream;
import java.util.Random;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.TextField;

import fi.hip.gb.midlet.core.LiteDescription;
import fi.hip.gb.midlet.core.LiteResult;
import fi.hip.gb.midlet.util.StorageUtils;

/**
 * Form for dispatching new jobs
 * 
 * @author Juho Karppinen
 */
public class DispatchForm extends Form implements CommandListener {
    /** name of the job */
    private TextField tfName;

    /** list of known services where user can select target servers */
    private ChoiceGroup cgServers;

    /** service URL or just hostname */
    private TextField tfService;

    /** class and method to be executed Classname.methodname */
    private TextField tfExec;
    
    /** parameters for the job, semicolon separated list */
    private TextField tfParameter;

    /** flags for the job, semicolon separated list */
    private TextField tfFlags;

    /** use this specific ID */
    private Long jobID;

    /** possible attachment files */
    private LiteResult attachment;

    private static final String WDS_STORAGE = "workdescription";

    /**
     * Create new dispathing form with recenly used values on the UI fields.
     * 
     * @param jobID
     *            if not null, job is sended using this ID
     * @param serverList
     *            list of known servers
     */
    public DispatchForm(Long jobID, String[] serverList) {
        super("Dispatch job");

        tfName = new TextField("Job name: ", MIDui.instance
                .getAppProperty("JobName"), 32, TextField.ANY);
        tfExec = new TextField("Class.method: ", MIDui.instance
                .getAppProperty("ExecName"), 32, TextField.ANY);
        tfParameter = new TextField("Parameters:", MIDui.instance
                .getAppProperty("Parameters"), 64, TextField.ANY);
        tfFlags = new TextField("Flags:", MIDui.instance
                .getAppProperty("Flags"), 64, TextField.ANY);
        cgServers = new ChoiceGroup("Available servers:", Choice.MULTIPLE,
                serverList, null);
        tfService = new TextField("Target host: ", MIDui.instance
                .getAppProperty("ServiceURL"), 128, TextField.ANY);

        readWds();
        append(tfName);
        append(tfExec);
        append(tfParameter);
        append(tfFlags);
        append(cgServers);
        append(tfService);

        addCommand(new Command("Dispatch", Command.OK, 1));
        addCommand(new Command("Back", Command.BACK, 1));
        setCommandListener(this);

        // use existing ID or create new
        if (jobID != null) {
            this.jobID = jobID;
            append("JobID: " + jobID);
        } else {
            this.jobID = new Long(new Random().nextLong());
        }
    }

    /**
     * Creates new dispathing form with recently used values on the UI fields
     * and with images as attachement.
     * 
     * @param attachmentImage
     *            images to be attached into description
     * @param jobID
     *            if not null, job is sended using this ID
     * @param serverList
     *            list of known servers
     */
    public DispatchForm(LiteResult attachmentImage, Long jobID,
            String[] serverList) {
        this(jobID, serverList);
        attachment = attachmentImage;
        tfParameter.setString(attachment.getName());
        Image image = Image.createImage(attachment.readBytes(), 
                0, 
                attachment.getSize());
        append(new ImageItem("Attachment: " + attachment.getName()
                + "\n size: " + attachment.getSize() + " bytes", 
                Thumbnails.createThumbnail(image, 20), 
                ImageItem.LAYOUT_LEFT,
                "image parameter"));
    }

    public void commandAction(Command c, Displayable disp) {
        if ((c.getCommandType() == Command.BACK)) {
            MIDui.back(true);
        } else if (c.getCommandType() == Command.OK) {
            MIDui.dispatchJob(getDescription());
        }
    }

    /**
     * Constructs the description from UI fields.
     * 
     * @return description containing all information of the UI
     */
    protected LiteDescription getDescription() {
        String classname = this.tfExec.getString();
        String methodname = "";
        int sep = classname.lastIndexOf('.');
        if(sep != -1) {
            methodname = classname.substring(sep+1);
            classname = classname.substring(0, sep);
        }
        LiteDescription wds = new LiteDescription(this.jobID, 
                this.tfName.getString(),
                MIDui.getConfig().getServerURL(),
                MIDui.getConfig().getJarURL(), 
                classname,
                methodname);

        // include checked hosts
        boolean[] selected = new boolean[this.cgServers.size()];
        this.cgServers.getSelectedFlags(selected);
        for (int i = 0; i < this.cgServers.size(); i++) {
            if (selected[i]) {
                wds.addSubElements(new LiteDescription(this.cgServers.getString(i), ""));
            }
        }
        // take typed hosts if exists
        if (tfService.size() > 0) {
            int startIndex = 0, endIndex = 0;
            while (startIndex < tfService.size()) {
                endIndex = tfService.getString().indexOf(";", startIndex + 1);
                if (endIndex == -1)
                    endIndex = tfService.size();
                wds.addSubElements(new LiteDescription(tfService.getString().substring(startIndex, endIndex), ""));
                startIndex = endIndex + 1;
            }
        }

        // take all parameters
        int startIndex = 0;
        int endIndex = 0;
        while (startIndex < tfParameter.size()) {
            endIndex = tfParameter.getString().indexOf(";", startIndex + 1);
            if (endIndex == -1)
                endIndex = tfParameter.size();
            wds.addParameter(tfParameter.getString().substring(startIndex,
                    endIndex));
            startIndex = endIndex + 1;
        }

        wds.getFlags().clear();
        wds.getFlags().setSerialized(tfFlags.getString());

        if (attachment != null)
            wds.addAttachment(attachment.getName(), attachment.readBytes());

        return wds;
    }

    /**
     * Read recent values from persistant storage to the UI fields.
     */
    private void readWds() {
        try {
            DataInputStream dis = StorageUtils.read(WDS_STORAGE, 1);
            byte[][] data = new byte[dis.readInt()][];
            for (int i = 0; i < data.length; i++) {
                int size = dis.readInt();
                dis.read(data[i], 0, size);
            }
            LiteDescription wds = new LiteDescription(data);

            tfName.setString(wds.getJobname());
            tfExec.setString(wds.getClassname() + "." + wds.getMethodname());

            String hostText = "";
            LiteDescription[] subArray = wds.getSubElements();
            for (int i = 0; i < subArray.length; i++) {
                boolean found = false;
                for (int k = 0; k < cgServers.size(); k++) {
                    if (cgServers.getString(k).equals(subArray[i].getServiceURL())) {
                        cgServers.setSelectedIndex(k, true);
                        found = true;
                    }
                }
                if (found == false) {
                    if (hostText.length() > 0)
                        hostText += ";";
                    hostText += subArray[i].getServiceURL();
                }
            }
            tfService.setString(hostText);

            String paramText = "";
            String[] paramArray = wds.getParameters();
            for (int i = 0; i < paramArray.length; i++) {
                paramText += paramArray[i];
                if (i + 1 < paramArray.length)
                    paramText += ";";
            }
            tfParameter.setString(paramText);

            tfFlags.setString(wds.getFlags().toString());
        } catch (Exception e) {
            System.out.println("Failed to load wds: " + e.getMessage());
        }
    }

    /**
     * Store the values so that they are available
     * next time when dispathing new job.
     * @param wds description to be saved into memory
     */
    public static void storeWds(LiteDescription wds) {
        try {
            StorageUtils.store(wds.serialize(), WDS_STORAGE, 1);
            System.out.println("Saved wds: " + wds.toString());
        } catch (Exception e) {
            System.out.println("Failed to store wds: " + e.getMessage());
        }
    }
}



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

  BluetoothBrowser.java
  Broadcasts.java
  CameraCanvas.java
  Configs.java
  DispatchForm.java
  GPSForm.java
  GenericImageCanvas.java
  GenericResultForm.java
  GraphicsCanvas.java
  InfoForm.java
  InputUI.java
  JobsForm.java
  Logging.java
  MIDui.java
  MediaPlayer.java
  PictureCanvas.java
  ProgressForm.java
  Results.java
  StatusForm.java
  TextForm.java
  Thumbnails.java