KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > samples > stream > StreamingClient


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.remoting.samples.stream;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.net.URL JavaDoc;
13 import org.jboss.remoting.Client;
14 import org.jboss.remoting.InvokerLocator;
15
16 /**
17  * This samples shows how the client can send an InputStream to
18  * the server.
19  *
20  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
21  */

22 public class StreamingClient
23 {
24    // Default locator values
25
private static String JavaDoc transport = "socket";
26    private static String JavaDoc host = "localhost";
27    private static int port = 5400;
28
29    private String JavaDoc locatorURI = transport + "://" + host + ":" + port;
30
31    private String JavaDoc localFileName = "sample.txt";
32    private String JavaDoc remoteFileName = "server_sample.txt";
33
34    public void sendStream() throws Throwable JavaDoc
35    {
36       FileInputStream JavaDoc fileInput = null;
37       Client remotingClient = null;
38
39       try
40       {
41          InvokerLocator locator = new InvokerLocator(locatorURI);
42          System.out.println("Calling on remoting server with locator uri of: " + locatorURI);
43
44          remotingClient = new Client(locator);
45          URL JavaDoc fileURL = this.getClass().getResource(localFileName);
46          if(fileURL == null)
47          {
48             throw new Exception JavaDoc("Can not find file " + localFileName);
49          }
50          File JavaDoc testFile = new File JavaDoc(fileURL.getFile());
51          fileInput = new FileInputStream JavaDoc(testFile);
52
53          System.out.println("Sending input stream for file " + localFileName + " to server.");
54          Object JavaDoc ret = remotingClient.invoke(fileInput, remoteFileName);
55
56          long fileLength = testFile.length();
57          System.out.println("Size of file sample.txt is " + fileLength);
58          System.out.println("Server returned " + ret + " as the size of the file read.");
59
60       }
61       finally
62       {
63          if(remotingClient != null)
64          {
65             remotingClient.disconnect();
66          }
67          if(fileInput != null)
68          {
69             try
70             {
71                fileInput.close();
72             }
73             catch(IOException JavaDoc e)
74             {
75                e.printStackTrace();
76             }
77          }
78       }
79    }
80
81    public void setRemoteFileName(String JavaDoc remoteFileName)
82    {
83       this.remoteFileName = remoteFileName;
84    }
85
86
87    /**
88     * Can pass transport and port to be used as parameters.
89     * Valid transports are 'rmi' and 'socket'.
90     *
91     * @param args
92     */

93    public static void main(String JavaDoc[] args)
94    {
95       String JavaDoc newFileName = null;
96       if(args != null && args.length == 1)
97       {
98          newFileName = args[0];
99       }
100       if(args != null && args.length == 2)
101       {
102          transport = args[0];
103          port = Integer.parseInt(args[1]);
104       }
105       if(args != null && args.length == 3)
106       {
107          transport = args[0];
108          port = Integer.parseInt(args[1]);
109          newFileName = args[2];
110       }
111       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
112       StreamingClient client = new StreamingClient();
113       if(newFileName != null)
114       {
115          client.setRemoteFileName(newFileName);
116       }
117       try
118       {
119          client.sendStream();
120       }
121       catch(Throwable JavaDoc e)
122       {
123          e.printStackTrace();
124       }
125    }
126
127
128 }
Popular Tags