KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > InvokeThread


1 /*
2   Copyright (C) 2002-2004 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.aspects.gui;
20
21 import java.util.Arrays JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import org.apache.log4j.Logger;
25 import org.objectweb.jac.core.Collaboration;
26 import org.objectweb.jac.core.rtti.AbstractMethodItem;
27 import org.objectweb.jac.core.rtti.ConstructorItem;
28 import org.objectweb.jac.core.rtti.MethodItem;
29 import org.objectweb.jac.util.Exceptions;
30
31 /**
32  * This class allows the programmer to invoke a given method in a new
33  * thread.
34  *
35  * <p>JAC programmers should use JAC to pass some attibutes of the
36  * current thread to the new thread.
37  *
38  * <p>Typical use:
39  * <ul><pre>
40  * InvokeThread.run( myObject, "myMethodName",
41  * new Object[] { arg0, ...},
42  * new String[] { attrName0, ... },
43  * new Object[] { attrValue0, ... },
44  * new String[] { lattrName0, ... },
45  * new Object[] { lattrValue0, ... } );
46  * </pre></ul> */

47
48 public class InvokeThread extends Thread JavaDoc {
49     static Logger logger = Logger.getLogger("gui.threads");
50     static Logger loggerInput = Logger.getLogger("gui.input");
51
52     InvokeEvent invoke;
53     Object JavaDoc returnValue;
54     String JavaDoc[] attrNames;
55     Object JavaDoc[] attrValues;
56     String JavaDoc[] lattrNames;
57     Object JavaDoc[] lattrValues;
58     Collaboration parentCollaboration;
59     boolean showResult = true;
60
61     /**
62      * Runs a method in a new thread and sets a display for this
63      * thread.
64      *
65      * @param invoke the invocation to perform
66      * @param attrNames the attribute names to set into the new thread
67      * collaboration
68      * @param attrValues the values of these attributes
69      * @param lattrNames the local attribute names to set into the new
70      * thread collaboration
71      * @param lattrValues the values of these local attributes
72      */

73     public static InvokeThread run(InvokeEvent invoke,
74                                    String JavaDoc[] attrNames, Object JavaDoc[] attrValues,
75                                    String JavaDoc[] lattrNames, Object JavaDoc[] lattrValues) {
76         InvokeThread ret = new InvokeThread(invoke);
77         ret.parentCollaboration = Collaboration.get();
78         ret.attrNames = attrNames;
79         ret.attrValues = attrValues;
80         ret.lattrNames = lattrNames;
81         ret.lattrValues = lattrValues;
82         ret.start();
83         return ret;
84     }
85
86     /**
87      * Runs a method in a new thread and sets a display for this
88      * thread. Do not show any results on the display.
89      *
90      * @param invoke the invocation to perform
91      * @param attrNames the attribute names to set into the new thread
92      * collaboration
93      * @param attrValues the values of these attributes
94      * @param lattrNames the local attribute names to set into the new
95      * thread collaboration
96      * @param lattrValues the values of these local attributes
97      */

98     public static
99     InvokeThread quietRun(InvokeEvent invoke,
100                           String JavaDoc[] attrNames, Object JavaDoc[] attrValues,
101                           String JavaDoc[] lattrNames, Object JavaDoc[] lattrValues)
102     {
103         InvokeThread ret = new InvokeThread(invoke);
104         ret.parentCollaboration = Collaboration.get();
105         ret.attrNames = attrNames;
106         ret.attrValues = attrValues;
107         ret.lattrNames = lattrNames;
108         ret.lattrValues = lattrValues;
109         // ret.showResult = false;
110
ret.start();
111         return ret;
112     }
113
114     /**
115      * Runs a method in a new thread.
116      *
117      * @param invoke the invocation to perform
118      */

119     public static InvokeThread run(InvokeEvent invoke) {
120         InvokeThread ret = new InvokeThread(invoke);
121         ret.start();
122         return ret;
123     }
124
125     /**
126      * Creates a new thread that will invoke a method when started.
127      *
128      * <p>The programmer should use the static <code>run</code>
129      * methods.
130      *
131      * @param invoke the invocation to perform
132      */

133     public InvokeThread(InvokeEvent invoke) {
134         this.invoke = invoke;
135     }
136
137     /**
138      * Creates a new thread that will invoke a method when started.
139      *
140      * <p>The programmer should use the static <code>run</code>
141      * methods.
142      *
143      * @param invoke the invocation to perform
144      * @param attrNames name of attributes to add to the context
145      * before invoking the method (may be null)
146      * @param attrValues values of the attributes (may be null)
147      * @param lattrNames name of local attributes to add to the context (may be null)
148      * before invoking the method (may be null)
149      * @param lattrValues values of the local attributes (may be null)
150      */

151     public InvokeThread(InvokeEvent invoke,
152                         String JavaDoc[] attrNames, Object JavaDoc[] attrValues,
153                         String JavaDoc[] lattrNames, Object JavaDoc[] lattrValues) {
154         this.invoke = invoke;
155         this.parentCollaboration = Collaboration.get();
156         this.attrNames = attrNames;
157         this.attrValues = attrValues;
158         this.lattrNames = lattrNames;
159         this.lattrValues = lattrValues;
160         this.showResult = false;
161     }
162
163     /**
164      * Runs the thread (and invoke the method that was given to the
165      * constructor with the right display in the collaboration).
166      *
167      * <p>Do not call this method directly.
168      */

169     public void run() {
170         Collaboration collab = Collaboration.get();
171         Object JavaDoc[] parameters = invoke.getParameters();
172         AbstractMethodItem method = invoke.getMethod();
173         Object JavaDoc substance = invoke.getSubstance();
174
175         logger.debug("invokeThread "+this+": "+invoke);
176         if (parentCollaboration!=null) {
177             Iterator JavaDoc it = parentCollaboration.attributeNames().iterator();
178             while (it.hasNext()) {
179                 String JavaDoc attrName = (String JavaDoc)it.next();
180                 collab.addAttribute(
181                     attrName,parentCollaboration.getAttribute(attrName));
182             }
183             logger.debug("application = "+parentCollaboration.getCurApp());
184             collab.setCurApp(parentCollaboration.getCurApp());
185         }
186         if (attrNames != null) {
187             for (int i=0; i<attrNames.length; i++) {
188                 logger.debug("setting attribute " + attrNames[i] +
189                           " to " + attrValues[i]);
190                 collab.addAttribute(
191                     attrNames[i], attrValues[i]);
192             }
193         }
194         if (lattrNames != null) {
195             for (int i=0; i<lattrNames.length; i++) {
196                 logger.debug("setting local attribute " +
197                           lattrNames[i] + " to " + lattrValues[i]);
198                 collab.addAttribute(
199                     lattrNames[i], lattrValues[i]);
200             }
201         }
202         DisplayContext context = (DisplayContext)collab
203             .getAttribute(GuiAC.DISPLAY_CONTEXT);
204
205         CustomizedDisplay display = context.getDisplay();
206         try {
207             logger.debug("InvokeThread " + invoke);
208          
209             Class JavaDoc[] paramTypes = method.getParameterTypes();
210             if (paramTypes.length != invoke.getParameters().length)
211                 throw new RuntimeException JavaDoc("Wrong number of parameters ("+
212                                            parameters.length+") for "+method);
213             for (int i=0; i<parameters.length; i++) {
214                 if (parameters[i]==null) {
215                     if (paramTypes[i] == float.class) {
216                         method.setParameter(parameters,i,new Float JavaDoc(0.0));
217                     } else if (paramTypes[i] == long.class) {
218                         method.setParameter(parameters,i,new Long JavaDoc(0));
219                     } else if (paramTypes[i] == double.class) {
220                         method.setParameter(parameters,i,new Double JavaDoc(0.0));
221                     } else if (paramTypes[i] == byte.class) {
222                         method.setParameter(parameters,i,new Byte JavaDoc((byte)0));
223                     } else if (paramTypes[i] == char.class) {
224                         method.setParameter(parameters,i,new Character JavaDoc(' '));
225                     } else if (paramTypes[i] == short.class) {
226                         method.setParameter(parameters,i,new Short JavaDoc((short)0));
227                     } else if (paramTypes[i] == int.class) {
228                         method.setParameter(parameters,i,new Integer JavaDoc(0));
229                     } else if (paramTypes[i] == boolean.class) {
230                         method.setParameter(parameters,i,Boolean.FALSE);
231                     }
232                 }
233             }
234
235             if (method instanceof ConstructorItem) {
236                 returnValue = ((ConstructorItem)method).newInstance(parameters);
237             } else {
238                 returnValue = ((MethodItem)method).invoke(substance, parameters);
239             }
240
241             if (display != null) {
242                 display.onInvocationReturn(substance,method);
243             }
244             List JavaDoc hooks = (List JavaDoc)method.getAttribute(GuiAC.POST_INVOKE_HOOKS);
245             if (hooks!=null) {
246                 Iterator JavaDoc i = hooks.iterator();
247                 while (i.hasNext()) {
248                     AbstractMethodItem hook = (AbstractMethodItem)i.next();
249                     try {
250                         loggerInput.debug("Invoking post hook "+hook.getName());
251                         hook.invoke(
252                             null,
253                             new Object JavaDoc[] {invoke});
254                     } catch (Exception JavaDoc e) {
255                         loggerInput.error("Post invoke hook for "+
256                                           substance+"."+
257                                           method.getFullName()+" failed",e);
258                     }
259                 }
260             }
261
262             if (method.getType() != void.class) {
263                 if (display != null && showResult) {
264
265                     if (collab.getAttribute(GuiAC.OPEN_VIEW)!=null) {
266                         display.openView(returnValue);
267                     } else {
268                         if(method.getAttribute(GuiAC.SMALL_TARGET_CONTAINER)!=null) {
269                             EventHandler.get().onSelection(context,method,returnValue,null,null,false);
270                         } else if (returnValue instanceof HandlerResult) {
271                             EventHandler.get().handleResult(context,(HandlerResult)returnValue);
272                         } else {
273                             display.show(returnValue);
274                         }
275                     }
276                 }
277             } else {
278                 if (display != null && showResult)
279                     display.refresh();
280             }
281         } catch (Exception JavaDoc e) {
282             Throwable JavaDoc te = Exceptions.getTargetException(e);
283             logger.debug(this+" TargetException is "+te);
284             if (te instanceof TimeoutException) {
285                 logger.debug(this+" Timeout");
286                 DialogView dialog = ((TimeoutException)te).getDialog();
287                 display.closeWindow(dialog,false);
288                 display.addTimedoutDialog(dialog);
289             } else if (display != null && showResult) {
290                 if (!(te instanceof org.objectweb.jac.util.VoidException))
291                     display.showModal(te,"Error","",context.getWindow(),false,false,true);
292                 else
293                     display.refresh();
294             }
295         }
296         logger.debug("invokeThread done "+this+": "+
297                      substance+"."+method+Arrays.asList(parameters));
298     }
299
300     Object JavaDoc getReturnValue() {
301         return returnValue;
302     }
303 }
304
305 class NotAvailableException extends Exception JavaDoc {}
306
Popular Tags