00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.ext.implicit;
00032
00033 import org.objectweb.proactive.core.UniqueID;
00034 import org.objectweb.proactive.core.body.request.BlockingRequestQueueImpl;
00035 import org.objectweb.proactive.core.body.request.Request;
00036 import org.objectweb.proactive.core.mop.MOP;
00037
00038
00039 public class ImplicitService extends BlockingRequestQueueImpl
00040 implements Implicit {
00041
00045 transient protected java.util.Hashtable shortcuts;
00046
00050 public final static java.util.Hashtable shortcutsTables = new java.util.Hashtable();
00051
00055 protected java.util.Hashtable sync;
00056
00057 public ImplicitService(UniqueID id) {
00058 super(id);
00059 sync = new java.util.Hashtable();
00060 }
00061
00073 public void forbid(String shortcut, String condition)
00074 throws InvalidAssociateDeclaration {
00075 java.lang.reflect.Method cond;
00076 System.out.println("ImplicitBody: forbid() for " + shortcut);
00077
00078 if (this.shortcuts == null) {
00079
00080 this.setShortcutsTable();
00081 }
00082
00083
00084 if (!(this.shortcuts.containsKey(shortcut))) {
00085 throw new InvalidAssociateDeclaration("No shortcut " + shortcut +
00086 " defined");
00087 }
00088
00089
00090
00091 cond = null;
00092
00093
00094
00095 if (!(cond.getReturnType().equals(java.lang.Boolean.TYPE))) {
00096 throw new InvalidAssociateDeclaration("Method " + condition +
00097 " does not return a boolean as expected");
00098 }
00099
00100
00101 this.sync.put(shortcut, cond);
00102
00103 return;
00104 }
00105
00106 public void run() {
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 }
00133
00134 public synchronized Request getOldestReadyRequest() {
00135 java.util.Iterator iterator = iterator();
00136 while (iterator.hasNext()) {
00137 Request r = (Request) iterator.next();
00138 java.lang.reflect.Method target = null;
00139
00140
00141 String methodName;
00142 if (this.shortcuts.contains(target)) {
00143 methodName = this.getShortcut(target);
00144 } else {
00145 methodName = target.getName();
00146 }
00147
00148
00149 java.lang.reflect.Method cond = (java.lang.reflect.Method) this.sync.get(methodName);
00150 if (cond != null) {
00151
00152 boolean test = this.testCondition(cond);
00153
00154
00155 if (test) {
00156 return r;
00157 }
00158 }
00159 }
00160 return null;
00161 }
00162
00163 private boolean testCondition(java.lang.reflect.Method cond) {
00164 boolean result = true;
00165 Object[] args = new Object[0];
00166
00167 System.out.println("ImplicitBody: testingCondition() on method" + cond);
00168
00169 try {
00170 result = false;
00171 } catch (Exception e) {
00172 System.err.println("Exception during invocation of method " +
00173 cond.getName());
00174 e.printStackTrace();
00175 }
00176 return result;
00177 }
00178
00179 public synchronized void serveOldestThatIsNot(String s) {
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 }
00193
00197 public void dumpShortcutsTable() {
00198 java.util.Enumeration en;
00199 String currentshc;
00200 java.lang.reflect.Method currentmethod;
00201 en = this.shortcuts.keys();
00202 System.out.println("--- Dump of the shortcuts table ---");
00203 while (en.hasMoreElements()) {
00204 currentshc = (String) en.nextElement();
00205 currentmethod = (java.lang.reflect.Method) this.shortcuts.get(currentshc);
00206 System.out.println(currentshc + "/" + currentmethod.toString());
00207 }
00208 }
00209
00210 public String getShortcut(java.lang.reflect.Method m) {
00211 java.util.Enumeration en;
00212 String currentshc;
00213 java.lang.reflect.Method currentmethod;
00214
00215 en = this.shortcuts.keys();
00216
00217 while (en.hasMoreElements()) {
00218 currentshc = (String) en.nextElement();
00219 currentmethod = (java.lang.reflect.Method) this.shortcuts.get(currentshc);
00220
00221 if (currentmethod.equals(m)) {
00222 return currentshc;
00223 }
00224 }
00225 return null;
00226 }
00227
00228 public void addShortcut(String shortcut, String name, String[] argumentstype) {
00229 int index = 0;
00230 int size;
00231 Class[] args;
00232 java.lang.reflect.Method met = null;
00233
00234
00235 if (this.shortcuts.containsKey(shortcut)) {
00236 System.err.println("Shortcut '" + shortcut + "' already exists.");
00237 return;
00238 }
00239
00240 size = argumentstype.length;
00241 args = new Class[size];
00242
00243
00244 try {
00245 for (index = 0; index < size; index++) {
00246
00247 args[index] = MOP.forName(argumentstype[index]);
00248 }
00249 met = null;
00250 } catch (ClassNotFoundException e) {
00251 System.err.println("Class not found : " + argumentstype[index]);
00252 return;
00253 }
00254
00255
00256
00257
00258
00259 this.shortcuts.put(shortcut, met);
00260 }
00261
00267 public void fillShortcutsTable() {
00268 java.lang.reflect.Method[] mets;
00269 java.lang.reflect.Method currentmethod;
00270 String currentshc;
00271 int size;
00272 int index;
00273 int index2;
00274
00275 mets = null;
00276 size = mets.length;
00277
00278 for (index = 0; index < size; index++) {
00279 currentmethod = mets[index];
00280 currentshc = currentmethod.getName();
00281
00282
00283
00284
00285
00286
00287
00288 }
00289 }
00290
00291 public void setShortcutsTable() {
00292
00293
00294 String reifiedObjectClassName;
00295
00296
00297 if (this.shortcuts == null) {
00298 this.shortcuts = new java.util.Hashtable();
00299 this.fillShortcutsTable();
00300
00301
00302 } else {
00303
00304 }
00305 }
00306
00310 public static boolean isOverloaded(java.lang.reflect.Method met,
00311 Class reifiedObjectClass) {
00312 int n = 0;
00313 java.lang.reflect.Method[] mets = reifiedObjectClass.getMethods();
00314 int size = mets.length;
00315 for (int index = 0; index < size; index++) {
00316 java.lang.reflect.Method currentmethod = mets[index];
00317 String currentshc = currentmethod.getName();
00318 if (currentshc.equals(met.getName())) {
00319 n++;
00320 }
00321 }
00322 return (n > 1);
00323 }
00324 }