List of usage examples for java.util.concurrent.atomic AtomicReference get
public final V get()
From source file:com.datamelt.nifi.processors.ExecuteRuleEngine.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { // map used to store the attribute name and its value from the content of the flow file final Map<String, String> propertyMap = new HashMap<>(); // get a logger instance final ComponentLog logger = getLogger(); // a header from the content if present final AtomicReference<HeaderRow> header = new AtomicReference<>(); AtomicBoolean error = new AtomicBoolean(); // get the flow file FlowFile flowFile = session.get();/* w w w . j av a2 s .c o m*/ if (flowFile == null) { return; } // list of rows from splitting the original flow file content ArrayList<RuleEngineRow> flowFileRows = new ArrayList<RuleEngineRow>(); // list of rows containing the detailed results of the ruleengine ArrayList<RuleEngineRow> flowFileDetails = new ArrayList<RuleEngineRow>(); boolean headerPresent = context.getProperty(ATTRIBUTE_HEADER_PRESENT).getValue().equals("true"); // put the name of the ruleengine zip file in the list of properties propertyMap.put(PROPERTY_RULEENGINE_ZIPFILE_NAME, context.getProperty(ATTRIBUTE_RULEENGINE_ZIPFILE).getValue()); final int batchSize = Integer.parseInt(context.getProperty(BATCH_SIZE_NAME).getValue()); // read flow file into input stream session.read(flowFile, new InputStreamCallback() { public void process(InputStream in) throws IOException { try { // iterator over the lines from the input stream LineIterator iterator = IOUtils.lineIterator(in, "utf-8"); // check if configuration indicates that a header row is present in the flow file content if (headerPresent) { logger.debug("configuration indicates a header row is present in flow file content"); // if there is at least one row of data and the header is not defined yet if (iterator.hasNext() && header.get() == null) { // set the header from the content header.set(new HeaderRow(iterator.nextLine(), separator)); } } // if no header row is present in the flow file content else { logger.debug("configuration indicates no header row is present in flow file content"); // use the header from the field names header.set(headerFromFieldNames); } // loop over all rows of data while (iterator.hasNext()) { // we handle the error per row of data error.set(false); // get a row to process String row = iterator.nextLine(); // check that we have data if (row != null && !row.trim().equals("")) { RowFieldCollection rowFieldCollection = null; try { rowFieldCollection = getRowFieldCollection(row, header.get()); logger.debug("RowFieldCollection header contains: " + rowFieldCollection.getHeader().getNumberOfFields() + " fields"); logger.debug("RowFieldCollection contains: " + rowFieldCollection.getNumberOfFields() + " fields"); // run the ruleengine with the given data from the flow file logger.debug("running business ruleengine..."); // run the business logic/rules against the data ruleEngine.run("flowfile", rowFieldCollection); // add some debugging output that might be useful logger.debug("number of rulegroups: " + ruleEngine.getNumberOfGroups()); logger.debug( "number of rulegroups passed: " + ruleEngine.getNumberOfGroupsPassed()); logger.debug( "number of rulegroups failed: " + ruleEngine.getNumberOfGroupsFailed()); logger.debug( "number of rulegroups skipped: " + ruleEngine.getNumberOfGroupsSkipped()); logger.debug("number of rules: " + ruleEngine.getNumberOfRules()); logger.debug("number of rules passed: " + ruleEngine.getNumberOfRulesPassed()); logger.debug("number of rules failed: " + ruleEngine.getNumberOfRulesFailed()); logger.debug("number of actions: " + ruleEngine.getNumberOfActions()); // add some properties of the ruleengine execution to the map addRuleEngineProperties(propertyMap); } catch (Exception ex) { error.set(true); logger.error(ex.getMessage(), ex); } // if no error occurred we create a save the data for the creation of the flow files if (!error.get()) { // process only if the collection of fields was changed by // a ruleengine action. this means the data was updated so // we will have to re-write/re-create the flow file content. if (rowFieldCollection.isCollectionUpdated()) { // put an indicator that the data was modified by the ruleengine propertyMap.put(PROPERTY_RULEENGINE_CONTENT_MODIFIED, "true"); logger.debug( "data was modified - updating flow file content with ruleengine results"); // the RuleEngineRow instance will contain the row of data and the map of properties // and will later be used when the flow files are created flowFileRows .add(new RuleEngineRow(getResultRow(rowFieldCollection), propertyMap)); } else { // put an indicator that the data was NOT modified by the ruleengine propertyMap.put(PROPERTY_RULEENGINE_CONTENT_MODIFIED, "false"); logger.debug("data was not modified - using original content"); // the RuleEngineRow instance will contain the row of data and the map of properties // and will later be used when the flow files are created flowFileRows.add(new RuleEngineRow(row, propertyMap)); } if (flowFileRows.size() >= batchSize) { // generate flow files from the individual rows List<FlowFile> splitFlowFiles = generateFlowFileSplits(context, session, flowFileRows, header.get(), headerPresent); // transfer all individual rows to success relationship if (splitFlowFiles.size() > 0) { session.transfer(splitFlowFiles, SUCCESS); } } // if the user configured detailed results if (context.getProperty(ATTRIBUTE_OUTPUT_DETAILED_RESULTS).getValue() .equals("true")) { // get the configured output type String outputType = context.getProperty(ATTRIBUTE_OUTPUT_DETAILED_RESULTS_TYPE) .getValue(); logger.debug("configuration set to output detailed results with type [" + outputType + "]"); // we need to create a flow file only, if the ruleengine results are according to the output type settings if (outputType.equals(OUTPUT_TYPE_ALL_GROUPS_ALL_RULES) || (outputType.equals(OUTPUT_TYPE_FAILED_GROUPS_ALL_RULES) && ruleEngine.getNumberOfGroupsFailed() > 0) || (outputType.equals(OUTPUT_TYPE_FAILED_GROUPS_FAILED_RULES) && ruleEngine.getNumberOfGroupsFailed() > 0) || (outputType.equals(OUTPUT_TYPE_FAILED_GROUPS_PASSED_RULES) && ruleEngine.getNumberOfGroupsFailed() > 0) || (outputType.equals(OUTPUT_TYPE_PASSED_GROUPS_ALL_RULES) && ruleEngine.getNumberOfGroupsPassed() > 0) || (outputType.equals(OUTPUT_TYPE_PASSED_GROUPS_FAILED_RULES) && ruleEngine.getNumberOfGroupsPassed() > 0 || (outputType.equals(OUTPUT_TYPE_PASSED_GROUPS_PASSED_RULES) && ruleEngine.getNumberOfGroupsPassed() > 0))) { // create the content for the flow file String content = getFlowFileRuleEngineDetailsContent(header.get(), headerPresent, outputType, row); // add results to the list flowFileDetails.add(new RuleEngineRow(content, propertyMap)); if (flowFileDetails.size() >= batchSize) { List<FlowFile> detailsFlowFiles = generateFlowFilesRuleEngineDetails( context, session, flowFileDetails, header.get(), headerPresent); // transfer all individual rows to detailed relationship if (detailsFlowFiles.size() > 0) { session.transfer(detailsFlowFiles, DETAILED_RESULTS); } } } } // clear the collections of ruleengine results ruleEngine.getRuleExecutionCollection().clear(); } // if we have an error we create a flow file from the current row of data and send it to the failure relationsship else { FlowFile failureFlowFile = generateFailureFlowFile(context, session, row, header.get(), headerPresent); session.transfer(failureFlowFile, FAILURE); } } } LineIterator.closeQuietly(iterator); } catch (Exception ex) { ex.printStackTrace(); logger.error("error running the business ruleengine", ex); } } }); // generate flow files from the individual rows List<FlowFile> splitFlowFiles = generateFlowFileSplits(context, session, flowFileRows, header.get(), headerPresent); // generate flow files from the individual rows List<FlowFile> detailsFlowFiles = generateFlowFilesRuleEngineDetails(context, session, flowFileDetails, header.get(), headerPresent); // transfer the original flow file session.transfer(flowFile, ORIGINAL); // transfer all individual rows to success relationship if (splitFlowFiles.size() > 0) { session.transfer(splitFlowFiles, SUCCESS); } // transfer all individual rows to success relationship if (detailsFlowFiles.size() > 0) { session.transfer(detailsFlowFiles, DETAILED_RESULTS); } }
From source file:org.dataconservancy.packaging.tool.integration.PackageGenerationTest.java
@Test public void verifyRemediationTest() throws Exception { PackageState state = initializer.initialize(DCS_PROFILE); Set<URI> originalFileLocations = new HashSet<>(); ipm2rdf.transformToNode(state.getPackageTree()) .walk(node -> originalFileLocations.add(node.getFileInfo().getLocation())); // The package should contain two files: // - READMX/*from w w w . j ava2 s. c o m*/ // - READM // // The file with the acute E will be remediated to a resource named 'READMX', which will collide with // an existing resource of the same name. // assert that our sample problem files are in the content to be packaged assertTrue(originalFileLocations.stream().anyMatch(uri -> uri.getPath().endsWith("READMX"))); // 0x0301 is the UTF-16 encoding of the 'COMBINING ACUTE ACCENT' combining diacritic // 0x00c9 is the UTF-16 encoding of 'LATIN CAPITAL LETTER E WITH ACUTE' assertTrue(originalFileLocations.stream().anyMatch(uri -> (uri.getPath().endsWith("README" + '\u0301')) || (uri.getPath().endsWith("READM" + '\u00c9')))); OpenedPackage opened = packager.createPackage(state, folder.getRoot()); AtomicBoolean foundIllegal = new AtomicBoolean(Boolean.FALSE); AtomicBoolean foundRemediated = new AtomicBoolean(Boolean.FALSE); AtomicReference<String> remediatedFilename = new AtomicReference<>(); AtomicBoolean foundCollision = new AtomicBoolean(Boolean.FALSE); AtomicReference<String> collidingFilename = new AtomicReference<>(); // Walk the generated package, and make sure that // 1. That a resource with illegal characters does not exist // 2. That a resource named 'READMX' does exist // 3. That a resource named after the SHA-1 hex of its identifier exists // 4. That those two resources originate from two different files in the original package content opened.getPackageTree().walk(node -> { if (node.getFileInfo() == null || !node.getFileInfo().isFile()) { return; } System.err.println(node.getFileInfo().getName()); System.err.println(" " + node.getFileInfo().getLocation().toString()); // this should not happen, because a file name with invalid characters should have // been remediated prior to being inserted into the package if (node.getFileInfo().getLocation().getPath().endsWith("README" + '\u0301') || node.getFileInfo().getLocation().getPath().endsWith("READM" + '\u00c9')) { foundIllegal.set(Boolean.TRUE); } if (node.getFileInfo().getLocation().getPath().endsWith(shaHex(node.getIdentifier().toString()))) { foundRemediated.set(Boolean.TRUE); remediatedFilename.set(node.getFileInfo().getName()); // short circuit return; } if (node.getFileInfo().getName().equals("READMX") || node.getFileInfo().getName().equals("READM")) { foundCollision.set(Boolean.TRUE); collidingFilename.set(node.getFileInfo().getName()); } }); assertFalse(foundIllegal.get()); assertTrue(foundCollision.get()); assertTrue(foundRemediated.get()); assertNotNull(remediatedFilename.get()); assertNotNull(collidingFilename.get()); assertNotEquals(remediatedFilename.get(), collidingFilename.get()); }
From source file:de.codesourcery.eve.skills.market.impl.EveCentralMarketDataProvider.java
@Override public Map<InventoryType, PriceInfoQueryResult> getPriceInfos(final MarketFilter filter, final IPriceQueryCallback callback, final InventoryType... items) throws PriceInfoUnavailableException { if (LOG.isDebugEnabled()) { LOG.debug("getPriceInfos(): filter = " + filter + ", items = " + items); }/*ww w .j a va2 s . c o m*/ if (ArrayUtils.isEmpty(items)) { return Collections.emptyMap(); } final AtomicReference<Map<InventoryType, PriceInfoQueryResult>> resultHolder = new AtomicReference<Map<InventoryType, PriceInfoQueryResult>>( new ConcurrentHashMap<InventoryType, PriceInfoQueryResult>()); final IUpdateStrategy updateStrategy = createUpdateStrategy(filter.getUpdateMode(), filter.getOrderType()); final Vector<NameValuePair> params = new Vector<NameValuePair>(); /* * NEEDS to be run on the EDT since Hibernate * lazy-fetching might kick in and * the Hibernate session is confined to the EDT. */ runOnEDT(new Runnable() { @Override public void run() { if (LOG.isDebugEnabled()) { LOG.debug("getPriceInfos(): update_strategy = " + updateStrategy); } for (InventoryType t : items) { // make sure we don't query over and over // for prices that are unavailable anyway if (isPriceMissingOnEveCentral(filter, t)) { if (!mayQueryAgainForMissingPrice(filter, t)) { if (LOG.isDebugEnabled()) { LOG.debug("getPriceInfos(): " + "Price for " + t + " " + "unavailable on eve-central , filter " + filter); } continue; } if (LOG.isDebugEnabled()) { LOG.debug("getPriceInfos(): [ retrying ] " + "Price for " + t + " " + "unavailable on eve-central , filter " + filter); } } final PriceInfoQueryResult cached = getCachedEntry(filter, t); resultHolder.get().put(t, cached); if (LOG.isDebugEnabled()) { if (cached.isEmpty()) { LOG.debug("getPriceInfos(): [ NOT CACHED ] type = " + t.getId() + " , name = " + t.getName()); } else { LOG.debug("getPriceInfos(): [ CACHE HIT ] " + cached); } } final boolean requiresUpdate; switch (filter.getOrderType()) { case BUY: requiresUpdate = updateStrategy.requiresUpdate(t, cached.hasBuyPrice() ? cached.buyPrice() : null); break; case SELL: requiresUpdate = updateStrategy.requiresUpdate(t, cached.hasSellPrice() ? cached.sellPrice() : null); break; case ANY: requiresUpdate = (updateStrategy.requiresUpdate(t, cached.hasBuyPrice() ? cached.buyPrice() : null) || updateStrategy.requiresUpdate(t, cached.hasSellPrice() ? cached.sellPrice() : null)); break; default: throw new RuntimeException("Unhandled switch/case: " + filter.getOrderType()); } if (LOG.isDebugEnabled()) { LOG.debug("getPriceInfos(): [ " + updateStrategy + "] requires_update => " + requiresUpdate + " , type=" + t.getName()); } if (requiresUpdate) { params.add(new BasicNameValuePair("typeid", t.getId().toString())); } } } }); if (params.isEmpty() || isOfflineMode()) { // all entries served from cache return resultHolder.get(); } addFilterToRequest(params, filter); /* * Query data from eve central */ final String responseXmlFromServer = eveCentralClient.sendRequestToServer(params); final AtomicReference<String> xml = new AtomicReference<String>(responseXmlFromServer); /* * NEEDS to be run on the EDT since Hibernate * lazy-fetching might kick in and * the Hibernate session is confined to the EDT. */ return runOnEventThread(new PriceCallable() { public Map<InventoryType, PriceInfoQueryResult> call() throws PriceInfoUnavailableException { final Map<InventoryType, PriceInfoQueryResult> realResult = resultHolder.get(); final Map<Long, List<PriceInfo>> result = parsePriceInfo(filter, xml.get()); // group prices by item types List<PriceInfo> updated = new ArrayList<>(); try { for (InventoryType type : items) { List<PriceInfo> info = result.get(type.getId()); if (info == null || info.isEmpty()) { // failed to fetch data, query user rememberPriceMissingOnEveCentral(filter, type); info = queryPriceFromUser(filter, callback, type); } forgetPriceMissingOnEveCentral(filter, type); for (PriceInfo dataFromServer : info) { dataFromServer.setRegion(filter.getRegion()); dataFromServer.setTimestamp(new EveDate(systemClock)); dataFromServer.setInventoryType(type); final PriceInfoQueryResult cachedResult = realResult.get(type); if (LOG.isDebugEnabled()) { LOG.debug("getPriceInfos(): from server: " + dataFromServer + " , cached=" + cachedResult); } PriceInfo existing; switch (filter.getOrderType()) { case BUY: existing = cachedResult.hasBuyPrice() ? cachedResult.buyPrice() : null; if (updateStrategy.requiresUpdate(type, existing)) { LOG.debug("getPriceInfos(): merging buy price."); realResult.put(type, cachedResult.merge(filter.getOrderType(), dataFromServer)); storeCacheEntry(dataFromServer); updated.add(dataFromServer); } break; case SELL: existing = cachedResult.hasSellPrice() ? cachedResult.sellPrice() : null; if (updateStrategy.requiresUpdate(type, existing)) { LOG.debug("getPriceInfos(): merging sell price."); realResult.put(type, cachedResult.merge(filter.getOrderType(), dataFromServer)); storeCacheEntry(dataFromServer); updated.add(dataFromServer); } break; case ANY: existing = cachedResult.hasBuyPrice() ? cachedResult.buyPrice() : null; if (updateStrategy.requiresUpdate(type, existing)) { LOG.debug("getPriceInfos(): merging buy price."); realResult.put(type, cachedResult.merge(PriceInfo.Type.BUY, dataFromServer)); storeCacheEntry(dataFromServer); updated.add(dataFromServer); } existing = cachedResult.hasSellPrice() ? cachedResult.sellPrice() : null; if (updateStrategy.requiresUpdate(type, existing)) { LOG.debug("getPriceInfos(): merging sell price."); realResult.put(type, cachedResult.merge(PriceInfo.Type.SELL, dataFromServer)); storeCacheEntry(dataFromServer); updated.add(dataFromServer); } break; default: throw new RuntimeException("Unhandled switch/case: " + filter.getOrderType()); } } } } finally { fireItemPriceChanged(updated); } return realResult; } }); }
From source file:com.ccserver.digital.service.LOSService.java
public LosResponseDTO submitCreditCardDocument(Long creditCardId) { AtomicReference<Object> refError = new AtomicReference<Object>(""); CreditCardApplication app = repository.findOne(creditCardId); LosResponseDTO losResponseDTO = new LosResponseDTO(); if (app == null || !Arrays.asList(new Status[] { Status.SubmittedApp, Status.Processing, Status.No_wf_initiated }) .contains(app.getStatus())) { losResponseDTO.setValidationError(new ValidationErrorDTO(Constants.APPLICATION_FIELD_INVALID, "The application status is invalid!")); return losResponseDTO; }//w w w .j av a2s . c om if (app.getSubmittedDocTime() != null) { losResponseDTO.setValidationError(new ValidationErrorDTO(Constants.MODIFYING_SUBMITTED_DOCUMENT, "The application's document submitted already")); return losResponseDTO; } List<CreditCardApplicationDocumentThumbNailDTO> entities = docService .getDocumentsThumbNailByApplicationId(creditCardId); if (CollectionUtils.isEmpty(entities)) { losResponseDTO.setValidationError( new ValidationErrorDTO(Constants.OBJECT_REQUIRED, "The application's documents are required!")); return losResponseDTO; } configLos = utilService.getConfigurations(); String messageId = java.util.UUID.randomUUID().toString(); com.vpbank.entity.vn.ba251.bd324.documentservice.configuredocumenthandlingoperationcreditcard._1.ObjectFactory objectFactory = new com.vpbank.entity.vn.ba251.bd324.documentservice.configuredocumenthandlingoperationcreditcard._1.ObjectFactory(); JAXBElement<ConfigureDocumentHandlingOperationCreditCardRequestType> jaxbRequest = objectFactory .createConfigureDocumentHandlingOperationCreditCardRequest( getRequestOfDoc(app, entities, messageId, refError)); if (refError.get().toString() != "") { losResponseDTO.setValidationError( new ValidationErrorDTO(Constants.FILE_SIZE_LIMIT_ERROR, "File Size is exceeded!")); return losResponseDTO; } Object result = null; try { result = new ExternalGatewaySupport(LOS_DOCUMENT_PACKAGE).getWebServiceTemplate() .marshalSendAndReceive(losConfig.getUrlDoc(), jaxbRequest, null); } catch (Exception ex) { logger.error(Utils.getErrorFormatLog("LOSService", "submitCreditCardDocument", "", "Can not call webservice", losConfig.getUrlDoc(), ex)); } if (result != null) { ConfigureDocumentHandlingOperationCreditCardResponseType resultResponse = ((JAXBElement<ConfigureDocumentHandlingOperationCreditCardResponseType>) result) .getValue(); String status = resultResponse.getResponseStatus().getStatus(); losResponseDTO.setStatus(status); ApplicationLOSDTO losApplicationDTO = mapper.applicationLOSToDTO(app.getApplicationLOS()); losApplicationDTO.setMessageDocId(messageId); if (status.equals("0")) { losApplicationDTO.setDocErrorCode(StringUtils.EMPTY); losApplicationDTO.setDocErrorMessage(StringUtils.EMPTY); CreditCardApplicationDTO dto = applicationService.updateLosDocStatus(app.getId(), Status.Processing, losApplicationDTO); losResponseDTO.setAppDTO(dto); } else { losApplicationDTO.setDocErrorCode(resultResponse.getResponseStatus().getGlobalErrorCode()); losApplicationDTO.setDocErrorMessage(getErrorDescription(resultResponse.getResponseStatus())); applicationService.updateLosDocStatus(app.getId(), null, losApplicationDTO); } losResponseDTO.setResponseStatusType(resultResponse.getResponseStatus()); return losResponseDTO; } losResponseDTO.setValidationError( new ValidationErrorDTO(Constants.APPLICATION_DOC_UNEXPECT_ERROR, "Unexpect error!")); return losResponseDTO; }
From source file:android.webkit.cts.WebViewTest.java
private Picture waitForPictureToHaveColor(int color, final TestPictureListener listener) throws Throwable { final int MAX_ON_NEW_PICTURE_ITERATIONS = 5; final AtomicReference<Picture> pictureRef = new AtomicReference<Picture>(); for (int i = 0; i < MAX_ON_NEW_PICTURE_ITERATIONS; i++) { final int oldCallCount = listener.callCount; runTestOnUiThread(new Runnable() { @Override/*from ww w.jav a2 s . co m*/ public void run() { pictureRef.set(mWebView.capturePicture()); } }); if (isPictureFilledWithColor(pictureRef.get(), color)) break; new PollingCheck(TEST_TIMEOUT) { @Override protected boolean check() { return listener.callCount > oldCallCount; } }.run(); } return pictureRef.get(); }
From source file:brooklyn.util.internal.ssh.sshj.SshjTool.java
/** * Executes the script in the background (`nohup ... &`), and then executes other ssh commands to poll for the * stdout, stderr and exit code of that original process (which will each have been written to separate files). * /*from w ww . ja v a 2s . com*/ * The polling is a "long poll". That is, it executes a long-running ssh command to retrieve the stdout, etc. * If that long-poll command fails, then we just execute another one to pick up from where it left off. * This means we do not need to execute many ssh commands (which are expensive), but can still return promptly * when the command completes. * * Much of this was motivated by https://issues.apache.org/jira/browse/BROOKLYN-106, which is no longer * an issue. The retries (e.g. in the upload-script) are arguably overkill given that {@link #acquire(SshAction)} * will already retry. However, leaving this in place as it could prove useful when working with flakey * networks in the future. * * TODO There are (probably) issues with this method when using {@link ShellTool#PROP_RUN_AS_ROOT}. * I (Aled) saw the .pid file having an owner of root:root, and a failure message in stderr of: * -bash: line 3: /tmp/brooklyn-20150113-161203056-XMEo-move_install_dir_from_user_to_.pid: Permission denied */ protected int execScriptAsyncAndPoll(final Map<String, ?> props, final List<String> commands, final Map<String, ?> env) { return new ToolAbstractAsyncExecScript(props) { private int maxConsecutiveSshFailures = 3; private Duration maxDelayBetweenPolls = Duration.seconds(20); private Duration pollTimeout = getOptionalVal(props, PROP_EXEC_ASYNC_POLLING_TIMEOUT, Duration.FIVE_MINUTES); private int iteration = 0; private int consecutiveSshFailures = 0; private int stdoutCount = 0; private int stderrCount = 0; private Stopwatch timer; public int run() { timer = Stopwatch.createStarted(); final String scriptContents = toScript(props, commands, env); if (LOG.isTraceEnabled()) LOG.trace("Running shell command at {} as async script: {}", host, scriptContents); // Upload script; try repeatedly because have seen timeout intermittently on vcloud-director (BROOKLYN-106 related). boolean uploadSuccess = Repeater .create("async script upload on " + SshjTool.this.toString() + " (for " + getSummary() + ")") .backoffTo(maxDelayBetweenPolls).limitIterationsTo(3).rethrowException() .until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { iteration++; if (LOG.isDebugEnabled()) { String msg = "Uploading (iteration=" + iteration + ") for async script on " + SshjTool.this.toString() + " (for " + getSummary() + ")"; if (iteration == 1) { LOG.trace(msg); } else { LOG.debug(msg); } } copyToServer(ImmutableMap.of("permissions", "0700"), scriptContents.getBytes(), scriptPath); return true; } }).run(); if (!uploadSuccess) { // Unexpected! Should have either returned true or have rethrown the exception; should never get false. String msg = "Unexpected state: repeated failure for async script upload on " + SshjTool.this.toString() + " (" + getSummary() + ")"; LOG.warn(msg + "; rethrowing"); throw new IllegalStateException(msg); } // Execute script asynchronously int execResult = asInt(acquire(new ShellAction(buildRunScriptCommand(), out, err, execTimeout)), -1); if (execResult != 0) return execResult; // Long polling to get the status try { final AtomicReference<Integer> result = new AtomicReference<Integer>(); boolean success = Repeater .create("async script long-poll on " + SshjTool.this.toString() + " (for " + getSummary() + ")") .backoffTo(maxDelayBetweenPolls).limitTimeTo(execTimeout) .until(new Callable<Boolean>() { @Override public Boolean call() throws Exception { iteration++; if (LOG.isDebugEnabled()) LOG.debug("Doing long-poll (iteration=" + iteration + ") for async script to complete on " + SshjTool.this.toString() + " (for " + getSummary() + ")"); Integer exitstatus = longPoll(); result.set(exitstatus); return exitstatus != null; } }).run(); if (!success) { // Timed out String msg = "Timeout for async script to complete on " + SshjTool.this.toString() + " (" + getSummary() + ")"; LOG.warn(msg + "; rethrowing"); throw new TimeoutException(msg); } return result.get(); } catch (Exception e) { LOG.debug("Problem polling for async script on " + SshjTool.this.toString() + " (for " + getSummary() + "); rethrowing after deleting temporary files", e); throw Exceptions.propagate(e); } finally { // Delete the temporary files created (and the `tail -c` commands that might have been left behind by long-polls). // Using pollTimeout so doesn't wait forever, but waits for a reasonable (configurable) length of time. // TODO also execute this if the `buildRunScriptCommand` fails, as that might have left files behind? try { int execDeleteResult = asInt( acquire(new ShellAction(deleteTemporaryFilesCommand(), out, err, pollTimeout)), -1); if (execDeleteResult != 0) { LOG.debug("Problem deleting temporary files of async script on " + SshjTool.this.toString() + " (for " + getSummary() + "): exit status " + execDeleteResult); } } catch (Exception e) { Exceptions.propagateIfFatal(e); LOG.debug("Problem deleting temporary files of async script on " + SshjTool.this.toString() + " (for " + getSummary() + "); continuing", e); } } } Integer longPoll() throws IOException { // Long-polling to get stdout, stderr + exit status of async task. // If our long-poll disconnects, we will just re-execute. // We wrap the stdout/stderr so that we can get the size count. // If we disconnect, we will pick up from that char of the stream. // TODO Additional stdout/stderr written by buildLongPollCommand() could interfere, // causing us to miss some characters. Duration nextPollTimeout = Duration.min(pollTimeout, Duration.millis(execTimeout.toMilliseconds() - timer.elapsed(TimeUnit.MILLISECONDS))); CountingOutputStream countingOut = (out == null) ? null : new CountingOutputStream(out); CountingOutputStream countingErr = (err == null) ? null : new CountingOutputStream(err); List<String> pollCommand = buildLongPollCommand(stdoutCount, stderrCount, nextPollTimeout); Duration sshJoinTimeout = nextPollTimeout.add(Duration.TEN_SECONDS); ShellAction action = new ShellAction(pollCommand, countingOut, countingErr, sshJoinTimeout); int longPollResult; try { longPollResult = asInt(acquire(action, 3, nextPollTimeout), -1); } catch (RuntimeTimeoutException e) { if (LOG.isDebugEnabled()) LOG.debug("Long-poll timed out on " + SshjTool.this.toString() + " (for " + getSummary() + "): " + e); return null; } stdoutCount += (countingOut == null) ? 0 : countingOut.getCount(); stderrCount += (countingErr == null) ? 0 : countingErr.getCount(); if (longPollResult == 0) { if (LOG.isDebugEnabled()) LOG.debug("Long-poll succeeded (exit status 0) on " + SshjTool.this.toString() + " (for " + getSummary() + ")"); return longPollResult; // success } else if (longPollResult == -1) { // probably a connection failure; try again if (LOG.isDebugEnabled()) LOG.debug("Long-poll received exit status -1; will retry on " + SshjTool.this.toString() + " (for " + getSummary() + ")"); return null; } else if (longPollResult == 125) { // 125 is the special code for timeout in long-poll (see buildLongPollCommand). // However, there is a tiny chance that the underlying command might have returned that exact exit code! // Don't treat a timeout as a "consecutiveSshFailure". if (LOG.isDebugEnabled()) LOG.debug("Long-poll received exit status " + longPollResult + "; most likely timeout; retrieving actual status on " + SshjTool.this.toString() + " (for " + getSummary() + ")"); return retrieveStatusCommand(); } else { // want to double-check whether this is the exit-code from the async process, or // some unexpected failure in our long-poll command. if (LOG.isDebugEnabled()) LOG.debug("Long-poll received exit status " + longPollResult + "; retrieving actual status on " + SshjTool.this.toString() + " (for " + getSummary() + ")"); Integer result = retrieveStatusCommand(); if (result != null) { return result; } } consecutiveSshFailures++; if (consecutiveSshFailures > maxConsecutiveSshFailures) { LOG.warn("Aborting on " + consecutiveSshFailures + " consecutive ssh connection errors (return -1) when polling for async script to complete on " + SshjTool.this.toString() + " (" + getSummary() + ")"); return -1; } else { LOG.info("Retrying after ssh connection error when polling for async script to complete on " + SshjTool.this.toString() + " (" + getSummary() + ")"); return null; } } Integer retrieveStatusCommand() throws IOException { // want to double-check whether this is the exit-code from the async process, or // some unexpected failure in our long-poll command. ByteArrayOutputStream statusOut = new ByteArrayOutputStream(); ByteArrayOutputStream statusErr = new ByteArrayOutputStream(); int statusResult = asInt( acquire(new ShellAction(buildRetrieveStatusCommand(), statusOut, statusErr, execTimeout)), -1); if (statusResult == 0) { // The status we retrieved really is valid; return it. // TODO How to ensure no additional output in stdout/stderr when parsing below? String statusOutStr = new String(statusOut.toByteArray()).trim(); if (Strings.isEmpty(statusOutStr)) { // suggests not yet completed; will retry with long-poll if (LOG.isDebugEnabled()) LOG.debug( "Long-poll retrieved status directly; command successful but no result available on " + SshjTool.this.toString() + " (for " + getSummary() + ")"); return null; } else { if (LOG.isDebugEnabled()) LOG.debug("Long-poll retrieved status directly; returning '" + statusOutStr + "' on " + SshjTool.this.toString() + " (for " + getSummary() + ")"); int result = Integer.parseInt(statusOutStr); return result; } } else if (statusResult == -1) { // probably a connection failure; try again with long-poll if (LOG.isDebugEnabled()) LOG.debug("Long-poll retrieving status directly received exit status -1; will retry on " + SshjTool.this.toString() + " (for " + getSummary() + ")"); return null; } else { if (out != null) { out.write(toUTF8ByteArray( "retrieving status failed with exit code " + statusResult + " (stdout follow)")); out.write(statusOut.toByteArray()); } if (err != null) { err.write(toUTF8ByteArray( "retrieving status failed with exit code " + statusResult + " (stderr follow)")); err.write(statusErr.toByteArray()); } if (LOG.isDebugEnabled()) LOG.debug("Long-poll retrieving status failed; returning " + statusResult + " on " + SshjTool.this.toString() + " (for " + getSummary() + ")"); return statusResult; } } }.run(); }
From source file:com.example.app.profile.ui.company.CompanyValueEditor.java
@Override public void init() { VTCropPictureEditorConfig webLogoConfig = _companyConfig.companyWebLogoConfig(); _webLogoEditor = new VTCropPictureEditor(webLogoConfig); _webLogoEditor.addClassName("company-web-logo"); _webLogoEditor.setDefaultResource(_appUtil.getDefaultResourceImage()); VTCropPictureEditorConfig emailLogoConfig = _companyConfig.companyEmailLogoConfig(); _emailLogoEditor = new VTCropPictureEditor(emailLogoConfig); _emailLogoEditor.addClassName("company-web-logo"); _emailLogoEditor.setDefaultResource(_appUtil.getDefaultResourceImage()); super.init(); Label webLogoInstructions = new Label(createText(INSTRUCTIONS_PICTURE_EDITOR_FMT(), webLogoConfig.getCropWidth(), webLogoConfig.getCropHeight())); webLogoInstructions.addClassName(CSS_INSTRUCTIONS); webLogoInstructions.withHTMLElement(HTMLElement.div); Label emailLogoInstructions = new Label(createText(INSTRUCTIONS_PICTURE_EDITOR_FMT(), emailLogoConfig.getCropWidth(), emailLogoConfig.getCropHeight())); emailLogoInstructions.addClassName(CSS_INSTRUCTIONS); emailLogoInstructions.withHTMLElement(HTMLElement.div); add(of("logos", of("prop", LABEL_WEB_LOGO(), _webLogoEditor, webLogoInstructions), of("prop", LABEL_EMAIL_LOGO(), _emailLogoEditor, emailLogoInstructions))); CommonEditorFields.addNameEditor(this); addEditorForProperty(() -> {//from w ww . j a v a2 s .c om final CompositeValueEditor<Location> editor = new CompositeValueEditor<>(Location.class); editor.addEditorForProperty(() -> { AddressValueEditorConfig cfg = new AddressValueEditorConfig(); return new AddressValueEditor(cfg); }, Location.ADDRESS_PROP); // editor.addEditorForProperty(() -> { // EmailAddressValueEditorConfig cfg = new EmailAddressValueEditorConfig(); // return new EmailAddressValueEditor(cfg); // }, Location.EMAIL_ADDRESS_PROP); editor.addEditorForProperty(() -> { PhoneNumberValueEditorConfig cfg = new PhoneNumberValueEditorConfig(); return new PhoneNumberValueEditor(cfg); }, Location.PHONE_NUMBER_PROP); return editor; }, Company.PRIMARY_LOCATION_PROP); addEditorForProperty(() -> { final URLEditor editor = new URLEditor(LABEL_WEBSITE(), null); editor.addClassName("website"); return editor; }, ce -> stringToURL(ce.getWebsiteLink(), null), (ce, url) -> ce.setWebsiteLink(urlToString(url))); if (_editMode == AbstractCompanyPropertyEditor.EditMode.StandardCompany) { final String superdomainName = _appUtil.getSite().getDefaultHostname().getName(); final AtomicReference<TextEditor> domainNameEditor = new AtomicReference<>(); final AtomicReference<Container> inputInstructionsRef = new AtomicReference<>(); final AtomicReference<Container> customDomainInstructionsRef = new AtomicReference<>(); final AtomicReference<Label> superdomainLabelRef = new AtomicReference<>(); Function<String, String> convertDomainUIValue = val -> { if (!StringFactory.isEmptyString(val)) { String converted = val; if (converted.endsWith('.' + superdomainName)) converted = converted.replace('.' + superdomainName, ""); converted = HOSTNAME_VALIDITY_PATTERN1.matcher(converted).replaceAll("-"); converted = HOSTNAME_VALIDITY_PATTERN2.matcher(converted).replaceAll("").toLowerCase(); return converted; } return val; }; addEditorForProperty(() -> { final TextEditor editor = new TextEditor(LABEL_SUB_DOMAIN(), null); final Container inputInstructions = _uiHelper .createInputInstructions(INSTRUCTIONS_SUB_DOMAIN(_terms.company())); final Container customDomainInstructions = _uiHelper .createInputInstructions(INSTRUCTIONS_CUSTOM_DOMAIN(_terms.company())); final Label superdomainNameLabel = new Label(createText('.' + superdomainName), span, "super-domain-name"); editor.moveToTop(customDomainInstructions); editor.moveToTop(inputInstructions); editor.moveToTop(editor.getLabel()); editor.add(editor.getValueComponent()); editor.add(superdomainNameLabel); editor.getValueComponent().addPropertyChangeListener(Field.PROP_TEXT, evt -> { if (editor.isEditable()) { String uiValue = editor.getValueComponent().getText(); editor.getValueComponent().setText(convertDomainUIValue.apply(uiValue)); } }); editor.setRequiredValueValidator(); domainNameEditor.set(editor); inputInstructionsRef.set(inputInstructions); customDomainInstructionsRef.set(customDomainInstructions); superdomainLabelRef.set(superdomainNameLabel); return editor; }, ce -> { final TextEditor editor = domainNameEditor.get(); String cehostname = ce.getHostname().getName(); if (cehostname == null) cehostname = ""; domainNameEditor.get().setEditable( !(!StringFactory.isEmptyString(cehostname) && !cehostname.endsWith('.' + superdomainName))); if (editor.isEditable()) { cehostname = convertDomainUIValue.apply(cehostname.replace('.' + superdomainName, "")); } inputInstructionsRef.get().setVisible(editor.isEditable()); superdomainLabelRef.get().setVisible(editor.isEditable()); customDomainInstructionsRef.get().setVisible(!editor.isEditable()); return cehostname; }, (ce, value) -> { if (domainNameEditor.get().isEditable()) ce.getHostname().setName(String.join(".", value, superdomainName)); else ce.getHostname().setName(ce.getHostname().getName()); }); } addEditorForProperty(() -> { final URLEditor editor = new URLEditor(LABEL_LINKEDIN(), null); editor.addClassName("linkedin"); return editor; }, company -> stringToURL(company.getLinkedInLink(), null), (company, url) -> company.setLinkedInLink(urlToString(url))); addEditorForProperty(() -> { final URLEditor editor = new URLEditor(LABEL_TWITTER(), null); editor.addClassName("twitter"); return editor; }, company -> stringToURL(company.getTwitterLink(), null), (company, url) -> company.setTwitterLink(urlToString(url))); addEditorForProperty(() -> { final URLEditor editor = new URLEditor(LABEL_FACEBOOK(), null); editor.addClassName("facebook"); return editor; }, company -> stringToURL(company.getFacebookLink(), null), (company, url) -> company.setFacebookLink(urlToString(url))); }
From source file:org.apache.servicemix.http.ProviderEndpointTest.java
public void testSoap() throws Exception { final AtomicReference<String> soapAction = new AtomicReference<String>(); EchoComponent echo = new EchoComponent(); echo.setService(new QName("http://servicemix.apache.org/samples/wsdl-first", "EchoService")); echo.setEndpoint("service"); container.activateComponent(echo, "echo"); HttpComponent http = new HttpComponent(); HttpConsumerEndpoint ep0 = new HttpConsumerEndpoint(); ep0.setService(new QName("http://servicemix.apache.org/samples/wsdl-first", "PersonService")); ep0.setEndpoint("consumer"); ep0.setTargetService(new QName("http://servicemix.apache.org/samples/wsdl-first", "EchoService")); ep0.setTargetEndpoint("service"); ep0.setLocationURI("http://localhost:8192/PersonService/"); ep0.setMarshaler(new DefaultHttpConsumerMarshaler() { public MessageExchange createExchange(HttpServletRequest request, ComponentContext context) throws Exception { soapAction.set(request.getHeader("SOAPAction")); return super.createExchange(request, context); //To change body of overridden methods use File | Settings | File Templates. }/*w ww .ja v a 2 s. co m*/ }); HttpSoapProviderEndpoint ep1 = new HttpSoapProviderEndpoint(); ep1.setService(new QName("http://servicemix.apache.org/samples/wsdl-first", "PersonService")); ep1.setEndpoint("soap"); ep1.setWsdl(new ClassPathResource("person.wsdl")); ep1.setValidateWsdl(false); // TODO: Soap 1.2 not handled yet ep1.setUseJbiWrapper(true); http.setEndpoints(new HttpEndpointType[] { ep0, ep1 }); container.activateComponent(http, "http"); container.start(); ServiceMixClient client = new DefaultServiceMixClient(container); InOut me = client.createInOutExchange(); me.setService(new QName("http://servicemix.apache.org/samples/wsdl-first", "PersonService")); me.setOperation(new QName("http://servicemix.apache.org/samples/wsdl-first", "GetPerson")); me.getInMessage().setContent( new StringSource("<jbi:message xmlns:jbi=\"http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper\"" + " xmlns:msg=\"http://servicemix.apache.org/samples/wsdl-first/types\" " + " name=\"Hello\" " + " type=\"msg:HelloRequest\" " + " version=\"1.0\">" + " <jbi:part>" + " <msg:GetPerson><msg:personId>id</msg:personId></msg:GetPerson>" + " </jbi:part>" + "</jbi:message>")); client.sendSync(me); client.done(me); assertEquals("\"urn:myaction\"", soapAction.get()); }
From source file:info.archinnov.achilles.test.integration.tests.AsyncQueryIT.java
@Test public void should_return_rows_for_native_query_async() throws Exception { CompleteBean entity1 = builder().randomId().name("DuyHai").age(35L).addFriends("foo", "bar") .addFollowers("George", "Paul").addPreference(1, "FR").addPreference(2, "Paris") .addPreference(3, "75014").version(CounterBuilder.incr(15L)).buid(); CompleteBean entity2 = builder().randomId().name("John DOO").age(35L).addFriends("qux", "twix") .addFollowers("Isaac", "Lara").addPreference(1, "US").addPreference(2, "NewYork") .version(CounterBuilder.incr(17L)).buid(); asyncManager.insert(entity1).getImmediately(); asyncManager.insert(entity2).getImmediately(); final RegularStatement statement = select("name", "age_in_years", "friends", "followers", "preferences") .from("CompleteBean").where(eq("id", bindMarker("id"))); final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Object> successSpy = new AtomicReference<>(); FutureCallback<Object> successCallBack = new FutureCallback<Object>() { @Override/* w ww .j a v a2 s .com*/ public void onSuccess(Object result) { successSpy.getAndSet(result); latch.countDown(); } @Override public void onFailure(Throwable t) { latch.countDown(); } }; final AchillesFuture<List<TypedMap>> future1 = asyncManager.nativeQuery(statement, entity1.getId()) .get(successCallBack); final AchillesFuture<List<TypedMap>> future2 = asyncManager.nativeQuery(statement, entity2.getId()).get(); latch.await(); final List<TypedMap> typedMaps1 = future1.get(); assertThat(typedMaps1).hasSize(1); TypedMap typedMap1 = typedMaps1.get(0); while (!future2.isDone()) { Thread.sleep(2); } final List<TypedMap> typedMaps2 = future2.get(); assertThat(typedMaps2).hasSize(1); TypedMap typedMap2 = typedMaps2.get(0); assertThat(typedMap1.get("name")).isEqualTo("DuyHai"); assertThat(typedMap1.get("age_in_years")).isEqualTo(35L); assertThat(typedMap1.<List<String>>getTyped("friends")).containsExactly("foo", "bar"); assertThat(typedMap1.<Set<String>>getTyped("followers")).contains("George", "Paul"); Map<Integer, String> preferences1 = typedMap1.getTyped("preferences"); assertThat(preferences1.get(1)).isEqualTo("FR"); assertThat(preferences1.get(2)).isEqualTo("Paris"); assertThat(preferences1.get(3)).isEqualTo("75014"); assertThat(typedMap2.get("name")).isEqualTo("John DOO"); assertThat(typedMap2.get("age_in_years")).isEqualTo(35L); assertThat(typedMap2.<List<String>>getTyped("friends")).containsExactly("qux", "twix"); assertThat(typedMap2.<Set<String>>getTyped("followers")).contains("Isaac", "Lara"); Map<Integer, String> preferences2 = typedMap2.getTyped("preferences"); assertThat(preferences2.get(1)).isEqualTo("US"); assertThat(preferences2.get(2)).isEqualTo("NewYork"); latch.await(); assertThat(successSpy.get()).isNotNull().isInstanceOf(List.class); }
From source file:com.wk.lodge.composite.web.tomcat.IntegrationCompositeTests.java
@Test public void testSync() throws Exception { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Throwable> failure = new AtomicReference<>(); URI uri = new URI("ws://localhost:" + port + "/composite"); WebSocketStompClient stompClient = new WebSocketStompClient(uri, this.headers, sockJsClient); stompClient.setMessageConverter(new MappingJackson2MessageConverter()); stompClient.connect(new StompMessageHandler() { private StompSession stompSession; @Override// w w w. j ava 2s .c om public void afterConnected(StompSession stompSession, StompHeaderAccessor headers) { this.stompSession = stompSession; this.stompSession.subscribe("/user/queue/device", null); try { SyncMessage sync = new SyncMessage(); sync.setType("sync"); sync.setTime(new Date().getTime()); stompSession.send("/app/sync", sync); } catch (Throwable t) { failure.set(t); latch.countDown(); } } @Override public void handleMessage(Message<byte[]> message) { try { String json = parseMessageJson(message); new JsonPathExpectationsHelper("type").assertValue(json, "sync"); new JsonPathExpectationsHelper("time").exists(json); } catch (Throwable t) { failure.set(t); } finally { this.stompSession.disconnect(); latch.countDown(); } } @Override public void handleError(Message<byte[]> message) { StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message); String error = "[Producer] " + accessor.getShortLogMessage(message.getPayload()); logger.error(error); failure.set(new Exception(error)); } @Override public void handleReceipt(String receiptId) { } @Override public void afterDisconnected() { } }); if (!latch.await(10, TimeUnit.SECONDS)) { fail("Sync response not received"); } else if (failure.get() != null) { throw new AssertionError("", failure.get()); } }