FileDocCategorySizeDatePackage
SingleLauncherApplication.javaAPI DocExample3147Mon Jan 09 11:02:00 GMT 2006None

SingleLauncherApplication

public class SingleLauncherApplication extends Object implements Runnable

Fields Summary
public static final int
PORT
public JLabel
label
public ServerSocket
server
Constructors Summary
Methods Summary
public voidfirstMain(java.lang.String[] args)

        JFrame frame = new JFrame("Single Launch Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String word = "";
        if(args.length >= 1) {
            word = args[0];
        }
        label = new JLabel("The word of the day is: " + word);
        frame.getContentPane().add(label);
        frame.pack();
        frame.show();
    
public voidlaunch(java.lang.String[] args)

    
        
        try {
            server = new ServerSocket(PORT);
            new Thread(this).start();
            firstMain(args);
        } catch (IOException ioex) {
            System.out.println("already running!");
            relaunch(args);
        }
    
public static voidmain(java.lang.String[] args)

        new SingleLauncherApplication().launch(args);
    
public voidotherMain(java.lang.String[] args)

        if(args.length >= 1) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    label.setText("The word of the day is: " + args[0]);
                }
            });
        }
    
public static voidp(java.lang.String str)

        System.out.println(str);
    
public voidrelaunch(java.lang.String[] args)

        try {
            // open a socket to the original instance
            Socket sock = new Socket("localhost",PORT);
            
            // write the args to the output stream
            OutputStreamWriter out = new OutputStreamWriter(sock.getOutputStream());
            for(int i=0; i<args.length; i++) {
                out.write(args[i]+"\n");
                p("wrote: " + args[i]);
            }
            // cleanup
            out.flush();
            out.close();
        } catch (Exception ex) {
            System.out.println("ex: " + ex);
            ex.printStackTrace();
        }
    
public voidrun()

        System.out.println("waiting for a connection");
        while(true) {
            try {
                // wait for a socket connection
                Socket sock = server.accept();
                
                // read the contents into a string buffer
                InputStreamReader in = new InputStreamReader(sock.getInputStream());
                StringBuffer sb = new StringBuffer();
                char[] buf = new char[256];
                while(true) {
                    int n = in.read(buf);
                    if(n < 0) { break; }
                    sb.append(buf,0,n);
                }
                // split the string buffer into strings
                String[] results = sb.toString().split("\\n");
                // call other main
                otherMain(results);
            } catch (IOException ex) {
                System.out.println("ex: " + ex);
                ex.printStackTrace();
            }
        }