MazeClient.java Sample

This topic inludes the source code for the MazeClient.java Sample.

Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/ProxyClient/mazegen/java_client/

Sample Source Code


001 package mazegen.java_client;
002 
003 import java.io.BufferedReader;
004 import java.io.InputStreamReader;
005 import java.io.DataInputStream;
006 import java.io.IOException;
007 import java.io.*;
008 import java.rmi.RemoteException;
009 
010 // proxy classes are located in the weblogic.jws.proxies package.
011 import weblogic.jws.proxies.*;
012 
013 /**
014  <p>This is an example of a web service client that uses the web service
015  * proxy generated by WebLogic Server.  This example is a simple Java
016  * console application that exercises the MazeGenerator web service and
017  * displays mazes returned from the web service.</p>
018  *
019  <p>See "MazeGenerator Sample" in the WebLogic Workshop documentation.</p>
020  *
021  <p>This client accesses the web service via proxy classes found in
022  * SamplesApp/APP-INF/lib/MazeGenerator.jar.  That JAR file is obtained by selecting
023  * the "Java Proxy" link on a web service's Test View Overview Page.  If
024  * the web service is hosted locally, the URL of the Overview Page is:
025  * http://localhost:7001/samples/proxy/mazegen/MazeGenerator.jws</p>
026  */
027 public class MazeClient
028 {
029 
030     private class InputParams
031     {
032         int rows;
033         int cols;
034 
035         public InputParams()
036         {
037             rows = -1;
038             cols = -1;
039         }
040     }
041 
042     /*
043      * <p>Declare the proxy objects.  There is a main proxy object with the
044      * same name as the web service but with "_Impl" appended, then
045      * protocol-specific proxie classes that contain the actual method stubs
046      * for the web service's operations.</p>
047      *
048      * <p>This web service supports SOAP over HTTP, so it
049      * has a proxy with the name <web service>SOAP.</p>
050      */
051     MazeGenerator_Impl m_Proxy = null;
052     MazeGeneratorSoap m_ProxySoap = null;
053 
054     /**
055      * Utility method that prints prompts to System.out and gets input
056      * values from System.in.
057      */
058     private InputParams getInputParams()
059     {
060         String input = null;
061         int rows = 0;
062         int columns = 0;
063 
064         char [] charBuf = new char[255];
065         int charsRead = 0;
066 
067         InputParams returnedParams = null;
068         InputParams params = new InputParams();
069 
070         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
071 
072         System.out.print("Enter rows:");
073         try
074         {
075             input = stdin.readLine();
076             rows = Integer.parseInt(input);
077         }
078         catch (IOException ex)
079         {
080             ex.printStackTrace();
081         }
082         catch (NumberFormatException ex) {
083             String msg = "Number of rows must be a valid integer (entered value: '" + input + "')";
084             throw(new NumberFormatException(msg));
085         
086         System.out.println(input);
087 
088         params.rows = rows;
089 
090         System.out.print("Enter columns:");
091         try
092         {
093             input = stdin.readLine();
094             columns = Integer.parseInt(input);
095         }
096         catch (IOException ex)
097         {
098             ex.printStackTrace();
099         }
100         catch (NumberFormatException ex) {
101             String msg = "Number of columns must be a valid integer (entered value: '" + input + "')";
102             throw(new NumberFormatException(msg));
103         
104         System.out.println(input);
105 
106         params.cols = columns;
107 
108         ifparams.rows > && params.cols > )
109         {
110             returnedParams = params;
111         }
112 
113         return returnedParams;
114     }
115 
116     /*
117      * Main work method.  Loops forever (until invalid input), calling
118      * getInputParams to collect input (desired number of rows and
119      * columns) and then calling the web service to get a maze of the
120      * desired size.
121      */
122     private void run()
123     {
124         InputParams params;
125 
126         try
127         {
128             /*
129              * Instantiate the main proxy class. The proxy class has the same name as the
130              * web service, with "_Impl" appended.
131              *
132              * Note that there is an alternative constructor in which you can pass the URL of
133              * the target service's WSDL (which is typically the URL of the service with ?WSDL
134              * appended).  That technique is used for "dynamic" proxies. The static WSDL is
135              * already built into the generated proxy and will be used if the no-argument
136              * proxy constructor is used (as below). Using the dynamic form causes an extra
137              * hit on the server for every web service request submitted.
138              */
139             m_Proxy = new MazeGenerator_Impl();
140         }
141         catch (IOException ex)
142         {
143             System.out.println("Error getting proxy");
144             ex.printStackTrace();
145         }
146         /*
147          * Get the protocol-specific proxy class.
148          */
149         m_ProxySoap = m_Proxy.getMazeGeneratorSoap();
150 
151         while(true)
152         {
153             // get the desired number of rows and columns
154             params = getInputParams();
155             ifparams != null )
156             {
157                 try
158                 {
159                     // invoke the web service to get the maze
160 
161                     String sMaze = m_ProxySoap.printRandomMaze(params.rows,
162                                                                params.cols);
163 
164                     // print the response
165                     System.out.println(sMaze);
166                 }
167                 catch (RemoteException ex)
168                 {
169                     System.out.println("Error invoking service via proxy");
170                     ex.printStackTrace();
171                 }
172             }
173             else
174             {
175                 System.out.println("MazeClient: Invalid maze size parameters entered");
176             }
177         }
178 
179     }
180 
181     public static void main(String [] args)
182     {
183 
184         MazeClient client = new MazeClient();
185         client.run();
186 
187         return;
188     }
189 }