KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > agent > SyncNotification


1 /*
2  * Copyright (C) 2004 - ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library 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 GNU
12  * 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 library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * Initial developer(s): ScalAgent Distributed Technologies
20  * Contributor(s):
21  */

22 package fr.dyade.aaa.agent;
23
24 /**
25  * This notification is used to synchronously call a
26  * local agent from a collocated thread.
27  */

28 public class SyncNotification extends Notification {
29   
30   private transient Context ctx;
31
32   protected SyncNotification() {
33     persistent = false;
34     ctx = new Context(this);
35   }
36
37   public Object JavaDoc[] invoke(AgentId to)
38     throws InterruptedException JavaDoc, Exception JavaDoc {
39     return ctx.invoke(to);
40   }
41
42   public void Throw(Exception JavaDoc exc) {
43     if (ctx != null) {
44       ctx.Throw(exc);
45     }
46   }
47
48   public void Return(Object JavaDoc[] values) {
49     if (ctx != null) {
50       ctx.Return(values);
51     }
52   }
53
54   public Object JavaDoc getValue(int index) {
55     if (ctx != null) {
56       return ctx.getValue(index);
57     } else return null;
58   }
59
60   public final Exception JavaDoc getException() {
61     if (ctx != null) {
62       return ctx.getException();
63     } else return null;
64   }
65
66   static class Result {
67     Object JavaDoc[] values;
68     Exception JavaDoc exc;
69   }
70
71   public static class Context {
72     private Notification syncRequest;
73
74     private Result res;
75
76     public Context(Notification syncRequest) {
77       this.syncRequest = syncRequest;
78       res = new Result();
79     }
80
81     public synchronized Object JavaDoc[] invoke(AgentId to)
82       throws InterruptedException JavaDoc, Exception JavaDoc {
83       Channel.sendTo(to, syncRequest);
84       wait();
85       if (res.exc != null) {
86         throw res.exc;
87       } else {
88         return res.values;
89       }
90     }
91
92     public synchronized void Throw(Exception JavaDoc exc) {
93       res.exc = exc;
94       notify();
95     }
96
97     public synchronized void Return(Object JavaDoc[] values) {
98       res.values = values;
99       notify();
100     }
101
102     public Object JavaDoc getValue(int index) {
103       if (res.values != null) {
104         return res.values[index];
105       } else return null;
106     }
107
108     public final Exception JavaDoc getException() {
109       return res.exc;
110     }
111   }
112 }
113
Popular Tags