List of usage examples for com.google.gson.stream JsonReader JsonReader
public JsonReader(Reader in)
From source file:org.apache.nifi.toolkit.zkmigrator.ZooKeeperMigrator.java
License:Apache License
void writeZooKeeper(InputStream zkData, AuthMode authMode, byte[] authData, boolean ignoreSource, boolean useExistingACL) throws IOException, ExecutionException, InterruptedException { // ensure that the chroot path exists ZooKeeper zooKeeperRoot = getZooKeeper(Joiner.on(',').join(zooKeeperEndpointConfig.getServers()), authMode, authData);// www . j a v a 2 s. com ensureNodeExists(zooKeeperRoot, zooKeeperEndpointConfig.getPath(), CreateMode.PERSISTENT); closeZooKeeper(zooKeeperRoot); ZooKeeper zooKeeper = getZooKeeper(zooKeeperEndpointConfig.getConnectString(), authMode, authData); JsonReader jsonReader = new JsonReader(new BufferedReader(new InputStreamReader(zkData))); Gson gson = new GsonBuilder().create(); jsonReader.beginArray(); // determine source ZooKeeperEndpointConfig for this data final ZooKeeperEndpointConfig sourceZooKeeperEndpointConfig = gson.fromJson(jsonReader, ZooKeeperEndpointConfig.class); LOGGER.info("Source data was obtained from ZooKeeper: {}", sourceZooKeeperEndpointConfig); Preconditions.checkArgument( !Strings.isNullOrEmpty(sourceZooKeeperEndpointConfig.getConnectString()) && !Strings.isNullOrEmpty(sourceZooKeeperEndpointConfig.getPath()) && sourceZooKeeperEndpointConfig.getServers() != null && sourceZooKeeperEndpointConfig.getServers().size() > 0, "Source ZooKeeper %s from %s is invalid", sourceZooKeeperEndpointConfig, zkData); Preconditions.checkArgument( Collections .disjoint(zooKeeperEndpointConfig.getServers(), sourceZooKeeperEndpointConfig.getServers()) || !zooKeeperEndpointConfig.getPath().equals(sourceZooKeeperEndpointConfig.getPath()) || ignoreSource, "Source ZooKeeper config %s for the data provided can not contain the same server and path as the configured destination ZooKeeper config %s", sourceZooKeeperEndpointConfig, zooKeeperEndpointConfig); // stream through each node read from the json input final Stream<DataStatAclNode> stream = StreamSupport .stream(new Spliterators.AbstractSpliterator<DataStatAclNode>(0, 0) { @Override public boolean tryAdvance(Consumer<? super DataStatAclNode> action) { try { // stream each DataStatAclNode from configured json file synchronized (jsonReader) { if (jsonReader.hasNext()) { action.accept(gson.fromJson(jsonReader, DataStatAclNode.class)); return true; } else { return false; } } } catch (IOException e) { throw new RuntimeException("unable to read nodes from json", e); } } }, false); final List<CompletableFuture<Stat>> writeFutures = stream.parallel().map(node -> { /* * create stage to determine the acls that should be applied to the node. * this stage will be used to initialize the chain */ final CompletableFuture<List<ACL>> determineACLStage = CompletableFuture .supplyAsync(() -> determineACLs(node, authMode, useExistingACL)); /* * create stage to apply acls to nodes and transform node to DataStatAclNode object */ final Function<List<ACL>, CompletableFuture<DataStatAclNode>> transformNodeStage = acls -> CompletableFuture .supplyAsync(() -> transformNode(node, acls)); /* * create stage to ensure that nodes exist for the entire path of the zookeeper node, must be invoked after the transformNode stage to * ensure that the node will exist after path migration */ final Function<DataStatAclNode, CompletionStage<String>> ensureNodeExistsStage = dataStatAclNode -> CompletableFuture .supplyAsync(() -> ensureNodeExists(zooKeeper, dataStatAclNode.getPath(), dataStatAclNode.getEphemeralOwner() == 0 ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL)); /* * create stage that waits for both the transformNode and ensureNodeExists stages complete, and also provides that the given transformed node is * available to the next stage */ final BiFunction<String, DataStatAclNode, DataStatAclNode> combineEnsureNodeAndTransferNodeStage = (u, dataStatAclNode) -> dataStatAclNode; /* * create stage to transmit the node to the destination zookeeper endpoint, must be invoked after the node has been transformed and its path * has been created (or already exists) in the destination zookeeper */ final Function<DataStatAclNode, CompletionStage<Stat>> transmitNodeStage = dataStatNode -> CompletableFuture .supplyAsync(() -> transmitNode(zooKeeper, dataStatNode)); /* * submit the stages chained together in the proper order to perform the processing on the given node */ final CompletableFuture<DataStatAclNode> dataStatAclNodeCompletableFuture = determineACLStage .thenCompose(transformNodeStage); return dataStatAclNodeCompletableFuture.thenCompose(ensureNodeExistsStage) .thenCombine(dataStatAclNodeCompletableFuture, combineEnsureNodeAndTransferNodeStage) .thenCompose(transmitNodeStage); }).collect(Collectors.toList()); CompletableFuture<Void> allWritesFuture = CompletableFuture .allOf(writeFutures.toArray(new CompletableFuture[writeFutures.size()])); final CompletableFuture<List<Stat>> finishedWrites = allWritesFuture .thenApply(v -> writeFutures.stream().map(CompletableFuture::join).collect(Collectors.toList())); final List<Stat> writesDone = finishedWrites.get(); if (LOGGER.isInfoEnabled()) { final int writeCount = writesDone.size(); LOGGER.info("{} {} transferred to {}", writeCount, writeCount == 1 ? "node" : "nodes", zooKeeperEndpointConfig); } jsonReader.close(); closeZooKeeper(zooKeeper); }
From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java
License:Apache License
private JsonReader createJsonReader(final InputStream in) throws EntityProviderException { if (in == null) { throw new EntityProviderException(EntityProviderException.INVALID_STATE .addContent(("Got not supported NULL object as content to de-serialize."))); }//from w ww . j ava 2 s . c o m try { return new JsonReader(new InputStreamReader(in, DEFAULT_CHARSET)); } catch (final UnsupportedEncodingException e) { throw new EntityProviderException( EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e); } }
From source file:org.apache.ranger.unixusersync.process.FileSourceUserGroupBuilder.java
License:Apache License
public Map<String, List<String>> readJSONfile(File jsonFile) throws Exception { Map<String, List<String>> ret = new HashMap<String, List<String>>(); JsonReader jsonReader = new JsonReader(new BufferedReader(new FileReader(jsonFile))); Gson gson = new GsonBuilder().create(); ret = gson.fromJson(jsonReader, ret.getClass()); return ret;//from www . j av a 2 s . c om }
From source file:org.apache.synapse.mediators.bsf.ScriptMediator.java
License:Apache License
private void processJSONPayload(MessageContext synCtx, ScriptMessageContext scriptMC) throws ScriptException { if (!(synCtx instanceof Axis2MessageContext)) { return;/*from w w w. j a v a 2 s . co m*/ } org.apache.axis2.context.MessageContext messageContext = ((Axis2MessageContext) synCtx) .getAxis2MessageContext(); String jsonString = (String) messageContext.getProperty("JSON_STRING"); Object jsonObject = null; prepareForJSON(scriptMC); if (JsonUtil.hasAJsonPayload(messageContext)) { try { JsonElement o = jsonParser.parse(new JsonReader(JsonUtil.newJsonPayloadReader(messageContext))); // first, check if the stream is valid. if (o.isJsonNull()) { logger.error("#processJSONPayload. JSON stream is not valid."); return; } jsonObject = this.jsEngine.eval(JsonUtil.newJavaScriptSourceReader(messageContext)); } catch (Exception e) { handleException("Failed to get the JSON payload from the input stream. Error>>>\n" + e.getLocalizedMessage()); } } else if (jsonString != null) { String jsonPayload = jsonParser.parse(jsonString).toString(); jsonObject = this.jsEngine.eval('(' + jsonPayload + ')'); } if (jsonObject != null) { scriptMC.setJsonObject(synCtx, jsonObject); } }
From source file:org.apache.tez.dag.app.rm.PrimaryTenantYarnTaskSchedulerService.java
License:Apache License
@Override public synchronized void serviceInit(Configuration conf) { this.vcoresPerTask = conf.getInt(TezConfiguration.TEZ_TASK_RESOURCE_CPU_VCORES, TezConfiguration.TEZ_TASK_RESOURCE_CPU_VCORES_DEFAULT); this.memoryPerTask = conf.getInt(TezConfiguration.TEZ_TASK_RESOURCE_CPU_VCORES, TezConfiguration.TEZ_TASK_RESOURCE_MEMORY_MB_DEFAULT); this.probabilisticTypeSelection = conf.getBoolean(TezConfiguration.TEZ_PROBABILISTIC_TYPE_SELECTION, TezConfiguration.TEZ_PROBABILISTIC_TYPE_SELECTION_DEFAULT); this.lowPreferenceWeight = conf.getDouble(TezConfiguration.TEZ_PROBABILISTIC_LOW_PREFERENCE_WEIGHT, TezConfiguration.TEZ_PROBABILISTIC_LOW_PREFERENCE_WEIGHT_DEFAULT); this.mediumPreferenceWeight = conf.getDouble(TezConfiguration.TEZ_PROBABILISTIC_MEDIUM_PREFERENCE_WEIGHT, TezConfiguration.TEZ_PROBABILISTIC_MEDIUM_PREFERENCE_WEIGHT_DEFAULT); this.highPreferenceWeight = conf.getDouble(TezConfiguration.TEZ_PROBABILISTIC_HIGH_PREFERENCE_WEIGHT, TezConfiguration.TEZ_PROBABILISTIC_HIGH_PREFERENCE_WEIGHT_DEFAULT); this.bestFitScheduling = conf.getBoolean(TezConfiguration.TEZ_BEST_FIT_SCHEDULING, TezConfiguration.TEZ_BEST_FIT_SCHEDULING_DEFAULT); this.thresholdShortAndMedium = conf.getDouble(TezConfiguration.TEZ_THRESHOLD_SHORT_AND_MEDIUM, TezConfiguration.TEZ_THRESHOLD_SHORT_AND_MEDIUM_DEFAULT); this.thresholdMediumAndLong = conf.getDouble(TezConfiguration.TEZ_THRESHOLD_MEDIUM_AND_LONG, TezConfiguration.TEZ_THRESHOLD_MEDIUM_AND_LONG_DEFAULT); this.dagExecutionHistoryEnabled = conf.getBoolean(TezConfiguration.TEZ_DAG_EXECUTION_HISTORY_ENABLED, TezConfiguration.TEZ_DAG_EXECUTION_HISTORY_ENABLED_DEFAULT); this.dagExecutionHistoryPath = conf.get(TezConfiguration.TEZ_DAG_EXECUTION_HISTORY_PATH, TezConfiguration.TEZ_DAG_EXECUTION_HISTORY_PATH_DEFAULT); // Build the utilization table utilizationTable = new UtilizationTable(probabilisticTypeSelection, lowPreferenceWeight, mediumPreferenceWeight, highPreferenceWeight, bestFitScheduling, conf); // Build the historical execution database if (this.dagExecutionHistoryEnabled) { File historyProfile = new File(this.dagExecutionHistoryPath); if (historyProfile.exists() && !historyProfile.isDirectory()) { try { FileInputStream fileStream = new FileInputStream(this.dagExecutionHistoryPath); InputStreamReader streamReader = new InputStreamReader(fileStream); JsonReader jsonReader = new JsonReader(streamReader); JsonParser jsonParser = new JsonParser(); JsonElement jsonElement = jsonParser.parse(jsonReader); Set<Entry<String, JsonElement>> entrySet = ((JsonObject) jsonElement).entrySet(); for (Entry<String, JsonElement> entry : entrySet) { JsonObject map = entry.getValue().getAsJsonObject(); this.dagExecutionHistoryDuration.put(entry.getKey(), map.get("total_duration").getAsDouble()); this.dagExecutionHistoryTasks.put(entry.getKey(), map.get("concurrent_tasks").getAsInt()); }// w w w . j a va2 s .com } catch (FileNotFoundException e) { LOG.warn("Can not find the specified history profile: " + historyProfile); this.dagExecutionHistoryEnabled = false; } catch (IOException e) { LOG.warn("Error reading the specified history profile: " + historyProfile); this.dagExecutionHistoryEnabled = false; } } else { LOG.warn("Specified DAG execution history profile does not exist: " + historyProfile); this.dagExecutionHistoryEnabled = false; } } super.serviceInit(conf); }
From source file:org.apache.zeppelin.helium.HeliumBundleFactory.java
License:Apache License
/** * @return main file name of this helium package (relative path) *//*ww w. jav a2 s .c o m*/ private String downloadPackage(HeliumPackage pkg, String[] nameAndVersion, File bundleDir, String templateWebpackConfig, String templatePackageJson, FrontendPluginFactory fpf) throws IOException, TaskRunnerException { if (bundleDir.exists()) { FileUtils.deleteQuietly(bundleDir); } FileUtils.forceMkdir(bundleDir); FileFilter copyFilter = new FileFilter() { @Override public boolean accept(File pathname) { String fileName = pathname.getName(); if (fileName.startsWith(".") || fileName.startsWith("#") || fileName.startsWith("~")) { return false; } else { return true; } } }; if (isLocalPackage(pkg)) { FileUtils.copyDirectory(new File(pkg.getArtifact()), bundleDir, copyFilter); } else { // if online package String version = nameAndVersion[1]; File tgz = new File(heliumLocalRepoDirectory, pkg.getName() + "-" + version + ".tgz"); tgz.delete(); // wget, extract and move dir to `bundles/${pkg.getName()}`, and remove tgz npmCommand(fpf, "pack " + pkg.getArtifact()); File extracted = new File(heliumBundleDirectory, "package"); FileUtils.deleteDirectory(extracted); List<String> entries = unTgz(tgz, heliumBundleDirectory); for (String entry : entries) logger.debug("Extracted " + entry); tgz.delete(); FileUtils.copyDirectory(extracted, bundleDir); FileUtils.deleteDirectory(extracted); } // 1. setup package.json File existingPackageJson = new File(bundleDir, "package.json"); JsonReader reader = new JsonReader(new FileReader(existingPackageJson)); Map<String, Object> packageJson = gson.fromJson(reader, new TypeToken<Map<String, Object>>() { }.getType()); Map<String, String> existingDeps = (Map<String, String>) packageJson.get("dependencies"); String mainFileName = (String) packageJson.get("main"); StringBuilder dependencies = new StringBuilder(); int index = 0; for (Map.Entry<String, String> e : existingDeps.entrySet()) { dependencies.append(" \"").append(e.getKey()).append("\": "); if (e.getKey().equals("zeppelin-vis") || e.getKey().equals("zeppelin-tabledata") || e.getKey().equals("zeppelin-spell")) { dependencies.append("\"file:../../" + HELIUM_LOCAL_MODULE_DIR + "/").append(e.getKey()) .append("\""); } else { dependencies.append("\"").append(e.getValue()).append("\""); } if (index < existingDeps.size() - 1) { dependencies.append(",\n"); } index = index + 1; } FileUtils.deleteQuietly(new File(bundleDir, PACKAGE_JSON)); templatePackageJson = templatePackageJson.replaceFirst("PACKAGE_NAME", pkg.getName()); templatePackageJson = templatePackageJson.replaceFirst("MAIN_FILE", mainFileName); templatePackageJson = templatePackageJson.replaceFirst("DEPENDENCIES", dependencies.toString()); FileUtils.write(new File(bundleDir, PACKAGE_JSON), templatePackageJson); // 2. setup webpack.config FileUtils.write(new File(bundleDir, "webpack.config.js"), templateWebpackConfig); return mainFileName; }
From source file:org.apache.zeppelin.helium.HeliumLocalRegistry.java
License:Apache License
private HeliumPackage readPackageInfo(File f) { try {/*from w w w. j a v a 2 s . c o m*/ JsonReader reader = new JsonReader(new StringReader(FileUtils.readFileToString(f))); reader.setLenient(true); return gson.fromJson(reader, HeliumPackage.class); } catch (IOException e) { logger.error(e.getMessage(), e); return null; } }
From source file:org.apache.zeppelin.interpreter.InterpreterSetting.java
License:Apache License
public static InterpreterSetting fromJson(String json) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); StringReader stringReader = new StringReader(json); JsonReader jsonReader = new JsonReader(stringReader); InterpreterSetting intpSetting = new InterpreterSetting(); try {// w ww.ja v a 2 s . c om jsonReader.beginObject(); while (jsonReader.hasNext()) { String tag = jsonReader.nextName(); if (tag.equals("id")) { String id = jsonReader.nextString(); intpSetting.setId(id); } else if (tag.equals("name")) { String name = jsonReader.nextString(); intpSetting.setName(name); } else if (tag.equals("group")) { String group = jsonReader.nextString(); intpSetting.setGroup(group); } else if (tag.equals("dependencies")) { String strDep = jsonReader.nextString(); List<Dependency> dependencies = gson.fromJson(strDep, new TypeToken<List<Dependency>>() { }.getType()); intpSetting.setDependencies(dependencies); } else if (tag.equals("properties")) { String strProp = jsonReader.nextString(); Map<String, InterpreterProperty> properties = gson.fromJson(strProp, new TypeToken<Map<String, InterpreterProperty>>() { }.getType()); intpSetting.setProperties(properties); } else if (tag.equals("interpreterOption")) { String strOption = jsonReader.nextString(); InterpreterOption intpOption = gson.fromJson(strOption, new TypeToken<InterpreterOption>() { }.getType()); intpSetting.setOption(intpOption); } else if (tag.equals("interpreterGroup")) { String strIntpInfos = jsonReader.nextString(); List<InterpreterInfo> intpInfos = gson.fromJson(strIntpInfos, new TypeToken<List<InterpreterInfo>>() { }.getType()); intpSetting.setInterpreterInfos(intpInfos); } else { LOGGER.error("Error data type!"); } } jsonReader.endObject(); jsonReader.close(); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } return intpSetting; }
From source file:org.apache.zeppelin.notebook.Notebook.java
License:Apache License
/** * import JSON as a new note.//w w w . j a v a2 s .co m * * @param sourceJson - the note JSON to import * @param noteName - the name of the new note * @return notebook ID * @throws IOException */ public Note importNote(String sourceJson, String noteName, AuthenticationInfo subject) throws IOException { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.setPrettyPrinting(); Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create(); JsonReader reader = new JsonReader(new StringReader(sourceJson)); reader.setLenient(true); Note newNote; try { Note oldNote = gson.fromJson(reader, Note.class); newNote = createNote(subject); if (noteName != null) newNote.setName(noteName); else newNote.setName(oldNote.getName()); List<Paragraph> paragraphs = oldNote.getParagraphs(); for (Paragraph p : paragraphs) { newNote.addCloneParagraph(p); } newNote.persist(subject); } catch (IOException e) { logger.error(e.toString(), e); throw e; } return newNote; }
From source file:org.ballerinalang.langserver.extensions.ballerina.example.BallerinaExampleServiceImpl.java
License:Open Source License
@Override public CompletableFuture<BallerinaExampleListResponse> list(BallerinaExampleListRequest request) { return CompletableFuture.supplyAsync(() -> { BallerinaExampleListResponse response = new BallerinaExampleListResponse(); Gson gson = new Gson(); Path bbeJSONPath = Paths.get(CommonUtil.BALLERINA_HOME).resolve(EXAMPLES_DIR).resolve(BBE_DEF_JSON); try {/*from w w w. j av a 2 s . c om*/ InputStreamReader fileReader = new InputStreamReader(new FileInputStream(bbeJSONPath.toFile()), StandardCharsets.UTF_8); JsonReader jsonReader = new JsonReader(fileReader); List<BallerinaExampleCategory> data = gson.fromJson(jsonReader, EXAMPLE_CATEGORY_TYPE); response.setSamples(data); } catch (FileNotFoundException e) { if (CommonUtil.LS_DEBUG_ENABLED) { String msg = e.getMessage(); logger.error("Error while fetching the list of examples" + ((msg != null) ? ": " + msg : ""), e); } response.setSamples(new ArrayList<>()); } return response; }); }