@@ -56,8 +56,9 @@ public class MicroclimateConnection {
5656 private IPath localWorkspacePath ;
5757 private String versionStr ;
5858 private String connectionErrorMsg = null ;
59+ private String socketNamespace = null ;
5960
60- public final MicroclimateSocket mcSocket ;
61+ private MicroclimateSocket mcSocket ;
6162
6263 private volatile boolean isConnected = true ;
6364
@@ -76,14 +77,7 @@ public MicroclimateConnection (URI uri) throws IOException, URISyntaxException,
7677 if (MicroclimateConnectionManager .getActiveConnection (uri .toString ()) != null ) {
7778 onInitFail (NLS .bind (Messages .MicroclimateConnection_ErrConnection_AlreadyExists , baseUrl ));
7879 }
79-
80- // Must set host, port fields before doing this
81- mcSocket = new MicroclimateSocket (this );
82- if (!mcSocket .blockUntilFirstConnection ()) {
83- close ();
84- throw new MicroclimateConnectionException (mcSocket .socketUri );
85- }
86-
80+
8781 JSONObject env = getEnvData (this .baseUrl );
8882
8983 this .versionStr = getMCVersion (env );
@@ -104,11 +98,27 @@ public MicroclimateConnection (URI uri) throws IOException, URISyntaxException,
10498 // This should never happen since we have already determined it is a supported version of microclimate.
10599 onInitFail (Messages .MicroclimateConnection_ErrConnection_WorkspaceErr );
106100 }
101+
102+ this .socketNamespace = getSocketNamespace (env );
103+
104+ mcSocket = new MicroclimateSocket (this );
105+ if (!mcSocket .blockUntilFirstConnection ()) {
106+ close ();
107+ throw new MicroclimateConnectionException (mcSocket .socketUri );
108+ }
107109
108110 refreshApps (null );
109111
110112 MCLogger .log ("Created " + this ); //$NON-NLS-1$
111113 }
114+
115+ public String getSocketNamespace () {
116+ return socketNamespace ;
117+ }
118+
119+ public MicroclimateSocket getMCSocket () {
120+ return mcSocket ;
121+ }
112122
113123 private void onInitFail (String msg ) throws ConnectException {
114124 MCLogger .log ("Initializing MicroclimateConnection failed: " + msg ); //$NON-NLS-1$
@@ -121,11 +131,8 @@ private void onInitFail(String msg) throws ConnectException {
121131 */
122132 public void close () {
123133 MCLogger .log ("Closing " + this ); //$NON-NLS-1$
124- if (mcSocket != null && mcSocket .socket != null ) {
125- if (mcSocket .socket .connected ()) {
126- mcSocket .socket .disconnect ();
127- }
128- mcSocket .socket .close ();
134+ if (mcSocket != null ) {
135+ mcSocket .close ();
129136 }
130137 for (MicroclimateApplication app : appMap .values ()) {
131138 app .dispose ();
@@ -250,6 +257,19 @@ private static Path getWorkspacePath(JSONObject env) throws JSONException {
250257 }
251258 return new Path (workspaceLoc );
252259 }
260+
261+ private static String getSocketNamespace (JSONObject env ) throws JSONException {
262+ if (env .has (MCConstants .KEY_ENV_MC_SOCKET_NAMESPACE )) {
263+ Object nsObj = env .get (MCConstants .KEY_ENV_MC_SOCKET_NAMESPACE );
264+ if (nsObj instanceof String ) {
265+ String namespace = (String )nsObj ;
266+ if (!namespace .isEmpty ()) {
267+ return namespace ;
268+ }
269+ }
270+ }
271+ return null ;
272+ }
253273
254274 /**
255275 * Refresh this connection's apps using the Microclimate project list endpoint.
@@ -547,6 +567,21 @@ public synchronized void clearConnectionError() {
547567 return ;
548568 }
549569 this .localWorkspacePath = path ;
570+
571+ String socketNS = getSocketNamespace (envData );
572+ if ((socketNS != null && !socketNS .equals (this .socketNamespace )) || (this .socketNamespace != null && !this .socketNamespace .equals (socketNS ))) {
573+ // The socket namespace has changed so need to recreate the socket
574+ this .socketNamespace = socketNS ;
575+ mcSocket .close ();
576+ mcSocket = new MicroclimateSocket (this );
577+ if (!mcSocket .blockUntilFirstConnection ()) {
578+ // Still not connected
579+ MCLogger .logError ("Failed to create a new socket with updated URI: " + mcSocket .socketUri );
580+ // Clear the message so that it just shows the basic disconnected message
581+ this .connectionErrorMsg = null ;
582+ return ;
583+ }
584+ }
550585 } catch (Exception e ) {
551586 MCLogger .logError ("An exception occurred while trying to update the connection information" , e );
552587 this .connectionErrorMsg = Messages .MicroclimateConnection_ErrConnection_UpdateCacheException ;
0 commit comments