~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/main/org/jboss/remoting/samples/stream/StreamingServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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.
 
6
*
 
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.
 
11
*
 
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.
 
16
*
 
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.
 
21
*/
 
22
 
 
23
package org.jboss.remoting.samples.stream;
 
24
 
 
25
import java.io.ByteArrayOutputStream;
 
26
import java.io.File;
 
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;
 
38
 
 
39
/**
 
40
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 
41
 */
 
42
public class StreamingServer
 
43
{
 
44
   // Default locator values
 
45
   private static String transport = "socket";
 
46
   private static String host = "localhost";
 
47
   private static int port = 5400;
 
48
 
 
49
   private String locatorURI = transport + "://" + host + ":" + port;
 
50
   private Connector connector = null;
 
51
 
 
52
   public void setupServer() throws Exception
 
53
   {
 
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());
 
58
      connector.create();
 
59
 
 
60
      TestStreamInvocationHandler invocationHandler = new TestStreamInvocationHandler();
 
61
      // first parameter is sub-system name.  can be any String value.
 
62
      connector.addInvocationHandler("test_stream", invocationHandler);
 
63
 
 
64
      connector.start(true);
 
65
   }
 
66
 
 
67
 
 
68
   /**
 
69
    * Can pass transport and port to be used as parameters.
 
70
    * Valid transports are 'rmi' and 'socket'.
 
71
    *
 
72
    * @param args
 
73
    */
 
74
   public static void main(String[] args)
 
75
   {
 
76
      if(args != null && args.length == 3)
 
77
      {
 
78
         transport = args[0];
 
79
         host = args[1];
 
80
         port = Integer.parseInt(args[2]);
 
81
      }
 
82
 
 
83
      StreamingServer server = new StreamingServer();
 
84
      try
 
85
      {
 
86
         server.setupServer();
 
87
 
 
88
         // wait forever, let the user kill us at any point (at which point, the client will detect we went down)
 
89
         while(true)
 
90
         {
 
91
            Thread.sleep(1000);
 
92
         }
 
93
 
 
94
      }
 
95
      catch(Exception e)
 
96
      {
 
97
         e.printStackTrace();
 
98
      }
 
99
   }
 
100
 
 
101
   /**
 
102
    * Simple invocation handler implementation.
 
103
    */
 
104
   public static class TestStreamInvocationHandler implements StreamInvocationHandler
 
105
   {
 
106
      private long streamSize = 0;
 
107
 
 
108
      /**
 
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.
 
111
       *
 
112
       * @param stream
 
113
       * @param param
 
114
       * @return
 
115
       */
 
116
      public Object handleStream(InputStream stream, InvocationRequest param)
 
117
      {
 
118
         try
 
119
         {
 
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())
 
124
            {
 
125
               newFile.createNewFile();
 
126
            }
 
127
 
 
128
            FileOutputStream out = new FileOutputStream(newFile, false);
 
129
 
 
130
            byte buf[] = new byte[4096];
 
131
            while(true)
 
132
            {
 
133
               int c = stream.read(buf);
 
134
               if(c < 0)
 
135
               {
 
136
                  break;
 
137
               }
 
138
               out.write(buf, 0, c);
 
139
            }
 
140
 
 
141
            out.flush();
 
142
            out.close();
 
143
 
 
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);
 
147
         }
 
148
         catch(Throwable e)
 
149
         {
 
150
            e.printStackTrace();
 
151
         }
 
152
         finally
 
153
         {
 
154
            try
 
155
            {
 
156
               stream.close();
 
157
            }
 
158
            catch(IOException e)
 
159
            {
 
160
               e.printStackTrace();
 
161
            }
 
162
         }
 
163
         return new Long(streamSize);
 
164
      }
 
165
 
 
166
      /**
 
167
       * called to handle a specific invocation
 
168
       *
 
169
       * @param invocation
 
170
       * @return
 
171
       * @throws Throwable
 
172
       */
 
173
      public Object invoke(InvocationRequest invocation) throws Throwable
 
174
      {
 
175
         // Print out the invocation request
 
176
         System.out.println("Invocation request is: " + invocation.getParameter());
 
177
 
 
178
         // Return the size of the file already streamed to the server (and written to disk).
 
179
         return new Long(streamSize);
 
180
      }
 
181
 
 
182
      /**
 
183
       * Adds a callback handler that will listen for callbacks from
 
184
       * the server invoker handler.
 
185
       *
 
186
       * @param callbackHandler
 
187
       */
 
188
      public void addListener(InvokerCallbackHandler callbackHandler)
 
189
      {
 
190
         // NO OP as do not handling callback listeners in this example
 
191
      }
 
192
 
 
193
      /**
 
194
       * Removes the callback handler that was listening for callbacks
 
195
       * from the server invoker handler.
 
196
       *
 
197
       * @param callbackHandler
 
198
       */
 
199
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
200
      {
 
201
         // NO OP as do not handling callback listeners in this example
 
202
      }
 
203
 
 
204
      /**
 
205
       * set the mbean server that the handler can reference
 
206
       *
 
207
       * @param server
 
208
       */
 
209
      public void setMBeanServer(MBeanServer server)
 
210
      {
 
211
         // NO OP as do not need reference to MBeanServer for this handler
 
212
      }
 
213
 
 
214
      /**
 
215
       * set the invoker that owns this handler
 
216
       *
 
217
       * @param invoker
 
218
       */
 
219
      public void setInvoker(ServerInvoker invoker)
 
220
      {
 
221
         // NO OP as do not need reference back to the server invoker
 
222
      }
 
223
   }
 
224
}
 
 
b'\\ No newline at end of file'