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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/stream/StreamingTestServer.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

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.test.remoting.stream;
 
24
 
 
25
import java.io.ByteArrayOutputStream;
 
26
import java.io.IOException;
 
27
import java.io.InputStream;
 
28
import java.util.Map;
 
29
import java.util.HashMap;
 
30
import javax.management.MBeanServer;
 
31
 
 
32
import org.apache.log4j.Logger;
 
33
import org.jboss.jrunit.extensions.ServerTestCase;
 
34
import org.jboss.remoting.InvocationRequest;
 
35
import org.jboss.remoting.InvokerLocator;
 
36
import org.jboss.remoting.ServerInvoker;
 
37
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
38
import org.jboss.remoting.stream.StreamInvocationHandler;
 
39
import org.jboss.remoting.transport.Connector;
 
40
 
 
41
/**
 
42
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 
43
 */
 
44
public class StreamingTestServer extends ServerTestCase
 
45
{
 
46
   private static Logger log = Logger.getLogger(StreamingTestServer.class);
 
47
   
 
48
   // Default locator values
 
49
   private static String transport = "socket";
 
50
   private static String host = "localhost";
 
51
   private static int port = 5400;
 
52
 
 
53
   private String locatorURI;
 
54
   private Connector connector = null;
 
55
 
 
56
   public void setupServer() throws Exception
 
57
   {
 
58
      String bindAddr = System.getProperty("jrunit.bind_addr", host);
 
59
      locatorURI = transport + "://" + bindAddr + ":" + port; 
 
60
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
61
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
62
      connector = new Connector();
 
63
      connector.setInvokerLocator(locator.getLocatorURI());
 
64
      connector.create();
 
65
 
 
66
      TestStreamInvocationHandler invocationHandler = new TestStreamInvocationHandler();
 
67
      // first parameter is sub-system name.  can be any String value.
 
68
      connector.addInvocationHandler("test_stream", invocationHandler);
 
69
 
 
70
      connector.start();
 
71
      log.info("Started remoting server with locator uri of: " + locatorURI);
 
72
   }
 
73
 
 
74
   protected void setUp() throws Exception
 
75
   {
 
76
      setupServer();
 
77
   }
 
78
 
 
79
   protected void tearDown() throws Exception
 
80
   {
 
81
      if(connector != null)
 
82
      {
 
83
         connector.stop();
 
84
         connector.destroy();
 
85
      }
 
86
   }
 
87
 
 
88
   /**
 
89
    * Can pass transport and port to be used as parameters.
 
90
    * Valid transports are 'rmi' and 'socket'.
 
91
    *
 
92
    * @param args
 
93
    */
 
94
   public static void main(String[] args)
 
95
   {
 
96
      if(args != null && args.length == 3)
 
97
      {
 
98
         transport = args[0];
 
99
         host = args[1];
 
100
         port = Integer.parseInt(args[2]);
 
101
      }
 
102
 
 
103
      StreamingTestServer server = new StreamingTestServer();
 
104
      try
 
105
      {
 
106
         server.setUp();
 
107
 
 
108
         // sleep the thread for 10 seconds while waiting for client to call
 
109
         Thread.sleep(10000);
 
110
 
 
111
         server.tearDown();
 
112
      }
 
113
      catch(Exception e)
 
114
      {
 
115
         e.printStackTrace();
 
116
      }
 
117
   }
 
118
 
 
119
   /**
 
120
    * Simple invocation handler implementation.
 
121
    */
 
122
   public static class TestStreamInvocationHandler implements StreamInvocationHandler
 
123
   {
 
124
      private InputStream stream = null;
 
125
 
 
126
      private int streamSize = 0;
 
127
 
 
128
      /**
 
129
       * called to handle a specific invocation
 
130
       *
 
131
       * @param invocation
 
132
       * @return
 
133
       * @throws Throwable
 
134
       */
 
135
      public Object invoke(InvocationRequest invocation) throws Throwable
 
136
      {
 
137
         // Print out the invocation request
 
138
         log.info("Invocation request is: " + invocation.getParameter());
 
139
 
 
140
         // Just going to return static string as this is just simple example code.
 
141
         return new Integer(streamSize);
 
142
      }
 
143
 
 
144
      /**
 
145
       * Adds a callback handler that will listen for callbacks from
 
146
       * the server invoker handler.
 
147
       *
 
148
       * @param callbackHandler
 
149
       */
 
150
      public void addListener(InvokerCallbackHandler callbackHandler)
 
151
      {
 
152
         // NO OP as do not handling callback listeners in this example
 
153
      }
 
154
 
 
155
      /**
 
156
       * Removes the callback handler that was listening for callbacks
 
157
       * from the server invoker handler.
 
158
       *
 
159
       * @param callbackHandler
 
160
       */
 
161
      public void removeListener(InvokerCallbackHandler callbackHandler)
 
162
      {
 
163
         // NO OP as do not handling callback listeners in this example
 
164
      }
 
165
 
 
166
      /**
 
167
       * set the mbean server that the handler can reference
 
168
       *
 
169
       * @param server
 
170
       */
 
171
      public void setMBeanServer(MBeanServer server)
 
172
      {
 
173
         // NO OP as do not need reference to MBeanServer for this handler
 
174
      }
 
175
 
 
176
      /**
 
177
       * set the invoker that owns this handler
 
178
       *
 
179
       * @param invoker
 
180
       */
 
181
      public void setInvoker(ServerInvoker invoker)
 
182
      {
 
183
         // NO OP as do not need reference back to the server invoker
 
184
      }
 
185
 
 
186
      public Object handleStream(InputStream stream, InvocationRequest param)
 
187
      {
 
188
         this.stream = stream;
 
189
 
 
190
         try
 
191
         {
 
192
            ByteArrayOutputStream out = new ByteArrayOutputStream();
 
193
            byte buf[] = new byte[4096];
 
194
            while(true)
 
195
            {
 
196
               int c = this.stream.read(buf);
 
197
               if(c < 0)
 
198
               {
 
199
                  break;
 
200
               }
 
201
               out.write(buf, 0, c);
 
202
            }
 
203
            byte[] bytes = out.toByteArray();
 
204
            streamSize = bytes.length;
 
205
            log.info("Read stream.  Contents is: " + new String(bytes));
 
206
         }
 
207
         catch(IOException e)
 
208
         {
 
209
            e.printStackTrace();
 
210
         }
 
211
         finally
 
212
         {
 
213
            try
 
214
            {
 
215
               stream.close();
 
216
            }
 
217
            catch(IOException e)
 
218
            {
 
219
               e.printStackTrace();
 
220
            }
 
221
         }
 
222
         // build return map
 
223
         Map retMap = new HashMap();
 
224
         retMap.put("subsystem", param.getSubsystem());
 
225
         retMap.put("clientid", param.getSessionId());
 
226
         retMap.put("paramval", param.getParameter());
 
227
         return retMap;
 
228
      }
 
229
   }
 
230
 
 
231
}
 
 
b'\\ No newline at end of file'