Merge r1028655 TUSCANY-3759: Add code to read stdin and stderr from spawned epmd process and reverse changes made by commit r1028378

git-svn-id: https://svn.apache.org/repos/asf/tuscany/sca-java-1.x/trunk@1028659 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/distribution/src/main/release/src/BUILDING b/distribution/src/main/release/src/BUILDING
index 40e95fa..926c8b3 100644
--- a/distribution/src/main/release/src/BUILDING
+++ b/distribution/src/main/release/src/BUILDING
@@ -26,14 +26,6 @@
    Unix users:
      export MAVEN_OPTS=-Xmx256m -XX:MaxPermSize=256m
 
-5) You don't need any other software installed to run the Apache Tuscany SCA
-   build. However, if you have Erlang/OTP installed and in your path, you could
-   experience build problems if the version of Erlang/OTP in your path isn't
-   compatible with the Apache Tuscany SCA Erlang binding.  To ensure that the
-   Apache Tuscany SCA build runs cleanly, you should either put the R12B version
-   of Erlang/OTP in your path or completely remove Erlang/OTP from your path
-   when running the Apache Tuscany SCA build. See TUSCANY-3759 for details.
-
 
 Building
 --------
diff --git a/modules/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceServiceTestCase.java b/modules/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceServiceTestCase.java
index 2073e4b..4b829df 100644
--- a/modules/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceServiceTestCase.java
+++ b/modules/binding-erlang-runtime/src/test/java/org/apache/tuscany/sca/binding/erlang/testing/ReferenceServiceTestCase.java
@@ -22,6 +22,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
+import java.io.InputStream;
 import java.io.IOException;
 
 import org.apache.tuscany.sca.binding.erlang.impl.TypeMismatchException;
@@ -76,9 +77,11 @@
 	private static Process epmdProcess;
 
 	@BeforeClass
-	public static void init() throws IOException {
+	public static void init() {
 		try {
 			epmdProcess = Runtime.getRuntime().exec(EPMD_COMMAND);
+            startReaderThread(epmdProcess.getInputStream());
+            startReaderThread(epmdProcess.getErrorStream());
 			SCADomain domain = SCADomain
 					.newInstance("ErlangReference.composite");
 			SCADomain.newInstance("ErlangService.composite");
@@ -103,6 +106,22 @@
 		}
 	}
 
+    private static void startReaderThread(final InputStream stream) {
+        Thread readerThread = new Thread() {
+            public void run() {
+                try {
+                    byte[] buf = new byte[100];
+                    while (true) {
+                        stream.read(buf);
+                    }
+                } catch (Exception e) {
+                    return;
+                }
+            }
+        };
+        readerThread.start();
+    }
+
 	@AfterClass
 	public static void clean() {
 		if (epmdProcess != null) {
@@ -737,6 +756,8 @@
 		args[0] = new OtpErlangString("world");
 		args[1] = new OtpErlangString("!");
 		refMbox.send("sayHello", "RPCServerMbox", new OtpErlangTuple(args));
+        // FIXME: this seems to help avoid occasional hangs
+        Thread.sleep(100);
 		OtpErlangString result = (OtpErlangString) refMbox.receiveMsg()
 				.getMsg();
 		assertEquals("Hello world !", result.stringValue());
diff --git a/samples/helloworld-erlang-reference/README b/samples/helloworld-erlang-reference/README
index ced99d6..e0c1a5c 100644
--- a/samples/helloworld-erlang-reference/README
+++ b/samples/helloworld-erlang-reference/README
@@ -8,9 +8,6 @@
 
 In order to run Erlang samples you need to have Erlang/OTP distribution installed - 
 epmd binary is required in your system path. See http://erlang.org for downloads.
-The Tuscany SCA Erlang binding is compatible with Erlang/OTP version R12B but
-doesn't work correctly with some later versions including R14B. See TUSCANY-3759
-for details.
 
 If you just want to run it to see what happens you need to run the server first 
 so open a command prompt, navigate to the helloworld-erlang-service sample directory 
diff --git a/samples/helloworld-erlang-reference/src/test/java/helloworld/HelloWorldErlangClientTestCase.java b/samples/helloworld-erlang-reference/src/test/java/helloworld/HelloWorldErlangClientTestCase.java
index 03d98f8..0776892 100644
--- a/samples/helloworld-erlang-reference/src/test/java/helloworld/HelloWorldErlangClientTestCase.java
+++ b/samples/helloworld-erlang-reference/src/test/java/helloworld/HelloWorldErlangClientTestCase.java
@@ -1,66 +1,88 @@
-/*

- * Licensed to the Apache Software Foundation (ASF) under one

- * or more contributor license agreements.  See the NOTICE file

- * distributed with this work for additional information

- * regarding copyright ownership.  The ASF licenses this file

- * to you under the Apache License, Version 2.0 (the

- * "License"); you may not use this file except in compliance

- * with the License.  You may obtain a copy of the License at

- * 

- *   http://www.apache.org/licenses/LICENSE-2.0

- * 

- * Unless required by applicable law or agreed to in writing,

- * software distributed under the License is distributed on an

- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

- * KIND, either express or implied.  See the License for the

- * specific language governing permissions and limitations

- * under the License.    

- */

-

-package helloworld;

-

-import helloworld.dynaignore.IgnorableRunner;

-import helloworld.dynaignore.IgnoreTest;

-import junit.framework.Assert;

-

-import org.apache.tuscany.sca.host.embedded.SCADomain;

-import org.junit.Test;

-import org.junit.runner.RunWith;

-

-/**

- * Test case for helloworld Erlang client

- */

-@RunWith(IgnorableRunner.class)

-public class HelloWorldErlangClientTestCase {

-

-	private static final String EPMD_COMMAND = "epmd";

-

-	@Test

-	public void testClient() throws Exception {

-		Process epmdProcess = null;

-		try {

-			epmdProcess = Runtime.getRuntime().exec(EPMD_COMMAND);

-		} catch (Exception e) {

-			System.out

-					.println("Cannot proceed - exception while executing "

-							+ EPMD_COMMAND

-							+ ": "

-							+ e.getMessage()

-							+ ". Valid and working Erlang/OTP distribution is required.");

-			throw new IgnoreTest();

-		}

-		SCADomain scaServiceDomain = SCADomain

-				.newInstance("helloworlderlangservice.composite");

-		SCADomain scaClientDomain = SCADomain

-				.newInstance("helloworlderlangreference.composite");

-		HelloWorldService helloWorldService = scaClientDomain.getService(

-				HelloWorldService.class, "HelloWorldServiceComponent");

-		String msg = helloWorldService.getGreetings("Smith");

-		Assert.assertEquals("Hello Smith", msg);

-		scaClientDomain.close();

-		scaServiceDomain.close();

-		epmdProcess.destroy();

-

-	}

-

-}

+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package helloworld;
+
+import java.io.InputStream;
+import java.io.IOException;
+
+import helloworld.dynaignore.IgnorableRunner;
+import helloworld.dynaignore.IgnoreTest;
+import junit.framework.Assert;
+
+import org.apache.tuscany.sca.host.embedded.SCADomain;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Test case for helloworld Erlang client
+ */
+@RunWith(IgnorableRunner.class)
+public class HelloWorldErlangClientTestCase {
+
+	private static final String EPMD_COMMAND = "epmd";
+
+	@Test
+	public void testClient() {
+		Process epmdProcess = null;
+		try {
+			epmdProcess = Runtime.getRuntime().exec(EPMD_COMMAND);
+		} catch (IOException e) {
+			System.out
+					.println("Cannot proceed - exception while executing "
+							+ EPMD_COMMAND
+							+ ": "
+							+ e.getMessage()
+							+ ". Valid and working Erlang/OTP distribution is required.");
+			throw new IgnoreTest();
+		}
+        startReaderThread(epmdProcess.getInputStream());
+        startReaderThread(epmdProcess.getErrorStream());
+
+		SCADomain scaServiceDomain = SCADomain
+				.newInstance("helloworlderlangservice.composite");
+		SCADomain scaClientDomain = SCADomain
+				.newInstance("helloworlderlangreference.composite");
+		HelloWorldService helloWorldService = scaClientDomain.getService(
+				HelloWorldService.class, "HelloWorldServiceComponent");
+		String msg = helloWorldService.getGreetings("Smith");
+		Assert.assertEquals("Hello Smith", msg);
+		scaClientDomain.close();
+		scaServiceDomain.close();
+
+		epmdProcess.destroy();
+	}
+
+    private static void startReaderThread(final InputStream stream) {
+        Thread readerThread = new Thread() {
+            public void run() {
+                try {
+                    byte[] buf = new byte[100];
+                    while (true) {
+                        stream.read(buf);
+                    }
+                } catch (Exception e) {
+                    return;
+                }
+            }
+        };
+        readerThread.start();
+    }
+
+}
diff --git a/samples/helloworld-erlang-service/README b/samples/helloworld-erlang-service/README
index a702f5d..ed21c91 100644
--- a/samples/helloworld-erlang-service/README
+++ b/samples/helloworld-erlang-service/README
@@ -8,9 +8,6 @@
 
 In order to run Erlang samples you need to have Erlang/OTP distribution installed - 
 epmd binary is required in your system path. See http://erlang.org for downloads.
-The Tuscany SCA Erlang binding is compatible with Erlang/OTP version R12B but
-doesn't work correctly with some later versions including R14B. See TUSCANY-3759
-for details.
 
 If you just want to run it to see what happens open a command prompt, navigate
 to this sample directory and do:
diff --git a/samples/helloworld-erlang-service/src/main/java/helloworld/HelloWorldServer.java b/samples/helloworld-erlang-service/src/main/java/helloworld/HelloWorldServer.java
index 83dbfe0..2e632c0 100644
--- a/samples/helloworld-erlang-service/src/main/java/helloworld/HelloWorldServer.java
+++ b/samples/helloworld-erlang-service/src/main/java/helloworld/HelloWorldServer.java
@@ -1,65 +1,84 @@
-/*

- * Licensed to the Apache Software Foundation (ASF) under one

- * or more contributor license agreements.  See the NOTICE file

- * distributed with this work for additional information

- * regarding copyright ownership.  The ASF licenses this file

- * to you under the Apache License, Version 2.0 (the

- * "License"); you may not use this file except in compliance

- * with the License.  You may obtain a copy of the License at

- * 

- *   http://www.apache.org/licenses/LICENSE-2.0

- * 

- * Unless required by applicable law or agreed to in writing,

- * software distributed under the License is distributed on an

- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

- * KIND, either express or implied.  See the License for the

- * specific language governing permissions and limitations

- * under the License.    

- */

-package helloworld;

-

-import java.io.IOException;

-

-import org.apache.tuscany.sca.host.embedded.SCADomain;

-

-/**

- * This server program shows how to create an SCA runtime, and start it which

- * activates the helloworld Web service endpoint.

- */

-public class HelloWorldServer {

-

-	private static final String EPMD_COMMAND = "epmd";

-

-	public static void main(String[] args) {

-		try {

-			Process process = null;

-			try {

-				process = Runtime.getRuntime().exec(EPMD_COMMAND);

-			} catch (Exception e) {

-				System.out

-						.println("Cannot proceed - exception while executing "

-								+ EPMD_COMMAND

-								+ ": "

-								+ e.getMessage()

-								+ ". Valid and working Erlang/OTP distribution is required.");

-			}

-			if (process != null) {

-				System.out.println("EPMD server started");

-				SCADomain scaDomain = SCADomain

-						.newInstance("helloworlderlangservice.composite");

-				System.out

-						.println("HelloWorld server started (press enter to shutdown)");

-				System.in.read();

-				process.destroy();

-				scaDomain.close();

-				System.out.println("EPMD server stopped");

-				System.out.println("HelloWorld server stopped");

-			}

-		} catch (IOException e) {

-			e.printStackTrace();

-		} catch (Exception e) {

-			e.printStackTrace();

-		}

-	}

-

-}

+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package helloworld;
+
+import java.io.InputStream;
+import java.io.IOException;
+
+import org.apache.tuscany.sca.host.embedded.SCADomain;
+
+/**
+ * This server program shows how to create an SCA runtime, and start it which
+ * activates the helloworld Web service endpoint.
+ */
+public class HelloWorldServer {
+
+	private static final String EPMD_COMMAND = "epmd";
+
+	public static void main(String[] args) {
+		try {
+			Process process = null;
+			try {
+				process = Runtime.getRuntime().exec(EPMD_COMMAND);
+			} catch (IOException e) {
+				System.out
+						.println("Cannot proceed - exception while executing "
+								+ EPMD_COMMAND
+								+ ": "
+								+ e.getMessage()
+								+ ". Valid and working Erlang/OTP distribution is required.");
+			}
+			if (process != null) {
+                startReaderThread(process.getInputStream());
+                startReaderThread(process.getErrorStream());
+				System.out.println("EPMD server started");
+
+				SCADomain scaDomain = SCADomain
+						.newInstance("helloworlderlangservice.composite");
+				System.out
+						.println("HelloWorld server started (press enter to shutdown)");
+				System.in.read();
+				scaDomain.close();
+                System.out.println("HelloWorld server stopped");
+
+                process.destroy();
+				System.out.println("EPMD server stopped");
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+	}
+
+    private static void startReaderThread(final InputStream stream) {
+        Thread readerThread = new Thread() {
+            public void run() {
+                try {
+                    byte[] buf = new byte[100];
+                    while (true) {
+                        stream.read(buf);
+                    }
+                } catch (Exception e) {
+                    return;
+                }
+            }
+        };
+        readerThread.start();
+    }
+
+}
diff --git a/samples/helloworld-erlang-service/src/test/java/helloworld/HelloWorldErlangServerTestCase.java b/samples/helloworld-erlang-service/src/test/java/helloworld/HelloWorldErlangServerTestCase.java
index 7f7d78c..a071e15 100644
--- a/samples/helloworld-erlang-service/src/test/java/helloworld/HelloWorldErlangServerTestCase.java
+++ b/samples/helloworld-erlang-service/src/test/java/helloworld/HelloWorldErlangServerTestCase.java
@@ -1,68 +1,87 @@
-/*

- * Licensed to the Apache Software Foundation (ASF) under one

- * or more contributor license agreements.  See the NOTICE file

- * distributed with this work for additional information

- * regarding copyright ownership.  The ASF licenses this file

- * to you under the Apache License, Version 2.0 (the

- * "License"); you may not use this file except in compliance

- * with the License.  You may obtain a copy of the License at

- *

- *   http://www.apache.org/licenses/LICENSE-2.0

- *

- * Unless required by applicable law or agreed to in writing,

- * software distributed under the License is distributed on an

- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY

- * KIND, either express or implied.  See the License for the

- * specific language governing permissions and limitations

- * under the License.

- */

-package helloworld;

-

-import static junit.framework.Assert.assertEquals;

-import static junit.framework.Assert.assertNotNull;

-

-import helloworld.dynaignore.IgnorableRunner;

-import helloworld.dynaignore.IgnoreTest;

-

-import java.io.IOException;

-

-import org.apache.tuscany.sca.host.embedded.SCADomain;

-import org.junit.Test;

-import org.junit.runner.RunWith;

-

-

-

-/**

- * Tests that the helloworld server is available

- */

-@RunWith(IgnorableRunner.class)

-public class HelloWorldErlangServerTestCase {

-

-	private static final String EPMD_COMMAND = "epmd";

-

-	@Test

-	public void testServiceCall() throws IOException {

-		Process epmdProcess = null;

-		try {

-			epmdProcess = Runtime.getRuntime().exec(EPMD_COMMAND);

-		} catch (Exception e) {

-			System.out

-					.println("Cannot proceed - exception while executing "

-							+ EPMD_COMMAND

-							+ ": "

-							+ e.getMessage()

-							+ ". Valid and working Erlang/OTP distribution is required.");

-			throw new IgnoreTest();

-		}

-		SCADomain scaDomain = SCADomain

-				.newInstance("helloworlderlangservice.composite");

-		HelloWorldService helloWorldService = scaDomain.getService(

-				HelloWorldService.class,

-				"HelloWorldServiceComponent/HelloWorldService");

-		assertNotNull(helloWorldService);

-		assertEquals("Hello Smith", helloWorldService.getGreetings("Smith"));

-		scaDomain.close();

-		epmdProcess.destroy();

-	}

-

-}

+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package helloworld;
+
+import java.io.InputStream;
+import java.io.IOException;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+
+import helloworld.dynaignore.IgnorableRunner;
+import helloworld.dynaignore.IgnoreTest;
+
+import org.apache.tuscany.sca.host.embedded.SCADomain;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Tests that the helloworld server is available
+ */
+@RunWith(IgnorableRunner.class)
+public class HelloWorldErlangServerTestCase {
+
+	private static final String EPMD_COMMAND = "epmd";
+
+	@Test
+	public void testServiceCall() {
+		Process epmdProcess = null;
+		try {
+			epmdProcess = Runtime.getRuntime().exec(EPMD_COMMAND);
+		} catch (IOException e) {
+			System.out
+					.println("Cannot proceed - exception while executing "
+							+ EPMD_COMMAND
+							+ ": "
+							+ e.getMessage()
+							+ ". Valid and working Erlang/OTP distribution is required.");
+			throw new IgnoreTest();
+		}
+        startReaderThread(epmdProcess.getInputStream());
+        startReaderThread(epmdProcess.getErrorStream());
+
+		SCADomain scaDomain = SCADomain
+				.newInstance("helloworlderlangservice.composite");
+		HelloWorldService helloWorldService = scaDomain.getService(
+				HelloWorldService.class,
+				"HelloWorldServiceComponent/HelloWorldService");
+		assertNotNull(helloWorldService);
+		assertEquals("Hello Smith", helloWorldService.getGreetings("Smith"));
+		scaDomain.close();
+
+		epmdProcess.destroy();
+	}
+
+    private static void startReaderThread(final InputStream stream) {
+        Thread readerThread = new Thread() {
+            public void run() {
+                try {
+                    byte[] buf = new byte[100];
+                    while (true) {
+                        stream.read(buf);
+                    }
+                } catch (Exception e) {
+                    return;
+                }
+            }
+        };
+        readerThread.start();
+    }
+
+}