ScriptFactory.java 8.6 KB
Newer Older
R
update  
roo00 已提交
1 2 3 4 5
package com.x.base.core.project.script;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Z
zhourui 已提交
6
import java.util.Map;
R
update  
roo00 已提交
7 8 9 10 11 12 13 14 15 16
import java.util.Objects;

import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.BooleanUtils;

R
roo00 已提交
17 18 19
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
R
update  
roo00 已提交
20 21
import com.x.base.core.entity.JpaObject;
import com.x.base.core.project.config.Config;
Z
zhourui 已提交
22
import com.x.base.core.project.tools.PropertyTools;
R
update  
roo00 已提交
23 24

import jdk.nashorn.api.scripting.ScriptObjectMirror;
Z
zhourui 已提交
25
import jdk.nashorn.internal.runtime.ScriptObject;
R
update  
roo00 已提交
26 27 28

public class ScriptFactory {

Z
Zhou Rui 已提交
29 30
	private ScriptFactory() {

Z
Zhou Rui 已提交
31 32
	}

R
update  
roo00 已提交
33 34 35 36 37 38 39 40
	public static final ScriptEngine scriptEngine = (new ScriptEngineManager())
			.getEngineByName(Config.SCRIPTING_ENGINE_NAME);

	private static CompiledScript COMPILEDSCRIPT_INITIALSERVICESCRIPTTEXT;
	private static CompiledScript COMPILEDSCRIPT_INITIALSCRIPTTEXT;

	public static final String BINDING_NAME_RESOURCES = "resources";
	public static final String BINDING_NAME_EFFECTIVEPERSON = "effectivePerson";
R
roo00 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54
	public static final String BINDING_NAME_WORKCONTEXT = "workContext";
	public static final String BINDING_NAME_GSON = "gson";
	public static final String BINDING_NAME_DATA = "data";
	public static final String BINDING_NAME_ORGANIZATION = "organization";
	public static final String BINDING_NAME_WEBSERVICESCLIENT = "webservicesClient";
	public static final String BINDING_NAME_DICTIONARY = "dictionary";
	public static final String BINDING_NAME_ROUTES = "routes";
	public static final String BINDING_NAME_ROUTE = "routes";
	public static final String BINDING_NAME_APPLICATIONS = "applications";

	public static final String BINDING_NAME_ASSIGNDATA = "assignData";

	public static final String BINDING_NAME_IDENTITY = "identity";

R
update  
roo00 已提交
55
	public static final String BINDING_NAME_PARAMETERS = "parameters";
R
roo00 已提交
56 57 58 59 60 61 62 63 64
	public static final String BINDING_NAME_JAXRSRESPONSE = "jaxrsResponse";
	public static final String BINDING_NAME_JAXWSRESPONSE = "jaxwsResponse";

	public static final String BINDING_NAME_JAXRSBODY = "jaxrsBody";
	public static final String BINDING_NAME_JAXRSHEAD = "jaxrsHead";

	public static final String BINDING_NAME_SERVICEVALUE = "serviceValue";
	public static final String BINDING_NAME_TASK = "task";
	public static final String BINDING_NAME_EXPIRE = "expire";
R
roo00 已提交
65 66
	public static final String BINDING_NAME_SERIAL = "serial";
	public static final String BINDING_NAME_PROCESS = "process";
R
update  
roo00 已提交
67

Z
Zhou Rui 已提交
68 69 70 71
	public static ScriptEngine newScriptEngine() {
		return (new ScriptEngineManager()).getEngineByName(Config.SCRIPTING_ENGINE_NAME);
	}

R
update  
roo00 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	public static CompiledScript initialServiceScriptText() throws Exception {
		if (COMPILEDSCRIPT_INITIALSERVICESCRIPTTEXT == null) {
			synchronized (ScriptFactory.class) {
				if (COMPILEDSCRIPT_INITIALSERVICESCRIPTTEXT == null) {
					String text = Config.initialServiceScriptText();
					COMPILEDSCRIPT_INITIALSERVICESCRIPTTEXT = ((Compilable) scriptEngine).compile(text);
				}
			}
		}
		return COMPILEDSCRIPT_INITIALSERVICESCRIPTTEXT;
	}

	public static CompiledScript compile(String text) throws Exception {
		return ((Compilable) scriptEngine).compile(text);
	}

	public static CompiledScript initialScriptText() throws Exception {
		if (COMPILEDSCRIPT_INITIALSCRIPTTEXT == null) {
			synchronized (ScriptFactory.class) {
				if (COMPILEDSCRIPT_INITIALSCRIPTTEXT == null) {
					String text = Config.initialScriptText();
					COMPILEDSCRIPT_INITIALSCRIPTTEXT = ((Compilable) scriptEngine).compile(text);
				}
			}
		}
		return COMPILEDSCRIPT_INITIALSCRIPTTEXT;
	}

	public static String functionalization(String text) {
		StringBuffer sb = new StringBuffer();
		sb.append("(function(){").append(System.lineSeparator());
		sb.append(Objects.toString(text, "")).append(System.lineSeparator());
		sb.append("})();");
		return sb.toString();
	}

	public static List<String> asStringList(Object o) throws Exception {
		return readAsStringList(o);
	}

	public static String asString(Object o) throws Exception {
		return Objects.toString(o);
	}

	public static Boolean asBoolean(Object o) throws Exception {
Z
zhourui 已提交
117 118 119 120 121 122 123
		if (null == o) {
			return false;
		}
		if (o instanceof Number) {
			return ((Number) o).intValue() != 0;
		}
		return BooleanUtils.toBooleanObject(Objects.toString(o, "false"));
R
update  
roo00 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	}

	public static List<String> asDistinguishedName(Object o) throws Exception {
		List<String> list = new ArrayList<>();
		if (null != o) {
			if (o instanceof CharSequence) {
				list.add(Objects.toString(o));
			} else if (o instanceof Iterable) {
				for (Object obj : (Iterable<?>) o) {
					if (null != obj) {
						if (obj instanceof CharSequence) {
							list.add(Objects.toString(obj));
						} else {
							Object d = PropertyUtils.getProperty(obj, JpaObject.DISTINGUISHEDNAME);
							if (null != d) {
								list.add(Objects.toString(d));
							}
						}
					}
				}
			} else if (o instanceof ScriptObjectMirror) {
				ScriptObjectMirror som = (ScriptObjectMirror) o;
				if (som.isArray()) {
					Object[] objs = (som.to(Object[].class));
					for (Object obj : objs) {
						if (null != obj) {
							if (obj instanceof CharSequence) {
								list.add(Objects.toString(obj));
							} else {
								Object d = PropertyUtils.getProperty(obj, JpaObject.DISTINGUISHEDNAME);
								if (null != d) {
									list.add(Objects.toString(d));
								}
							}
						}
					}
				} else {
					Object d = PropertyUtils.getProperty(o, JpaObject.DISTINGUISHEDNAME);
					if (null != d) {
						list.add(Objects.toString(d));
					}
				}
			}
		}
		return list;
	}

	private static List<String> readAsStringList(Object obj) throws Exception {
		List<String> list = new ArrayList<>();
		for (Object o : iterator(obj)) {
			list.add(Objects.toString(o));
		}
		return list;
	}

	private static List<Object> iterator(Object obj) throws Exception {
		List<Object> results = new ArrayList<>();
		iterator(obj, results);
		return results;
	}

	private static void iterator(Object obj, List<Object> results) throws Exception {
		if (null == obj) {
			return;
		}
		List<Object> list = new ArrayList<>();
		if (obj.getClass().isArray()) {
			for (Object o : (Object[]) obj) {
				list.add(o);
			}
		} else if (obj instanceof Collection) {
			for (Object o : (Collection<?>) obj) {
				list.add(o);
			}
		} else if (obj instanceof ScriptObjectMirror) {
			ScriptObjectMirror som = (ScriptObjectMirror) obj;
			if (som.isArray()) {
				Object[] os = (som.to(Object[].class));
				for (Object o : os) {
					list.add(o);
				}
			} else {
				results.add(som);
			}
		} else {
			results.add(obj);
		}
		for (Object o : list) {
			iterator(o, results);
		}
	}
R
roo00 已提交
215

R
roo00 已提交
216
	public static List<String> asDistinguishedNameList(Object o) throws Exception {
R
roo00 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
		List<String> list = new ArrayList<>();
		if (null != o) {
			if (o instanceof CharSequence) {
				list.add(Objects.toString(o, ""));
			} else if (o instanceof JsonObject) {
				JsonObject jsonObject = (JsonObject) o;
				if (jsonObject.has(JpaObject.DISTINGUISHEDNAME)) {
					list.add(jsonObject.get(JpaObject.DISTINGUISHEDNAME).getAsString());
				}
			} else if (o instanceof JsonArray) {
				for (JsonElement jsonElement : (JsonArray) o) {
					if (jsonElement.isJsonObject()) {
						JsonObject jsonObject = jsonElement.getAsJsonObject();
						if (jsonObject.has(JpaObject.DISTINGUISHEDNAME)) {
							list.add(jsonObject.get(JpaObject.DISTINGUISHEDNAME).getAsString());
						}
					}
				}
			} else if (o instanceof Iterable) {
				for (Object obj : (Iterable<?>) o) {
					if (null != obj) {
						if (obj instanceof CharSequence) {
							list.add(Objects.toString(obj, ""));
						} else {
Z
zhourui 已提交
241
							list.add(PropertyTools.getOrElse(obj, JpaObject.DISTINGUISHEDNAME, String.class, ""));
R
roo00 已提交
242 243 244 245 246 247 248 249 250 251 252 253
						}
					}
				}
			} else if (o instanceof ScriptObjectMirror) {
				ScriptObjectMirror som = (ScriptObjectMirror) o;
				if (som.isArray()) {
					Object[] objs = (som.to(Object[].class));
					for (Object obj : objs) {
						if (null != obj) {
							if (obj instanceof CharSequence) {
								list.add(Objects.toString(obj, ""));
							} else {
Z
zhourui 已提交
254 255 256 257 258 259 260 261
								if (obj instanceof ScriptObject) {
									ScriptObject so = (ScriptObject) obj;
									if (so.containsKey(JpaObject.DISTINGUISHEDNAME)) {
										list.add(Objects.toString(so.get(JpaObject.DISTINGUISHEDNAME), ""));
									}
								} else {
									list.add(PropertyTools.getOrElse(obj, JpaObject.DISTINGUISHEDNAME, String.class,
											""));
R
roo00 已提交
262 263 264 265 266
								}
							}
						}
					}
				} else {
Z
zhourui 已提交
267
					list.add(PropertyTools.getOrElse(o, JpaObject.DISTINGUISHEDNAME, String.class, ""));
R
roo00 已提交
268
				}
Z
zhourui 已提交
269 270
			} else {
				list.add(Objects.toString(o, ""));
R
roo00 已提交
271 272 273 274 275 276
			}
		}
		return list;
	}

}