KickJava   Java API By Example, From Geeks To Geeks.

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


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.TreeLogger;
19 import com.google.gwt.core.ext.UnableToCompleteException;
20 import com.google.gwt.core.ext.typeinfo.TypeOracle;
21 import com.google.gwt.dev.cfg.ModuleDef;
22 import com.google.gwt.dev.cfg.ModuleDefLoader;
23 import com.google.gwt.dev.cfg.Rules;
24 import com.google.gwt.dev.jdt.ByteCodeCompiler;
25 import com.google.gwt.dev.jdt.RebindOracle;
26 import com.google.gwt.dev.jdt.SourceOracle;
27
28 import java.io.File JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  * Provides an environment for a {@link com.google.gwt.dev.shell.ModuleSpace}
34  * that works appropriately for the development shell.
35  */

36 public class ShellModuleSpaceHost implements ModuleSpaceHost {
37
38   private static Map JavaDoc byteCodeCompilersByModule = new HashMap JavaDoc();
39
40   protected final File JavaDoc genDir;
41
42   protected final TypeOracle typeOracle;
43
44   private CompilingClassLoader classLoader;
45
46   private final TreeLogger logger;
47
48   private final ModuleDef module;
49
50   private final File JavaDoc outDir;
51
52   private RebindOracle rebindOracle;
53
54   private ModuleSpace space;
55
56   /**
57    * @param module the module associated with the hosted module space
58    */

59   public ShellModuleSpaceHost(TreeLogger logger, TypeOracle typeOracle,
60       ModuleDef module, File JavaDoc genDir, File JavaDoc outDir) {
61     this.logger = logger;
62     this.typeOracle = typeOracle;
63     this.module = module;
64     this.genDir = genDir;
65
66     // Combine the user's output dir with the module name to get the
67
// module-specific output dir.
68
this.outDir = new File JavaDoc(outDir, module.getName());
69   }
70
71   public CompilingClassLoader getClassLoader() {
72     checkForModuleSpace();
73     return classLoader;
74   }
75
76   public String JavaDoc[] getEntryPointTypeNames() {
77     checkForModuleSpace();
78     return module.getEntryPointTypeNames();
79   }
80
81   public TreeLogger getLogger() {
82     return logger;
83   }
84
85   public void onModuleReady(ModuleSpace readySpace)
86       throws UnableToCompleteException {
87     this.space = readySpace;
88
89     // Create a host for the hosted mode compiler.
90
// We add compilation units to it as deferred binding generators write them.
91
//
92
SourceOracle srcOracle = new HostedModeSourceOracle(typeOracle);
93
94     // Create or find the compiler to be used by the compiling class loader.
95
//
96
ByteCodeCompiler compiler = getOrCreateByteCodeCompiler(srcOracle);
97
98     // Establish an environment for JavaScript property providers to run.
99
//
100
ModuleSpacePropertyOracle propOracle = new ModuleSpacePropertyOracle(
101         module.getProperties(), readySpace);
102
103     // Set up the rebind oracle for the module.
104
// It has to wait until now because we need to inject javascript.
105
//
106
Rules rules = module.getRules();
107     rebindOracle = new StandardRebindOracle(typeOracle, propOracle, rules,
108         genDir, outDir, module.getCacheManager());
109
110     // Create a completely isolated class loader which owns all classes
111
// associated with a particular module. This effectively builds a
112
// separate 'domain' for each running module, so that they all behave as
113
// though they are running separately. This allows the shell to run
114
// multiple modules, both in succession and simultaneously, without getting
115
// confused.
116
//
117
// Note that the compiling class loader has no parent. This keeps it from
118
// accidentally 'escaping' its domain and loading classes from the system
119
// class loader (the one that loaded the shell itself).
120
//
121
classLoader = new CompilingClassLoader(logger, compiler, typeOracle);
122   }
123
124   public String JavaDoc rebind(TreeLogger rebindLogger, String JavaDoc sourceTypeName)
125       throws UnableToCompleteException {
126     checkForModuleSpace();
127     return rebindOracle.rebind(rebindLogger, sourceTypeName);
128   }
129
130   ByteCodeCompiler getOrCreateByteCodeCompiler(SourceOracle srcOracle) {
131     ByteCodeCompiler compiler;
132     synchronized (byteCodeCompilersByModule) {
133       compiler = (ByteCodeCompiler) byteCodeCompilersByModule.get(module);
134       if (compiler == null) {
135         compiler = new ByteCodeCompiler(srcOracle, module.getCacheManager());
136         if (ModuleDefLoader.getEnableCachingModules()) {
137           byteCodeCompilersByModule.put(module, compiler);
138         }
139       }
140     }
141     return compiler;
142   }
143
144   private void checkForModuleSpace() {
145     if (space == null) {
146       throw new IllegalStateException JavaDoc("Module initialization error");
147     }
148   }
149 }
150
Popular Tags