Skip to content

Commit a6101df

Browse files
eharris369GitHub Enterprise
authored andcommitted
Merge pull request #190 from eharris/165-moreWorkForOpenShellAction
Issue #165: More work for container shell
2 parents fcfe856 + bdc3fc0 commit a6101df

File tree

10 files changed

+107
-78
lines changed

10 files changed

+107
-78
lines changed

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

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public class MCEclipseApplication extends MicroclimateApplication {
6464
private ILaunch launch = null;
6565

6666
MCEclipseApplication(MicroclimateConnection mcConnection,
67-
String id, String name, ProjectType projectType, String pathInWorkspace, String containerId, String contextRoot)
67+
String id, String name, ProjectType projectType, String pathInWorkspace, String contextRoot)
6868
throws MalformedURLException {
69-
super(mcConnection, id, name, projectType, pathInWorkspace, containerId, contextRoot);
69+
super(mcConnection, id, name, projectType, pathInWorkspace, contextRoot);
7070
}
7171

7272
public synchronized boolean hasAppConsole() {
@@ -170,10 +170,12 @@ public void dispose() {
170170
@Override
171171
public void resetValidation() {
172172
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
173-
try {
174-
project.deleteMarkers(MARKER_TYPE, true, IResource.DEPTH_INFINITE);
175-
} catch (CoreException e) {
176-
MCLogger.logError("Failed to delete existing markers for the " + name + " project.", e);
173+
if (project != null && project.isAccessible()) {
174+
try {
175+
project.deleteMarkers(MARKER_TYPE, true, IResource.DEPTH_INFINITE);
176+
} catch (CoreException e) {
177+
MCLogger.logError("Failed to delete existing markers for the " + name + " project.", e);
178+
}
177179
}
178180
}
179181

@@ -190,27 +192,29 @@ public void validationWarning(String filePath, String message, String quickFixId
190192
private void validationEvent(int severity, String filePath, String message, String quickFixId, String quickFixDescription) {
191193
try {
192194
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
193-
IResource resource = project;
194-
if (filePath != null && !filePath.isEmpty()) {
195-
IPath path = new Path(filePath);
196-
if (filePath.startsWith(project.getName())) {
197-
path = path.removeFirstSegments(1);
198-
}
199-
IFile file = project.getFile(path);
200-
if (file != null && file.exists()) {
201-
resource = file;
195+
if (project != null && project.isAccessible()) {
196+
IResource resource = project;
197+
if (filePath != null && !filePath.isEmpty()) {
198+
IPath path = new Path(filePath);
199+
if (filePath.startsWith(project.getName())) {
200+
path = path.removeFirstSegments(1);
201+
}
202+
IFile file = project.getFile(path);
203+
if (file != null && file.exists()) {
204+
resource = file;
205+
}
202206
}
207+
final IMarker marker = resource.createMarker(MARKER_TYPE);
208+
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
209+
marker.setAttribute(IMarker.LINE_NUMBER, 1);
210+
marker.setAttribute(IMarker.MESSAGE, message);
211+
if (quickFixId != null && !quickFixId.isEmpty()) {
212+
marker.setAttribute(CONNECTION_URL, mcConnection.baseUrl.toString());
213+
marker.setAttribute(PROJECT_ID, projectID);
214+
marker.setAttribute(QUICK_FIX_ID, quickFixId);
215+
marker.setAttribute(QUICK_FIX_DESCRIPTION, quickFixDescription);
216+
}
203217
}
204-
final IMarker marker = resource.createMarker(MARKER_TYPE);
205-
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
206-
marker.setAttribute(IMarker.LINE_NUMBER, 1);
207-
marker.setAttribute(IMarker.MESSAGE, message);
208-
if (quickFixId != null && !quickFixId.isEmpty()) {
209-
marker.setAttribute(CONNECTION_URL, mcConnection.baseUrl.toString());
210-
marker.setAttribute(PROJECT_ID, projectID);
211-
marker.setAttribute(QUICK_FIX_ID, quickFixId);
212-
marker.setAttribute(QUICK_FIX_DESCRIPTION, quickFixDescription);
213-
}
214218
} catch (CoreException e) {
215219
MCLogger.logError("Failed to create a marker for the " + name + " application: " + message, e);
216220
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ public class MicroclimateApplication {
3030
public final String contextRoot; // can be null
3131
public final IPath fullLocalPath;
3232
public final ProjectType projectType;
33-
public final String containerId;
3433

34+
3535
private StartMode startMode;
3636
private AppState appState;
3737
private BuildStatus buildStatus;
3838
private String buildDetails;
3939
private boolean autoBuild = true;
4040
private boolean enabled = true;
41+
private String containerId;
4142

4243
// Must be updated whenever httpPort changes. Can be null
4344
private URL baseUrl;
@@ -47,14 +48,13 @@ public class MicroclimateApplication {
4748
private int httpPort = -1, debugPort = -1;
4849

4950
MicroclimateApplication(MicroclimateConnection mcConnection,
50-
String id, String name, ProjectType projectType, String pathInWorkspace, String containerId, String contextRoot)
51+
String id, String name, ProjectType projectType, String pathInWorkspace, String contextRoot)
5152
throws MalformedURLException {
5253

5354
this.mcConnection = mcConnection;
5455
this.projectID = id;
5556
this.name = name;
5657
this.projectType = projectType;
57-
this.containerId = containerId;
5858
this.contextRoot = contextRoot;
5959
this.host = mcConnection.baseUrl.getHost();
6060

@@ -110,6 +110,10 @@ public synchronized void setEnabled(boolean enabled) {
110110
this.enabled = enabled;
111111
}
112112

113+
public synchronized void setContainerId(String id) {
114+
this.containerId = id;
115+
}
116+
113117
// Getters for our project state fields
114118

115119
/**
@@ -150,6 +154,10 @@ public synchronized boolean isAutoBuild() {
150154
public synchronized boolean isEnabled() {
151155
return enabled;
152156
}
157+
158+
public synchronized String getContainerId() {
159+
return containerId;
160+
}
153161

154162
public boolean isActive() {
155163
return getAppState() == AppState.STARTING || getAppState() == AppState.STARTED;
@@ -164,7 +172,7 @@ public boolean isSupportedProject() {
164172
}
165173

166174
public boolean hasBuildLog() {
167-
return (projectType != ProjectType.NODE);
175+
return (!projectType.isType(ProjectType.TYPE_NODE));
168176
}
169177

170178
public synchronized void setHttpPort(int httpPort) {

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static MicroclimateApplication createApp(MicroclimateConnection mcConnect
7373
String name = appJso.getString(MCConstants.KEY_NAME);
7474
String id = appJso.getString(MCConstants.KEY_PROJECT_ID);
7575

76-
ProjectType type = ProjectType.UNKNOWN;
76+
ProjectType type = ProjectType.UNKNOWN_TYPE;
7777
try {
7878
// from portal: projectType and buildType are equivalent - however
7979
// buildType is always present, projectType is missing for disabled/stopped projects
@@ -82,22 +82,21 @@ public static MicroclimateApplication createApp(MicroclimateConnection mcConnect
8282
// String typeStr = appJso.getString(MCConstants.KEY_PROJECT_TYPE);
8383

8484
String typeStr = appJso.getString(MCConstants.KEY_BUILD_TYPE);
85-
type = ProjectType.fromInternalType(typeStr);
85+
String languageStr = appJso.getString(MCConstants.KEY_LANGUAGE);
86+
type = new ProjectType(typeStr, languageStr);
8687
}
8788
catch(JSONException e) {
8889
MCLogger.logError(e.getMessage() + " in: " + appJso); //$NON-NLS-1$
8990
}
9091

9192
String loc = appJso.getString(MCConstants.KEY_LOC_DISK);
9293

93-
String containerId = appJso.getString(MCConstants.KEY_CONTAINER_ID);
94-
9594
String contextRoot = null;
9695
if(appJso.has(MCConstants.KEY_CONTEXTROOT)) {
9796
contextRoot = appJso.getString(MCConstants.KEY_CONTEXTROOT);
9897
}
9998

100-
MicroclimateApplication mcApp = MicroclimateObjectFactory.createMicroclimateApplication(mcConnection, id, name, type, loc, containerId, contextRoot);
99+
MicroclimateApplication mcApp = MicroclimateObjectFactory.createMicroclimateApplication(mcConnection, id, name, type, loc, contextRoot);
101100

102101
updateApp(mcApp, appJso);
103102
return mcApp;
@@ -132,6 +131,13 @@ public static void updateApp(MicroclimateApplication mcApp, JSONObject appJso) {
132131
mcApp.setBuildStatus(buildStatus, detail);
133132
}
134133

134+
// Get the container id
135+
String containerId = null;
136+
if (appJso.has(MCConstants.KEY_CONTAINER_ID)) {
137+
containerId = appJso.getString(MCConstants.KEY_CONTAINER_ID);
138+
}
139+
mcApp.setContainerId(containerId);
140+
135141
// Get the ports if they are available
136142
try {
137143
if (appJso.has(MCConstants.KEY_PORTS) && (appJso.get(MCConstants.KEY_PORTS) instanceof JSONObject)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public static MicroclimateConnection createMicroclimateConnection(URI uri) throw
2121
}
2222

2323
public static MicroclimateApplication createMicroclimateApplication(MicroclimateConnection mcConnection,
24-
String id, String name, ProjectType projectType, String pathInWorkspace, String containerId, String contextRoot) throws Exception {
25-
return new MCEclipseApplication(mcConnection, id, name, projectType, pathInWorkspace, containerId, contextRoot);
24+
String id, String name, ProjectType projectType, String pathInWorkspace, String contextRoot) throws Exception {
25+
return new MCEclipseApplication(mcConnection, id, name, projectType, pathInWorkspace, contextRoot);
2626
}
2727

2828
}

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public void requestValidate(MicroclimateApplication app) throws JSONException, I
373373

374374
JSONObject buildPayload = new JSONObject();
375375
buildPayload.put(MCConstants.KEY_PROJECT_ID, app.projectID);
376-
buildPayload.put(MCConstants.KEY_PROJECT_TYPE, app.projectType.internalType);
376+
buildPayload.put(MCConstants.KEY_PROJECT_TYPE, app.projectType.type);
377377

378378
HttpResult result = HttpUtil.post(url, buildPayload);
379379
if (!result.isGoodResponse) {
@@ -388,7 +388,7 @@ public void requestValidateGenerate(MicroclimateApplication app) throws JSONExce
388388

389389
JSONObject buildPayload = new JSONObject();
390390
buildPayload.put(MCConstants.KEY_PROJECT_ID, app.projectID);
391-
buildPayload.put(MCConstants.KEY_PROJECT_TYPE, app.projectType.internalType);
391+
buildPayload.put(MCConstants.KEY_PROJECT_TYPE, app.projectType.type);
392392
buildPayload.put(MCConstants.KEY_AUTO_GENERATE, true);
393393

394394
HttpResult result = HttpUtil.post(url, buildPayload);
@@ -444,14 +444,11 @@ public String toPrefsString() {
444444

445445
public void requestProjectCreate(ProjectType type, String name)
446446
throws JSONException, IOException {
447-
switch(type) {
448-
case LIBERTY:
447+
if (type.isType(ProjectType.TYPE_LIBERTY)) {
449448
requestMicroprofileProjectCreate(name);
450-
break;
451-
case SPRING:
449+
} else if (type.isType(ProjectType.TYPE_SPRING)) {
452450
requestSpringProjectCreate(name);
453-
break;
454-
default:
451+
} else {
455452
MCLogger.log("Creation of projects with type " + type + " is not supported.");
456453
}
457454
}
@@ -502,7 +499,7 @@ public IPath getWorkspacePath() {
502499
}
503500

504501
public boolean supportsProjectType(ProjectType type) {
505-
return type == ProjectType.LIBERTY ||
506-
(type == ProjectType.SPRING && mcVersion >= MCConstants.SPRING_MC_VERSION);
502+
return type.isType(ProjectType.TYPE_LIBERTY) ||
503+
(type.isType(ProjectType.TYPE_SPRING) && mcVersion >= MCConstants.SPRING_MC_VERSION);
507504
}
508505
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,13 @@ private void onProjectChanged(JSONObject event) throws JSONException {
250250
}
251251

252252
app.setEnabled(true);
253+
254+
// Update container id
255+
String containerId = null;
256+
if (event.has(MCConstants.KEY_CONTAINER_ID)) {
257+
containerId = event.getString(MCConstants.KEY_CONTAINER_ID);
258+
}
259+
app.setContainerId(containerId);
253260

254261
// Update ports
255262
JSONObject portsObj = event.getJSONObject(MCConstants.KEY_PORTS);

dev/com.ibm.microclimate.core/src/com/ibm/microclimate/core/internal/constants/ProjectType.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,34 @@
99

1010
package com.ibm.microclimate.core.internal.constants;
1111

12-
import com.ibm.microclimate.core.internal.MCLogger;
13-
14-
public enum ProjectType {
15-
16-
LIBERTY("liberty", "Microprofile", true),
17-
SPRING("spring", "Spring", true),
18-
NODE("nodejs", "Node.js", false),
19-
SWIFT("swift", "Swift", false),
20-
DOCKER("docker", "Docker", false),
21-
UNKNOWN("unknown", "Unknown", false);
22-
23-
public final String internalType, userFriendlyType;
24-
public final boolean isDebuggable;
25-
26-
private ProjectType(String internalType, String userFriendlyType, boolean isDebuggable) {
27-
this.internalType = internalType;
28-
this.userFriendlyType = userFriendlyType;
29-
this.isDebuggable = isDebuggable;
12+
public class ProjectType {
13+
14+
public static final String UNKNOWN = "unknown";
15+
16+
public static final String TYPE_LIBERTY = "liberty";
17+
public static final String TYPE_SPRING = "spring";
18+
public static final String TYPE_NODE = "nodejs";
19+
public static final String TYPE_DOCKER = "docker";
20+
21+
public static final String LANGUAGE_JAVA = "java";
22+
public static final String LANGUAGE_PYTHON = "python";
23+
24+
public static final ProjectType UNKNOWN_TYPE = new ProjectType(UNKNOWN, UNKNOWN);
25+
26+
public final String type;
27+
public final String language;
28+
29+
public ProjectType(String type, String language) {
30+
this.type = type;
31+
this.language = language;
3032
}
31-
32-
public static ProjectType fromInternalType(String internalType) {
33-
for (ProjectType type : ProjectType.values()) {
34-
if (type.internalType.equals(internalType)) {
35-
return type;
36-
}
37-
}
38-
MCLogger.logError("Unknown internal project type " + internalType);
39-
return ProjectType.UNKNOWN;
33+
34+
public boolean isType(String type) {
35+
return type != null && type.equals(this.type);
36+
}
37+
38+
public boolean isLanguage(String language) {
39+
return language != null && language.equals(this.language);
4040
}
41+
4142
}

dev/com.ibm.microclimate.test/src/com/ibm/microclimate/test/LibertyDebugTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class LibertyDebugTest extends BaseDebugTest {
66

77
static {
88
projectName = "libertydebugtest";
9-
projectType = ProjectType.LIBERTY;
9+
projectType = new ProjectType(ProjectType.TYPE_LIBERTY, ProjectType.LANGUAGE_JAVA);
1010
relativeURL = "/v1/example";
1111
srcPath = "src/main/java/application/rest/v1/Example.java";
1212
currentText = "Congratulations";

dev/com.ibm.microclimate.test/src/com/ibm/microclimate/test/SpringDebugTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class SpringDebugTest extends BaseDebugTest {
66

77
static {
88
projectName = "springdebugtest";
9-
projectType = ProjectType.SPRING;
9+
projectType = new ProjectType(ProjectType.TYPE_SPRING, ProjectType.LANGUAGE_JAVA);
1010
relativeURL = "/v1";
1111
srcPath = "src/main/java/application/rest/v1/Example.java";
1212
currentText = "Congratulations";

dev/com.ibm.microclimate.ui/src/com/ibm/microclimate/ui/internal/actions/ContainerShellAction.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.ibm.microclimate.core.internal.MCLogger;
2525
import com.ibm.microclimate.core.internal.MicroclimateApplication;
26+
import com.ibm.microclimate.core.internal.constants.ProjectType;
2627

2728
public class ContainerShellAction implements IObjectActionDelegate {
2829

@@ -50,7 +51,7 @@ public void selectionChanged(IAction action, ISelection selection) {
5051
Object obj = sel.getFirstElement();
5152
if (obj instanceof MicroclimateApplication) {
5253
app = (MicroclimateApplication)obj;
53-
action.setEnabled(app.isEnabled());
54+
action.setEnabled(app.isEnabled() && app.getContainerId() != null);
5455
return;
5556
}
5657
}
@@ -65,7 +66,7 @@ public void run(IAction action) {
6566
return;
6667
}
6768

68-
if (app.containerId == null || app.containerId.isEmpty()) {
69+
if (app.getContainerId() == null) {
6970
MCLogger.logError("ContainerShellAction ran but the container id for the application is not set: " + app.name); //$NON-NLS-1$
7071
return;
7172
}
@@ -75,12 +76,17 @@ public void run(IAction action) {
7576
MCLogger.logError("ContainerShellAction ran but the local terminal laucher delegate is null"); //$NON-NLS-1$
7677
return;
7778
}
79+
80+
String command = "bash";
81+
if (app.projectType.isType(ProjectType.TYPE_DOCKER) && app.projectType.isLanguage(ProjectType.LANGUAGE_PYTHON)) {
82+
command = "sh";
83+
}
7884

7985
Map<String, Object> properties = new HashMap<>();
8086
properties.put(ITerminalsConnectorConstants.PROP_DELEGATE_ID, delegate.getId());
8187
properties.put(ITerminalsConnectorConstants.PROP_TITLE, app.name);
8288
properties.put(ITerminalsConnectorConstants.PROP_PROCESS_PATH, "docker");
83-
properties.put(ITerminalsConnectorConstants.PROP_PROCESS_ARGS, "exec -it " + app.containerId + " bash");
89+
properties.put(ITerminalsConnectorConstants.PROP_PROCESS_ARGS, "exec -it " + app.getContainerId() + " " + command);
8490
delegate.execute(properties, null);
8591
}
8692

0 commit comments

Comments
 (0)