|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.brooklyn.core.effector.script; |
| 20 | + |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import javax.script.Bindings; |
| 24 | +import javax.script.ScriptContext; |
| 25 | +import javax.script.ScriptEngine; |
| 26 | +import javax.script.ScriptEngineManager; |
| 27 | +import javax.script.ScriptException; |
| 28 | +import javax.script.SimpleScriptContext; |
| 29 | + |
| 30 | +import com.google.common.base.Preconditions; |
| 31 | +import com.google.common.reflect.TypeToken; |
| 32 | + |
| 33 | +import org.apache.brooklyn.api.effector.Effector; |
| 34 | +import org.apache.brooklyn.api.effector.ParameterType; |
| 35 | +import org.apache.brooklyn.config.ConfigKey; |
| 36 | +import org.apache.brooklyn.core.config.ConfigKeys; |
| 37 | +import org.apache.brooklyn.core.effector.AddEffector; |
| 38 | +import org.apache.brooklyn.core.effector.EffectorBody; |
| 39 | +import org.apache.brooklyn.core.effector.Effectors; |
| 40 | +import org.apache.brooklyn.core.effector.Effectors.EffectorBuilder; |
| 41 | +import org.apache.brooklyn.util.core.ResourceUtils; |
| 42 | +import org.apache.brooklyn.util.core.config.ConfigBag; |
| 43 | +import org.apache.brooklyn.util.core.flags.SetFromFlag; |
| 44 | +import org.apache.brooklyn.util.core.flags.TypeCoercions; |
| 45 | +import org.apache.brooklyn.util.core.task.Tasks; |
| 46 | +import org.apache.brooklyn.util.exceptions.Exceptions; |
| 47 | +import org.apache.brooklyn.util.text.Strings; |
| 48 | + |
| 49 | +public final class ScriptEffector<T> extends AddEffector { |
| 50 | + |
| 51 | + @SetFromFlag("lang") |
| 52 | + public static final ConfigKey<String> EFFECTOR_SCRIPT_LANGUAGE = ConfigKeys.newStringConfigKey( |
| 53 | + "script.language", "The scripting language the effector is written in", "JavaScript"); |
| 54 | + |
| 55 | + @SetFromFlag("content") |
| 56 | + public static final ConfigKey<String> EFFECTOR_SCRIPT_CONTENT = ConfigKeys.newStringConfigKey( |
| 57 | + "script.content", "The script code to evaluate for the effector"); |
| 58 | + |
| 59 | + @SetFromFlag("script") |
| 60 | + public static final ConfigKey<String> EFFECTOR_SCRIPT_URL = ConfigKeys.newStringConfigKey( |
| 61 | + "script.url", "A URL for the script to evaluate for the effector"); |
| 62 | + |
| 63 | + @SetFromFlag("return") |
| 64 | + public static final ConfigKey<String> EFFECTOR_SCRIPT_RETURN_VAR = ConfigKeys.newStringConfigKey( |
| 65 | + "script.return.var", "An optional script variable to return from the effector"); |
| 66 | + |
| 67 | + @SetFromFlag("type") |
| 68 | + public static final ConfigKey<Class<?>> EFFECTOR_SCRIPT_RETURN_TYPE = ConfigKeys.newConfigKey( |
| 69 | + new TypeToken<Class<?>>() { }, |
| 70 | + "script.return.type", "The type of the return value from the effector", Object.class); |
| 71 | + |
| 72 | + public ScriptEffector(ConfigBag params) { |
| 73 | + super(newEffectorBuilder(params).build()); |
| 74 | + } |
| 75 | + |
| 76 | + public ScriptEffector(Map<String,?> params) { |
| 77 | + this(ConfigBag.newInstance(params)); |
| 78 | + } |
| 79 | + |
| 80 | + public static EffectorBuilder<Object> newEffectorBuilder(ConfigBag params) { |
| 81 | + EffectorBuilder<Object> eff = AddEffector.newEffectorBuilder(Object.class, params); |
| 82 | + eff.impl(new Body(eff.buildAbstract(), params)); |
| 83 | + return eff; |
| 84 | + } |
| 85 | + |
| 86 | + protected static class Body extends EffectorBody<Object> { |
| 87 | + private final Effector<?> effector; |
| 88 | + private final String script; |
| 89 | + private final String language; |
| 90 | + private final String returnVar; |
| 91 | + private final Class<?> returnType; |
| 92 | + private final ScriptEngineManager factory = new ScriptEngineManager(); |
| 93 | + private final ScriptEngine engine; |
| 94 | + |
| 95 | + public Body(Effector<?> eff, ConfigBag params) { |
| 96 | + this.effector = eff; |
| 97 | + String content = params.get(EFFECTOR_SCRIPT_CONTENT); |
| 98 | + String url = params.get(EFFECTOR_SCRIPT_URL); |
| 99 | + if (Strings.isNonBlank(content)) { |
| 100 | + this.script = content; |
| 101 | + } else { |
| 102 | + this.script = ResourceUtils.create().getResourceAsString(Preconditions.checkNotNull(url, "Script URL or content must be specified")); |
| 103 | + } |
| 104 | + this.language = params.get(EFFECTOR_SCRIPT_LANGUAGE); |
| 105 | + this.returnVar = params.get(EFFECTOR_SCRIPT_RETURN_VAR); |
| 106 | + this.returnType = params.get(EFFECTOR_SCRIPT_RETURN_TYPE); |
| 107 | + this.engine = Preconditions.checkNotNull(factory.getEngineByName(language), "Engine for requested language does not exist"); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Object call(ConfigBag params) { |
| 112 | + ScriptContext context = new SimpleScriptContext(); |
| 113 | + |
| 114 | + // Store effector arguments as engine scope bindings |
| 115 | + for (ParameterType<?> param: effector.getParameters()) { |
| 116 | + context.setAttribute(param.getName(), params.get(Effectors.asConfigKey(param)), ScriptContext.ENGINE_SCOPE); |
| 117 | + } |
| 118 | + |
| 119 | + // Add global scope object bindings |
| 120 | + context.setAttribute("entity", entity(), ScriptContext.ENGINE_SCOPE); |
| 121 | + context.setAttribute("managementContext", entity().getManagementContext(), ScriptContext.ENGINE_SCOPE); |
| 122 | + context.setAttribute("task", Tasks.current(), ScriptContext.ENGINE_SCOPE); |
| 123 | + context.setAttribute("config", params.getAllConfig(), ScriptContext.ENGINE_SCOPE); |
| 124 | + |
| 125 | + try { |
| 126 | + // Execute the script and return result |
| 127 | + Object result = engine.eval(script, context); |
| 128 | + if (Strings.isNonBlank(returnVar)) { |
| 129 | + result = context.getAttribute(returnVar, ScriptContext.ENGINE_SCOPE); |
| 130 | + } |
| 131 | + return TypeCoercions.coerce(result, returnType); |
| 132 | + } catch (ScriptException e) { |
| 133 | + throw Exceptions.propagate(e); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments