Skip to content

Commit 7e95625

Browse files
committed
Merge remote-tracking branch 'openshift-ping/master' into HEAD
# Conflicts: # activemq/pom.xml # common/pom.xml # common/src/main/java/org/jgroups/ping/common/OpenshiftPing.java # common/src/main/java/org/openshift/ping/common/server/JBossServer.java # common/src/main/java/org/openshift/ping/common/server/JBossServerFactory.java # dist/eap/build/pom.xml # dist/eap/download/pom.xml # dist/eap/pom.xml # dist/pom.xml # dist/wildfly/build/pom.xml # dist/wildfly/download/pom.xml # dist/wildfly/pom.xml # dns/pom.xml # examples/apps/basic-app-cache/pom.xml # examples/apps/basic-web-session/pom.xml # examples/apps/hello-servlet/pom.xml # examples/apps/pom.xml # kube/pom.xml # kube/src/test/java/org/jgroups/ping/kube/test/PingTestBase.java # kube/src/test/java/org/jgroups/ping/kube/test/ServerTestBase.java # pom.xml
2 parents 0b4eb5c + 46b7642 commit 7e95625

File tree

6 files changed

+81
-35
lines changed

6 files changed

+81
-35
lines changed

common/src/main/java/org/jgroups/ping/common/OpenshiftPing.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public abstract class OpenshiftPing extends PING {
6868
private String _serverName;
6969

7070
private static Method sendMethod; //handled via reflection due to JGroups 3/4 incompatibility
71+
private static Method setSrcMethod;
7172

7273
public OpenshiftPing(String systemEnvPrefix) {
7374
super();
@@ -81,6 +82,12 @@ public OpenshiftPing(String systemEnvPrefix) {
8182
} catch (Exception e) {
8283
throw new CompatibilityException("Could not find suitable 'up' method.", e);
8384
}
85+
try {
86+
//the return parameter changed in JGroups 3/4 :D
87+
setSrcMethod = Message.class.getMethod("setSrc", Address.class);
88+
} catch (Exception e) {
89+
throw new CompatibilityException("Could not find suitable 'setSrc' method.", e);
90+
}
8491
}
8592

8693
protected final String getSystemEnvName(String systemEnvSuffix) {
@@ -195,26 +202,14 @@ protected void sendMcastDiscoveryRequest(Message msg) {
195202
return;
196203
}
197204
if (msg.getSrc() == null) {
198-
setMessageSourceAddress(msg);
199-
205+
setSrc(msg);
200206
}
201207
for (InetSocketAddress node : nodes) {
202208
// forward the request to each node
203209
timer.execute(new SendDiscoveryRequest(node, msg));
204210
}
205211
}
206212

207-
private void setMessageSourceAddress(Message msg) {
208-
//public void setSrc(Address new_src) {src_addr=new_src;} - JGroups 3.6.13
209-
//public Message setSrc(Address new_src) {src_addr=new_src; return this;} - JGroups 4.0.1
210-
//unfortunately need to use reflections
211-
try {
212-
msg.getClass().getMethod("setSrc", Address.class).invoke(msg, local_addr);
213-
} catch (Exception e) {
214-
log.warn("Setting local address for discovery failed.");
215-
}
216-
}
217-
218213
public void handlePingRequest(InputStream stream) throws Exception {
219214
DataInputStream dataInput = new DataInputStream(stream);
220215
Message msg = new Message();
@@ -226,6 +221,16 @@ public void handlePingRequest(InputStream stream) throws Exception {
226221
}
227222
}
228223

224+
private void setSrc(Message msg) {
225+
try {
226+
setSrcMethod.invoke(msg, local_addr);
227+
} catch (IllegalAccessException e) {
228+
e.printStackTrace();
229+
} catch (Exception e) {
230+
throw new CompatibilityException("Could not invoke 'setSrc' method.", e);
231+
}
232+
}
233+
229234
private void sendUp(Message msg) {
230235
try {
231236
if(CompatibilityUtils.isJGroups4()) {

common/src/main/java/org/jgroups/ping/common/server/JDKServerFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public boolean isAvailable() {
2929
}
3030
}
3131

32+
@Override
3233
public Server createServer(int port) {
3334
return new JDKServer(port);
3435
}

common/src/main/java/org/jgroups/ping/common/server/UndertowServer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
import org.jgroups.JChannel;
2222

23+
import java.io.InputStream;
24+
25+
import org.jgroups.JChannel;
26+
2327
import io.undertow.Undertow;
2428
import io.undertow.server.HttpHandler;
2529
import io.undertow.server.HttpServerExchange;

common/src/main/java/org/jgroups/ping/common/server/UndertowServerFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public boolean isAvailable() {
2929
}
3030
}
3131

32+
@Override
3233
public Server createServer(int port) {
3334
return new UndertowServer(port);
3435
}

kube/src/test/java/org/jgroups/ping/kube/test/PingTestBase.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,47 @@
1616

1717
package org.jgroups.ping.kube.test;
1818

19+
import java.lang.reflect.Method;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122
import java.util.Objects;
2223
import java.util.concurrent.TimeoutException;
2324

25+
import org.jgroups.Channel;
2426
import org.jgroups.JChannel;
2527
import org.jgroups.View;
2628
import org.jgroups.util.Util;
2729
import org.junit.Assert;
2830
import org.junit.Test;
31+
import org.openshift.ping.common.compatibility.CompatibilityException;
32+
import org.openshift.ping.common.compatibility.CompatibilityUtils;
2933

3034
/**
3135
* @author <a href="mailto:[email protected]">Ales Justin</a>
3236
*/
3337
public abstract class PingTestBase extends TestBase {
3438

39+
//handles via reflection because of JGroups 3/4 incompatibility.
40+
private static final Method waitForViewMethod;
41+
42+
static {
43+
try {
44+
if (CompatibilityUtils.isJGroups4()) {
45+
waitForViewMethod = Util.class.getMethod("waitUntilAllChannelsHaveSameView", long.class, long.class, JChannel[].class);
46+
} else {
47+
Method m;
48+
try {
49+
m = Util.class.getMethod("waitUntilAllChannelsHaveSameSize", long.class, long.class, Channel[].class);
50+
} catch (NoSuchMethodException e) {
51+
m = Util.class.getMethod("waitUntilAllChannelsHaveSameView", long.class, long.class, Channel[].class);
52+
}
53+
waitForViewMethod = m;
54+
}
55+
} catch (NoSuchMethodException e) {
56+
throw new CompatibilityException("Could not find proper 'waitUntilAllChannelsHaveSame*' method.", e);
57+
}
58+
}
59+
3560
@Test
3661
public void testCluster() throws Exception {
3762
doTestCluster();

kube/src/test/java/org/jgroups/ping/kube/test/ServerTestBase.java

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.io.DataOutputStream;
2121
import java.lang.reflect.Constructor;
22+
import java.lang.reflect.Method;
2223
import java.net.HttpURLConnection;
2324
import java.net.URL;
2425
import java.util.Arrays;
@@ -74,12 +75,11 @@ protected Protocol createPing() {
7475
public void testResponse() throws Exception {
7576
Address local_addr = pinger.getLocalAddress();
7677
PhysicalAddress physical_addr = (PhysicalAddress) pinger
77-
.down(new Event(Event.GET_PHYSICAL_ADDRESS, local_addr));
78-
PingHeader hdr = new TestPingHeader();
78+
.down(new Event(Event.GET_PHYSICAL_ADDRESS, local_addr));
7979
PingData data = createPingData(local_addr, physical_addr);
80+
final PingHeader hdr = getPingHeader(data);
8081
Message msg = new Message(null).setFlag(Message.Flag.DONT_BUNDLE)
81-
.putHeader(pinger.getId(), hdr).setBuffer(streamableToBuffer(data));
82-
82+
.putHeader(pinger.getId(), hdr).setBuffer(streamableToBuffer(data));
8383
URL url = new URL("http://localhost:8888");
8484
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
8585
conn.addRequestProperty(Server.CLUSTER_NAME, TestBase.CLUSTER_NAME);
@@ -93,6 +93,34 @@ public void testResponse() throws Exception {
9393
Assert.assertEquals(200, conn.getResponseCode());
9494
}
9595

96+
private static Buffer streamableToBuffer(Streamable obj) {
97+
final ByteArrayOutputStream out_stream = new ByteArrayOutputStream(1024);
98+
DataOutputStream out = new DataOutputStream(out_stream);
99+
try {
100+
Util.writeStreamable(obj, out);
101+
return new Buffer(out_stream.toByteArray());
102+
} catch (Exception ex) {
103+
return null;
104+
}
105+
}
106+
107+
private PingHeader getPingHeader(PingData data) {
108+
try {
109+
if(CompatibilityUtils.isJGroups4()) {
110+
Constructor<PingHeader> constructor = PingHeader.class.getConstructor(null);
111+
PingHeader header = constructor.newInstance(null);
112+
Method clusterName = header.getClass().getMethod("clusterName", String.class);
113+
clusterName.invoke(header, TestBase.CLUSTER_NAME);
114+
return header;
115+
} else {
116+
Constructor<PingHeader> constructor = PingHeader.class.getConstructor(byte.class, PingData.class, String.class);
117+
return constructor.newInstance(PingHeader.GET_MBRS_RSP, data, TestBase.CLUSTER_NAME);
118+
}
119+
} catch (Exception e) {
120+
throw new CompatibilityException("Could not find or invoke proper 'PingHeader' constructor");
121+
}
122+
}
123+
96124
/*
97125
* Handled via reflection because of JGroups 3/4 incompatibility.
98126
*/
@@ -112,17 +140,6 @@ private PingData createPingData(Address local_addr, PhysicalAddress physical_add
112140
}
113141
}
114142

115-
private static Buffer streamableToBuffer(Streamable obj) {
116-
final ByteArrayOutputStream out_stream = new ByteArrayOutputStream(512);
117-
DataOutputStream out = new DataOutputStream(out_stream);
118-
try {
119-
Util.writeStreamable(obj, out);
120-
return new Buffer(out_stream.toByteArray());
121-
} catch (Exception ex) {
122-
return null;
123-
}
124-
}
125-
126143
private static final class TestKubePing extends KubePing {
127144
static {
128145
ClassConfigurator.addProtocol(JGROUPS_KUBE_PING_ID, TestKubePing.class);
@@ -141,11 +158,4 @@ protected Client getClient() {
141158
}
142159
}
143160
}
144-
145-
private static final class TestPingHeader extends PingHeader {
146-
private TestPingHeader() {
147-
cluster_name = TestBase.CLUSTER_NAME;
148-
type = PingHeader.GET_MBRS_REQ;
149-
}
150-
}
151161
}

0 commit comments

Comments
 (0)