home *** CD-ROM | disk | FTP | other *** search
Java Source | 2003-04-29 | 5.2 KB | 228 lines |
- package com.jproxy.samples.ejb.test;
-
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.*;
- import javax.naming.*;
-
- public class PerformanceApplet
- extends JApplet
- {
- /** The GUI support */
- JTextArea message = new JTextArea();
- JTextArea comment = new JTextArea();
- JTextField naming = new JTextField();
- JTextField providerUrl = new JTextField();
- JTextField numberOfLoops = new JTextField();
- JTextField numberOfBytes = new JTextField ();
-
- JPanel panel = new JPanel();
-
- PerformanceClient client = new PerformanceClient();
-
- //Construct the applet
- public PerformanceApplet()
- {
- super();
- }
-
- //Initialize the applet
- public void init()
- {
- getContentPane().add(panel);
-
- showComponents();
-
- comment.setText("JProxy Performance Test");
- message.setText("[log info:]\r\n");
- }
-
- //Start the applet
- public void start(){}
-
- //Stop the applet
- public void stop()
- {
- if(client.isInitialized)
- {
- try{
- client.destroy();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
- }
-
- //Destroy the applet
- public void destroy()
- {
- System.out.println("In applet finalizer!");
- }
-
- //Get Applet information
- public String getAppletInfo()
- {
- return "Applet Information";
- }
-
- //Get parameter info
- public String[][] getParameterInfo()
- {
- return null;
- }
-
- void showComponents()
- {
- panel.setBackground(Color.white);
- panel.setLayout(null);
-
- Font font = new Font("Arial", Font.BOLD, 20);
- String fontName = font.getName();
- font = new Font(fontName, Font.PLAIN, 14);
-
- int x = 10;
- int y = 10;
- int w = 500-2*x;
- comment.setFont(font);
- comment.setBounds(x, y, w, 60);
- comment.setEnabled(false);
- panel.add(comment);
-
-
- y += 75;
- JLabel rem1 = new JLabel("1. \"java.naming.factory.initial\":");
- rem1.setBounds(x, y, 200, 20);
- panel.add(rem1);
- naming.setFont(font);
- naming.setBounds(x+200, y, w-x-200, 20);
- String n = getParameter("java.naming.factory.initial");
- if(n==null) n = "com.jproxy.proxy.NamingContextFactory";
- naming.setText(n);
- panel.add(naming);
-
-
- y += 25;
- JLabel rem2 = new JLabel("2. \"java.naming.provider.url\":");
- rem2.setBounds(x, y, 200, 20);
- panel.add(rem2);
- providerUrl.setFont(font);
- providerUrl.setBounds(w-200, y, 200, 20);
-
-
- String url = getParameter("java.naming.provider.url");
- if(url==null)
- url = "@host@:@port@";
- int i = url.indexOf("@host@");
- if(i!=-1)
- url = replaceInString(url, "@host@", getCodeBase().getHost());
- i = url.indexOf("@port@");
- if(i!=-1)
- url = replaceInString(url, "@port@", (getCodeBase().getPort()!=-1) ? ""+getCodeBase().getPort(): "");
- providerUrl.setText(url);
- panel.add(providerUrl);
-
-
- y += 25;
- JLabel rem5 = new JLabel("3. Number of loops (1-100):");
- rem5.setBounds(x, y, 200, 20);
- panel.add(rem5);
- numberOfLoops.setFont(font);
- numberOfLoops.setBounds(w-40, y, 40, 20);
- numberOfLoops.setText("1");
- panel.add(numberOfLoops);
-
-
- y += 25;
- JLabel rem3 = new JLabel("4. Number of bytes (0-100000):");
- rem3.setBounds(x, y, 200, 20);
- panel.add(rem3);
- numberOfBytes.setFont(font);
- numberOfBytes.setBounds(w-80, y, 80, 20);
- numberOfBytes.setText("10000");
- panel.add(numberOfBytes);
-
-
- y += 25;
- JButton testButton = new JButton("Test");
- testButton.setFont(font);
- testButton.setBounds(w-100, y, 100, 20);
- panel.add(testButton);
- testButton.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e)
- {
- message.setText(message.getText()+"\r\n"+action());
- }
- });
-
-
- y += 25;
- message.setFont(font);
- message.setBackground(Color.lightGray);
- message.setEditable(false);
- JScrollPane messageSb = new JScrollPane();
- messageSb.getViewport().add(message);
- messageSb.setBounds(x, y, w-x, 125);
- panel.add(messageSb);
-
- }
-
- String action()
- {
- boolean isFine = true;
- String s = "";
-
- try{
- client.loops = Integer.parseInt(numberOfLoops.getText().trim());
- if(client.loops>100)
- throw new Exception();
- }catch(Exception e){
- return "Wrong #_of_loops parameter!\r\n";
- }
- try{
- client.bytes = Integer.parseInt(numberOfBytes.getText().trim());
- if(client.bytes>100000)
- throw new Exception();
- }catch(Exception e){
- return "Wrong #_of_bytes parameter!\r\n";
- }
-
- try{
- if(!client.isInitialized)
- {
- client.env.setProperty(Context.PROVIDER_URL, providerUrl.getText().trim());
- client.env.setProperty(Context.INITIAL_CONTEXT_FACTORY, naming.getText().trim());
- System.out.println(client.env);
- client.init();
- }
- client.test();
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return client.error+""+client.log;
- }
-
- static public String replaceInString(String str, String oldStr, String newStr)
- {
- int newIndex = 0;
- int oldIndex = 0;
- boolean replaced = false;
- StringBuffer res = new StringBuffer("");
- while(true)
- {
- newIndex = str.indexOf(oldStr, oldIndex);
- if(newIndex==-1)
- break;
- res.append(str.substring(oldIndex, newIndex));
- res.append(newStr);
- oldIndex = newIndex + oldStr.length();
- }
- res.append(str.substring(oldIndex));
- return new String(res);
- }
-
- }
-