private void createServletAndModifyWebXML(
IProject project, final IDataModel config,IProgressMonitor monitor,
boolean useSpring, boolean useCayenne, boolean usePerformanceFilter) {
WebApp webApp = null;
WebArtifactEdit artifactEdit = null;
try {
artifactEdit = ClickUtils.getWebArtifactEditForWrite(project);
webApp = artifactEdit.getWebApp();
// create or update servlet ref
Servlet servlet = ClickUtils.findClickServlet(webApp, useSpring);
if (servlet != null) {
// remove old mappings
ClickUtils.removeURLMappings(webApp, servlet);
}
servlet = ClickUtils.createOrUpdateServletRef(webApp, config, servlet, useSpring);
if(useSpring){
ParamValue contextParam = CommonFactory.eINSTANCE.createParamValue();
contextParam.setName("contextConfigLocation");
contextParam.setValue("WEB-INF/spring-beans.xml");
webApp.getContextParams().add(contextParam);
Listener listener = CommonFactory.eINSTANCE.createListener();
listener.setListenerClassName("org.springframework.web.context.ContextLoaderListener");
webApp.getListeners().add(listener);
}
// Add PerformanceFilter
if(usePerformanceFilter){
Filter filter = WebapplicationFactory.eINSTANCE.createFilter();
filter.setName("PerformanceFilter");
filter.setFilterClassName("org.apache.click.extras.filter.PerformanceFilter");
if (webApp.getJ2EEVersionID() >= J2EEVersionConstants.J2EE_1_4_ID) {
// J2EE 1.4
ParamValue initParam = CommonFactory.eINSTANCE.createParamValue();
initParam.setName("cachable-paths");
initParam.setValue("/assets/*");
filter.getInitParamValues().add(initParam);
} else {
// J2EE 1.2 or 1.3
InitParam initParam = WebapplicationFactory.eINSTANCE.createInitParam();
initParam.setParamName("cachable-paths");
initParam.setParamValue("/assets/*");
filter.getInitParams().add(initParam);
}
webApp.getFilters().add(filter);
FilterMapping mapping = WebapplicationFactory.eINSTANCE.createFilterMapping();
mapping.setServletName(servlet.getServletName());
mapping.setFilter(filter);
webApp.getFilterMappings().add(mapping);
String[] filterPatterns = {"*.css", "*.js", "*.gif", "*.png"};
for(String pattern: filterPatterns){
mapping = WebapplicationFactory.eINSTANCE.createFilterMapping();
mapping.setFilter(filter);
mapping.setUrlPattern(pattern);
webApp.getFilterMappings().add(mapping);
}
}
// init mappings
String[] listOfMappings = {"*.htm"};
ClickUtils.setUpURLMappings(webApp, listOfMappings, servlet);
// welcome-file-list
ClickUtils.createOrUpdateFilelist(webApp);
// Add Cayenne Support
if(useCayenne){
Filter filter = WebapplicationFactory.eINSTANCE.createFilter();
filter.setFilterClassName(ClickUtils.CAYENNE_FILTER_CLASS);
filter.setName("DataContextFilter");
if (webApp.getJ2EEVersionID() >= J2EEVersionConstants.J2EE_1_4_ID) {
// J2EE 1.4
ParamValue initParam = CommonFactory.eINSTANCE.createParamValue();
initParam.setName("session-scope");
initParam.setValue("false");
filter.getInitParamValues().add(initParam);
} else {
// J2EE 1.2 or 1.3
InitParam initParam = WebapplicationFactory.eINSTANCE.createInitParam();
initParam.setParamName("session-scope");
initParam.setParamValue("false");
filter.getInitParams().add(initParam);
}
webApp.getFilters().add(filter);
FilterMapping mapping = WebapplicationFactory.eINSTANCE.createFilterMapping();
mapping.setServletName(servlet.getServletName());
mapping.setFilter(filter);
webApp.getFilterMappings().add(mapping);
}
} catch(Exception ex){
ClickPlugin.log(ex);
} finally {
if (artifactEdit != null) {
// save and dispose
artifactEdit.saveIfNecessary(monitor);
artifactEdit.dispose();
}
}
}