Skip to content

Commit 195f3eb

Browse files
committed
Update reference working with error dialog for user feedback of problems.
1 parent 8b78085 commit 195f3eb

File tree

6 files changed

+213
-46
lines changed

6 files changed

+213
-46
lines changed

src/ServiceStackEclipse/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
</extension>
5959
<extension point="org.eclipse.ui.handlers">
6060
<handler
61-
class="net.servicestack.eclipse.handlers.SampleHandler"
61+
class="net.servicestack.eclipse.handlers.UpdateReferenceHandler"
6262
commandId="net.servicestack.eclipse.commands.sampleCommand">
6363
<visibleWhen>
6464
<with

src/ServiceStackEclipse/src/net/servicestack/eclipse/handlers/SampleHandler.java

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package net.servicestack.eclipse.handlers;
2+
3+
import org.eclipse.core.resources.IFile;
4+
5+
public class UpdateReferenceCurrentSelection {
6+
7+
private static UpdateReferenceCurrentSelection instance = null;
8+
9+
private UpdateReferenceCurrentSelection() {}
10+
11+
public static UpdateReferenceCurrentSelection getInstance() {
12+
if (instance == null) {
13+
instance = new UpdateReferenceCurrentSelection();
14+
}
15+
return instance;
16+
}
17+
18+
public IFile UpdateReferenceFile;
19+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package net.servicestack.eclipse.handlers;
2+
3+
import java.io.BufferedReader;
4+
import java.io.ByteArrayInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.InputStreamReader;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.ArrayList;
11+
import java.util.HashMap;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
import net.servicestack.eclipse.nativetypes.INativeTypesHandler;
16+
import net.servicestack.eclipse.nativetypes.JavaNativeTypesHandler;
17+
18+
import org.eclipse.core.commands.AbstractHandler;
19+
import org.eclipse.core.commands.ExecutionEvent;
20+
import org.eclipse.core.commands.ExecutionException;
21+
import org.eclipse.core.resources.IFile;
22+
import org.eclipse.core.resources.IResource;
23+
import org.eclipse.core.runtime.CoreException;
24+
import org.eclipse.core.runtime.IProgressMonitor;
25+
import org.eclipse.core.runtime.IStatus;
26+
import org.eclipse.core.runtime.MultiStatus;
27+
import org.eclipse.core.runtime.OperationCanceledException;
28+
import org.eclipse.core.runtime.Status;
29+
import org.eclipse.core.runtime.jobs.Job;
30+
import org.eclipse.swt.widgets.Shell;
31+
import org.eclipse.ui.IWorkbenchWindow;
32+
import org.eclipse.ui.PlatformUI;
33+
import org.eclipse.ui.handlers.HandlerUtil;
34+
import org.eclipse.ui.progress.UIJob;
35+
import org.eclipse.ui.services.ISourceProviderService;
36+
import org.eclipse.jface.dialogs.ErrorDialog;
37+
import org.eclipse.jface.dialogs.MessageDialog;
38+
39+
/**
40+
* Our sample handler extends AbstractHandler, an IHandler base class.
41+
* @see org.eclipse.core.commands.IHandler
42+
* @see org.eclipse.core.commands.AbstractHandler
43+
*/
44+
public class UpdateReferenceHandler extends AbstractHandler {
45+
/**
46+
* The constructor.
47+
*/
48+
public UpdateReferenceHandler() {
49+
}
50+
51+
/**
52+
* the command has been executed, so extract extract the needed information
53+
* from the application context.
54+
*/
55+
public Object execute(ExecutionEvent event) throws ExecutionException {
56+
Job job = new Job("Updating ServiceStack Reference") {
57+
final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
58+
@Override
59+
protected IStatus run(IProgressMonitor monitor) {
60+
61+
monitor.beginTask("Updating ServiceStack Reference", 5);
62+
// Get the source provider service
63+
IFile fileToUpdate = UpdateReferenceCurrentSelection.getInstance().UpdateReferenceFile;
64+
if(fileToUpdate == null) {
65+
showErrorDialogOnUIThread(shell,"Unable to read ServiceStack reference from project.");
66+
return Status.CANCEL_STATUS;
67+
}
68+
String fileContents = "";
69+
try {
70+
fileContents = getStringFromInputStream(fileToUpdate.getContents());
71+
} catch (IOException e) {
72+
// TODO Auto-generated catch block
73+
e.printStackTrace();
74+
} catch (CoreException e) {
75+
// TODO Auto-generated catch block
76+
e.printStackTrace();
77+
}
78+
79+
if(fileContents == null || fileContents.length() == 0) {
80+
showErrorDialogOnUIThread(shell,"Unable to read ServiceStack reference from project.");
81+
return Status.CANCEL_STATUS;
82+
}
83+
84+
INativeTypesHandler nativeTypesHandler = new JavaNativeTypesHandler();
85+
Map<String,String> options = nativeTypesHandler.parseComments(fileContents);
86+
String baseUrl = options.get("BaseUrl");
87+
options.remove("BaseUrl");
88+
String updatedCode = "";
89+
monitor.worked(1);
90+
try {
91+
updatedCode = nativeTypesHandler.getUpdatedCode(baseUrl, options);
92+
}
93+
catch (FileNotFoundException e) {
94+
showErrorDialogOnUIThread(shell,"Failed to updated ServiceStack reference. Server responded with 404." + e.getLocalizedMessage());
95+
96+
return Status.CANCEL_STATUS;
97+
}
98+
catch (IOException e) {
99+
showErrorDialogOnUIThread(shell,"Failed to updated ServiceStack reference. " + e.getClass().getName() + " - " + e.getLocalizedMessage());
100+
101+
return Status.CANCEL_STATUS;
102+
}
103+
104+
monitor.worked(3);
105+
if(updatedCode == null || updatedCode.length() == 0) {
106+
showErrorDialogOnUIThread(shell, "Failed to updated ServiceStack reference. Response was empty. Check BaseUrl.");
107+
return Status.CANCEL_STATUS;
108+
}
109+
if(!nativeTypesHandler.isFileAServiceStackReference(updatedCode)) {
110+
showErrorDialogOnUIThread(shell, "Response from server was not a valid ServiceStack reference. Check BaseUrl.");
111+
return Status.CANCEL_STATUS;
112+
}
113+
InputStream stream = new ByteArrayInputStream(updatedCode.getBytes(StandardCharsets.UTF_8));
114+
try {
115+
fileToUpdate.setContents(stream, 0, null);
116+
fileToUpdate.refreshLocal(IResource.DEPTH_ZERO, null);
117+
} catch (CoreException e) {
118+
showErrorDialogOnUIThread(shell,"Failed to write ServiceStack reference. " + e.getLocalizedMessage());
119+
return Status.CANCEL_STATUS;
120+
}
121+
monitor.worked(1);
122+
monitor.done();
123+
return Status.OK_STATUS;
124+
}
125+
126+
};
127+
128+
job.schedule();
129+
130+
return null;
131+
}
132+
133+
private void showErrorDialogOnUIThread(final Shell shell, final String message) {
134+
UIJob uiJob = new UIJob("update servicestack reference error") {
135+
136+
@Override
137+
public IStatus runInUIThread(IProgressMonitor monitor) {
138+
MessageDialog.openError(
139+
shell,
140+
"Error",
141+
message);
142+
return Status.OK_STATUS;
143+
}
144+
145+
};
146+
uiJob.schedule();
147+
}
148+
149+
private String getStringFromInputStream(InputStream inputStream) throws IOException {
150+
InputStreamReader is = new InputStreamReader(inputStream);
151+
StringBuilder sb=new StringBuilder();
152+
BufferedReader br = new BufferedReader(is);
153+
String read = br.readLine();
154+
155+
while(read != null) {
156+
//System.out.println(read);
157+
sb.append(read);
158+
sb.append(System.lineSeparator());
159+
read =br.readLine();
160+
161+
}
162+
163+
return sb.toString();
164+
}
165+
166+
}

src/ServiceStackEclipse/src/net/servicestack/eclipse/nativetypes/JavaNativeTypesHandler.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,37 @@ public Map<String, String> parseComments(String codeOutput) {
1717
Map<String, String> result = new HashMap<>();
1818
Scanner scanner = new Scanner(codeOutput);
1919
List<String> linesOfCode = new ArrayList<>();
20-
Integer i = 0;
2120
while (scanner.hasNextLine()) {
2221
String line = scanner.nextLine();
2322
linesOfCode.add(line);
23+
if(line.startsWith("*/")) break;
24+
}
25+
scanner.close();
26+
27+
int startParamsIndex = 0;
28+
String baseUrl = null;
29+
for(String item : linesOfCode) {
30+
startParamsIndex++;
31+
if(item.startsWith("BaseUrl:")) {
32+
baseUrl = item.split(":",2)[1].trim();
33+
break;
34+
}
35+
}
36+
37+
if(!baseUrl.endsWith("/")) {
38+
baseUrl += "/";
39+
}
40+
41+
result.put("BaseUrl", baseUrl);
42+
for(int i = startParamsIndex; i < linesOfCode.size(); i++) {
2443
String configLine = linesOfCode.get(i);
25-
if (!configLine.startsWith("//") && configLine.contains(":")) {
44+
if(!configLine.startsWith("//") && configLine.contains(":")) {
2645
String[] keyVal = configLine.split(":");
27-
result.put(keyVal[0].substring(2).trim(), keyVal[1].trim());
46+
result.put(keyVal[0],keyVal[1].trim());
2847
}
29-
if (line.startsWith("*/")) break;
30-
i++;
3148
}
32-
scanner.close();
49+
50+
3351
return result;
3452
}
3553

src/ServiceStackEclipse/src/net/servicestack/eclipse/testers/SelectionTester.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.io.InputStream;
66
import java.io.InputStreamReader;
77

8+
import net.servicestack.eclipse.handlers.UpdateReferenceCurrentSelection;
9+
810
import org.eclipse.core.expressions.PropertyTester;
911
import org.eclipse.core.resources.IFile;
1012
import org.eclipse.core.resources.IProject;
@@ -55,6 +57,7 @@ public boolean test(Object receiver, String property, Object[] args, Object expe
5557
for(int i = 0; i < 10; i++) {
5658
String line = br.readLine();
5759
if(line.startsWith("/* Options:")) {
60+
UpdateReferenceCurrentSelection.getInstance().UpdateReferenceFile = file;
5861
return true;
5962
}
6063
}

0 commit comments

Comments
 (0)