2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.remoting.samples.stream;
25
import java.io.ByteArrayOutputStream;
27
import java.io.FileWriter;
28
import java.io.IOException;
29
import java.io.InputStream;
30
import java.io.FileOutputStream;
31
import javax.management.MBeanServer;
32
import org.jboss.remoting.InvocationRequest;
33
import org.jboss.remoting.InvokerLocator;
34
import org.jboss.remoting.ServerInvoker;
35
import org.jboss.remoting.callback.InvokerCallbackHandler;
36
import org.jboss.remoting.stream.StreamInvocationHandler;
37
import org.jboss.remoting.transport.Connector;
40
* @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
42
public class StreamingServer
44
// Default locator values
45
private static String transport = "socket";
46
private static String host = "localhost";
47
private static int port = 5400;
49
private String locatorURI = transport + "://" + host + ":" + port;
50
private Connector connector = null;
52
public void setupServer() throws Exception
54
InvokerLocator locator = new InvokerLocator(locatorURI);
55
System.out.println("Starting remoting server with locator uri of: " + locatorURI);
56
connector = new Connector();
57
connector.setInvokerLocator(locator.getLocatorURI());
60
TestStreamInvocationHandler invocationHandler = new TestStreamInvocationHandler();
61
// first parameter is sub-system name. can be any String value.
62
connector.addInvocationHandler("test_stream", invocationHandler);
64
connector.start(true);
69
* Can pass transport and port to be used as parameters.
70
* Valid transports are 'rmi' and 'socket'.
74
public static void main(String[] args)
76
if(args != null && args.length == 3)
80
port = Integer.parseInt(args[2]);
83
StreamingServer server = new StreamingServer();
88
// wait forever, let the user kill us at any point (at which point, the client will detect we went down)
102
* Simple invocation handler implementation.
104
public static class TestStreamInvocationHandler implements StreamInvocationHandler
106
private long streamSize = 0;
109
* takes the incoming stream and writes out to a file specified by the other param specified.
110
* will return the size of the file.
116
public Object handleStream(InputStream stream, InvocationRequest param)
120
String fileName = (String)param.getParameter();
121
System.out.println("Received input stream from client to write out to file " + fileName);
122
File newFile = new File(fileName);
123
if(!newFile.exists())
125
newFile.createNewFile();
128
FileOutputStream out = new FileOutputStream(newFile, false);
130
byte buf[] = new byte[4096];
133
int c = stream.read(buf);
138
out.write(buf, 0, c);
144
streamSize = newFile.length();
145
System.out.println("New file " + fileName + " has been written out to " + newFile.getAbsolutePath());
146
System.out.println("Size of " + newFile.getAbsolutePath() + " is " + streamSize);
163
return new Long(streamSize);
167
* called to handle a specific invocation
173
public Object invoke(InvocationRequest invocation) throws Throwable
175
// Print out the invocation request
176
System.out.println("Invocation request is: " + invocation.getParameter());
178
// Return the size of the file already streamed to the server (and written to disk).
179
return new Long(streamSize);
183
* Adds a callback handler that will listen for callbacks from
184
* the server invoker handler.
186
* @param callbackHandler
188
public void addListener(InvokerCallbackHandler callbackHandler)
190
// NO OP as do not handling callback listeners in this example
194
* Removes the callback handler that was listening for callbacks
195
* from the server invoker handler.
197
* @param callbackHandler
199
public void removeListener(InvokerCallbackHandler callbackHandler)
201
// NO OP as do not handling callback listeners in this example
205
* set the mbean server that the handler can reference
209
public void setMBeanServer(MBeanServer server)
211
// NO OP as do not need reference to MBeanServer for this handler
215
* set the invoker that owns this handler
219
public void setInvoker(ServerInvoker invoker)
221
// NO OP as do not need reference back to the server invoker
b'\\ No newline at end of file'