All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.logicbus.service.EnvQuery Maven / Gradle / Ivy

There is a newer version: 1.6.16
Show newest version
package com.logicbus.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.anysoft.util.DefaultProperties;
import com.anysoft.util.PropertiesConstants;
import com.logicbus.backend.AbstractServant;
import com.logicbus.backend.Context;
import com.logicbus.backend.ServantException;
import com.logicbus.backend.message.JsonMessage;
import com.logicbus.backend.message.XMLMessage;
import com.logicbus.models.servant.ServiceDescription;


/**
 * 查询当前JVM环境的Env变量
 * 
 * @author duanyy
 * 
 * @since 1.2.5.2
 * 
 * @version 1.4.0 [20141117 duanyy] 
* - 将MessageDoc和Context进行合并整合
*/ public class EnvQuery extends AbstractServant { protected DefaultProperties toArray = null; protected void onDestroy() { } protected void onCreate(ServiceDescription sd) throws ServantException { String array = PropertiesConstants.getString( sd.getProperties(), "arrays", "java.class.path=:;java.library.path=:;PATH=:;CLASSPATH=:;java.ext.dirs=:;sun.boot.class.path=:", true); String delimeter1 = PropertiesConstants.getString( sd.getProperties(), "arrays.delimeter1", ";", true); String delimeter2 = PropertiesConstants.getString( sd.getProperties(), "arrays.delimeter2", "=", true); toArray = new DefaultProperties("Default",sd.getProperties()); toArray.loadFromString(array,delimeter1,delimeter2); } protected int onXml(Context ctx) { XMLMessage msg = (XMLMessage) ctx.asMessage(XMLMessage.class); Document doc = msg.getDocument(); Element root = msg.getRoot(); { Element envs = doc.createElement("envs"); Map map = System.getenv(); Set> entries = map.entrySet(); Iterator> iter = entries.iterator(); while (iter.hasNext()){ Entry value = iter.next(); writeXml(value.getKey(),value.getValue(),"env",envs,doc); } root.appendChild(envs); } { Element envs = doc.createElement("properties"); Set> entries = System.getProperties().entrySet(); Iterator> iter = entries.iterator(); while (iter.hasNext()){ Entry value = iter.next(); writeXml(value.getKey().toString(),value.getValue().toString(),"property",envs,doc); } root.appendChild(envs); } return 0; } private void writeXml(String id, String value, String tag,Element envs, Document doc) { Element env = doc.createElement(tag); String delimeter = PropertiesConstants.getString(toArray, id, "",true); if (validString(delimeter)){ String [] values = value.split(delimeter); for (String v:values){ Element vElem = doc.createElement("value"); vElem.setAttribute("value", v); env.appendChild(vElem); } env.setAttribute("id", id); }else{ env.setAttribute("id", id); env.setAttribute("value", value); } envs.appendChild(env); } protected int onJson(Context ctx) { JsonMessage msg = (JsonMessage) ctx.asMessage(JsonMessage.class); Map json = msg.getRoot(); { List envs = new ArrayList(); Map map = System.getenv(); Set> entries = map.entrySet(); Iterator> iter = entries.iterator(); while (iter.hasNext()){ Entry value = iter.next(); writeJson(value.getKey(),value.getValue(),envs); } json.put("envs", envs); } { List envs = new ArrayList(); Set> entries = System.getProperties().entrySet(); Iterator> iter = entries.iterator(); while (iter.hasNext()){ Entry value = iter.next(); writeJson(value.getKey().toString(),value.getValue().toString(),envs); } json.put("envs", envs); } return 0; } private void writeJson(String id, String value, List envs) { Map env = new HashMap(2); String delimeter = PropertiesConstants.getString(toArray, id, "",true); if (validString(delimeter)){ String [] values = value.split(delimeter); List vList = new ArrayList(values.length); for (String v:values){ Map vMap = new HashMap(1); vMap.put("value", v); vList.add(vMap); } env.put("value", vList); env.put("id", id); }else{ env.put("id", id); env.put("value", value); } envs.add(env); } protected boolean validString(String str){ return str != null && str.length() > 0; } }