protected String getResourceProperty(TxSupport txn, String pUrl, String pid, String name) { return txn.httpRequest( new int[] {HttpURLConnection.HTTP_OK, HttpURLConnection.HTTP_NO_CONTENT}, getResourceUpdateUrl(pUrl, pid, name, null).toString(), "GET", TxMediaType.PLAIN_MEDIA_TYPE); }
public static void startContainer(String txnMgrUrl, String packages, Class<?>... classes) throws Exception { TxSupport.setTxnMgrUrl(txnMgrUrl); if (USE_RESTEASY) startRestEasy(classes); else startJersey(packages); }
public Work makeWork( TxSupport txn, String baseURI, String id, String txId, String enlistUrl, boolean twoPhaseAware, boolean isVolatile, String recoveryUrl, String fault) { String linkHeader = twoPhaseAware ? txn.makeTwoPhaseAwareParticipantLinkHeader(baseURI, isVolatile, id, txId) : txn.makeTwoPhaseUnAwareParticipantLinkHeader(baseURI, isVolatile, id, txId, true); return new Work(id, txId, baseURI + '/' + id, linkHeader, enlistUrl, recoveryUrl, fault); }
/** * Modify a transactional participant * * @param txn the transaction * @param pUrl the transactional participant * @param pid an id * @param name name of a property to update * @param value the new value of the property * @return the response body */ protected String modifyResource( TxSupport txn, String pUrl, String pid, String name, String value) { // tell the resource to modify some data and pass the transaction enlistment url along with the // request return txn.httpRequest( new int[] {HttpURLConnection.HTTP_OK}, getResourceUpdateUrl(pUrl, pid, name, value).toString(), "GET", TxMediaType.PLAIN_MEDIA_TYPE); }
@SuppressWarnings({"UnusedDeclaration"}) @GET @Path("{pId}/{tId}/participant") public String getStatus( @PathParam("pId") @DefaultValue("") String pId, @PathParam("tId") @DefaultValue("") String tId) { Work work = faults.get(pId); if (work == null) throw new WebApplicationException(HttpURLConnection.HTTP_NOT_FOUND); return TxSupport.toStatusContent(work.status); }
private String moveParticipant( Work work, String nid, String register, boolean twoPhaseAware, boolean isVolatile) { TxSupport txn = new TxSupport(); faults.remove(work.id); work = makeWork( txn, PURL, nid, work.tid, work.enlistUrl, twoPhaseAware, isVolatile, work.recoveryUrl, work.fault); faults.put(nid, work); // if register is true then tell the transaction manager about the new location - otherwise // the old // URIs will be used for transaction termination. This is used to test that the coordinator // uses // the recovery URI correctly if ("true".equals(register)) { Map<String, String> reqHeaders = new HashMap<String, String>(); reqHeaders.put("Link", work.pLinks); txn.httpRequest( new int[] {HttpURLConnection.HTTP_OK}, work.recoveryUrl, "PUT", TxMediaType.POST_MEDIA_TYPE, null, null, reqHeaders); } return nid; }
@SuppressWarnings({"UnusedDeclaration"}) @PUT @Path("{pId}/{tId}/terminator") public Response terminate( @PathParam("pId") @DefaultValue("") String pId, @PathParam("tId") @DefaultValue("") String tId, String content) { TxStatus status = TxSupport.toTxStatus(content); // String status = TxSupport.getStatus(content); Work work = faults.get(pId); if (work == null) return Response.status(HttpURLConnection.HTTP_NOT_FOUND).build(); String fault = work.fault; if (status.isPrepare()) { if ("READONLY".equals(fault)) { // faults.remove(pId); work.status = TxStatus.TransactionReadOnly.name(); } else if ("PREPARE_FAIL".equals(fault)) { // faults.remove(pId); return Response.status(HttpURLConnection.HTTP_CONFLICT).build(); // throw new WebApplicationException(HttpURLConnection.HTTP_CONFLICT); } else { if ("PDELAY".equals(fault)) { try { Thread.sleep(2000); } catch (InterruptedException e) { } } work.status = TxStatus.TransactionPrepared.name(); } } else if (status.isCommit() || status.isCommitOnePhase()) { if ("H_HAZARD".equals(fault)) work.status = TxStatus.TransactionHeuristicHazard.name(); else if ("H_ROLLBACK".equals(fault)) work.status = TxStatus.TransactionHeuristicRollback.name(); else if ("H_MIXED".equals(fault)) work.status = TxStatus.TransactionHeuristicMixed.name(); else { if ("CDELAY".equals(fault)) { try { Thread.sleep(3000); } catch (InterruptedException e) { // ok } } work.status = status.isCommitOnePhase() ? TxStatus.TransactionCommittedOnePhase.name() : TxStatus.TransactionCommitted.name(); work.end(true); } } else if (status.isAbort()) { if ("H_HAZARD".equals(fault)) work.status = TxStatus.TransactionHeuristicHazard.name(); else if ("H_COMMIT".equals(fault)) work.status = TxStatus.TransactionHeuristicCommit.name(); else if ("H_MIXED".equals(fault)) work.status = TxStatus.TransactionHeuristicMixed.name(); else { if ("ADELAY".equals(fault)) { try { Thread.sleep(2000); } catch (InterruptedException e) { // ok } } work.status = TxStatus.TransactionRolledBack.name(); work.end(false); // faults.remove(pId); } } else { return Response.status(HttpURLConnection.HTTP_BAD_REQUEST).build(); // throw new WebApplicationException(HttpURLConnection.HTTP_BAD_REQUEST); } // return TxSupport.toStatusContent(work.status); return Response.ok(TxSupport.toStatusContent(work.status)).build(); }
@POST @Produces(TxMediaType.PLAIN_MEDIA_TYPE) public String enlist( @Context UriInfo info, @QueryParam("pId") @DefaultValue("") String pId, @QueryParam("fault") @DefaultValue("") String fault, @QueryParam("twoPhaseAware") @DefaultValue("true") String twoPhaseAware, @QueryParam("isVolatile") @DefaultValue("false") String isVolatile, String enlistUrl) throws IOException { Work work = faults.get(pId); TxSupport txn = new TxSupport(); String txId = enlistUrl.substring(enlistUrl.lastIndexOf('/') + 1); boolean isTwoPhaseAware = "true".equals(twoPhaseAware); boolean isVolatileParticipant = "true".equals(isVolatile); String vRegistration = null; // URI for registering with the volatile phase String vParticipantLink = null; // URI for handling pre and post 2PC phases String path = TxSupport.extractUri(info); if (work == null) { int id = ++pid; work = makeWork( txn, path, String.valueOf(id), txId, enlistUrl, isTwoPhaseAware, isVolatileParticipant, null, fault); } else { Work newWork = makeWork( txn, path, work.id, txId, enlistUrl, isTwoPhaseAware, isVolatileParticipant, null, fault); newWork.oldState = work.oldState; newWork.newState = work.newState; work = newWork; } if (enlistUrl.indexOf(',') != -1) { String[] urls = enlistUrl.split(","); if (urls.length < 2) throw new WebApplicationException(HttpURLConnection.HTTP_BAD_REQUEST); enlistUrl = urls[0]; vRegistration = urls[1]; String vParticipant = new StringBuilder(path) .append('/') .append(work.id) .append('/') .append(txId) .append('/') .append("vp") .toString(); vParticipantLink = txn.addLink2(new StringBuilder(), TxLinkNames.VOLATILE_PARTICIPANT, vParticipant, true) .toString(); } try { // enlist TestResource in the transaction as a participant work.recoveryUrl = txn.enlistParticipant(enlistUrl, work.pLinks); if (vParticipantLink != null) txn.enlistVolatileParticipant(vRegistration, vParticipantLink); } catch (HttpResponseException e) { throw new WebApplicationException(e.getActualResponse()); } work.status = TxStatus.TransactionActive.name(); work.start(); faults.put(work.id, work); return work.id; }
@SuppressWarnings({"UnusedDeclaration"}) @GET public String getBasic( @Context UriInfo info, @QueryParam("pId") @DefaultValue("") String pId, @QueryParam("context") @DefaultValue("") String ctx, @QueryParam("name") @DefaultValue("") String name, @QueryParam("value") @DefaultValue("") String value, @QueryParam("query") @DefaultValue("pUrl") String query, @QueryParam("arg") @DefaultValue("") String arg, @QueryParam("twoPhaseAware") @DefaultValue("true") String twoPhaseAware, @QueryParam("isVolatile") @DefaultValue("false") String isVolatileParticipant, @QueryParam("register") @DefaultValue("true") String register) { Work work = faults.get(pId); String res = null; boolean isVolatile = "true".equals(isVolatileParticipant); boolean isTwoPhaseAware = "true".equals(twoPhaseAware); if (name.length() != 0) { if (value.length() != 0) { if (work == null) { work = makeWork( new TxSupport(), TxSupport.extractUri(info), String.valueOf(++pid), null, null, isTwoPhaseAware, isVolatile, null, null); work.oldState.put(name, value); faults.put(work.id, work); return work.id; } work.newState.put(name, value); } if (work != null) { if ("syncCount".equals(name)) res = String.valueOf(work.syncCount); else if ("commitCnt".equals(name)) res = String.valueOf(work.commitCnt); else if ("prepareCnt".equals(name)) res = String.valueOf(work.prepareCnt); else if ("rollbackCnt".equals(name)) res = String.valueOf(work.rollbackCnt); else if ("commmitOnePhaseCnt".equals(name)) res = String.valueOf(work.commmitOnePhaseCnt); else if (work.inTxn()) res = work.newState.get(name); else res = work.oldState.get(name); } } if (work == null) throw new WebApplicationException(HttpURLConnection.HTTP_NOT_FOUND); if ("move".equals(query)) res = moveParticipant(work, arg, register, isTwoPhaseAware, isVolatile); else if ("recoveryUrl".equals(query)) res = work.recoveryUrl; else if ("status".equals(query)) res = work.status; else if (res == null) res = work.pLinks; return res; // null will generate a 204 status code (no content) }
protected String enlistResource(TxSupport txn, String pUrl) { return txn.enlistTestResource(pUrl, false); }