2626import java .util .ArrayList ;
2727import java .util .HashSet ;
2828import java .util .List ;
29+ import java .util .Map ;
2930import java .util .Set ;
3031
3132import org .slf4j .Logger ;
7071public class DataHub {
7172
7273 static final private Logger LOGGER = LoggerFactory .getLogger (DataHub .class );
73- static final public String STAGING_NAME = "data-hub-STAGING" ;
74- static final public String FINAL_NAME = "data-hub-FINAL" ;
75- static final public String MODULES_DB_NAME = "data-hub-modules" ;
74+
7675 private ManageConfig config ;
7776 private ManageClient client ;
78- public static String HUB_NAME = "data-hub" ;
79- public static int FORESTS_PER_HOST = 4 ;
80- private String host ;
81- private int stagingRestPort ;
82- private int finalRestPort ;
83- private String username ;
84- private String password ;
8577
8678 private File assetInstallTimeFile = new File ("./assetInstallTime.properties" );
79+ private HubConfig hubConfig ;
8780
88- private final static int DEFAULT_STAGING_REST_PORT = 8010 ;
89- private final static int DEFAULT_FINAL_REST_PORT = 8011 ;
90-
91- public DataHub (HubConfig config ) {
92- this (config .getHost (), config .getStagingPort (), config .getFinalPort (), config .getAdminUsername (), config .getAdminPassword ());
81+ public DataHub (HubConfig hubConfig ) {
82+ init (hubConfig );
9383 }
9484
9585 public DataHub (String host , String username , String password ) {
96- this (host , DEFAULT_STAGING_REST_PORT , DEFAULT_FINAL_REST_PORT , username , password );
86+ hubConfig = new HubConfig ();
87+ hubConfig .host = host ;
88+ hubConfig .adminUsername = username ;
89+ hubConfig .adminPassword = password ;
90+ init (hubConfig );
9791 }
9892
99- public DataHub (String host , int stagingRestPort , int finalRestPort , String username , String password ) {
100- config = new ManageConfig (host , 8002 , username , password );
93+ private void init (HubConfig hubConfig ) {
94+ this .hubConfig = hubConfig ;
95+ config = new ManageConfig (hubConfig .host , 8002 , hubConfig .adminUsername , hubConfig .adminPassword );
10196 client = new ManageClient (config );
102- this .host = host ;
103- this .stagingRestPort = stagingRestPort ;
104- this .finalRestPort = finalRestPort ;
105- this .username = username ;
106- this .password = password ;
10797 }
10898
10999 public void setAssetInstallTimeFile (File assetInstallTimeFile ) {
@@ -117,35 +107,47 @@ public void setAssetInstallTimeFile(File assetInstallTimeFile) {
117107 public boolean isInstalled () {
118108 ServerManager sm = new ServerManager (client );
119109 DatabaseManager dm = new DatabaseManager (client );
120- boolean stagingAppServerExists = sm .exists (STAGING_NAME );
121- boolean finalAppServerExists = sm .exists (FINAL_NAME );
122- boolean appserversOk = (stagingAppServerExists && finalAppServerExists );
110+ boolean stagingAppServerExists = sm .exists (hubConfig .stagingHttpName );
111+ boolean finalAppServerExists = sm .exists (hubConfig .finalHttpName );
112+ boolean tracingAppServerExists = sm .exists (hubConfig .tracingHttpName );
113+ boolean appserversOk = (stagingAppServerExists && finalAppServerExists && tracingAppServerExists );
123114
124- boolean stagingDbExists = dm .exists (STAGING_NAME );
125- boolean finalDbExists = dm .exists (FINAL_NAME );
115+ boolean stagingDbExists = dm .exists (hubConfig .stagingDbName );
116+ boolean finalDbExists = dm .exists (hubConfig .finalDbName );
117+ boolean tracingDbExists = dm .exists (hubConfig .stagingDbName );
126118
127119 boolean stagingForestsExist = false ;
128120 boolean finalForestsExist = false ;
121+ boolean tracingForestsExist = false ;
129122
130123 boolean stagingIndexesOn = false ;
131124 boolean finalIndexesOn = false ;
125+ boolean tracingIndexesOn = false ;
132126
133127 if (stagingDbExists ) {
134- Fragment f = dm .getPropertiesAsXml (STAGING_NAME );
128+ Fragment f = dm .getPropertiesAsXml (hubConfig . stagingDbName );
135129 stagingIndexesOn = Boolean .parseBoolean (f .getElementValue ("//m:triple-index" ));
136130 stagingIndexesOn = stagingIndexesOn && Boolean .parseBoolean (f .getElementValue ("//m:collection-lexicon" ));
137- stagingForestsExist = (dm .getForestIds (STAGING_NAME ).size () == FORESTS_PER_HOST );
131+ stagingForestsExist = (dm .getForestIds (hubConfig . stagingDbName ).size () == hubConfig . stagingForestsPerHost );
138132 }
139133
140134 if (finalDbExists ) {
141- Fragment f = dm .getPropertiesAsXml (FINAL_NAME );
135+ Fragment f = dm .getPropertiesAsXml (hubConfig . finalDbName );
142136 finalIndexesOn = Boolean .parseBoolean (f .getElementValue ("//m:triple-index" ));
143137 finalIndexesOn = finalIndexesOn && Boolean .parseBoolean (f .getElementValue ("//m:collection-lexicon" ));
144- finalForestsExist = (dm .getForestIds (FINAL_NAME ).size () == FORESTS_PER_HOST );
138+ finalForestsExist = (dm .getForestIds (hubConfig .finalDbName ).size () == hubConfig .finalForestsPerHost );
139+ }
140+
141+ if (tracingDbExists ) {
142+ tracingIndexesOn = true ;
143+ int forests = dm .getForestIds (hubConfig .tracingDbName ).size ();
144+ tracingForestsExist = (forests == hubConfig .tracingForestsPerHost );
145145 }
146+
146147 boolean dbsOk = (stagingDbExists && stagingIndexesOn &&
147- finalDbExists && finalIndexesOn );
148- boolean forestsOk = (stagingForestsExist && finalForestsExist );
148+ finalDbExists && finalIndexesOn &&
149+ tracingDbExists && tracingIndexesOn );
150+ boolean forestsOk = (stagingForestsExist && finalForestsExist && tracingForestsExist );
149151
150152 return (appserversOk && dbsOk && forestsOk );
151153 }
@@ -157,9 +159,9 @@ public boolean isInstalled() {
157159 public void validateServer () throws ServerValidationException {
158160 try {
159161 AdminConfig adminConfig = new AdminConfig ();
160- adminConfig .setHost (host );
161- adminConfig .setUsername (username );
162- adminConfig .setPassword (password );
162+ adminConfig .setHost (hubConfig . host );
163+ adminConfig .setUsername (hubConfig . adminUsername );
164+ adminConfig .setPassword (hubConfig . adminPassword );
163165 AdminManager am = new AdminManager (adminConfig );
164166 String versionString = am .getServerVersion ();
165167 int major = Integer .parseInt (versionString .substring (0 , 1 ));
@@ -175,16 +177,26 @@ public void validateServer() throws ServerValidationException {
175177
176178 private AppConfig getAppConfig () throws IOException {
177179 AppConfig config = new AppConfig ();
178- config .setHost (host );
179- config .setRestPort (stagingRestPort );
180- config .setName (HUB_NAME );
181- config .setRestAdminUsername (username );
182- config .setRestAdminPassword (password );
180+ config .setHost (hubConfig .host );
181+ config .setRestPort (hubConfig .stagingPort );
182+ config .setName (hubConfig .name );
183+ config .setRestAdminUsername (hubConfig .adminUsername );
184+ config .setRestAdminPassword (hubConfig .adminPassword );
185+ config .setModulesDatabaseName (hubConfig .modulesDbName );
186+
183187 List <String > paths = new ArrayList <String >();
184188 paths .add (new ClassPathResource ("ml-modules" ).getPath ());
189+
185190 String configPath = new ClassPathResource ("ml-config" ).getPath ();
186191 config .setConfigDir (new ConfigDir (new File (configPath )));
187192 config .setModulePaths (paths );
193+
194+ Map <String , String > customTokens = config .getCustomTokens ();
195+ customTokens .put ("%%STAGING_DATABASE%%" , hubConfig .stagingDbName );
196+ customTokens .put ("%%FINAL_DATABASE%%" , hubConfig .finalDbName );
197+ customTokens .put ("%%TRACING_DATABASE%%" , hubConfig .tracingDbName );
198+ customTokens .put ("%%MODULES_DATABASE%%" , hubConfig .modulesDbName );
199+
188200 return config ;
189201 }
190202
@@ -193,6 +205,7 @@ private AppConfig getAppConfig() throws IOException {
193205 * @throws IOException
194206 */
195207 public void install () throws IOException {
208+ LOGGER .debug ("Installing the Data Hub into MarkLogic" );
196209 // clean up any lingering cache for deployed modules
197210 PropertiesModuleManager moduleManager = new PropertiesModuleManager (this .assetInstallTimeFile );
198211 moduleManager .deletePropertiesFile ();
@@ -206,14 +219,15 @@ public void install() throws IOException {
206219
207220 private DatabaseClient getDatabaseClient (int port ) {
208221 AppConfig config = new AppConfig ();
209- config .setHost (host );
210- config .setName (HUB_NAME );
211- config .setRestAdminUsername (username );
212- config .setRestAdminPassword (password );
213- DatabaseClient client = DatabaseClientFactory .newClient (host , port , username , password ,
222+ config .setHost (hubConfig . host );
223+ config .setName (hubConfig . name );
224+ config .setRestAdminUsername (hubConfig . adminUsername );
225+ config .setRestAdminPassword (hubConfig . adminPassword );
226+ DatabaseClient client = DatabaseClientFactory .newClient (hubConfig . host , port , hubConfig . adminUsername , hubConfig . adminPassword ,
214227 config .getRestAuthentication (), config .getRestSslContext (), config .getRestSslHostnameVerifier ());
215228 return client ;
216229 }
230+
217231 /**
218232 * Installs User Provided modules into the Data Hub
219233 *
@@ -224,15 +238,12 @@ private DatabaseClient getDatabaseClient(int port) {
224238 * @throws IOException
225239 */
226240 public Set <File > installUserModules (String pathToUserModules ) throws IOException {
227- AppConfig config = new AppConfig ();
228- config .setHost (host );
229- config .setRestPort (finalRestPort );
230- config .setName (HUB_NAME );
231- config .setRestAdminUsername (username );
232- config .setRestAdminPassword (password );
241+ LOGGER .debug ("Installing user modules into MarkLogic" );
233242
234- DatabaseClient stagingClient = getDatabaseClient (stagingRestPort );
235- DatabaseClient finalClient = getDatabaseClient (finalRestPort );
243+ AppConfig config = getAppConfig ();
244+
245+ DatabaseClient stagingClient = getDatabaseClient (hubConfig .stagingPort );
246+ DatabaseClient finalClient = getDatabaseClient (hubConfig .finalPort );
236247
237248
238249 Set <File > loadedFiles = new HashSet <File >();
@@ -274,7 +285,8 @@ else if (isConformanceDir) {
274285 }
275286
276287 public JsonNode validateUserModules () {
277- DatabaseClient client = getDatabaseClient (stagingRestPort );
288+ LOGGER .debug ("validating user modules" );
289+ DatabaseClient client = getDatabaseClient (hubConfig .stagingPort );
278290 EntitiesValidator ev = new EntitiesValidator (client );
279291 return ev .validate ();
280292 }
@@ -290,22 +302,27 @@ private List<Command> getCommands(AppConfig config) {
290302
291303 // Databases
292304 List <Command > dbCommands = new ArrayList <Command >();
293- DeployHubDatabaseCommand staging = new DeployHubDatabaseCommand (STAGING_NAME );
294- staging .setForestsPerHost (FORESTS_PER_HOST );
305+ DeployHubDatabaseCommand staging = new DeployHubDatabaseCommand (hubConfig . stagingDbName );
306+ staging .setForestsPerHost (hubConfig . stagingForestsPerHost );
295307 dbCommands .add (staging );
296308
297- DeployHubDatabaseCommand finalDb = new DeployHubDatabaseCommand (FINAL_NAME );
298- finalDb .setForestsPerHost (FORESTS_PER_HOST );
309+ DeployHubDatabaseCommand finalDb = new DeployHubDatabaseCommand (hubConfig . finalDbName );
310+ finalDb .setForestsPerHost (hubConfig . finalForestsPerHost );
299311 dbCommands .add (finalDb );
300312
301- dbCommands .add (new DeployModulesDatabaseCommand (MODULES_DB_NAME ));
313+ DeployHubDatabaseCommand tracingDb = new DeployHubDatabaseCommand (hubConfig .tracingDbName );
314+ tracingDb .setForestsPerHost (hubConfig .tracingForestsPerHost );
315+ dbCommands .add (tracingDb );
316+
317+ dbCommands .add (new DeployModulesDatabaseCommand (hubConfig .modulesDbName ));
302318 dbCommands .add (new DeployTriggersDatabaseCommand ());
303319 dbCommands .add (new DeploySchemasDatabaseCommand ());
304320 commands .addAll (dbCommands );
305321
306322 // App Servers
307- commands .add (new DeployRestApiCommand (STAGING_NAME , stagingRestPort ));
308- commands .add (new DeployRestApiCommand (FINAL_NAME , finalRestPort ));
323+ commands .add (new DeployRestApiCommand (hubConfig .stagingHttpName , hubConfig .stagingPort ));
324+ commands .add (new DeployRestApiCommand (hubConfig .finalHttpName , hubConfig .finalPort ));
325+ commands .add (new DeployRestApiCommand (hubConfig .tracingHttpName , hubConfig .tracePort ));
309326
310327 // Modules
311328 commands .add (new LoadModulesCommand ());
@@ -320,6 +337,7 @@ private List<Command> getCommands(AppConfig config) {
320337 * @throws IOException
321338 */
322339 public void uninstall () throws IOException {
340+ LOGGER .debug ("Uninstalling the Data Hub from MarkLogic" );
323341 AdminManager manager = new AdminManager ();
324342 AppConfig config = getAppConfig ();
325343 SimpleAppDeployer deployer = new SimpleAppDeployer (client , manager );
0 commit comments