Repository: sqoop Updated Branches: refs/heads/sqoop2 d38e239b9 -> 2ce59da58
SQOOP-1844: Sqoop2: Start Derby server in DerbyProvider on random port (Jarek Jarcec Cecho via Veena Basavaraj) Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/2ce59da5 Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/2ce59da5 Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/2ce59da5 Branch: refs/heads/sqoop2 Commit: 2ce59da58fea98af2c1e236b166fe130676083b4 Parents: d38e239 Author: Abraham Elmahrek <[email protected]> Authored: Fri Dec 5 18:04:53 2014 -0800 Committer: Abraham Elmahrek <[email protected]> Committed: Fri Dec 5 18:04:53 2014 -0800 ---------------------------------------------------------------------- .../sqoop/common/test/db/DerbyProvider.java | 11 +++- .../sqoop/common/test/utils/NetworkUtils.java | 64 ++++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/sqoop/blob/2ce59da5/common-test/src/main/java/org/apache/sqoop/common/test/db/DerbyProvider.java ---------------------------------------------------------------------- diff --git a/common-test/src/main/java/org/apache/sqoop/common/test/db/DerbyProvider.java b/common-test/src/main/java/org/apache/sqoop/common/test/db/DerbyProvider.java index 8b4643c..2597325 100644 --- a/common-test/src/main/java/org/apache/sqoop/common/test/db/DerbyProvider.java +++ b/common-test/src/main/java/org/apache/sqoop/common/test/db/DerbyProvider.java @@ -21,6 +21,7 @@ import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.derby.drda.NetworkServerControl; import org.apache.sqoop.common.test.utils.LoggerWriter; +import org.apache.sqoop.common.test.utils.NetworkUtils; import java.net.InetAddress; @@ -35,13 +36,19 @@ public class DerbyProvider extends DatabaseProvider { public static final String DRIVER = "org.apache.derby.jdbc.ClientDriver"; + // Used port for this instance + int port; + NetworkServerControl server = null; @Override public void start() { // Start embedded server try { - server = new NetworkServerControl(InetAddress.getByName("localhost"), 1527); + port = NetworkUtils.findAvailablePort(); + LOG.info("Will bind to port " + port); + + server = new NetworkServerControl(InetAddress.getByName("localhost"), port); server.start(new LoggerWriter(LOG, Level.INFO)); // Start won't thrown an exception in case that it fails to start, one @@ -99,7 +106,7 @@ public class DerbyProvider extends DatabaseProvider { @Override public String getConnectionUrl() { - return "jdbc:derby://localhost:1527/memory:sqoop;create=true"; + return "jdbc:derby://localhost:" + port + "/memory:sqoop;create=true"; } @Override http://git-wip-us.apache.org/repos/asf/sqoop/blob/2ce59da5/common-test/src/main/java/org/apache/sqoop/common/test/utils/NetworkUtils.java ---------------------------------------------------------------------- diff --git a/common-test/src/main/java/org/apache/sqoop/common/test/utils/NetworkUtils.java b/common-test/src/main/java/org/apache/sqoop/common/test/utils/NetworkUtils.java new file mode 100644 index 0000000..87534c7 --- /dev/null +++ b/common-test/src/main/java/org/apache/sqoop/common/test/utils/NetworkUtils.java @@ -0,0 +1,64 @@ +/** + * 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 org.apache.sqoop.common.test.utils; + +import java.io.IOException; +import java.net.ServerSocket; + +/** + * Network related utilities. + */ +public class NetworkUtils { + + /** + * Create port number that is available on this machine for + * subsequent use. + * + * Please note that this method doesn't guarantee that you + * will be able to subsequently use that port as there is + * a race condition when this method can return port number + * that is available at the time of the call, but will be + * used by different process before the caller will have a + * chance to use it. + * + * This is merely a helper method to find a random available + * port on the machine to enable multiple text executions + * on the same machine or deal with situation when some tools + * (such as Derby) do need specific port and can't be started + * on "random" port. + * + * @return Available port number + * @throws IOException + */ + public static int findAvailablePort() throws IOException { + ServerSocket socket = null; + + try { + socket = new ServerSocket(0); + return socket.getLocalPort(); + } finally { + if(socket != null) { + socket.close(); + } + } + } + + private NetworkUtils() { + // Instantiation is prohibited + } +}
