| 1 | package com.hammurapi.common.concurrent; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Arrays; |
| 5 | import java.util.Collection; |
| 6 | import java.util.concurrent.ExecutionException; |
| 7 | import java.util.concurrent.ExecutorService; |
| 8 | import java.util.concurrent.Future; |
| 9 | import java.util.concurrent.ScheduledExecutorService; |
| 10 | import java.util.concurrent.TimeUnit; |
| 11 | |
| 12 | import com.hammurapi.common.Context; |
| 13 | import com.hammurapi.convert.ConversionException; |
| 14 | import com.hammurapi.convert.Converter; |
| 15 | |
| 16 | /** |
| 17 | * Synapse which operates within JVM. |
| 18 | * @author Pavel Vlasov |
| 19 | * |
| 20 | * @param <R> |
| 21 | */ |
| 22 | public class LocalSynapse<C, R> implements Synapse<C, R, String> { |
| 23 | |
| 24 | // TODO - Option whether to chain property set or not, e.g. no chaining for service calls |
| 25 | // TODO - Option to provide sub-set path so the invocable sees only the subset of invoker's property set. |
| 26 | |
| 27 | private long delay; |
| 28 | private long period; |
| 29 | private TimeUnit timeUnit; |
| 30 | private Invocable<C, R, String> invocable; |
| 31 | private ClassLoader classLoader; |
| 32 | private Class<?>[] parameterTypes; |
| 33 | private boolean async; |
| 34 | private boolean chainPropertySet; |
| 35 | private String psPrefix; |
| 36 | private Converter converter; |
| 37 | private Context context; |
| 38 | |
| 39 | // /** |
| 40 | // * Creates synapse without delay and without repetitive execution. |
| 41 | // */ |
| 42 | // public LocalSynapse(boolean async, ClassLoader classLoader) { |
| 43 | // this(async,classLoader,0,0,null,true,null); |
| 44 | // } |
| 45 | |
| 46 | public LocalSynapse( |
| 47 | boolean async, |
| 48 | ClassLoader classLoader, |
| 49 | Converter converter, |
| 50 | Context context, |
| 51 | long delay, |
| 52 | long period, |
| 53 | TimeUnit timeUnit, |
| 54 | boolean chainPropertySet, |
| 55 | String psPrefix) { |
| 56 | this.async = async; |
| 57 | this.classLoader = classLoader; |
| 58 | if (this.classLoader==null) { |
| 59 | this.classLoader = this.getClass().getClassLoader(); |
| 60 | } |
| 61 | this.delay = delay; |
| 62 | this.period = period; |
| 63 | this.timeUnit = timeUnit; |
| 64 | this.chainPropertySet = chainPropertySet; |
| 65 | this.psPrefix = psPrefix; |
| 66 | this.converter = converter; |
| 67 | this.context = context; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void setInvocable(Invocable<C, R, String> invocable) { |
| 72 | this.invocable = invocable; |
| 73 | this.parameterTypes = invocable.getParameterTypes(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Future<Iterable<R>> invoke(final C context, final Invocation<String> invocation, ExecutorService executorService) { |
| 78 | // TODO - control invocation chain, substitute invocation and add invocation chain element. |
| 79 | // Synchronous invocation |
| 80 | if (async || executorService==null) { |
| 81 | if (timeUnit!=null && (delay>0 || period>0)) { |
| 82 | throw new InvocationException("Cannot schedule invocations"); |
| 83 | } |
| 84 | |
| 85 | final Collection<R> ret = new ArrayList<R>(); |
| 86 | try { |
| 87 | boolean hasArgumentIterators = false; |
| 88 | boolean needsConversion = false; |
| 89 | int idx=0; |
| 90 | for (Object arg: invocation.getArguments()) { |
| 91 | if (arg instanceof ArgumentIterator) { |
| 92 | hasArgumentIterators = true; |
| 93 | break; |
| 94 | } |
| 95 | if (arg!=null && !parameterTypes[idx].isInstance(arg)) { |
| 96 | needsConversion = true; |
| 97 | } |
| 98 | ++idx; |
| 99 | } |
| 100 | |
| 101 | if (hasArgumentIterators) { |
| 102 | Object[] args = Arrays.copyOf(invocation.getArguments(), invocation.getArguments().length); |
| 103 | _invoke(context, invocation, executorService, args, 0, ret); |
| 104 | } else { |
| 105 | if (needsConversion) { |
| 106 | final Object[] args = Arrays.copyOf(invocation.getArguments(), invocation.getArguments().length); |
| 107 | for (int i=0; i<args.length; ++i) { |
| 108 | args[i] = convert(args[i], i); |
| 109 | } |
| 110 | InvocationImpl<String> iv = new InvocationImpl<String>(invocation); |
| 111 | iv.setPropertySet(preparePropertySet(executorService, invocation.getPropertySet())); |
| 112 | ret.add(invocable.invoke(context, iv, executorService)); |
| 113 | } else { |
| 114 | ret.add(invocable.invoke(context, invocation, executorService)); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return new Future<Iterable<R>>() { |
| 119 | |
| 120 | @Override |
| 121 | public boolean cancel(boolean mayInterruptIfRunning) { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean isCancelled() { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public boolean isDone() { |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Iterable<R> get() { |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Iterable<R> get(long timeout, TimeUnit unit) { |
| 142 | return ret; |
| 143 | } |
| 144 | }; |
| 145 | } catch (final Exception e) { |
| 146 | return new Future<Iterable<R>>() { |
| 147 | |
| 148 | @Override |
| 149 | public boolean cancel(boolean mayInterruptIfRunning) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public boolean isCancelled() { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public boolean isDone() { |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public Iterable<R> get() throws ExecutionException { |
| 165 | throw new ExecutionException(e); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public Iterable<R> get(long timeout, TimeUnit unit) throws ExecutionException { |
| 170 | throw new ExecutionException(e); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (timeUnit==null || (delay<=0 && period<=0)) { |
| 178 | // TODO - Asynchronous invocations. |
| 179 | |
| 180 | |
| 181 | } |
| 182 | |
| 183 | if (!(executorService instanceof ScheduledExecutorService)) { |
| 184 | throw new InvocationException("Cannot schedule invocations - executor service is not a ScheduledExecutorService"); |
| 185 | } |
| 186 | |
| 187 | // TODO Scheduled invocations. |
| 188 | return null; |
| 189 | } |
| 190 | |
| 191 | private PropertySet<String> preparePropertySet(ExecutorService executorService, PropertySet<String> propertySet) { |
| 192 | if (propertySet==null) { |
| 193 | if (chainPropertySet) { |
| 194 | return new LocalStringPropertySet(executorService, converter, context, classLoader); |
| 195 | } |
| 196 | return null; |
| 197 | } |
| 198 | |
| 199 | if (psPrefix!=null) { |
| 200 | if (chainPropertySet) { |
| 201 | return new LocalStringPropertySet(executorService, converter, context, classLoader, propertySet.subset(psPrefix)); |
| 202 | } |
| 203 | return propertySet.subset(psPrefix); |
| 204 | } |
| 205 | if (chainPropertySet) { |
| 206 | return new LocalStringPropertySet(executorService, converter, context, classLoader, propertySet); |
| 207 | } |
| 208 | return propertySet; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Iterates over argument iterators |
| 213 | * @param invocation |
| 214 | * @param executorService |
| 215 | * @param args |
| 216 | * @param i Current argument index |
| 217 | */ |
| 218 | private void _invoke(final C context, final Invocation<String> invocation, ExecutorService executorService, final Object[] args, int idx, Collection<R> collector) { |
| 219 | if (idx==args.length) { |
| 220 | InvocationImpl<String> iv = new InvocationImpl<String>(invocation); |
| 221 | iv.setArguments(args); |
| 222 | iv.setPropertySet(preparePropertySet(executorService, invocation.getPropertySet())); |
| 223 | |
| 224 | collector.add(invocable.invoke(context, iv, executorService)); |
| 225 | } else { |
| 226 | if (args[idx] instanceof ArgumentIterator) { |
| 227 | ArgumentIterator<Object> ai = (ArgumentIterator<Object>) args[idx]; |
| 228 | while (ai.hasNext()) { |
| 229 | args[idx] = convert(ai.next(), idx); |
| 230 | _invoke(context, invocation, executorService, args, idx+1, collector); |
| 231 | } |
| 232 | } else { |
| 233 | if (args[idx]!=null && !parameterTypes[idx].isInstance(args[idx])) { |
| 234 | args[idx] = convert(args[idx], idx); |
| 235 | } |
| 236 | _invoke(context, invocation, executorService, args, idx+1, collector); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
| 242 | private Object convert(Object arg, int idx) { |
| 243 | if (arg==null || parameterTypes[idx].isInstance(arg)) { |
| 244 | return null; |
| 245 | } |
| 246 | Object ret = converter.convert(arg, parameterTypes[idx], context); |
| 247 | if (ret==null) { |
| 248 | throw new ConversionException("Cannot convert "+arg+" to "+parameterTypes[idx]); |
| 249 | } |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public Class<Future<Iterable<R>>> getReturnType() { |
| 255 | // Strange, but doesn't compile otherwise. |
| 256 | return (Class<Future<Iterable<R>>>) (Class) Future.class; |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public Class<?>[] getParameterTypes() { |
| 261 | return invocable.getParameterTypes(); |
| 262 | } |
| 263 | |
| 264 | } |