KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > HelloClient


1 /*
2  * @(#)HelloClient.java 1.5 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 import java.rmi.RemoteException JavaDoc;
38 import java.net.MalformedURLException JavaDoc;
39 import java.rmi.NotBoundException JavaDoc;
40 import javax.rmi.*;
41 import java.util.Vector JavaDoc;
42 import javax.naming.NamingException JavaDoc;
43 import javax.naming.InitialContext JavaDoc;
44 import javax.naming.Context JavaDoc;
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 JavaDoc contentPane;
53     JFrame t;
54     
55     public static void main( String JavaDoc args[] ) {
56         Context JavaDoc ic;
57         Object JavaDoc objref;
58         HelloInterface hi;
59
60         HelloClient helloClient = new HelloClient();
61         try {
62             ic = new InitialContext JavaDoc();
63         } catch (NamingException JavaDoc 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         // STEP 1: Get the Object reference from the Name Service
70
// using JNDI call.
71
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 JavaDoc 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         // STEP 2: Narrow the object reference to the concrete type and
83
// invoke the method.
84
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 JavaDoc e) {
90             System.out.println("narrow failed");
91             textArea.append("Narrow Failed\n");
92             e.printStackTrace();
93             return;
94         } catch( Exception JavaDoc 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