Skip to content

Commit 3bb74cb

Browse files
committed
Issue #79: Add support for multiple log files
1 parent 0919587 commit 3bb74cb

24 files changed

+721
-113
lines changed

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/HttpUtil.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2018 IBM Corporation and others.
2+
* Copyright (c) 2018, 2019 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -96,7 +96,7 @@ public static HttpResult get(URI uri) throws IOException {
9696
}
9797
}
9898
}
99-
99+
100100
public static HttpResult post(URI uri, JSONObject payload) throws IOException {
101101
HttpURLConnection connection = null;
102102

@@ -105,12 +105,30 @@ public static HttpResult post(URI uri, JSONObject payload) throws IOException {
105105
connection = (HttpURLConnection) uri.toURL().openConnection();
106106

107107
connection.setRequestMethod("POST");
108-
connection.setRequestProperty("Content-Type", "application/json");
109-
connection.setDoOutput(true);
108+
109+
if (payload != null) {
110+
connection.setRequestProperty("Content-Type", "application/json");
111+
connection.setDoOutput(true);
112+
113+
DataOutputStream payloadStream = new DataOutputStream(connection.getOutputStream());
114+
payloadStream.write(payload.toString().getBytes());
115+
}
110116

111-
DataOutputStream payloadStream = new DataOutputStream(connection.getOutputStream());
112-
payloadStream.write(payload.toString().getBytes());
117+
return new HttpResult(connection);
118+
} finally {
119+
if (connection != null) {
120+
connection.disconnect();
121+
}
122+
}
123+
}
124+
125+
public static HttpResult post(URI uri) throws IOException {
126+
HttpURLConnection connection = null;
113127

128+
MCLogger.log("Empty POST TO " + uri);
129+
try {
130+
connection = (HttpURLConnection) uri.toURL().openConnection();
131+
connection.setRequestMethod("POST");
114132
return new HttpResult(connection);
115133
} finally {
116134
if (connection != null) {

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/MCEclipseApplication.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
import java.net.MalformedURLException;
1515
import java.util.ArrayList;
16+
import java.util.HashSet;
1617
import java.util.List;
18+
import java.util.Set;
1719

1820
import org.eclipse.core.resources.IContainer;
1921
import org.eclipse.core.resources.IFile;
@@ -43,6 +45,8 @@
4345

4446
import com.ibm.microclimate.core.MicroclimateCorePlugin;
4547
import com.ibm.microclimate.core.internal.connection.MicroclimateConnection;
48+
import com.ibm.microclimate.core.internal.console.ProjectLogInfo;
49+
import com.ibm.microclimate.core.internal.console.SocketConsole;
4650
import com.ibm.microclimate.core.internal.constants.ProjectCapabilities;
4751
import com.ibm.microclimate.core.internal.constants.ProjectType;
4852
import com.ibm.microclimate.core.internal.launch.MicroclimateLaunchConfigDelegate;
@@ -65,10 +69,13 @@ public class MCEclipseApplication extends MicroclimateApplication {
6569
// in seconds
6670
public static final int DEFAULT_DEBUG_CONNECT_TIMEOUT = 3;
6771

68-
// Consoles, null if not showing
72+
// Old style consoles, null if not showing
6973
private IConsole appConsole = null;
7074
private IConsole buildConsole = null;
7175

76+
// New consoles
77+
private Set<SocketConsole> activeConsoles = new HashSet<SocketConsole>();
78+
7279
// Debug launch, null if not debugging
7380
private ILaunch launch = null;
7481

@@ -102,6 +109,25 @@ public synchronized IConsole getBuildConsole() {
102109
return buildConsole;
103110
}
104111

112+
public synchronized void addConsole(SocketConsole console) {
113+
activeConsoles.add(console);
114+
}
115+
116+
public synchronized SocketConsole getConsole(ProjectLogInfo logInfo) {
117+
for (SocketConsole console : activeConsoles) {
118+
if (console.logInfo.isThisLogInfo(logInfo)) {
119+
return console;
120+
}
121+
}
122+
return null;
123+
}
124+
125+
public synchronized void removeConsole(SocketConsole console) {
126+
if (console != null) {
127+
activeConsoles.remove(console);
128+
}
129+
}
130+
105131
public synchronized void setLaunch(ILaunch launch) {
106132
this.launch = launch;
107133
}
@@ -234,16 +260,17 @@ public void dispose() {
234260
clearDebugger();
235261

236262
// Clean up the consoles
237-
List<IConsole> consoles = new ArrayList<IConsole>();
263+
List<IConsole> consoleList = new ArrayList<IConsole>();
238264
if (appConsole != null) {
239-
consoles.add(appConsole);
265+
consoleList.add(appConsole);
240266
}
241267
if (buildConsole != null) {
242-
consoles.add(buildConsole);
268+
consoleList.add(buildConsole);
243269
}
244-
if (!consoles.isEmpty()) {
270+
consoleList.addAll(activeConsoles);
271+
if (!consoleList.isEmpty()) {
245272
IConsoleManager consoleManager = ConsolePlugin.getDefault().getConsoleManager();
246-
consoleManager.removeConsoles(consoles.toArray(new IConsole[consoles.size()]));
273+
consoleManager.removeConsoles(consoleList.toArray(new IConsole[consoleList.size()]));
247274
}
248275
super.dispose();
249276
}

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/MicroclimateApplication.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313

1414
import java.net.MalformedURLException;
1515
import java.net.URL;
16+
import java.util.List;
1617

1718
import org.eclipse.core.runtime.IPath;
1819
import org.json.JSONObject;
1920

2021
import com.ibm.microclimate.core.internal.connection.MicroclimateConnection;
22+
import com.ibm.microclimate.core.internal.console.ProjectLogInfo;
2123
import com.ibm.microclimate.core.internal.constants.AppState;
2224
import com.ibm.microclimate.core.internal.constants.BuildStatus;
2325
import com.ibm.microclimate.core.internal.constants.MCConstants;
@@ -46,6 +48,7 @@ public class MicroclimateApplication {
4648
private String containerId;
4749
private ProjectCapabilities projectCapabilities;
4850
private String action;
51+
private List<ProjectLogInfo> logInfos;
4952

5053
// Must be updated whenever httpPort changes. Can be null
5154
private URL baseUrl;
@@ -130,6 +133,10 @@ public synchronized void setAction(String action) {
130133
this.action = action;
131134
}
132135

136+
public synchronized void setLogInfos(List<ProjectLogInfo> logInfos) {
137+
this.logInfos = logInfos;
138+
}
139+
133140
/**
134141
* Can return null if this project hasn't started yet (ie httpPort == -1)
135142
*/
@@ -193,6 +200,10 @@ public boolean isImporting() {
193200
public boolean isAvailable() {
194201
return isEnabled() && !isImporting();
195202
}
203+
204+
public List<ProjectLogInfo> getLogInfos() {
205+
return logInfos;
206+
}
196207

197208
public boolean hasBuildLog() {
198209
return (!projectType.isType(ProjectType.TYPE_NODEJS));

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/MicroclimateApplicationFactory.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
package com.ibm.microclimate.core.internal;
1313

1414
import java.util.HashSet;
15+
import java.util.List;
1516
import java.util.Set;
1617

1718
import org.json.JSONArray;
1819
import org.json.JSONException;
1920
import org.json.JSONObject;
2021

2122
import com.ibm.microclimate.core.internal.connection.MicroclimateConnection;
23+
import com.ibm.microclimate.core.internal.console.ProjectLogInfo;
2224
import com.ibm.microclimate.core.internal.constants.MCConstants;
2325
import com.ibm.microclimate.core.internal.constants.ProjectType;
2426
import com.ibm.microclimate.core.internal.constants.StartMode;
@@ -215,5 +217,14 @@ public static void updateApp(MicroclimateApplication mcApp, JSONObject appJso) {
215217
} catch(JSONException e) {
216218
MCLogger.logError("Error parsing project json: " + appJso, e); //$NON-NLS-1$
217219
}
220+
221+
try {
222+
// Set the log information
223+
List<ProjectLogInfo> logInfos = mcApp.mcConnection.requestProjectLogs(mcApp);
224+
mcApp.setLogInfos(logInfos);
225+
} catch (Exception e) {
226+
MCLogger.logError("An error occurred while updating the log information for project: " + mcApp.name, e);
227+
}
228+
218229
}
219230
}

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/connection/MicroclimateConnection.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.ibm.microclimate.core.internal.MCUtil;
3939
import com.ibm.microclimate.core.internal.MicroclimateApplication;
4040
import com.ibm.microclimate.core.internal.MicroclimateApplicationFactory;
41+
import com.ibm.microclimate.core.internal.console.ProjectLogInfo;
4142
import com.ibm.microclimate.core.internal.constants.MCConstants;
4243
import com.ibm.microclimate.core.internal.constants.ProjectType;
4344
import com.ibm.microclimate.core.internal.messages.Messages;
@@ -437,6 +438,69 @@ public void requestProjectBuild(MicroclimateApplication app, String action)
437438
HttpUtil.post(url, buildPayload);
438439
}
439440

441+
public List<ProjectLogInfo> requestProjectLogs(MicroclimateApplication app) throws JSONException, IOException {
442+
String endpoint = MCConstants.APIPATH_PROJECT_LIST + "/" //$NON-NLS-1$
443+
+ app.projectID + "/" //$NON-NLS-1$
444+
+ MCConstants.APIPATH_LOGS;
445+
446+
URI uri = baseUrl.resolve(endpoint);
447+
HttpResult result = HttpUtil.get(uri);
448+
checkResult(result, uri, true);
449+
450+
List<ProjectLogInfo> logList = new ArrayList<ProjectLogInfo>();
451+
JSONObject logs = new JSONObject(result.response);
452+
JSONArray buildLogs = logs.getJSONArray(MCConstants.KEY_LOG_BUILD);
453+
logList.addAll(getLogs(buildLogs, MCConstants.KEY_LOG_BUILD));
454+
JSONArray appLogs = logs.getJSONArray(MCConstants.KEY_LOG_APP);
455+
logList.addAll(getLogs(appLogs, MCConstants.KEY_LOG_APP));
456+
return logList;
457+
}
458+
459+
private List<ProjectLogInfo> getLogs(JSONArray logs, String type) throws JSONException {
460+
List<ProjectLogInfo> logList = new ArrayList<ProjectLogInfo>();
461+
if (logs != null) {
462+
for (int i = 0; i < logs.length(); i++) {
463+
JSONObject log = logs.getJSONObject(i);
464+
if (log.has(MCConstants.KEY_LOG_NAME)) {
465+
String logName = log.getString(MCConstants.KEY_LOG_NAME);
466+
String workspacePath = null;
467+
if (log.has(MCConstants.KEY_LOG_WORKSPACE_PATH)) {
468+
workspacePath = log.getString(MCConstants.KEY_LOG_WORKSPACE_PATH);
469+
}
470+
ProjectLogInfo logInfo = new ProjectLogInfo(type, logName, workspacePath);
471+
logList.add(logInfo);
472+
} else {
473+
MCLogger.log("An item in the log list does not have the key: " + MCConstants.KEY_LOG_NAME);
474+
}
475+
}
476+
}
477+
return logList;
478+
}
479+
480+
public void requestEnableLogStream(MicroclimateApplication app, ProjectLogInfo logInfo) throws IOException {
481+
String endpoint = MCConstants.APIPATH_PROJECT_LIST + "/" //$NON-NLS-1$
482+
+ app.projectID + "/" //$NON-NLS-1$
483+
+ MCConstants.APIPATH_LOGS + "/" //$NON-NLS-1$
484+
+ logInfo.type + "/" //$NON-NLS-1$
485+
+ logInfo.logName;
486+
487+
URI uri = baseUrl.resolve(endpoint);
488+
HttpResult result = HttpUtil.post(uri);
489+
checkResult(result, uri, false);
490+
}
491+
492+
public void requestDisableLogStream(MicroclimateApplication app, ProjectLogInfo logInfo) throws IOException {
493+
String endpoint = MCConstants.APIPATH_PROJECT_LIST + "/" //$NON-NLS-1$
494+
+ app.projectID + "/" //$NON-NLS-1$
495+
+ MCConstants.APIPATH_LOGS + "/" //$NON-NLS-1$
496+
+ logInfo.type + "/" //$NON-NLS-1$
497+
+ logInfo.logName;
498+
499+
URI uri = baseUrl.resolve(endpoint);
500+
HttpResult result = HttpUtil.delete(uri);
501+
checkResult(result, uri, false);
502+
}
503+
440504
public void requestValidate(MicroclimateApplication app) throws JSONException, IOException {
441505
boolean projectIdInPath = checkVersion(1901, "2019_M1_E");
442506

@@ -519,6 +583,17 @@ public JSONObject requestProjectCapabilities(MicroclimateApplication app) throws
519583
return capabilities;
520584
}
521585

586+
private void checkResult(HttpResult result, URI uri, boolean checkContent) throws IOException {
587+
if (!result.isGoodResponse) {
588+
final String msg = String.format("Received bad response code %d for uri %s with error message %s", //$NON-NLS-1$
589+
result.responseCode, uri, result.error);
590+
throw new IOException(msg);
591+
} else if (checkContent && result.response == null) {
592+
// I don't think this will ever happen.
593+
throw new IOException("Server returned good response code, but the content of the result is null for uri: " + uri); //$NON-NLS-1$
594+
}
595+
}
596+
522597
public boolean isConnected() {
523598
return isConnected;
524599
}

0 commit comments

Comments
 (0)