/** * This will make sure a registry exists and is valid m_port. If its not available or does not work for some reason, * it will select another port. This should really not happen usually. But it can. * * @return this for fluent API. Or IllegalStateException if a port has not been detected successfully. */ public synchronized RMIRegistry selectGracefully() { // if( ( m_port = select( m_defaultPort ) ) == UNSELECTED ) { int alternativePort = new FreePort(m_altMin, m_altTo).getPort(); if ((m_port = select(alternativePort)) == UNSELECTED) { throw new IllegalStateException("No port found for RMI at all. Even though " + alternativePort + " should have worked. Thats.. not. good. at. all."); } printTakenStatus(); // } return this; }
/** * Finds a free socket upon first calll and returns the same for every next call. * * @return a free port (from first call) * * @throws RuntimeException if no port has been found. (TODO change this to an apropriate checked exception) */ public int getPort() { if( m_found == -1 ) { m_found = findFree(); } return m_found; }
private int findFree() { for( int i = m_from; i <= m_to; i++ ) { if( isFree( i ) ) { return i; } } throw new RuntimeException( "No free port in range " + m_from + ":" + m_to ); }
@Override public void runTest(TestDescription description, TestListener listener) { String filterExpression = "(&(objectClass=org.ops4j.pax.exam.ProbeInvoker))"; try { ServerSocket serverSocket = new ServerSocket(port.getPort()); TestListenerTask task = new TestListenerTask(serverSocket, listener); ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(task); RemoteServiceReference[] references = remoteFramework.getServiceReferences( filterExpression, system.getTimeout().getValue(), TimeUnit.MILLISECONDS); remoteFramework.invokeMethodOnService(references[0], "runTestClass", description.toString()); executor.shutdown(); serverSocket.close(); } catch (InvocationTargetException exc) { LOG.error("", exc.getCause()); throw new TestContainerException(exc.getCause()); } // CHECKSTYLE:SKIP catch (Exception exc) { throw new TestContainerException(exc); } }
/** * This will make sure a registry exists and is valid m_port. * If its not available or does not work for some reason, it will select another port. * This should really not happen usually. But it can. * * @return this for fluent API. Or IllegalStateException if a port has not been detected successfully. */ public synchronized RMIRegistry selectGracefully() { //if( ( m_port = select( m_defaultPort ) ) == UNSELECTED ) { int alternativePort = new FreePort( m_altMin, m_altTo ).getPort(); if( ( m_port = select( alternativePort ) ) == UNSELECTED ) { throw new IllegalStateException( "No port found for RMI at all. Even though " + alternativePort + " should have worked. Thats.. not. good. at. all." ); } printTakenStatus(); //} return this; }
@Override public void start() throws IOException { try { port = new FreePort(20000, 21000); system = system.fork(new Option[] { systemProperty("java.protocol.handler.pkgs").value("org.ops4j.pax.url"), systemProperty(EXAM_INVOKER_PORT).value(Integer.toString(port.getPort())) }); List<String> vmArgs = createVmArguments();