This content has been marked as final.
Show 1 reply
-
1. Re: Setting generated ports with embedded glassfish
dmatej Jul 29, 2013 4:06 AM (in response to dmatej)Okay, I got it - finally I cloned the arquillian-glassfish-container GitHub repo and thought I should implement it. But finally I have found a nice thing in your code - a CommandRunner instance bound to the InitialContext. So other ports can be generated like this:
@RunWith(Arquillian.class) public class ITestSupport { private static final Logger LOG = LoggerFactory.getLogger(ITestSupport.class); @Deployment public static JavaArchive init() throws Exception { runCommand("set", "configs.config.server-config.jms-service.type=EMBEDDED"); runCommand("set", "configs.config.server-config.jms-service.jms-host.default_JMS_host.port=" + ServerUtils.getFreePort()); runCommand("set", "configs.config.server-config.iiop-service.iiop-listener.orb-listener-1.port=" + ServerUtils.getFreePort()); runCommand("set", "configs.config.server-config.iiop-service.iiop-listener.SSL.port=" + ServerUtils.getFreePort()); runCommand("set", "configs.config.server-config.iiop-service.iiop-listener.SSL_MUTUALAUTH.port=" + ServerUtils.getFreePort()); // .... and init JDBC, JMS and whatsoever ... final JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "xxx.jar").addPackages(true, "cz.i.xxx") .addAsManifestResource("META-INF/persistence.xml", "persistence.xml"); LOG.info("jar: \n{}", jar.toString(true)); return jar; } /** * Execute the command with parameters and return a result. * * @param command - a commanfd to run * @param parameters - command's parameters * @return result of the command * @throws GlassFishException - cannot communicate with the instance * @throws IllegalStateException - invalid parameters or command or runner not found */ public static CommandResult runCommand(String command, String... parameters) throws GlassFishException { LOG.debug("runCommand(command={}, parameters={})", command, parameters); try { final InitialContext ctx = new InitialContext(); final CommandRunner runner = (CommandRunner) ctx.lookup("org.glassfish.embeddable.CommandRunner"); if (runner == null) { throw new IllegalStateException("No command runner instance found in initial context!"); } CommandResult result = runner.run(command, parameters); checkCommandResult(command, result); return result; } catch (NamingException e) { throw new IllegalStateException("Cannot run command " + command, e); } } private static void checkCommandResult(String cmd, CommandResult result) { LOG.info("Command: {}\n Result.status:\n {}\n Result.out:\n {}\n Result.failCause:\n {}\n", new Object[] { cmd, result.getExitStatus(), result.getOutput(), result.getFailureCause()}); if (result.getExitStatus().ordinal() != 0) { throw new IllegalStateException("Command '" + cmd + "' was unsuccessful: " + result.getOutput(), result.getFailureCause()); } } }
And only for completeness:
public class ServerUtils { /** * Tries to alocate a free local port. * * @return a free local port number. * @throws IllegalStateException if it fails for 20 attempts in cycle */ public static int getFreePort() throws IllegalStateException { int counter = 0; while (true) { counter++; try { final ServerSocket socket = new ServerSocket(0); final int port = socket.getLocalPort(); socket.setSoTimeout(1); socket.setReuseAddress(true); socket.close(); return port; } catch (IOException e) { if (counter >= 20) { throw new IllegalStateException("Cannot open random port, tried 20 times.", e); } } } } ... }
Have a nice day! :-)