| 1 |
/* |
| 2 |
* Copyright 2002-2004 The Apache Software Foundation. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package test.concurrency; |
| 18 |
|
| 19 |
import junit.framework.TestCase; |
| 20 |
import org.apache.axis.AxisFault; |
| 21 |
import org.apache.axis.MessageContext; |
| 22 |
import org.apache.axis.client.Call; |
| 23 |
import org.apache.axis.client.Service; |
| 24 |
import org.apache.axis.components.logger.LogFactory; |
| 25 |
import org.apache.axis.configuration.BasicServerConfig; |
| 26 |
import org.apache.axis.handlers.soap.SOAPService; |
| 27 |
import org.apache.axis.providers.java.RPCProvider; |
| 28 |
import org.apache.axis.server.AxisServer; |
| 29 |
import org.apache.axis.transport.local.LocalTransport; |
| 30 |
import org.apache.commons.logging.Log; |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Test the "application" scope option - lots of threads call the same service |
| 35 |
* multiple times, and we confirm that only a single instance of the service |
| 36 |
* object was created. |
| 37 |
* |
| 38 |
* @author Glen Daniels ([email protected]) |
| 39 |
*/ |
| 40 |
public class TestApplicationScope extends TestCase { |
| 41 |
protected static Log log = |
| 42 |
LogFactory.getLog(TestApplicationScope.class.getName()); |
| 43 |
|
| 44 |
private BasicServerConfig config; |
| 45 |
private AxisServer server; |
| 46 |
private String SERVICE_NAME = "TestService"; |
| 47 |
|
| 48 |
public TestApplicationScope(String s) { |
| 49 |
super(s); |
| 50 |
} |
| 51 |
|
| 52 |
protected void setUp() throws Exception { |
| 53 |
config = new BasicServerConfig(); |
| 54 |
server = new AxisServer(config); |
| 55 |
|
| 56 |
// Deploy a service which contains an option that we expect to be |
| 57 |
// available by asking the MessageContext in the service method (see |
| 58 |
// PropertyHandler.java). |
| 59 |
|
| 60 |
RPCProvider provider = new RPCProvider(); |
| 61 |
SOAPService service = new SOAPService(provider); |
| 62 |
service.setName(SERVICE_NAME); |
| 63 |
service.setOption("className", TestService.class.getName()); |
| 64 |
service.setOption("scope", "application"); |
| 65 |
service.setOption("allowedMethods", "*"); |
| 66 |
config.deployService(SERVICE_NAME, service); |
| 67 |
} |
| 68 |
|
| 69 |
public class TestRunnable implements Runnable { |
| 70 |
private int reps; |
| 71 |
|
| 72 |
public TestRunnable(int reps) { |
| 73 |
this.reps = reps; |
| 74 |
} |
| 75 |
|
| 76 |
public void run() { |
| 77 |
LocalTransport transport = new LocalTransport(server); |
| 78 |
transport.setRemoteService(SERVICE_NAME); |
| 79 |
Call call = new Call(new Service()); |
| 80 |
call.setTransport(transport); |
| 81 |
|
| 82 |
for (int i = 0; i < reps; i++) { |
| 83 |
try { |
| 84 |
String ret = (String)call.invoke("hello", null); |
| 85 |
if (ret == null) { |
| 86 |
MessageContext msgContext = call.getMessageContext(); |
| 87 |
String respStr = msgContext.getResponseMessage().getSOAPPartAsString(); |
| 88 |
|
| 89 |
String reqStr = msgContext.getRequestMessage().getSOAPPartAsString(); |
| 90 |
String nullStr = "Got null response! Request message:\r\n" + reqStr + "\r\n\r\n" + |
| 91 |
"Response message:\r\n" + respStr; |
| 92 |
log.fatal(nullStr); |
| 93 |
setError(new Exception(nullStr)); |
| 94 |
} else if (!ret.equals(TestService.MESSAGE)) { |
| 95 |
setError(new Exception("Messages didn't match (got '" + |
| 96 |
ret + |
| 97 |
"' wanted '" + |
| 98 |
TestService.MESSAGE + |
| 99 |
"'!")); |
| 100 |
return; |
| 101 |
} |
| 102 |
} catch (AxisFault axisFault) { |
| 103 |
setError(axisFault); |
| 104 |
return; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
private Exception error = null; |
| 111 |
synchronized void setError(Exception e) { |
| 112 |
if (error == null) { |
| 113 |
error = e; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public void testApplicationScope() throws Exception { |
| 118 |
int threads = 50; |
| 119 |
int reps = 10; |
| 120 |
|
| 121 |
ThreadGroup group = new ThreadGroup("TestThreads"); |
| 122 |
|
| 123 |
for (int i = 0; i < threads; i++) { |
| 124 |
TestRunnable tr = new TestRunnable(reps); |
| 125 |
Thread thread = new Thread(group, tr, "TestThread #" + i); |
| 126 |
thread.start(); |
| 127 |
} |
| 128 |
|
| 129 |
while (group.activeCount() > 0 && error == null) { |
| 130 |
Thread.sleep(100); |
| 131 |
} |
| 132 |
|
| 133 |
if (error != null) { |
| 134 |
throw error; |
| 135 |
} |
| 136 |
} |
| 137 |
} |