home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / JAVA Utilities / JProxy 1.1.0 / SAMPLES.JAR / com / jproxy / samples / ejb / test / PerformanceApplet.java < prev    next >
Encoding:
Java Source  |  2003-04-29  |  5.2 KB  |  228 lines

  1. package com.jproxy.samples.ejb.test;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import java.util.*;
  7. import javax.naming.*;
  8.  
  9. public class PerformanceApplet
  10.     extends JApplet
  11. {
  12.     /** The GUI support */
  13.     JTextArea message = new JTextArea();
  14.     JTextArea comment = new JTextArea();
  15.     JTextField naming = new JTextField();
  16.     JTextField  providerUrl = new JTextField();
  17.     JTextField  numberOfLoops = new JTextField();
  18.     JTextField  numberOfBytes = new JTextField ();
  19.  
  20.     JPanel panel = new JPanel();
  21.  
  22.     PerformanceClient client = new PerformanceClient();
  23.  
  24.     //Construct the applet
  25.     public PerformanceApplet()
  26.     {
  27.         super();
  28.     }
  29.  
  30.     //Initialize the applet
  31.     public void init()
  32.     {
  33.         getContentPane().add(panel);
  34.  
  35.         showComponents();
  36.  
  37.         comment.setText("JProxy Performance Test");
  38.         message.setText("[log info:]\r\n");
  39.     }
  40.  
  41.     //Start the applet
  42.     public void start(){}
  43.  
  44.     //Stop the applet
  45.     public void stop()
  46.     {
  47.         if(client.isInitialized)
  48.         {
  49.             try{
  50.                 client.destroy();
  51.             }
  52.             catch(Exception e){
  53.                 e.printStackTrace();
  54.             }
  55.         }
  56.     }
  57.  
  58.     //Destroy the applet
  59.     public void destroy()
  60.     {
  61.         System.out.println("In applet finalizer!");
  62.     }
  63.  
  64.     //Get Applet information
  65.     public String getAppletInfo()
  66.     {
  67.         return "Applet Information";
  68.     }
  69.  
  70.     //Get parameter info
  71.     public String[][] getParameterInfo()
  72.     {
  73.         return null;
  74.     }
  75.  
  76.     void showComponents()
  77.     {
  78.         panel.setBackground(Color.white);
  79.         panel.setLayout(null);
  80.  
  81.         Font font = new Font("Arial", Font.BOLD, 20);
  82.         String fontName = font.getName();
  83.         font = new Font(fontName, Font.PLAIN, 14);
  84.  
  85.         int x = 10;
  86.         int y = 10;
  87.         int w = 500-2*x;
  88.         comment.setFont(font);
  89.         comment.setBounds(x, y, w, 60);
  90.         comment.setEnabled(false);
  91.         panel.add(comment);
  92.  
  93.  
  94.         y += 75;
  95.         JLabel rem1 = new JLabel("1. \"java.naming.factory.initial\":");
  96.         rem1.setBounds(x, y, 200, 20);
  97.         panel.add(rem1);
  98.         naming.setFont(font);
  99.         naming.setBounds(x+200, y, w-x-200, 20);
  100.         String n = getParameter("java.naming.factory.initial");
  101.         if(n==null) n = "com.jproxy.proxy.NamingContextFactory";
  102.         naming.setText(n);
  103.         panel.add(naming);
  104.  
  105.  
  106.         y += 25;
  107.         JLabel rem2 = new JLabel("2. \"java.naming.provider.url\":");
  108.         rem2.setBounds(x, y, 200, 20);
  109.         panel.add(rem2);
  110.         providerUrl.setFont(font);
  111.         providerUrl.setBounds(w-200, y, 200, 20);
  112.  
  113.  
  114.         String url = getParameter("java.naming.provider.url");
  115.         if(url==null)
  116.             url = "@host@:@port@";
  117.         int i = url.indexOf("@host@");
  118.         if(i!=-1)
  119.             url = replaceInString(url, "@host@", getCodeBase().getHost());
  120.         i = url.indexOf("@port@");
  121.         if(i!=-1)
  122.             url = replaceInString(url, "@port@", (getCodeBase().getPort()!=-1) ? ""+getCodeBase().getPort(): "");
  123.         providerUrl.setText(url);
  124.         panel.add(providerUrl);
  125.  
  126.  
  127.         y += 25;
  128.         JLabel rem5 = new JLabel("3. Number of loops (1-100):");
  129.         rem5.setBounds(x, y, 200, 20);
  130.         panel.add(rem5);
  131.         numberOfLoops.setFont(font);
  132.         numberOfLoops.setBounds(w-40, y, 40, 20);
  133.         numberOfLoops.setText("1");
  134.         panel.add(numberOfLoops);
  135.  
  136.  
  137.         y += 25;
  138.         JLabel rem3 = new JLabel("4. Number of bytes (0-100000):");
  139.         rem3.setBounds(x, y, 200, 20);
  140.         panel.add(rem3);
  141.         numberOfBytes.setFont(font);
  142.         numberOfBytes.setBounds(w-80, y, 80, 20);
  143.         numberOfBytes.setText("10000");
  144.         panel.add(numberOfBytes);
  145.  
  146.  
  147.         y += 25;
  148.         JButton  testButton = new JButton("Test");
  149.         testButton.setFont(font);
  150.         testButton.setBounds(w-100, y, 100, 20);
  151.         panel.add(testButton);
  152.         testButton.addActionListener(new ActionListener()
  153.         {
  154.             public void actionPerformed(ActionEvent e)
  155.             {
  156.                 message.setText(message.getText()+"\r\n"+action());
  157.             }
  158.         });
  159.  
  160.  
  161.         y += 25;
  162.         message.setFont(font);
  163.         message.setBackground(Color.lightGray);
  164.         message.setEditable(false);
  165.         JScrollPane messageSb = new JScrollPane();
  166.         messageSb.getViewport().add(message);
  167.         messageSb.setBounds(x, y, w-x, 125);
  168.         panel.add(messageSb);
  169.  
  170.     }
  171.  
  172.     String action()
  173.     {
  174.         boolean isFine = true;
  175.         String s = "";
  176.  
  177.         try{
  178.             client.loops = Integer.parseInt(numberOfLoops.getText().trim());
  179.             if(client.loops>100)
  180.                 throw new Exception();
  181.         }catch(Exception e){
  182.             return "Wrong #_of_loops parameter!\r\n";
  183.         }
  184.         try{
  185.             client.bytes = Integer.parseInt(numberOfBytes.getText().trim());
  186.             if(client.bytes>100000)
  187.                 throw new Exception();
  188.         }catch(Exception e){
  189.             return "Wrong #_of_bytes parameter!\r\n";
  190.         }
  191.  
  192.         try{
  193.             if(!client.isInitialized)
  194.             {
  195.                 client.env.setProperty(Context.PROVIDER_URL, providerUrl.getText().trim());
  196.                 client.env.setProperty(Context.INITIAL_CONTEXT_FACTORY, naming.getText().trim());
  197.                 System.out.println(client.env);
  198.                 client.init();
  199.             }
  200.             client.test();
  201.         }
  202.         catch(Exception e){
  203.             e.printStackTrace();
  204.         }
  205.         return client.error+""+client.log;
  206.     }
  207.  
  208.     static public String replaceInString(String str, String oldStr, String newStr)
  209.     {
  210.         int newIndex = 0;
  211.         int oldIndex = 0;
  212.         boolean replaced = false;
  213.         StringBuffer res = new StringBuffer("");
  214.         while(true)
  215.         {
  216.             newIndex = str.indexOf(oldStr, oldIndex);
  217.             if(newIndex==-1)
  218.                 break;
  219.             res.append(str.substring(oldIndex, newIndex));
  220.             res.append(newStr);
  221.             oldIndex = newIndex + oldStr.length();
  222.         }
  223.         res.append(str.substring(oldIndex));
  224.         return new String(res);
  225.     }
  226.  
  227. }
  228.