/hazelcast/src/main/java/com/hazelcast/impl/management/ScriptExecutorCallable.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 149 lines · 115 code · 16 blank · 18 comment · 15 complexity · 341f6bf12a2f6ec442fdbe67b9caecc7 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.impl.management;
  17. import com.hazelcast.core.HazelcastInstance;
  18. import com.hazelcast.core.HazelcastInstanceAware;
  19. import com.hazelcast.logging.ILogger;
  20. import com.hazelcast.logging.Logger;
  21. import com.hazelcast.nio.DataSerializable;
  22. import com.hazelcast.nio.SerializationHelper;
  23. import java.io.DataInput;
  24. import java.io.DataOutput;
  25. import java.io.IOException;
  26. import java.lang.reflect.Method;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. import java.util.Map.Entry;
  30. import java.util.Set;
  31. import java.util.concurrent.Callable;
  32. import java.util.concurrent.ExecutionException;
  33. import java.util.logging.Level;
  34. public class ScriptExecutorCallable<V> implements DataSerializable, Callable<V>, HazelcastInstanceAware {
  35. private static final long serialVersionUID = -4729129143589252665L;
  36. private static final ILogger logger = Logger.getLogger(ScriptExecutorCallable.class.getName());
  37. private static final String SCRIPT_ENGINE_MANAGER_CLASS = "javax.script.ScriptEngineManager";
  38. private static final Object/*ScriptEngineManager*/ scriptEngineManager;
  39. private static final Method/*ScriptEngineManager.getEngineByName*/ mGetEngineByName;
  40. private static final Exception scriptEngineLoadError;
  41. static {
  42. Object manager = null;
  43. Method method = null;
  44. Exception error = null;
  45. try {
  46. final Class scriptEngineManagerClass = Class.forName(SCRIPT_ENGINE_MANAGER_CLASS);
  47. manager = scriptEngineManagerClass.newInstance();
  48. method = scriptEngineManagerClass.getMethod("getEngineByName", new Class[]{String.class});
  49. } catch (Exception e) {
  50. error = e;
  51. logger.log(Level.WARNING, "ScriptEngineManager could not be loaded!", e);
  52. }
  53. scriptEngineManager = manager;
  54. mGetEngineByName = method;
  55. scriptEngineLoadError = error;
  56. }
  57. private String engineName;
  58. private String script;
  59. private Map<String, Object> bindings;
  60. private transient HazelcastInstance hazelcast;
  61. public ScriptExecutorCallable() {
  62. }
  63. public ScriptExecutorCallable(String engineName, String script) {
  64. super();
  65. this.engineName = engineName;
  66. this.script = script;
  67. }
  68. public ScriptExecutorCallable(String engineName, String script, Map<String, Object> bindings) {
  69. super();
  70. this.engineName = engineName;
  71. this.script = script;
  72. this.bindings = bindings;
  73. }
  74. public V call() throws Exception {
  75. if (scriptEngineLoadError != null) {
  76. throw new ExecutionException("ScriptEngineManager could not be loaded!", scriptEngineLoadError);
  77. }
  78. // ScriptEngine engine = ScriptEngineManager.getEngineByName(engineName);
  79. Object/*ScriptEngine*/ engine = mGetEngineByName.invoke(scriptEngineManager, engineName);
  80. if (engine == null) {
  81. throw new IllegalArgumentException("Could not find ScriptEngine named '" + engineName + "'.");
  82. }
  83. Method put = engine.getClass().getMethod("put", new Class[]{String.class, Object.class});
  84. put.invoke(engine, "hazelcast", hazelcast);
  85. if (bindings != null) {
  86. Set<Entry<String, Object>> entries = bindings.entrySet();
  87. for (Entry<String, Object> entry : entries) {
  88. // ScriptEngine.put(key, value);
  89. put.invoke(engine, entry.getKey(), entry.getValue());
  90. }
  91. }
  92. Method eval = engine.getClass().getMethod("eval", new Class[]{String.class});
  93. // Object result = ScriptEngine.eval(script);
  94. Object result = eval.invoke(engine, script);
  95. if (result == null) {
  96. return null;
  97. }
  98. return (V) result;
  99. }
  100. public void writeData(DataOutput out) throws IOException {
  101. out.writeUTF(engineName);
  102. out.writeUTF(script);
  103. if (bindings != null) {
  104. out.writeInt(bindings.size());
  105. Set<Entry<String, Object>> entries = bindings.entrySet();
  106. for (Entry<String, Object> entry : entries) {
  107. out.writeUTF(entry.getKey());
  108. SerializationHelper.writeObject(out, entry.getValue());
  109. }
  110. } else {
  111. out.writeInt(0);
  112. }
  113. }
  114. public void readData(DataInput in) throws IOException {
  115. engineName = in.readUTF();
  116. script = in.readUTF();
  117. int size = in.readInt();
  118. if (size > 0) {
  119. bindings = new HashMap<String, Object>(size);
  120. for (int i = 0; i < size; i++) {
  121. String key = in.readUTF();
  122. Object value = SerializationHelper.readObject(in);
  123. bindings.put(key, value);
  124. }
  125. }
  126. }
  127. public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
  128. this.hazelcast = hazelcastInstance;
  129. }
  130. public void setBindings(Map<String, Object> bindings) {
  131. this.bindings = bindings;
  132. }
  133. }