Java Code Examples for com.jeesuite.common.json.JsonUtils#toObject()

The following examples show how to use com.jeesuite.common.json.JsonUtils#toObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigParseUtils.java    From jeesuite-config with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static void parseConfigToKVMap(Map<String, Object> result, AppconfigEntity config) {
	if(config.getType() == 1){
		if(config.getName().toLowerCase().endsWith(".xml")){
			parseDataFromXML(result,config.getContents());
		}else if(config.getName().toLowerCase().endsWith(".yml") || config.getName().toLowerCase().endsWith(".yaml")){
			parseDataFromYaml(result,config.getContents());
		}else{				
			parseFromProps(result, config.getContents());
		}
	}else if(config.getType() == 2){
		result.put(config.getName(), config.getContents());
	}else if(config.getType() == 3){
		Map<String,Object> configs = JsonUtils.toObject(config.getContents(), Map.class);
		result.putAll(configs);
	}
	
	//替换引用
	String value;
	for (String key : result.keySet()) {
		value =  StringUtils.trimToEmpty(result.get(key).toString());
		if(value.contains("${")){
			setReplaceHolderRefValue(result,key,value);
		}
	}
}
 
Example 2
Source File: ConfigcenterContext.java    From jeesuite-config with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Map<String,Object> fetchConfigFromServer(){
	Map<String,Object> result = null;
	String errorMsg = null;
       for (String apiBaseUrl : apiBaseUrls) {
       	String url = buildTokenParameter(String.format("%s/api/fetch_all_configs?appName=%s&env=%s&version=%s", apiBaseUrl,app,env,version));
   		System.out.println("fetch configs url:" + url);
   		String jsonString = null;
   		try {
   			HttpResponseEntity response = HttpUtils.get(url);
       		if(response.isSuccessed()){
       			jsonString = response.getBody();
       			result = JsonUtils.toObject(jsonString, Map.class);
       			if(result.containsKey("code")){
       				errorMsg = result.get("msg").toString();
       				System.err.println("fetch error:"+errorMsg);
       				result = null;
       			}else{
       				break;
       			}
       		}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
       //
       if(result == null){
       	System.out.println(">>>>>remote Config fecth error, load from local Cache");
       	result = LocalCacheUtils.read();
       }else{
       	LocalCacheUtils.write(result);
       }
       return result;
}
 
Example 3
Source File: LocalCacheUtils.java    From jeesuite-config with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static Map<String, Object> read() {
	try {
		File dir = new File(localStorageDir);
		if (!dir.exists())
			dir.mkdirs();
		File file = new File(dir, "config-cache.json");
		if (!file.exists()) {
			return null;
		}

		StringBuilder buffer = new StringBuilder();
		InputStream is = new FileInputStream(file);
		String line;
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		line = reader.readLine();
		while (line != null) { 
			buffer.append(line); 
			line = reader.readLine();
		}
		reader.close();
		is.close();
		return JsonUtils.toObject(buffer.toString(), Map.class);
	} catch (Exception e) {
		
	}
	return null;
}
 
Example 4
Source File: KafkaMonitor.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
private synchronized void fetchProducerStatFromZK(){
	Map<String,List<ProducerStat>> currentStats = new HashMap<>();
	List<String> groups = zkClient.getChildren(SendCounterHandler.ROOT);
	if(groups == null)return ;
	
	List<String> nodes;
	List<ProducerStat> stats;
	String groupPath,topicPath,nodePath;
	for (String group : groups) {
		stats = currentStats.get(group);
		if(stats == null){
			stats = new ArrayList<>();
			currentStats.put(group, stats);
		}
		groupPath = SendCounterHandler.ROOT + "/" + group;
		List<String> topics = zkClient.getChildren(groupPath);
		for (String topic : topics) {				
			topicPath = groupPath + "/" + topic;
			nodes = zkClient.getChildren(topicPath);
			for (String node : nodes) {
				nodePath = topicPath + "/" + node;
				Object data = zkClient.readData(nodePath);
				if(data != null){
					ProducerStat stat = JsonUtils.toObject(data.toString(), ProducerStat.class);
					stat.setSource(node);
					stats.add(stat);
				}
			}
		}
	}
	producerStats = currentStats;
}
 
Example 5
Source File: User.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	
	User user = new User();
	user.setId(100);
	user.setName("jack");
	
	DefaultMessage message = new DefaultMessage(user).consumerAckRequired(true).header("key1", "value1");
	String json = JsonUtils.toJson(message);
	System.out.println(json);
	
	message = JsonUtils.toObject(json,DefaultMessage.class);
	System.out.println(message);
}
 
Example 6
Source File: SchedulerMonitor.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public JobGroupInfo getJobGroupInfo(String groupName){

		if(StringUtils.isBlank(groupName)){
			logger.warn("getJobGroupInfo groupName is required");
			return null;
		}
		JobGroupInfo groupInfo = new JobGroupInfo();
		groupInfo.setName(groupName);
        if(asyncEventBus != null){
        	groupInfo.setJobs(JobContext.getContext().getRegistry().getAllJobs());
        	return groupInfo;
        }
		//
		String path = ZkJobRegistry.ROOT + groupName;
		List<String> children = zkClient.getChildren(path);
		for (String child : children) {
			if("nodes".equals(child)){
				path = ZkJobRegistry.ROOT + groupName + "/nodes";
				groupInfo.setClusterNodes(zkClient.getChildren(path));
			}else{
				path = ZkJobRegistry.ROOT + groupName + "/" + child;
				Object data = zkClient.readData(path);
				if(data != null){						
					JobConfig jobConfig = JsonUtils.toObject(data.toString(), JobConfig.class);
					groupInfo.getJobs().add(jobConfig);
				}
			}
		}
		
		if(groupInfo.getClusterNodes().size() > 0){				
			return groupInfo;
		}
		
		return null;
	
	}
 
Example 7
Source File: CustomResponseErrorHandler.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
@Override
public void handleError(ClientHttpResponse response) throws IOException {
	int code = response.getRawStatusCode();
	String content = CharStreams.toString(new InputStreamReader(response.getBody(), StandardCharsets.UTF_8));
	
	Map<?, ?> responseItmes = null;
	if(code == 404 && StringUtils.isNotBlank(content)){
		responseItmes = JsonUtils.toObject(content, Map.class);
		throw new JeesuiteBaseException(404, "Page Not Found["+responseItmes.get("path")+"]");
	}

	int errorCode = 500;
	String errorMsg = content;
	try {responseItmes = JsonUtils.toObject(content, Map.class);} catch (Exception e) {}
	if(responseItmes != null){
		if(responseItmes.containsKey("code")){
			errorCode = Integer.parseInt(responseItmes.get("code").toString());
		}
		if(responseItmes.containsKey("msg")){
			errorMsg = responseItmes.get("msg").toString();
		}else if(responseItmes.containsKey("message")){
			errorMsg = responseItmes.get("message").toString();
		}
	}
	
	if(StringUtils.isBlank(errorMsg)){
		errorMsg = DEFAULT_ERROR_MSG;
	}
	
	throw new JeesuiteBaseException(errorCode, errorMsg + "(Remote)");
}
 
Example 8
Source File: ClearCommand.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
public static ClearCommand deserialize(String json) {
	return JsonUtils.toObject(json, ClearCommand.class);
}
 
Example 9
Source File: ZkJobRegistry.java    From jeesuite-libs with Apache License 2.0 4 votes vote down vote up
private synchronized JobConfig getConfigFromZK(String path, Stat stat) {
	Object data = stat == null ? zkClient.readData(path) : zkClient.readData(path, stat);
	return data == null ? null : JsonUtils.toObject(data.toString(), JobConfig.class);
}