1 Reply Latest reply on Jul 29, 2013 4:06 AM by dmatej

    Setting generated ports with embedded glassfish

    dmatej

      Hello,

       

      Until today, we used directly embedded glassfish to test our application. It was prepared by one class by the first test that needed it.

      This class generated self signed certificate, configured resources (with some hacks to avoid committing to the real database) and logging, and started glassfish listening on generated free ports (HTTP, IIOP, JMS, ...)

       

      Now I prepared some tests with Arquillian - it was really simple, I love it! But I have a problem - the default IIOP port 3700 is not free and I had not found any way to set another one.

      And more, I cannot generate the ports, I had to use the arquillian.xml even to set the HTTP listener port - I think this is one step back.

       

      Is there some way I missed to set the ports from the code?

      How can I set the IIOP port?

        • 1. Re: Setting generated ports with embedded glassfish
          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! :-)