package org.cipres.webapp.guigen.applet; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import javax.swing.UIManager; import javax.swing.JApplet; import org.cipres.helpers.RegistryForApplet; import org.cipres.guigen.CmdParam; import org.cipres.guigen.*; import org.cipres.guigen.command.ServiceCommands; import org.cipres.guigen.interfaces.RegistryInterface; import org.cipres.CipresIDL.api1.CommandObject; import java.awt.Dimension; import java.awt.Toolkit; import java.io.File; import java.util.Observable; import java.util.Observer; import javax.swing.JFrame; import javax.swing.JTextArea; import java.net.HttpURLConnection; import javax.swing.*; import java.awt.*; import org.apache.log4j.Logger; import org.cipres.util.CipresLogger; public class GuigenApplet extends javax.swing.JApplet implements java.util.Observer { ServiceCommandPanel cmdpnl; URLConnection con; ObjectOutputStream os; ObjectInputStream is; public void init() { super.init(); try { /* Get the name of a service commands xml file, based on the user's choice of tool (see index.jsp), and send the name of the file to the servlet so that it will send us the service commands file and the corresponding uixml, which we'll then use to display a gui. As you can see in GuigenServlet.java, the xml files are stored in $CATALINA_HOME/webapps/echo/WEB-INF/classes/conf, which comes from src/resources/conf. In the real website the command xml file to be used will likely be based on the type of the submitted data, the size of the dataset, and the type of analysis the user wants to run. Note that the reason we're using java serialization rather than sending xml data is that I wasn't able to run castor, our xml serialization tool, in an applet. */ final String cmdXmlName = this.getParameter("defaults"); con = getServletConnection("echo"); os = new ObjectOutputStream(con.getOutputStream()); os.writeObject(cmdXmlName); os.flush(); /* Servlet sends us 1) a CmdParam object that represents the guixml file, 2) a registry, 3) the top level tool's registry entry string, 4) the top level uiid, 5) ServiceCommands object with initial data for the gui's fields. */ is = new ObjectInputStream(con.getInputStream()); CmdParam param = (CmdParam)is.readObject(); RegistryForApplet appletRegistry = (RegistryForApplet)is.readObject(); String topLevelTool = (String)is.readObject(); String topLevelUiid = (String)is.readObject(); ServiceCommands initialValues = (ServiceCommands)is.readObject(); is.close(); is = null; // Display the gui UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); this.ShowGui(topLevelUiid, topLevelTool, param, initialValues, appletRegistry); } catch (final Exception e) { e.printStackTrace(); if (os != null) { try { os.close(); } catch (Exception ee) {;} } if (is != null) { try { is.close(); } catch (Exception ee) {;} } os = null; is = null; // should we cast con to HTTPUrlConnection and disconnect it? } } private URLConnection getServletConnection(String servlet) throws MalformedURLException, IOException { URL urlServlet = new URL(getCodeBase(), servlet); System.out.println("Connecting to url " + urlServlet.toString()); System.out.flush(); URLConnection con = urlServlet.openConnection(); con.setDoInput(true); con.setDoOutput(true); con.setUseCaches(false); con.setDefaultUseCaches(false); con.setRequestProperty( "Content-Type", "application/x-java-serialized-object"); return con; } public void ShowGui( final String serviceName, final String registryEntry, final CmdParam param, final ServiceCommands initialValues, RegistryInterface registry) throws Exception { cmdpnl = new ServiceCommandPanel(param); cmdpnl.setRegistryObject(registry); cmdpnl.setServiceDisplayName(serviceName); cmdpnl.setServiceUid(registryEntry); // arrange that our update() method is called when the user presses launch. cmdpnl.addObserver(this); cmdpnl.setHideCancelButton(true); cmdpnl.initializeCommands(initialValues); showFrames(""); } /* Show the gui in the browser's window. */ private void showFrames(final String title) throws Exception { try { System.out.println("IN SHOWFRAMES"); System.out.flush(); Container cp = getContentPane(); cp.add(cmdpnl.getServicePanel()); cmdpnl.getServicePanel().repaint(); } catch (final Exception ex) { // logger.error("", ex); throw ex; } } /* //This version shows the gui in a separate window. private void showFrames(final String title) throws Exception { try { System.out.println("IN SHOWFRAMES"); System.out.flush(); final JFrame frame = new JFrame(); frame.setSize(350, 350); frame.setContentPane(cmdpnl.getServicePanel()); cmdpnl.getServicePanel().repaint(); frame.setTitle(title); // set frame size frame.pack(); // Center the window final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); final Dimension frameSize = frame.getSize(); frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); frame.setVisible(true); } catch (final Exception ex) { // logger.error("", ex); throw ex; } } */ public void update(final Observable o, final Object arg) { try { ServiceCommands sc = null; org.cipres.CipresIDL.api1.CommandObject idlCmdObj = null; if (arg instanceof PostObject) { PostObject po = (PostObject)arg; final int type = po.getType(); final Object obj = po.getData(); switch(type) { case PostObject.COMMAND_OBJECT: { if (obj instanceof ServiceCommands) { cmdpnl.enableInput(false); sc = (ServiceCommands)po.getData(); sendServiceCommands(sc); URL nexturl = new URL(getDocumentBase(), "gotparams.jsp"); System.out.println("redirecting to " + nexturl); System.out.flush(); getAppletContext().showDocument(nexturl); } else { System.out.println("Got a PostObject of type COMMAND_OBJECT whose data isn't of type ServiceCommand"); } break; } default: { System.out.println("Received update object of type " + type + " and value " + obj); break; } } } else { System.out.println("Received update object " + arg); } } catch(Exception e) { e.printStackTrace(); } } void sendServiceCommands(ServiceCommands sc) { try { if (sc == null) { System.err.println("Service Commands are null"); } System.err.println("Sending service commands to echo2"); System.err.flush(); con = getServletConnection("echo2"); os = new ObjectOutputStream(con.getOutputStream()); os.writeObject(sc); os.flush(); System.err.println("Sending service commands - ok"); System.err.flush(); is = new ObjectInputStream(con.getInputStream()); String s = (String)is.readObject(); is.close(); is = null; System.err.println("read string response: " + s); System.err.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (Exception e) {;} } os = null; } } public void start() { super.start(); System.out.println("Applet starting."); } public void stop() { super.stop(); System.out.println("Applet stopping."); } public void destroy() { super.destroy(); System.out.println("Applet destroyed."); } }