001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.internal;
016:
017: import org.apache.tapestry.ioc.IOCUtilities;
018: import org.apache.tapestry.ioc.Registry;
019: import org.apache.tapestry.ioc.RegistryBuilder;
020: import org.apache.tapestry.ioc.def.ContributionDef;
021: import org.apache.tapestry.ioc.def.ModuleDef;
022: import org.apache.tapestry.ioc.internal.util.InternalUtils;
023: import org.apache.tapestry.ioc.services.SymbolProvider;
024: import org.apache.tapestry.services.Alias;
025: import org.apache.tapestry.services.TapestryModule;
026: import org.apache.tapestry.test.PageTester;
027:
028: /**
029: * This class is used to build the {@link Registry}. The Registry contains
030: * {@link org.apache.tapestry.ioc.services.TapestryIOCModule} and {@link TapestryModule}, any
031: * modules identified by {@link #addModules(RegistryBuilder)}, plus the application module.
032: * <p>
033: * The application module is optional.
034: * <p>
035: * The application module is identified as <em>package</em>.services.<em>appName</em>Module,
036: * where <em>package</em> and the <em>appName</em> are specified by the caller.
037: */
038: public class TapestryAppInitializer {
039: private final SymbolProvider _appProvider;
040:
041: private final String _appPackage;
042:
043: private final String _appName;
044:
045: private final String _aliasMode;
046:
047: private final long _startTime;
048:
049: private long _registryCreatedTime;
050:
051: private final RegistryBuilder _builder = new RegistryBuilder();
052:
053: public TapestryAppInitializer(String appPackage, String appName,
054: String aliasMode) {
055: this (new SingleKeySymbolProvider(
056: InternalConstants.TAPESTRY_APP_PACKAGE_PARAM,
057: appPackage), appName, aliasMode);
058: }
059:
060: /**
061: * @param appProvider
062: * provides symbols for the application (normally, from the ServletContext init
063: * parameters)
064: * @param appName
065: * the name of the application (i.e., the name of the application servlet)
066: * @param aliasMode
067: * the mode, used by the {@link Alias} service, normally "servlet"
068: * @param serviceOverrides
069: * specific service overrides (used by {@link PageTester}
070: * @param moduleDefs
071: * additional module definitions to be mixed in to those automatically located
072: */
073: public TapestryAppInitializer(SymbolProvider appProvider,
074: String appName, String aliasMode) {
075: _appProvider = appProvider;
076:
077: _appPackage = _appProvider
078: .valueForSymbol(InternalConstants.TAPESTRY_APP_PACKAGE_PARAM);
079:
080: _appName = appName;
081: _aliasMode = aliasMode;
082:
083: _startTime = System.currentTimeMillis();
084:
085: IOCUtilities.addDefaultModules(_builder);
086:
087: // This gets added automatically.
088:
089: addModules(TapestryModule.class);
090:
091: String className = _appPackage + ".services."
092: + InternalUtils.capitalize(_appName) + "Module";
093:
094: try {
095: // This class is possibly loaded by a parent class loader of the application class
096: // loader. The context class loader should have the approprite view to the module class,
097: // if any.
098:
099: Class moduleClass = Thread.currentThread()
100: .getContextClassLoader().loadClass(className);
101:
102: _builder.add(moduleClass);
103: } catch (ClassNotFoundException ex) {
104: // That's OK, not all applications will have a module class, even though any
105: // non-trivial application will.
106: }
107:
108: // Add a synthetic module that contributes symbol sources.
109:
110: addSyntheticSymbolSourceModule();
111: }
112:
113: /**
114: * Adds additional modules.
115: *
116: * @param moduleDefs
117: */
118: public void addModules(ModuleDef... moduleDefs) {
119: for (ModuleDef def : moduleDefs)
120: _builder.add(def);
121: }
122:
123: public void addModules(Class... moduleBuilderClasses) {
124: _builder.add(moduleBuilderClasses);
125: }
126:
127: private void addSyntheticSymbolSourceModule() {
128: ContributionDef symbolSourceContribution = new SyntheticSymbolSourceContributionDef(
129: "ServletContext", _appProvider,
130: "before:ApplicationDefaults");
131:
132: ContributionDef aliasModeContribution = new SyntheticSymbolSourceContributionDef(
133: "AliasMode", new SingleKeySymbolProvider(
134: InternalConstants.TAPESTRY_ALIAS_MODE_SYMBOL,
135: _aliasMode), "before:ServletContext");
136:
137: ContributionDef appNameContribution = new SyntheticSymbolSourceContributionDef(
138: "AppName", new SingleKeySymbolProvider(
139: InternalConstants.TAPESTRY_APP_NAME_SYMBOL,
140: _appName), "before:ServletContext");
141:
142: _builder.add(new SyntheticModuleDef(symbolSourceContribution,
143: aliasModeContribution, appNameContribution));
144: }
145:
146: public Registry getRegistry() {
147: _registryCreatedTime = System.currentTimeMillis();
148:
149: return _builder.build();
150: }
151:
152: /**
153: * @return the system time (in ms) when the registry has been created successfully.
154: */
155: public long getRegistryCreatedTime() {
156: return _registryCreatedTime;
157: }
158:
159: /**
160: * @return the time when the initialization was started.
161: */
162: public long getStartTime() {
163: return _startTime;
164: }
165: }
|