77import com .marklogic .appdeployer .command .CommandContext ;
88import com .marklogic .appdeployer .command .SortOrderConstants ;
99import com .marklogic .client .DatabaseClient ;
10+ import com .marklogic .hub .HubClient ;
1011import com .marklogic .hub .HubConfig ;
12+ import com .marklogic .hub .impl .HubClientImpl ;
1113import com .marklogic .hub .impl .HubConfigImpl ;
1214import org .apache .commons .io .IOUtils ;
1315import org .apache .commons .lang3 .StringUtils ;
3335public class ConfigureAppServerBasePaths extends AbstractCommand {
3436
3537 private final HubConfigImpl hubConfig ;
38+ private final HubClientImpl hubClient ;
3639 private ObjectMapper mapper = new ObjectMapper ();
3740
38- public ConfigureAppServerBasePaths (HubConfig hubConfig ) {
41+ public ConfigureAppServerBasePaths (HubConfig hubConfig , HubClient hubClient ) {
3942 this .hubConfig = (HubConfigImpl ) hubConfig ;
43+ this .hubClient = (HubClientImpl ) hubClient ;
4044 setExecuteSortOrder (SortOrderConstants .DEPLOY_OTHER_SERVERS + 1 );
4145 }
4246
@@ -53,9 +57,10 @@ private void updateAppServersBasePaths() {
5357 try (CloseableHttpClient httpClient = clientBuilder .build ()) {
5458 String url = "https://" + hubConfig .getHost () + "/api/service/dataHubEndpoints" ;
5559 HttpPost postRequest = new HttpPost (url );
56- StringEntity entity = new StringEntity (getBastPathConfig ());
60+ StringEntity entity = new StringEntity (getBasePathConfig ());
5761 postRequest .setHeader ("Content-Type" , "application/json" );
58- postRequest .setHeader ("Authorization" , "bearer " + getAccessToken ());
62+ String accessToken = getAccessToken ();
63+ postRequest .setHeader ("Authorization" , "bearer " + accessToken );
5964 postRequest .setEntity (entity );
6065 httpClient .execute (postRequest );
6166 } catch (IOException e ) {
@@ -65,12 +70,13 @@ private void updateAppServersBasePaths() {
6570
6671 private String getAccessToken () {
6772 String tokenRequestURL = "https://" .concat (hubConfig .getHost ()).concat ("/token" );
73+ logger .info ("tokenRequestURL: " + tokenRequestURL );
6874 HttpClientBuilder clientBuilder = HttpClientBuilder .create ();
6975 try (CloseableHttpClient httpClient = clientBuilder .build ()) {
7076 {
7177 List <NameValuePair > postParams = new ArrayList <>();
7278 postParams .add (new BasicNameValuePair ("grant_type" , "apikey" ));
73- postParams .add (new BasicNameValuePair ("key" , hubConfig .getCloudApiKey ()));
79+ postParams .add (new BasicNameValuePair ("key" , hubConfig .getCloudApiKey (). trim () ));
7480
7581 HttpPost postRequest = new HttpPost (tokenRequestURL );
7682 postRequest .setHeader ("Content-Type" , "application/x-www-form-urlencoded" );
@@ -88,7 +94,7 @@ private String getAccessToken() {
8894 }
8995 }
9096
91- private String getBastPathConfig () {
97+ private String getBasePathConfig () {
9298 ObjectNode baseConfig = mapper .createObjectNode ();
9399 ObjectNode appServers = mapper .createObjectNode ();
94100 ObjectNode jobServer = mapper .createObjectNode ();
@@ -105,11 +111,11 @@ private String getBastPathConfig() {
105111 String jobBasePath = jobBasePathArray [jobBasePathArray .length - 1 ];
106112
107113 baseConfig .put ("adminPath" , hubConfig .getAdminConfig ().getBasePath ());
108- baseConfig .put ("appServers" , appServers );
114+ baseConfig .putIfAbsent ("appServers" , appServers );
109115
110- appServers .put ("staging" , stagingServer );
111- appServers .put ("final" , finalServer );
112- appServers .put ("jobs" , jobServer );
116+ appServers .putIfAbsent ("staging" , stagingServer );
117+ appServers .putIfAbsent ("final" , finalServer );
118+ appServers .putIfAbsent ("jobs" , jobServer );
113119
114120 stagingServer .put ("name" , hubConfig .getStagingDbName ());
115121 stagingServer .put ("path" , stagingBasePath .concat ("/" ));
@@ -124,19 +130,29 @@ private String getBastPathConfig() {
124130 }
125131
126132 private void waitForGateWayToRestart () {
127- DatabaseClient databaseClient = hubConfig . newStagingClient ();
133+ DatabaseClient databaseClient = hubClient . getStagingClient ();
128134 int maxTimeToWaitInMs = 90000 ;
129135 int maxRetries = 15 ;
130136 int sleepTime = maxTimeToWaitInMs /maxRetries ;
131- while (! databaseClient . checkConnection (). isConnected () && maxRetries > 0 ) {
132- logger . info ( "Checking gateway status: " + databaseClient . checkConnection (). isConnected ());
137+
138+ while ( maxRetries > 0 ) {
133139 try {
134- Thread .sleep (sleepTime );
135- } catch (InterruptedException e ) {
136- Thread .currentThread ().interrupt ();
140+ if (databaseClient .checkConnection ().isConnected ()) {
141+ logger .info ("Checking gateway status: " + databaseClient .checkConnection ().isConnected ());
142+ break ;
143+ } else {
144+ logger .info ("Checking gateway status: " + databaseClient .checkConnection ().isConnected ());
145+ Thread .sleep (sleepTime );
146+ maxRetries --;
147+ }
148+ } catch (Exception e ) {
149+ if (e instanceof InterruptedException ) {
150+ Thread .currentThread ().interrupt ();
151+ }
152+ logger .info (e .getMessage ());
153+ logger .info ("waitForGateWayToRestart catch block" );
154+ maxRetries --;
137155 }
138- maxRetries --;
139156 }
140- logger .info ("Checking gateway status: " + databaseClient .checkConnection ().isConnected ());
141157 }
142158}
0 commit comments