1 36 37 import java.rmi.RemoteException ; 38 import java.net.MalformedURLException ; 39 import java.rmi.NotBoundException ; 40 import javax.rmi.*; 41 import java.util.Vector ; 42 import javax.naming.NamingException ; 43 import javax.naming.InitialContext ; 44 import javax.naming.Context ; 45 import javax.swing.*; 46 import java.awt.*; 47 import java.awt.event.*; 48 49 public class HelloClient { 50 51 static JTextArea textArea; 52 java.awt.Container contentPane; 53 JFrame t; 54 55 public static void main( String args[] ) { 56 Context ic; 57 Object objref; 58 HelloInterface hi; 59 60 HelloClient helloClient = new HelloClient(); 61 try { 62 ic = new InitialContext (); 63 } catch (NamingException e) { 64 System.out.println("failed to obtain context" + e); 65 textArea.append("Failed to obtain Context \n"); 66 e.printStackTrace(); 67 return; 68 } 69 try { 72 objref = ic.lookup("HelloService"); 73 System.out.println("Client: Obtained a ref. to Hello server."); 74 textArea.append("Client Obtained a reference to Hello Server \n"); 75 } catch (NamingException e) { 76 System.out.println("failed to lookup object reference"); 77 textArea.append("Failed to lookup object reference\n"); 78 e.printStackTrace(); 79 return; 80 } 81 82 try { 85 hi = (HelloInterface) PortableRemoteObject.narrow( 86 objref, HelloInterface.class); 87 hi.sayHello( " Java Web Start Application - HelloClient " ); 88 textArea.append("Invoked method sayHello() successfully \n"); 89 } catch (ClassCastException e) { 90 System.out.println("narrow failed"); 91 textArea.append("Narrow Failed\n"); 92 e.printStackTrace(); 93 return; 94 } catch( Exception e ) { 95 System.err.println( "Exception " + e + "Caught" ); 96 e.printStackTrace( ); 97 return; 98 } 99 textArea.append("Thanks for choosing Java Web Start\n"); 100 } 101 102 public HelloClient() 103 { 104 105 106 t = new JFrame("RMI-IIOP CORBA Sample"); 107 contentPane = t.getContentPane(); 108 contentPane.setLayout( new FlowLayout() ); 109 contentPane.setBackground(Color.white); 110 textArea = new JTextArea(); 111 contentPane.add(textArea); 112 textArea.setText("Welcome to HelloWorld RMI Client-Server demo\n"); 113 textArea.append("RMI Client started by Java Web Start.\n"); 114 t.addWindowListener(new WindowAdapter(){ 115 public void windowClosing(WindowEvent e) 116 { 117 t.setVisible(false); 118 System.exit(0); 119 } 120 }); 121 t.pack(); 122 t.setSize(500,400); 123 t.setVisible(true); 124 } 125 126 } 127 128 129 130 | Popular Tags |