| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.seasar.teeda.core.webapp; |
| 17 | |
|
| 18 | |
import java.io.InputStream; |
| 19 | |
|
| 20 | |
import javax.faces.application.StateManager; |
| 21 | |
import javax.faces.application.ViewHandler; |
| 22 | |
import javax.faces.internal.ConverterBuilder; |
| 23 | |
import javax.faces.internal.ConverterResource; |
| 24 | |
import javax.faces.internal.FacesConfigOptions; |
| 25 | |
import javax.faces.internal.HotDeployConverterBuilderImpl; |
| 26 | |
import javax.faces.internal.HotDeployValidatorBuilderImpl; |
| 27 | |
import javax.faces.internal.InternalConstants; |
| 28 | |
import javax.faces.internal.NormalConverterBuilderImpl; |
| 29 | |
import javax.faces.internal.NormalValidatorBuilderImpl; |
| 30 | |
import javax.faces.internal.ValidatorBuilder; |
| 31 | |
import javax.faces.internal.ValidatorLookupStrategy; |
| 32 | |
import javax.faces.internal.ValidatorLookupStrategyUtil; |
| 33 | |
import javax.faces.internal.ValidatorResource; |
| 34 | |
import javax.faces.webapp.FacesServlet; |
| 35 | |
import javax.servlet.ServletContext; |
| 36 | |
|
| 37 | |
import org.seasar.framework.container.S2Container; |
| 38 | |
import org.seasar.framework.container.hotdeploy.HotdeployUtil; |
| 39 | |
import org.seasar.framework.util.InputStreamUtil; |
| 40 | |
import org.seasar.framework.util.StringUtil; |
| 41 | |
import org.seasar.teeda.core.JsfConstants; |
| 42 | |
import org.seasar.teeda.core.config.faces.FacesConfigBuilder; |
| 43 | |
import org.seasar.teeda.core.config.faces.assembler.AssemblerAssembler; |
| 44 | |
import org.seasar.teeda.core.config.faces.element.FacesConfig; |
| 45 | |
import org.seasar.teeda.core.config.webapp.WebappConfigBuilder; |
| 46 | |
import org.seasar.teeda.core.config.webapp.element.WebappConfig; |
| 47 | |
import org.seasar.teeda.core.util.DIContainerUtil; |
| 48 | |
import org.seasar.teeda.core.util.ServletContextUtil; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | 0 | public class TeedaInitializer { |
| 54 | |
|
| 55 | |
private ServletContext servletContext; |
| 56 | |
|
| 57 | |
public void initializeFaces() { |
| 58 | 0 | ServletContext context = getServletContext(); |
| 59 | 0 | if (context == null) { |
| 60 | 0 | throw new IllegalStateException("servletContext must not be null."); |
| 61 | |
} |
| 62 | 0 | initializeFacesConfigOptions(context); |
| 63 | 0 | initializeFacesConfigCustomOptions(context); |
| 64 | 0 | buildFacesConfig(); |
| 65 | 0 | buildWebAppConfig(context); |
| 66 | 0 | buildResources(); |
| 67 | 0 | } |
| 68 | |
|
| 69 | |
protected void buildResources() { |
| 70 | 0 | final S2Container container = DIContainerUtil.getContainer(); |
| 71 | 0 | ValidatorBuilder validatorBuilder = (ValidatorBuilder) DIContainerUtil |
| 72 | 0 | .getComponentNoException(ValidatorBuilder.class); |
| 73 | 0 | if (validatorBuilder == null) { |
| 74 | 0 | if (HotdeployUtil.isHotdeploy()) { |
| 75 | 0 | validatorBuilder = new HotDeployValidatorBuilderImpl(container); |
| 76 | |
} else { |
| 77 | 0 | validatorBuilder = new NormalValidatorBuilderImpl(container); |
| 78 | |
} |
| 79 | |
} |
| 80 | 0 | ValidatorResource.setValidatorBuilder(validatorBuilder); |
| 81 | 0 | final ValidatorLookupStrategy strategy = (ValidatorLookupStrategy) DIContainerUtil |
| 82 | |
.getComponentNoException(ValidatorLookupStrategy.class); |
| 83 | 0 | ValidatorLookupStrategyUtil.setValidatorLookupStrategy(strategy); |
| 84 | |
|
| 85 | 0 | ConverterBuilder converterBuilder = (ConverterBuilder) DIContainerUtil |
| 86 | |
.getComponentNoException(ConverterBuilder.class); |
| 87 | 0 | if (converterBuilder == null) { |
| 88 | 0 | if (HotdeployUtil.isHotdeploy()) { |
| 89 | 0 | converterBuilder = new HotDeployConverterBuilderImpl(container); |
| 90 | |
} else { |
| 91 | 0 | converterBuilder = new NormalConverterBuilderImpl(container); |
| 92 | |
} |
| 93 | |
} |
| 94 | 0 | ConverterResource.setConverterBuilder(converterBuilder); |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
public ServletContext getServletContext() { |
| 98 | 0 | return servletContext; |
| 99 | |
} |
| 100 | |
|
| 101 | |
public void setServletContext(ServletContext servletContext) { |
| 102 | 0 | this.servletContext = servletContext; |
| 103 | 0 | } |
| 104 | |
|
| 105 | |
protected String getInitParameter(final ServletContext context, |
| 106 | |
final String key) { |
| 107 | 0 | return context.getInitParameter(key); |
| 108 | |
} |
| 109 | |
|
| 110 | |
protected void initializeFacesConfigOptions( |
| 111 | |
final ServletContext servletContext) { |
| 112 | 0 | final String configFilesAttr = getInitParameter(servletContext, |
| 113 | |
FacesServlet.CONFIG_FILES_ATTR); |
| 114 | 0 | FacesConfigOptions.setConfigFiles(configFilesAttr); |
| 115 | 0 | final String savingMethod = getInitParameter(servletContext, |
| 116 | |
StateManager.STATE_SAVING_METHOD_PARAM_NAME); |
| 117 | 0 | if (savingMethod != null) { |
| 118 | 0 | FacesConfigOptions |
| 119 | |
.setSavingStateInClient(StateManager.STATE_SAVING_METHOD_CLIENT |
| 120 | |
.equalsIgnoreCase(savingMethod)); |
| 121 | |
} |
| 122 | 0 | final String suffix = getInitParameter(servletContext, |
| 123 | |
ViewHandler.DEFAULT_SUFFIX_PARAM_NAME); |
| 124 | 0 | if (suffix != null) { |
| 125 | 0 | FacesConfigOptions.setDefaultSuffix(suffix); |
| 126 | |
} else { |
| 127 | 0 | FacesConfigOptions.setDefaultSuffix(ViewHandler.DEFAULT_SUFFIX); |
| 128 | |
} |
| 129 | 0 | final String lifecycleId = getInitParameter(servletContext, |
| 130 | |
FacesServlet.LIFECYCLE_ID_ATTR); |
| 131 | 0 | if (lifecycleId != null) { |
| 132 | 0 | FacesConfigOptions.setLifecycleId(lifecycleId); |
| 133 | |
} |
| 134 | 0 | final String defaultGridAsync = getInitParameter(servletContext, |
| 135 | |
InternalConstants.DEFAULT_GRID_ASYNC); |
| 136 | 0 | if (defaultGridAsync != null) { |
| 137 | 0 | FacesConfigOptions.setDefaultGridAsync(Boolean.valueOf( |
| 138 | |
defaultGridAsync).booleanValue()); |
| 139 | |
} |
| 140 | 0 | final String gridFirstRenderRowCount = getInitParameter(servletContext, |
| 141 | |
InternalConstants.GRID_FIRST_RENDER_ROW_COUNT); |
| 142 | 0 | if (gridFirstRenderRowCount != null) { |
| 143 | 0 | FacesConfigOptions.setGridFirstRenderRowCount( |
| 144 | |
Integer.parseInt(gridFirstRenderRowCount)); |
| 145 | |
} |
| 146 | 0 | final String defaultLayoutPath = getInitParameter(servletContext, |
| 147 | |
InternalConstants.DEFAULT_LAYOUT_PATH); |
| 148 | 0 | if (defaultLayoutPath != null) { |
| 149 | 0 | FacesConfigOptions.setDefaultLayoutPath(defaultLayoutPath); |
| 150 | |
} |
| 151 | 0 | } |
| 152 | |
|
| 153 | |
protected void initializeFacesConfigCustomOptions( |
| 154 | |
final ServletContext servletContext) { |
| 155 | 0 | final String javaScriptNotPermittedPath = getInitParameter( |
| 156 | |
servletContext, JsfConstants.JAVASCRIPT_NOT_PERMITTED_PATH); |
| 157 | 0 | if (javaScriptNotPermittedPath != null) { |
| 158 | 0 | String[] paths = StringUtil.split(javaScriptNotPermittedPath, ","); |
| 159 | 0 | FacesConfigOptions.setJavascriptNotPermittedPath(paths); |
| 160 | |
} |
| 161 | 0 | final String compressState = getInitParameter(servletContext, |
| 162 | |
JsfConstants.COMPRESS_STATE_ATTR); |
| 163 | 0 | if (compressState != null) { |
| 164 | 0 | FacesConfigOptions.setCompressState(compressState |
| 165 | |
.equalsIgnoreCase("true")); |
| 166 | |
} |
| 167 | 0 | final String redirectUrl = getInitParameter(servletContext, |
| 168 | |
JsfConstants.REDIRECT_URL); |
| 169 | 0 | if (redirectUrl != null) { |
| 170 | 0 | FacesConfigOptions.setRedirectUrl(redirectUrl); |
| 171 | |
} |
| 172 | 0 | } |
| 173 | |
|
| 174 | |
protected void buildWebAppConfig(final ServletContext servletContext) { |
| 175 | 0 | WebappConfigBuilder webAppConfigBuilder = (WebappConfigBuilder) DIContainerUtil |
| 176 | |
.getComponent(WebappConfigBuilder.class); |
| 177 | 0 | InputStream is = null; |
| 178 | 0 | WebappConfig webappConfig = null; |
| 179 | |
try { |
| 180 | 0 | is = ServletContextUtil.getResourceAsStream(servletContext, |
| 181 | |
JsfConstants.WEB_XML_PATH); |
| 182 | 0 | webappConfig = webAppConfigBuilder.build(is, |
| 183 | |
JsfConstants.WEB_XML_PATH); |
| 184 | |
} finally { |
| 185 | 0 | InputStreamUtil.close(is); |
| 186 | 0 | } |
| 187 | 0 | servletContext.setAttribute(WebappConfig.class.getName(), webappConfig); |
| 188 | 0 | } |
| 189 | |
|
| 190 | |
protected void buildFacesConfig() { |
| 191 | 0 | FacesConfigBuilder facesConfigBuilder = (FacesConfigBuilder) DIContainerUtil |
| 192 | |
.getComponent(FacesConfigBuilder.class); |
| 193 | 0 | FacesConfig facesConfig = facesConfigBuilder.buildFacesConfigs(); |
| 194 | 0 | AssemblerAssembler assembler = (AssemblerAssembler) DIContainerUtil |
| 195 | |
.getComponent(AssemblerAssembler.class); |
| 196 | 0 | assembler.assembleFactories(facesConfig); |
| 197 | 0 | assembler.assembleApplication(facesConfig); |
| 198 | 0 | assembler.assembleManagedBeans(facesConfig); |
| 199 | 0 | assembler.assmbleNavigationRules(facesConfig); |
| 200 | 0 | assembler.assembleLifecycle(facesConfig); |
| 201 | 0 | assembler.assembleRenderKits(facesConfig); |
| 202 | 0 | } |
| 203 | |
|
| 204 | |
} |