Skip to content

Commit

Permalink
Merge pull request #8949 from spacecowboy/3.0-unbindshellport
Browse files Browse the repository at this point in the history
Fixed shutdown of Neo4jShell to unbind its port
  • Loading branch information
spacecowboy committed Mar 10, 2017
2 parents 5df1693 + 56d8d9a commit eaaca44
Show file tree
Hide file tree
Showing 19 changed files with 426 additions and 379 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public abstract class AbstractClient implements ShellClient
private long timeConnection;
private volatile boolean end;
private final Collection<String> multiLine = new ArrayList<>();
private Serializable id;
protected Serializable id;
private String prompt;

private final Map<String, Serializable> initialSession;
protected final Map<String, Serializable> initialSession;

public AbstractClient( Map<String, Serializable> initialSession, CtrlCHandler signalHandler )
{
this.signalHandler = signalHandler;
Expand Down
171 changes: 86 additions & 85 deletions community/shell/src/main/java/org/neo4j/shell/impl/RemoteClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,103 +36,104 @@
*/
public class RemoteClient extends AbstractClient
{
private ShellServer server;
private final RmiLocation serverLocation;
private final Output out;
private ShellServer server;
private final RmiLocation serverLocation;
private final Output out;

public RemoteClient( Map<String, Serializable> initialSession, RmiLocation serverLocation, CtrlCHandler ctrlcHandler ) throws ShellException
public RemoteClient( Map<String,Serializable> initialSession, RmiLocation serverLocation,
CtrlCHandler ctrlcHandler ) throws ShellException
{
this( initialSession, serverLocation, RemoteOutput.newOutput(), ctrlcHandler );
}

public RemoteClient( Map<String, Serializable> initialSession, RmiLocation serverLocation, Output output ) throws ShellException
public RemoteClient( Map<String,Serializable> initialSession, RmiLocation serverLocation, Output output )
throws ShellException
{
this( initialSession, serverLocation, output, InterruptSignalHandler.getHandler() );
}

/**
* @param serverLocation the RMI location of the server to connect to.
* @throws ShellException if no server was found at the RMI location.
*/
public RemoteClient( Map<String, Serializable> initialSession, RmiLocation serverLocation, Output out, CtrlCHandler ctrlcHandler ) throws ShellException
{
super( initialSession, ctrlcHandler );
this.serverLocation = serverLocation;
this.out = out;
this.server = findRemoteServer();
}
/**
* @param serverLocation the RMI location of the server to connect to.
* @throws ShellException if no server was found at the RMI location.
*/
public RemoteClient( Map<String,Serializable> initialSession, RmiLocation serverLocation, Output out,
CtrlCHandler ctrlcHandler ) throws ShellException
{
super( initialSession, ctrlcHandler );
this.serverLocation = serverLocation;
this.out = out;
this.server = findRemoteServer();
}

private ShellServer findRemoteServer() throws ShellException
{
try
{
ShellServer result = ( ShellServer ) this.serverLocation.getBoundObject();
sayHi( result );
updateTimeForMostRecentConnection();
return result;
}
catch ( RemoteException e )
{
throw ShellException.wrapCause( e );
}
}
private ShellServer findRemoteServer() throws ShellException
{
try
{
ShellServer result = (ShellServer) this.serverLocation.getBoundObject();
sayHi( result );
updateTimeForMostRecentConnection();
return result;
}
catch ( RemoteException e )
{
throw ShellException.wrapCause( e );
}
}

public Output getOutput()
{
return this.out;
}
public Output getOutput()
{
return this.out;
}

public ShellServer getServer()
{
// Poke the server by calling a method, f.ex. the welcome() method.
// If the connection is lost then try to reconnect, using the last
// server lookup address.
boolean hadServer = this.server != null;
boolean shouldTryToReconnect = this.server == null;
try
{
if ( !shouldTryToReconnect )
server.getName();
}
catch ( RemoteException e )
{
shouldTryToReconnect = true;
}
public ShellServer getServer()
{
// Poke the server by calling a method, f.ex. the welcome() method.
// If the connection is lost then try to reconnect, using the last
// server lookup address.
boolean hadServer = this.server != null;
boolean shouldTryToReconnect = this.server == null;
try
{
if ( !shouldTryToReconnect )
{
server.welcome( initialSession );
}
}
catch ( RemoteException | ShellException ignored )
{
shouldTryToReconnect = true;
}

Exception originException = null;
if ( shouldTryToReconnect )
{
this.server = null;
try
{
this.server = findRemoteServer();
if ( hadServer )
{
getOutput().println( "[Reconnected to server]" );
}
}
catch ( ShellException | RemoteException ee )
{
// Ok
originException = ee;
}
}

Exception originException = null;
if ( shouldTryToReconnect )
{
this.server = null;
try
{
this.server = findRemoteServer();
if ( hadServer )
getOutput().println( "[Reconnected to server]" );
}
catch ( ShellException ee )
{
// Ok
originException = ee;
}
catch ( RemoteException ee )
{
// Ok
originException = ee;
}
}
if ( this.server == null )
{
throw new RuntimeException( "Server closed or cannot be reached anymore: " + originException.getMessage(),
originException );
}
return this.server;
}

if ( this.server == null )
{
throw new RuntimeException(
"Server closed or cannot be reached anymore: " +
originException.getMessage(), originException );
}
return this.server;
}

public void shutdown()
{
super.shutdown();
tryUnexport( this.out );
}
public void shutdown()
{
super.shutdown();
tryUnexport( this.out );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
class RemotelyAvailableServer extends UnicastRemoteObject implements ShellServer
{
private final ShellServer actual;
private RmiLocation location;

RemotelyAvailableServer( ShellServer actual ) throws RemoteException
{
Expand Down Expand Up @@ -90,6 +91,9 @@ public void shutdown() throws RemoteException
{
try
{
if (this.location != null) {
this.location.unbind();
}
unexportObject( this, true );
}
catch ( NoSuchObjectException e )
Expand All @@ -107,7 +111,8 @@ public void makeRemotelyAvailable( int port, String name ) throws RemoteExceptio
@Override
public void makeRemotelyAvailable( String host, int port, String name ) throws RemoteException
{
RmiLocation.location( host, port, name ).bind( this );
this.location = RmiLocation.location( host, port, name );
this.location.bind( this );
}

@Override
Expand Down

0 comments on commit eaaca44

Please sign in to comment.