package com.bea.estore.startup;
import java.io.*;
import java.net.*;
import java.lang.Runtime;
import java.lang.Thread;
import java.util.*;
import weblogic.common.*;
import java.net.*;/**
 * This startup class displays a message in the server shell informing
 * the user which URL can be used to access the Pet Store application.
 * On Windows, a browser will be automatically launched to the
 * appropriate URL.
 *
 * @author Copyright (c) 2000 by BEA Systems, Inc. All Rights Reserved.
 */
public class StartBrowser implements Runnable,T3StartupDef{
  private String port;
  private String host;
  private String app;
  private Socket socket;
  private final static boolean debug   = false;
  private static int SLEEPTIME = 500;
  private T3ServicesDef services;
  private final static String PETSTORE = "petstore";
  private final static String TOUR = "wlstour";  /**
   * Constructs a StartBrowser with the specified host and port values
   * and startMode which determines whether Weblogic Tour or PetStore is started.
   *
   * @param h   hostname and port
   */
  public StartBrowser(String h, String p, String a) {
    host = h;
    port = p;
    app = a;  }  /**
   * Default constructor.
   *
   */
   public StartBrowser() {
 }  public void setServices(T3ServicesDef services) {
    this.services = services;
  }
  /**
   * Loops indefinitely trying to create a socket to host/port
   * waits sleepTime in between each try.
   * On a successful socket create, start browser.
   */
  public void run() {
      boolean loop = true;
    while (loop) {
      try {
        socket = new Socket(host, new Integer(port).intValue());
        socket.close();        //launch browser
        String[] cmdArray = new String[3];
        cmdArray[0] = "beaexec.exe";
        cmdArray[1] = "-target:browser";
        cmdArray[2] = "-command:\"http://"+host+":"+port+app+"\"";
        try {
          Process p = Runtime.getRuntime().exec(cmdArray);
          p.getInputStream().close();
          p.getOutputStream().close();
          p.getErrorStream().close();
        }
        catch (IOException ioe) {
        }
        loop = false;
      } catch (Exception e) {
        try {
          Thread.sleep(SLEEPTIME); // try every 500 ms
        } catch (InterruptedException ie) {}
        finally {
          try {
            socket.close();
          } catch (Exception se) {}
        }
      }
    }
  }  /**
   * Print message in the server shell informing
   * the user which URL can be used to access the Pet Store application.
   * On Windows, start a new thread to launch the browser.
   */
  public String startup(String name, Hashtable args)
       throws Exception
  {
    String p = (String)args.get("port");
    String h = InetAddress.getLocalHost().getHostName();
    if (h == null)
      h="localhost";
    String wlserver = h+":"+p;    String serverText;
    String app;
    if (startPetStoreOnlyMode()) {
      serverText = "PetStore Server";
      app = "/petstore/index.jsp";
    } else {
      serverText = "Weblogic Server Tour";
      app = "";
    }
    wlserver += app;
    String os = System.getProperty("os.name");
    if (os.indexOf("Windows") != -1) {
      System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
      System.out.println(" After the server has booted, your browser should \n" +
                         " automatically launch and point to the "+serverText+" \n" +
                         " running on this server. If your browser fails to launch, \n" +
                         " point your browser to the URL \n" +
                         " \"http://"+wlserver+"\"");
      System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
      Thread t = new Thread(new StartBrowser(h, p, app));
      t.start();
    }
    else {
      System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
      System.out.println(" After the server has booted, point your browser \n" +
                         " to the URL \"http://"+wlserver+"\" \n" +
                         " to view the "+serverText+" running on this \n" +
                         " server.");
      System.out.println(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }
    return "";
  }  /**
   * Determine mode of operation- either WebLogic Tour or just PetStore.
   * Change boolean petStoreOnly and .equal check to set default behavior.
   */
  private boolean startPetStoreOnlyMode()
  {
    boolean petStoreOnly = true;
    String petMode = System.getProperty("pet.mode");
    if (petMode != null && petMode.length() > 0 && petMode.equals(TOUR))
      petStoreOnly = false;    return petStoreOnly;
  }
}

解决方案 »

  1.   

    你好像是调用beaexec.exe来运行浏览器,如果是这样,就不太通用了,而且不能跨平台。
    或者是否能实现:在应用程序界面上显示一个url,点鼠标则打开浏览器。
      

  2.   

    Runtime.getRuntime().exec("start " + uRL); 
    可以在windows下打开浏览器。不知道载unix下如何?
      

  3.   

    Runtime.getRuntime().exec("start " + uRL); 
    可以在windows下打开浏览器。不知道载unix下如何?
    --------------------------------------------------
    不行,我早试过了。我以前也是这样打开的,但在linux就没法用mozilla来打开。
      

  4.   

    No one can answer it?