KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > env > CallbackFunction


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.env;
31
32 import com.caucho.quercus.program.AbstractFunction;
33
34 /**
35  * Represents a call to a function.
36  */

37 public class CallbackFunction extends Callback {
38  // public static final CallbackFunction INVALID_CALLBACK = new CallbackFunction(null, "Invalid Callback");
39

40   private Env _env;
41   private String JavaDoc _funName;
42
43   private AbstractFunction _fun;
44
45  // private boolean _isInvalid = false;
46

47   public CallbackFunction(Env env, String JavaDoc funName)
48   {
49     _env = env;
50     _funName = funName;
51   }
52
53   public CallbackFunction(AbstractFunction fun)
54   {
55     _fun = fun;
56   }
57
58   public CallbackFunction(AbstractFunction fun, String JavaDoc funName)
59   {
60     _fun = fun;
61     _funName = funName;
62   }
63 /*
64   public CallbackFunction(String funName, boolean isInvalid)
65   {
66     _funName = funName;
67     _isInvalid = isInvalid;
68   }
69   */

70
71   /**
72    * Allow subclasses to set the abstract function directly.
73    */

74   protected void setFunction(AbstractFunction fun)
75   {
76     _fun = fun;
77   }
78   
79   public boolean isValid()
80   {
81     if (_fun != null)
82       return true;
83
84     return _env.findFunction(_funName) != null;
85
86     //return _isInvalid;
87
}
88
89   /**
90    * Evaluates the callback with no arguments.
91    *
92    * @param env the calling environment
93    */

94   public Value call(Env env)
95   {
96     return getFunction().call(env);
97   }
98
99   /**
100    * Evaluates the callback with 1 argument.
101    *
102    * @param env the calling environment
103    */

104   public Value call(Env env, Value a1)
105   {
106     return getFunction().call(env, a1);
107   }
108
109   /**
110    * Evaluates the callback with 2 arguments.
111    *
112    * @param env the calling environment
113    */

114   public Value call(Env env, Value a1, Value a2)
115   {
116     return getFunction().call(env, a1, a2);
117   }
118
119   /**
120    * Evaluates the callback with 3 arguments.
121    *
122    * @param env the calling environment
123    */

124   public Value call(Env env, Value a1, Value a2, Value a3)
125   {
126     return getFunction().call(env, a1, a2, a3);
127   }
128
129   /**
130    * Evaluates the callback with 3 arguments.
131    *
132    * @param env the calling environment
133    */

134   public Value call(Env env, Value a1, Value a2, Value a3,
135                  Value a4)
136   {
137     return getFunction().call(env, a1, a2, a3, a4);
138   }
139
140   /**
141    * Evaluates the callback with 3 arguments.
142    *
143    * @param env the calling environment
144    */

145   public Value call(Env env, Value a1, Value a2, Value a3,
146             Value a4, Value a5)
147   {
148     return getFunction().call(env, a1, a2, a3, a4, a5);
149   }
150
151   public Value call(Env env, Value []args)
152   {
153     return getFunction().call(env, args);
154   }
155
156   public String JavaDoc getFunctionName()
157   {
158     return _funName;
159   }
160
161   public String JavaDoc getCallbackName()
162   {
163     return _funName;
164   }
165
166   public AbstractFunction getFunction()
167   {
168     if (_fun == null)
169       _fun = _env.getFunction(_funName);
170
171     return _fun;
172   }
173
174   public String JavaDoc toString()
175   {
176     return getClass().getName() + '[' + _funName + ']';
177   }
178
179   public boolean isInternal()
180   {
181     return getFunction() instanceof JavaInvoker;
182   }
183 }
184
Popular Tags