| author | pmcmahan | 2012-10-24 12:53:50 (EDT) |
|---|---|---|
| committer | pmcmahan | 2012-10-24 12:53:50 (EDT) |
| commit | c82563a8d6fd546076fcf684015bb0b1b77a451a (patch) (side-by-side diff) | |
| tree | 8241cafeeaf775e01c882a7395dbc257c21dcbaf | |
| parent | 82542c689f9dd86f481b64cfa21ebcd71e180077 (diff) | |
| download | org.eclipse.lyo.client-c82563a8d6fd546076fcf684015bb0b1b77a451a.zip org.eclipse.lyo.client-c82563a8d6fd546076fcf684015bb0b1b77a451a.tar.gz org.eclipse.lyo.client-c82563a8d6fd546076fcf684015bb0b1b77a451a.tar.bz2 | |
Add support for canceled automation requests
Change-Id: I73d45f46aeb844dd0db5f38de3612f3790467fa8
3 files changed, 131 insertions, 12 deletions
diff --git a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/automation/AutomationAdapter.java b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/automation/AutomationAdapter.java index 1956f6b..a139c91 100644 --- a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/automation/AutomationAdapter.java +++ b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/automation/AutomationAdapter.java @@ -504,14 +504,14 @@ public class AutomationAdapter extends AbstractResource implements IConstants { if (result != null) { + completeRequest(request); + populateResultFromRequest(request, result); saveResult(result); } - completeRequest(request); - } if (!isStopped) { @@ -584,9 +584,14 @@ public class AutomationAdapter extends AbstractResource implements IConstants { * @param request * @throws URISyntaxException * @throws AutomationException + * @throws OAuthException + * @throws IOException */ private void completeRequest(AutomationRequest request) - throws AutomationException { + throws AutomationException, IOException, OAuthException, + URISyntaxException { + + assertNotCanceled(request); request.setStates(new URI[] { URI.create( AutomationConstants.STATE_COMPLETE) }); @@ -945,6 +950,8 @@ public class AutomationAdapter extends AbstractResource implements IConstants { "The adapter has not logged into the server."); } + + assertNotCanceled(request); URI scriptURI = (URI) request.getExtendedProperties().get( PROPERTY_QM_EXECUTES_TEST_SCRIPT); @@ -1121,9 +1128,11 @@ public class AutomationAdapter extends AbstractResource implements IConstants { * @throws AutomationException * @throws URISyntaxException * @throws IOException + * @throws OAuthException */ public URI uploadAttachment(File file, AutomationRequest request) - throws AutomationException, URISyntaxException, IOException { + throws AutomationException, URISyntaxException, IOException, + OAuthException { if (client == null) { @@ -1131,6 +1140,8 @@ public class AutomationAdapter extends AbstractResource implements IConstants { "The adapter has not logged into the server."); } + + assertNotCanceled(request); URI attachmentUploadUrl = (URI) request.getExtendedProperties().get( PROPERTY_RQM_UPLOAD_ATTACHMENT_URL); @@ -1194,9 +1205,12 @@ public class AutomationAdapter extends AbstractResource implements IConstants { * @param request * @throws URISyntaxException * @throws AutomationException + * @throws OAuthException + * @throws IOException */ public void sendProgressForRequest(int i, AutomationRequest request) - throws URISyntaxException, AutomationException { + throws URISyntaxException, AutomationException, IOException, + OAuthException { if (!isRegistered) { @@ -1204,6 +1218,8 @@ public class AutomationAdapter extends AbstractResource implements IConstants { "Adapter is not registered with the Automation Service Provider"); } + + assertNotCanceled(request); // don't allow progress to surpass 99 until the request has completed if (i > 99) { @@ -1250,9 +1266,12 @@ public class AutomationAdapter extends AbstractResource implements IConstants { * @param request * @throws URISyntaxException * @throws AutomationException + * @throws OAuthException + * @throws IOException */ public void sendStatusForRequest(StatusResponse statusResponse, - AutomationRequest request) throws AutomationException { + AutomationRequest request) throws AutomationException, IOException, + OAuthException, URISyntaxException { if (!isRegistered) { @@ -1261,6 +1280,8 @@ public class AutomationAdapter extends AbstractResource implements IConstants { } + assertNotCanceled(request); + AutomationRequest requestCopy = new AutomationRequest(request.getAbout()); requestCopy.getExtendedProperties().put(PROPERTY_RQM_STATUS_RESPONSE, @@ -1301,9 +1322,12 @@ public class AutomationAdapter extends AbstractResource implements IConstants { * @param request * @throws URISyntaxException * @throws AutomationException + * @throws OAuthException + * @throws IOException */ public void sendMessageForRequest(Message message, AutomationRequest request) - throws AutomationException { + throws AutomationException, IOException, OAuthException, + URISyntaxException { if (!isRegistered) { @@ -1312,6 +1336,8 @@ public class AutomationAdapter extends AbstractResource implements IConstants { } + assertNotCanceled(request); + AutomationRequest requestCopy = new AutomationRequest(request.getAbout()); requestCopy.getExtendedProperties().put(PROPERTY_RQM_MESSAGE, @@ -1394,6 +1420,45 @@ public class AutomationAdapter extends AbstractResource implements IConstants { } } + + /** + * Checks to see if the AutomationRequest's current state at the Service + * Provider indicates that the request has been canceled. + * + * @param request + * @throws AutomationRequestCanceledException + * if the Request has been canceled. + * @throws IOException + * @throws OAuthException + * @throws URISyntaxException + */ + private void assertNotCanceled(AutomationRequest request) + throws AutomationRequestCanceledException, IOException, + OAuthException, URISyntaxException { + + URI selectUri = appendOslcProperties(request.getAbout(), + "oslc_auto:state"); + + AutomationRequest requestAtServiceProvider; + + synchronized (client) { + + requestAtServiceProvider = client.getResource( + selectUri.toString(), OslcMediaType.APPLICATION_RDF_XML) + .getEntity(AutomationRequest.class); + + } + + // oslc_auto:state is defined as one-or-many in the specification + URI stateUri = requestAtServiceProvider.getStates()[0]; + + if (URI.create(AutomationConstants.STATE_CANCELED).equals(stateUri)) { + + throw new AutomationRequestCanceledException(request); + + } + + } /** * A runnable which can be used by a Thread created by the program diff --git a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/automation/AutomationRequestCanceledException.java b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/automation/AutomationRequestCanceledException.java new file mode 100644 index 0000000..fa155b9 --- a/dev/null +++ b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/automation/AutomationRequestCanceledException.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2012 IBM Corporation. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v. 1.0 which accompanies this distribution. + * + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * + * Paul McMahan <pmcmahan@us.ibm.com> - initial implementation + *******************************************************************************/ +package org.eclipse.lyo.client.oslc.samples.automation; + +import org.eclipse.lyo.client.oslc.resources.AutomationRequest; + +/** + * An exception thrown when an AutomationRequest has been canceled + */ +public class AutomationRequestCanceledException extends AutomationException { + + private final AutomationRequest request; + + private static final long serialVersionUID = 1L; + + public AutomationRequestCanceledException(AutomationRequest request) { + super(); + this.request = request; + } + + public AutomationRequest getCanceledRequest() { + return request; + } + +} diff --git a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/automation/RQMAutomationSample.java b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/automation/RQMAutomationSample.java index 96be6ef..21c23c5 100644 --- a/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/automation/RQMAutomationSample.java +++ b/org.eclipse.lyo.samples.clients/src/main/java/org/eclipse/lyo/client/oslc/samples/automation/RQMAutomationSample.java @@ -16,6 +16,7 @@ package org.eclipse.lyo.client.oslc.samples.automation; import java.io.File; +import java.io.IOException; import java.lang.Thread.UncaughtExceptionHandler; import java.net.URI; import java.net.URISyntaxException; @@ -28,6 +29,8 @@ import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import net.oauth.OAuthException; + import org.eclipse.lyo.client.oslc.resources.AutomationConstants; import org.eclipse.lyo.client.oslc.resources.AutomationRequest; import org.eclipse.lyo.client.oslc.resources.AutomationResult; @@ -181,6 +184,19 @@ public class RQMAutomationSample implements IConstants, IAutomationRequestHandle // update progress indication adapter.sendProgressForRequest(99, request); + logger.info("Returning a result with verdict " + + result.getVerdicts()[0]); + + + } catch (AutomationRequestCanceledException e) { + + logger.info("Automation Request \"" + + e.getCanceledRequest().getTitle() + "\" was canceled."); + + // clean up any resources created for test execution here + + result = null; + } catch (Exception e) { // cancel the request since it could not be completed @@ -190,9 +206,6 @@ public class RQMAutomationSample implements IConstants, IAutomationRequestHandle } - logger.info("Returning a result with verdict " - + result.getVerdicts()[0]); - return result; } @@ -203,12 +216,15 @@ public class RQMAutomationSample implements IConstants, IAutomationRequestHandle * @param inputParameters * @throws InterruptedException * @throws AutomationException + * @throws URISyntaxException + * @throws OAuthException + * @throws IOException */ private void executeScript(Document script, ParameterInstance[] inputParameters, AutomationAdapter adapter, AutomationRequest request) throws InterruptedException, - AutomationException { - + AutomationException, IOException, OAuthException, + URISyntaxException { String scriptTitle = script.getDocumentElement() .getElementsByTagNameNS(NAMESPACE_URI_DC_ELEMENTS, "title") |

