KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > model > RtSingleValuePort


1 /**
2  * Copyright (C) 2002 Kelua SA
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 of the License, or (at your option) 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 USA
17  */

18
19 package org.objectweb.kilim.model;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Stack JavaDoc;
23
24 import org.objectweb.kilim.KilimException;
25 import org.objectweb.kilim.description.KILIM;
26 import org.objectweb.kilim.description.NamedElement;
27 import org.objectweb.kilim.description.Port;
28 import org.objectweb.kilim.description.TemplateDescription;
29
30 /**
31  * @author horn
32  */

33 public class RtSingleValuePort extends RtComponentInterface implements ComponentInterface {
34     //state fields
35
private RuntimeSource provider;
36     private Object JavaDoc resultValue;
37     private boolean gotAValue;
38     
39     //fields required for displaying error messages in case of multiple bindings on the unary port.
40
private boolean firstCall = true;
41     private int nbBound;
42     private boolean msgDisplayed;
43
44     /**
45      * The public constructor for run time descriptors of single value ports.
46      * @param aPort : the template port descriptor
47      * @param aContainer : either the containing component or slot
48      */

49     public RtSingleValuePort(Port aPort, ContainerElement aContainer) {
50         super(aPort, aContainer);
51     }
52     
53     /**
54      * @see org.objectweb.kilim.model.RuntimeSource#hasValue()
55      */

56     public boolean hasValue() {
57         return gotAValue;
58     }
59     
60     /**
61      * Method getBufferedValue : returns the current buffered value. This method returns null when invoked prior to the evaluation.
62      * @return Object
63      */

64     public Object JavaDoc getBufferedValue() {
65         return resultValue;
66     }
67     
68     /**
69      * @see org.objectweb.kilim.model.ComponentInterface#isSingleValueInterface()
70      */

71     public boolean isSingleValuePort() {
72         return true;
73     }
74     
75     /**
76      * @see org.objectweb.kilim.model.ComponentInterface#isCollectionInterface()
77      */

78     public boolean isCollectionPort() {
79         return false;
80     }
81     
82     /**
83      * @see org.objectweb.kilim.model.ComponentInterface#isProvider()
84      */

85     public boolean isProvider() {
86         return false;
87     }
88     
89     /**
90      * @see org.objectweb.kilim.model.ComponentInterface#isProperty()
91      */

92     public boolean isProperty() {
93         return false;
94     }
95
96     /**
97      * @see org.objectweb.kilim.model.ComponentElement#getLocalName()
98      */

99     public String JavaDoc getLocalName() {
100         NamedElement elem = (NamedElement) getElementDescription();
101         return elem.getLocalName();
102     }
103         
104     /**
105      * @see org.objectweb.kilim.model.RtComponentInterface#bindProvider(RuntimeSource, boolean)
106      */

107     public void bindProvider(RuntimeSource aProvider, boolean jReplace) throws KilimException {
108         //The Kilim2 runtime does not accept bindings to null providers. One should use either a RTNullElement or
109
//a RTExternalValue whose value is null.
110
if (aProvider == null) {
111             String JavaDoc tmpName = getContainingComponent().getTemplate().getName();
112             throw new KilimException("attempt to bind a null provider to an unary port " + getQualifiedName() + "(template : " + tmpName + ")");
113         }
114
115         //detection of illegal multiple binding on unary ports.
116
provider = aProvider;
117         nbBound++;
118                 
119         if (nbBound > 1 && !jReplace) {
120             if (!msgDisplayed) {
121                 msgDisplayed = true;
122                 TemplateDescription tmp0 = getElementDescription().getContainingTemplate();
123                 String JavaDoc lName = getLocalName();
124                 String JavaDoc tmpList = "";
125                 do {
126                     Iterator JavaDoc iter = tmp0.getBindings(lName, true);
127                     if (iter != KILIM.EMPTY_ITERATOR) {
128                         tmpList = tmpList + "\"" + tmp0.getName() + "\" ";
129                     }
130                 } while ((tmp0 = tmp0.getSuperTemplate()) != null);
131                 
132                 TemplateDescription tmp1 = getContainingComponent().getTemplate();
133                 
134                 if (tmp1 != tmp0) {
135                     do {
136                         Iterator JavaDoc iter = tmp1.getBindings(lName, true);
137                         if (iter != KILIM.EMPTY_ITERATOR) {
138                             tmpList = tmpList + " : " + tmp1.getName();
139                         }
140                     } while ((tmp1 = tmp1.getSuperTemplate()) != null);
141                 }
142             }
143                         
144             //System.err.println("warning : rebinding a unary interface " + getQualifiedName());
145
}
146     }
147     
148     /**
149      * @see org.objectweb.kilim.model.ComponentInterface#unbindProvider(RuntimeSource)
150      */

151     public void unbindProvider(RuntimeSource aProvider) throws KilimException {
152         if (provider == null) {
153             throw new KilimException("attempt to unbind a provider from the empty interface " + getQualifiedName());
154         }
155         
156         //unbinding a provider does not remove the previous buffered value (if any).
157
provider = null;
158     }
159     
160     /**
161      * @see org.objectweb.kilim.model.RuntimeSource#getValue()
162      */

163     protected Object JavaDoc specificGetValue() throws KilimException {
164         if (gotAValue) {
165             return resultValue;
166         }
167         
168         svpStack.push(this);
169             
170         //It is legal in Kilim2 grammar to have "unprovided ports" i.e. ports having no declared explicit or inlined provider.
171
//Kilim2 runtime handles them as ports bound to a external reference whose value is null.
172
if (provider == null) {
173             provider = new RtExternalValue(getLocalName() + KILIM.getUniqueIndex(), null);
174         } else {
175             resultValue = ((RuntimeSource) provider).getValue();
176             gotAValue = true;
177         }
178         svpStack.pop();
179         return resultValue;
180     }
181         
182     /**
183      * @see org.objectweb.kilim.model.ComponentInterface#bindValue(Object, RuntimeContext)
184      */

185     protected void specificBindValue(Object JavaDoc aValue) throws KilimException {
186         if (provider != null) {
187             throw new KilimException("attempt to bind a value to a unary port " + getQualifiedName() + " having a provider " + provider);
188         }
189         resultValue = aValue;
190         gotAValue = true;
191     }
192             
193     /**
194      * @see org.objectweb.kilim.model.RtComponentInterface#specificUnbindValue()
195      */

196     protected void specificUnbindValue() throws KilimException {
197         if (!gotAValue) {
198             throw new KilimException("attempt to unbind a Value from a port " + getQualifiedName() + " without value");
199         }
200         resultValue = null;
201         gotAValue = false;
202     }
203     
204     /**
205      * @see org.objectweb.kilim.model.RuntimeSource#checkValue(Stack)
206      */

207     public boolean checkValue(Stack JavaDoc exclude) throws KilimException {
208         if (hasValue()) {
209             return true;
210         }
211                 
212         int eSize = exclude.size();
213         for (int i = 0; i < eSize; i++) {
214             if (this == exclude.get(i)) {
215                 return false;
216             }
217         }
218         
219         if (provider == null) {
220             return false;
221         }
222
223         return provider.checkValue(exclude);
224     }
225 }
Popular Tags