KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > shell > ModuleSpacePropertyOracle


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.shell;
17
18 import com.google.gwt.core.ext.BadPropertyValueException;
19 import com.google.gwt.core.ext.PropertyOracle;
20 import com.google.gwt.core.ext.TreeLogger;
21 import com.google.gwt.dev.cfg.Properties;
22 import com.google.gwt.dev.cfg.Property;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  * Implements a {@link PropertyOracle} in terms of a module space, which makes
29  * it possible to execute property providers.
30  */

31 public class ModuleSpacePropertyOracle implements PropertyOracle {
32
33   private final Map JavaDoc prevAnswers = new HashMap JavaDoc();
34
35   private final Properties props;
36
37   private final ModuleSpace space;
38
39   public ModuleSpacePropertyOracle(Properties props, ModuleSpace space) {
40     this.space = space;
41     this.props = props;
42   }
43
44   /**
45    * Executes JavaScript to find the property value.
46    */

47   public String JavaDoc getPropertyValue(TreeLogger logger, String JavaDoc propertyName)
48       throws BadPropertyValueException {
49     if (propertyName == null) {
50       throw new NullPointerException JavaDoc("propertyName");
51     }
52
53     Property prop = props.find(propertyName);
54     if (prop == null) {
55       // Don't know this property; that's not good.
56
//
57
throw new BadPropertyValueException(propertyName);
58     }
59
60     // Check if this property has already been queried for; if so, return
61
// the same answer. This is necessary to match web mode behavior since
62
// property providers are only called once. We cache even values that
63
// cause exceptions to be thrown to make sure we are consistent even
64
// in throwing exceptions for the same property.
65
if (prevAnswers.containsKey(propertyName)) {
66       return (String JavaDoc) prevAnswers.get(propertyName);
67     } else {
68       String JavaDoc value = computePropertyValue(logger, propertyName, prop);
69       prevAnswers.put(propertyName, value);
70       return value;
71     }
72   }
73
74   /**
75    * Returns the value of the specified property.
76    *
77    * @throws BadPropertyValueException if the property value could not be
78    * computed, or if the returned result is not a legal value for this
79    * property.
80    */

81   private String JavaDoc computePropertyValue(TreeLogger logger, String JavaDoc propertyName,
82       Property prop) throws BadPropertyValueException {
83     String JavaDoc value;
84     // If there is an active value, use that.
85
//
86
value = prop.getActiveValue();
87
88     // In case there isn't an active value...
89
if (value == null) {
90       // Invokes the script function.
91
//
92
try {
93         // Invoke the property provider function in JavaScript.
94
//
95
value = space.invokeNativeString("__gwt_getProperty", null,
96             new Class JavaDoc[] {String JavaDoc.class}, new Object JavaDoc[] {prop.getName()});
97       } catch (Throwable JavaDoc e) {
98         // Treat as an unknown value.
99
//
100
String JavaDoc msg = "Error while executing the JavaScript provider for property '"
101             + propertyName + "'";
102         logger.log(TreeLogger.ERROR, msg, e);
103         throw new BadPropertyValueException(propertyName, "<failed to compute>");
104       }
105     }
106
107     // value may be null if the provider returned an unknown property value.
108
if (prop.isKnownValue(value)) {
109       return value;
110     } else {
111       // Bad value due to the provider returning an unknown value.
112
// The fact that the provider returned an invalid value will also
113
// have been reported to the JS bad property value handler function.
114
throw new BadPropertyValueException(propertyName, value);
115     }
116   }
117 }
118
Popular Tags