### Eclipse Workspace Patch 1.0 #P org.eclipse.jst.jsf.common diff --git src/org/eclipse/jst/jsf/common/webxml/WebXmlUpdater.java src/org/eclipse/jst/jsf/common/webxml/WebXmlUpdater.java index e3261ea..45851d1 100644 --- src/org/eclipse/jst/jsf/common/webxml/WebXmlUpdater.java +++ src/org/eclipse/jst/jsf/common/webxml/WebXmlUpdater.java @@ -19,10 +19,12 @@ import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.jst.j2ee.model.IModelProvider; import org.eclipse.jst.j2ee.model.ModelProviderManager; +import org.eclipse.jst.javaee.web.IWebCommon; import org.eclipse.jst.javaee.web.WebApp; import org.eclipse.jst.jsf.common.webxml.internal.AbstractWebXmlUpdater; import org.eclipse.jst.jsf.common.webxml.internal.WebXmlUpdaterForJ2EE; import org.eclipse.jst.jsf.common.webxml.internal.WebXmlUpdaterForJavaEE; +import org.eclipse.jst.jsf.common.webxml.internal.WebXmlUpdaterForJavaEECommon; /** @@ -55,6 +57,15 @@ /** + * @return IWebCommon object associated with this WebXmlUpdater object + */ + public IWebCommon getWebAppForJavaEECommon () + { + return (IWebCommon) updater.getWebApp(); + } + + + /** * @return WebApp object associated with this WebXmlUpdater object */ public WebApp getWebAppForJavaEE () @@ -80,6 +91,15 @@ public IModelProvider getProvider() { return provider; + } + + + /** + * @return True if this webapp is a Java EE app or web fragment. + */ + public boolean isJavaEECommonWebapp () + { + return updater instanceof WebXmlUpdaterForJavaEECommon; } @@ -239,8 +259,8 @@ if (webAppObj != null) { - if (webAppObj instanceof WebApp) // Java EE - return new WebXmlUpdaterForJavaEE(webAppObj, project, getProvider(), monitor); + if (webAppObj instanceof IWebCommon) // Java EE (web app or web fragment) + return new WebXmlUpdaterForJavaEECommon(webAppObj, project, getProvider(), monitor); else if (webAppObj instanceof org.eclipse.jst.j2ee.webapplication.WebApp) // J2EE return new WebXmlUpdaterForJ2EE(webAppObj, project, getProvider(), monitor); } diff --git src/org/eclipse/jst/jsf/common/webxml/WebXmlUtils.java src/org/eclipse/jst/jsf/common/webxml/WebXmlUtils.java index 48d2250..a1d135b 100644 --- src/org/eclipse/jst/jsf/common/webxml/WebXmlUtils.java +++ src/org/eclipse/jst/jsf/common/webxml/WebXmlUtils.java @@ -28,4 +28,11 @@ * Path to deployment descriptor of webapp */ public static final IPath WEB_XML_PATH = new Path("WEB-INF").append("web.xml"); //$NON-NLS-1$ //$NON-NLS-2$ + + /** + * Path to deployment descriptor of web fragment + */ + public static final IPath WEB_FRAGMENT_XML_PATH = + new Path("META-INF").append("web-fragment.xml"); //$NON-NLS-1$ //$NON-NLS-2$ + } diff --git src/org/eclipse/jst/jsf/common/webxml/WebXmlUtilsForJavaEECommon.java src/org/eclipse/jst/jsf/common/webxml/WebXmlUtilsForJavaEECommon.java new file mode 100644 index 0000000..055316d --- /dev/null +++ src/org/eclipse/jst/jsf/common/webxml/WebXmlUtilsForJavaEECommon.java @@ -0,0 +1,610 @@ +/******************************************************************************* + * Copyright (c) 2001, 2008 Oracle Corporation and others. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Oracle Corporation - initial API and implementation + *******************************************************************************/ + + +package org.eclipse.jst.jsf.common.webxml; + +import java.util.Arrays; +import java.util.List; + +import org.eclipse.jst.javaee.core.Description; +import org.eclipse.jst.javaee.core.JavaeeFactory; +import org.eclipse.jst.javaee.core.Listener; +import org.eclipse.jst.javaee.core.ParamValue; +import org.eclipse.jst.javaee.core.UrlPatternType; +import org.eclipse.jst.javaee.web.Filter; +import org.eclipse.jst.javaee.web.FilterMapping; +import org.eclipse.jst.javaee.web.IWebCommon; +import org.eclipse.jst.javaee.web.Servlet; +import org.eclipse.jst.javaee.web.ServletMapping; +import org.eclipse.jst.javaee.web.WebFactory; + + +/** + * Web.xml editing utilities for Java EE (web app and web fragment). + * + * @author ian.trimble@oracle.com + * + */ +public class WebXmlUtilsForJavaEECommon +{ + /** + * @param webapp + * @param servletName + * @param servletClass + * @param loadOnStartup + */ + public static void addServlet (final IWebCommon webapp, + final String servletName, + final String servletClass, + final String loadOnStartup) + { + if (existsServlet(webapp, servletName, servletClass)) + return; + + // Create new servlet + final Servlet servlet = WebFactory.eINSTANCE.createServlet(); + servlet.setServletName(servletName); + servlet.setServletClass(servletClass); + servlet.setLoadOnStartup(loadOnStartup); + + webapp.getServlets().add(servlet); + } + + + /** + * @param webapp + * @param servletName + * @param servletClass + * @return true if the servlet exists. + */ + public static boolean existsServlet (final IWebCommon webapp, + final String servletName, + final String servletClass) + { + return findServlet(webapp, servletName, servletClass) != null; + } + + + /** + * @param webapp + * @param servletName + * @param servletClass + * @return the servlet or null if not found. + */ + public static Servlet findServlet (final IWebCommon webapp, + final String servletName, + final String servletClass) + { + for (final Object s : webapp.getServlets()) + { + final Servlet servlet = (Servlet) s; + if (servlet.getServletName().equals(servletName) && servlet.getServletClass().equals(servletClass)) + { + return servlet; + } + } + + return null; + } + + + /** + * @param servletName + * @param webApp + * @return the servlet or null if not found. + */ + public static Servlet findServlet (final String servletName, + final IWebCommon webApp) + { + for (final Object servlet : webApp.getServlets()) + { + if (((Servlet) servlet).getServletClass().trim().equals(servletName)) + return (Servlet) servlet; + } + + return null; + } + + + /** + * @param webApp + * @param servlet + */ + public static void removeServlet (final IWebCommon webApp, + final Servlet servlet) + { + webApp.getServlets().remove(servlet); + } + + + /** + * @param webApp + * @param servletName + * @param servletClass + * @param urlPatternString + */ + public static void addServletMapping (final IWebCommon webApp, + final String servletName, + final String servletClass, + final String urlPatternString) + { + if (existsServletMapping(webApp, servletName, urlPatternString)) + return; + + // Create new servlet mapping. + final ServletMapping servletMapping = WebFactory.eINSTANCE.createServletMapping(); + servletMapping.setServletName(servletName); + servletMapping.getUrlPatterns().add(createUrlPattern(urlPatternString)); + + webApp.getServletMappings().add(servletMapping); + } + + + /** + * @param webApp + * @param servletName + * @param urlPatternString + * @return true if the servlet mapping exists. + */ + public static boolean existsServletMapping (final IWebCommon webApp, + final String servletName, + final String urlPatternString) + { + return findServletMapping(webApp, servletName, urlPatternString) != null; + } + + + /** + * @param webApp + * @param servletName + * @param urlPatternString + * @return the servlet mapping or null if doesn't exist. + */ + public static ServletMapping findServletMapping (final IWebCommon webApp, + final String servletName, + final String urlPatternString) + { + for (final Object mapping : webApp.getServletMappings()) + { + final ServletMapping servletMappingToCheck = (ServletMapping) mapping; + if (servletMappingToCheck.getServletName().trim().equals(servletName)) + { + // We found a servlet with the same name. Check for urls + for (final Object pattern : servletMappingToCheck.getUrlPatterns()) + if (((UrlPatternType) pattern).getValue().equals(urlPatternString)) + return servletMappingToCheck; + } + } + + return null; + } + + /** + * @param webApp + * @param servlet + */ + public static void removeServletMappings (final IWebCommon webApp, + final Servlet servlet) + { + final List mappings = webApp.getServletMappings(); + String servletName = servlet.getServletName(); + + if (servletName != null) + { + servletName = servletName.trim(); + for (int i = mappings.size() - 1; i >= 0; --i) + { + final ServletMapping mapping = (ServletMapping) mappings.get(i); + if (mapping != null && mapping.getServletName() != null && mapping.getServletName().trim().equals(servletName)) + { + mappings.remove(mapping); + } + } + } + } + + + /** + * @param webapp + * @param filterName + * @param filterClass + */ + public static void addFilter (final IWebCommon webapp, + final String filterName, + final String filterClass) + { + if (existsFilter(webapp, filterName, filterClass)) + return; + + webapp.getFilters().add(createFilter(filterName, filterClass)); + } + + + /** + * @param filterName + * @param filterClass + * @return the filter or null if it doesn't exist. + */ + public static Filter createFilter (final String filterName, + final String filterClass) + { + final Filter filter = WebFactory.eINSTANCE.createFilter(); + filter.setFilterName(filterName); + filter.setFilterClass(filterClass); + return filter; + } + + + /** + * @param webapp + * @param filterName + * @param filterClass + * @return true if the filter exists. + */ + public static boolean existsFilter (final IWebCommon webapp, + final String filterName, + final String filterClass) + { + return findFilter(webapp, filterName, filterClass) != null; + } + + + /** + * @param webapp + * @param filterName + * @param filterClass + * @return the filter or null if not found. + */ + public static Filter findFilter (final IWebCommon webapp, + final String filterName, + final String filterClass) + { + for (final Object f : webapp.getFilters()) + { + final Filter filter = (Filter) f; + if (filter.getFilterName().trim().equals(filterName) && filter.getFilterClass().trim().equals(filterClass)) + { + return filter; + } + } + + return null; + } + + + /** + * @param webApp + * @param filterClassName + * @return the filter or null if not found. + */ + public static Filter findFilter (final IWebCommon webApp, + final String filterClassName) + { + for (final Object filter : webApp.getFilters()) + { + if (((Filter) filter).getFilterClass().trim().equals(filterClassName)) + return (Filter) filter; + } + + return null; + } + + + /** + * @param webApp + * @param filter + */ + public static void removeFilter (final IWebCommon webApp, + final Filter filter) + { + webApp.getFilters().remove(filter); + } + + + /** + * @param webapp + * @param filterName + * @param servletName + */ + public static void addFilterMapping (final IWebCommon webapp, + final String filterName, + final String servletName) + { + if (existsFilterMapping(webapp, filterName, servletName)) + return; + + webapp.getFilterMappings().add(createFilterMapping(filterName, servletName)); + } + + + /** + * @param filterName + * @param servletName + * @return the filter mapping or null if not found. + */ + public static FilterMapping createFilterMapping (final String filterName, + final String servletName) + { + final FilterMapping filterMapping = WebFactory.eINSTANCE.createFilterMapping(); + filterMapping.setFilterName(filterName); + filterMapping.getServletNames().add(servletName); + + return filterMapping; + } + + + /** + * @param webapp + * @param filterName + * @param servletName + * @return true if the filter mapping exists. + */ + public static boolean existsFilterMapping (final IWebCommon webapp, + final String filterName, + final String servletName) + { + return findFilterMapping(webapp, filterName, servletName) != null; + } + + + /** + * @param webapp + * @param filterName + * @param servletName + * @return the filter mapping or null. + */ + public static FilterMapping findFilterMapping (final IWebCommon webapp, + final String filterName, + final String servletName) + { + for (final Object fm : webapp.getFilterMappings()) + { + final FilterMapping filterMapping = (FilterMapping) fm; + + if (filterMapping.getFilterName().trim().equals(filterName) && filterMapping.getServletNames().contains(servletName)) + { + return filterMapping; + } + } + + return null; + } + + + /** + * @param webApp + * @param filter + */ + public static void removeFilterMappings (final IWebCommon webApp, + final Filter filter) + { + final List mappings = webApp.getFilterMappings(); + String filterName = filter.getFilterName(); + + if (filterName != null) + { + filterName = filterName.trim(); + for (int i = mappings.size() - 1; i >= 0; --i) + { + final FilterMapping mapping = (FilterMapping) mappings.get(i); + if (mapping != null && mapping.getFilterName() != null && mapping.getFilterName().trim().equals(filterName)) + { + mappings.remove(mapping); + } + } + } + } + + + /** + * @param webApp + * @param paramName + * @param paramValue + * @param description + */ + public static void addContextParam (final IWebCommon webApp, + final String paramName, + final String paramValue, + final String description) + { + if (existsContextParam(webApp, paramName, paramValue)) + return; + + webApp.getContextParams().add(createContextParam(paramName, paramValue, description)); + } + + + /** + * @param paramName + * @param paramValue + * @param descriptionString + * @return the param value or null if not found. + */ + public static ParamValue createContextParam (final String paramName, + final String paramValue, + final String descriptionString) + { + final ParamValue param = JavaeeFactory.eINSTANCE.createParamValue(); + param.setParamName(paramName); + param.setParamValue(paramValue); + + if (descriptionString != null) + { + final Description description = JavaeeFactory.eINSTANCE.createDescription(); + description.setValue(descriptionString); + param.getDescriptions().add(description); + } + + + return param; + } + + + /** + * @param webApp + * @param paramName + * @param paramValue + * @return true if the context param exists. + */ + public static boolean existsContextParam (final IWebCommon webApp, + final String paramName, + final String paramValue) + { + return findContextParam(webApp, paramName, paramValue) != null; + } + + + /** + * @param webApp + * @param paramName + * @param paramValue + * @return the param value or null if not found. + */ + public static ParamValue findContextParam (final IWebCommon webApp, + final String paramName, + final String paramValue) + { + for (final Object param : webApp.getContextParams()) + { + final ParamValue contextParam = (ParamValue) param; + if (contextParam.getParamName().equals(paramName) && contextParam.getParamValue().equals(paramValue)) + return contextParam; + } + + return null; + } + + + /** + * @param webApp + * @param paramName Name of context param + * @return Value of the given context param + */ + public static String getContextParamValue (final IWebCommon webApp, + final String paramName) + { + for (final Object param : webApp.getContextParams()) + { + final ParamValue contextParam = (ParamValue) param; + if (contextParam.getParamName().equals(paramName)) + return contextParam.getParamValue(); + } + + return null; + } + + + /** + * @param webApp + * @param paramName Name of context param + * @param valuesDelimiterRegex + * @return Values of the given context param as a list + */ + public static List getContextParamValuesAsList (final IWebCommon webApp, + final String paramName, + final String valuesDelimiterRegex) + { + final String valueString = getContextParamValue(webApp, paramName); + return Arrays.asList(valueString.split(valuesDelimiterRegex)); + } + + + /** + * Updates the value of a context param if it exists. Otherwise, adds this + * as a new context param. + * + * @param webApp + * @param paramName + * @param paramValue + */ + public static void setContextParamValue (final IWebCommon webApp, + final String paramName, + final String paramValue) + { + ParamValue contextParam = null; + + for (final Object p : webApp.getContextParams()) + { + final ParamValue param = (ParamValue) p; + if (param.getParamName().equals(paramName)) + { + contextParam = param; + break; + } + } + + if (contextParam == null) + { + webApp.getContextParams().add(createContextParam(paramName, paramValue, null)); + } + else + { + contextParam.setParamValue(paramValue); + } + } + + + /** + * @param webapp + * @param listenerClass + */ + public static void addListener (final IWebCommon webapp, + final String listenerClass) + { + if (existsListener(webapp, listenerClass)) + return; + + // Create new listener + final Listener listener = JavaeeFactory.eINSTANCE.createListener(); + listener.setListenerClass(listenerClass); + + webapp.getListeners().add(listener); + } + + + /** + * @param webapp + * @param listenerClass + * @return true if the listener exists. + */ + public static boolean existsListener (final IWebCommon webapp, + final String listenerClass) + { + return findListener(webapp, listenerClass) != null; + } + + + /** + * @param webapp + * @param listenerClass + * @return the listener or null if not found. + */ + public static Listener findListener (final IWebCommon webapp, + final String listenerClass) + { + for (final Object listener : webapp.getListeners()) + if (((Listener) listener).getListenerClass().equals(listenerClass)) + return (Listener) listener; + + return null; + } + + + /** + * @param urlPatternString + * @return the UrlPattern or null. + */ + public static UrlPatternType createUrlPattern (final String urlPatternString) + { + final UrlPatternType urlPattern = JavaeeFactory.eINSTANCE.createUrlPatternType(); + urlPattern.setValue(urlPatternString); + return urlPattern; + } +} diff --git src/org/eclipse/jst/jsf/common/webxml/internal/WebXmlUpdaterForJavaEECommon.java src/org/eclipse/jst/jsf/common/webxml/internal/WebXmlUpdaterForJavaEECommon.java new file mode 100644 index 0000000..dcfb486 --- /dev/null +++ src/org/eclipse/jst/jsf/common/webxml/internal/WebXmlUpdaterForJavaEECommon.java @@ -0,0 +1,169 @@ +/******************************************************************************* + * Copyright (c) 2001, 2009 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation + *******************************************************************************/ + + +package org.eclipse.jst.jsf.common.webxml.internal; + +import static org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon.findFilter; +import static org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon.findServlet; + +import java.util.List; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jst.j2ee.model.IModelProvider; +import org.eclipse.jst.javaee.web.Filter; +import org.eclipse.jst.javaee.web.IWebCommon; +import org.eclipse.jst.javaee.web.Servlet; +import org.eclipse.jst.javaee.web.WebFragment; +import org.eclipse.jst.jsf.common.webxml.WebXmlUtils; +import org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon; +import org.eclipse.jst.jsf.common.webxml.internal.operations.ContextParamAdderForJavaEECommon; +import org.eclipse.jst.jsf.common.webxml.internal.operations.FilterAdderForJavaEECommon; +import org.eclipse.jst.jsf.common.webxml.internal.operations.FilterMapperAdderForJavaEECommon; +import org.eclipse.jst.jsf.common.webxml.internal.operations.FilterRemoverForJavaEECommon; +import org.eclipse.jst.jsf.common.webxml.internal.operations.ListenerAdderForJavaEECommon; +import org.eclipse.jst.jsf.common.webxml.internal.operations.ServletAdderForJavaEECommon; +import org.eclipse.jst.jsf.common.webxml.internal.operations.ServletMappingAdderForJavaEECommon; +import org.eclipse.jst.jsf.common.webxml.internal.operations.ServletRemoverForJavaEECommon; + + +/** + * @author ian.trimble@oracle.com + * + */ +public class WebXmlUpdaterForJavaEECommon extends AbstractWebXmlUpdater +{ + private final IWebCommon webApp; + private IPath modelPath; + + /** + * @param webAppObj + * @param project + * @param provider + * @param monitor + */ + public WebXmlUpdaterForJavaEECommon (final Object webAppObj, + final IProject project, + final IModelProvider provider, + final IProgressMonitor monitor) + { + super(webAppObj, project, provider, monitor); + webApp = (IWebCommon) webAppObj; + modelPath = WebXmlUtils.WEB_XML_PATH; + if (webAppObj instanceof WebFragment) { + modelPath = WebXmlUtils.WEB_FRAGMENT_XML_PATH; + } + } + + + @Override + public void addServlet (final String servletName, + final String servletClass, + final String loadOnStartup) + { + provider.modify(new ServletAdderForJavaEECommon(project, servletName, servletClass, loadOnStartup), modelPath); + } + + + @Override + public void removeServlet (final String servletClassName) + { + final Servlet servlet = findServlet(servletClassName, webApp); + if (servlet == null) + throw new IllegalArgumentException("Cannot find servlet named \"" + servletClassName + "\""); //$NON-NLS-1$//$NON-NLS-2$ + + provider.modify(new ServletRemoverForJavaEECommon(project, servletClassName), modelPath); + } + + + @Override + public void addServletMapping (final String servletName, + final String servletClass, + final String urlPattern) + { + provider.modify(new ServletMappingAdderForJavaEECommon(project, servletName, servletClass, urlPattern), modelPath); + } + + + @Override + public void addFilter (final String filterName, + final String filterClass) + { + provider.modify(new FilterAdderForJavaEECommon(project, filterName, filterClass), modelPath); + } + + + @Override + public void removeFilter (final String filterClassName) + { + final Filter filter = findFilter(webApp, filterClassName); + if (filter == null) + throw new IllegalArgumentException("Cannot find filter named \"" + filterClassName + "\""); //$NON-NLS-1$//$NON-NLS-2$ + + provider.modify(new FilterRemoverForJavaEECommon(project, filterClassName), modelPath); + } + + + @Override + public void addFilterMapping (final String filterName, + final String filterClass, + final String servletName) + { + provider.modify(new FilterMapperAdderForJavaEECommon(project, filterName, servletName), modelPath); + } + + + @Override + public String getContextParamValue (final String paramName) + { + return WebXmlUtilsForJavaEECommon.getContextParamValue(webApp, paramName); + } + + + @Override + public List getContextParamValuesAsList (final String paramName, + final String valuesDelimiterRegex) + { + return WebXmlUtilsForJavaEECommon.getContextParamValuesAsList(webApp, paramName, valuesDelimiterRegex); + } + + + @Override + public void setContextParamValue(String paramName, String paramValue) + { + WebXmlUtilsForJavaEECommon.setContextParamValue(webApp, paramName, paramValue); + } + + + @Override + public void addContextParam (final String paramName, + final String paramValue, + final String description) + { + provider.modify(new ContextParamAdderForJavaEECommon(project, paramName, paramValue, description), modelPath); + } + + + @Override + public void addListener (final String listenerClass) + { + provider.modify(new ListenerAdderForJavaEECommon(project, listenerClass), modelPath); + } + + + @Override + public Object getWebApp() + { + return webApp; + } +} diff --git src/org/eclipse/jst/jsf/common/webxml/internal/operations/ContextParamAdderForJavaEECommon.java src/org/eclipse/jst/jsf/common/webxml/internal/operations/ContextParamAdderForJavaEECommon.java new file mode 100644 index 0000000..b1cb886 --- /dev/null +++ src/org/eclipse/jst/jsf/common/webxml/internal/operations/ContextParamAdderForJavaEECommon.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright (c) 2001, 2008 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation + *******************************************************************************/ + + +package org.eclipse.jst.jsf.common.webxml.internal.operations; + +import org.eclipse.core.resources.IProject; +import org.eclipse.jst.j2ee.model.ModelProviderManager; +import org.eclipse.jst.javaee.web.IWebCommon; +import org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon; + + +/** + * Runnable to add a context-param to web.xml. + * + * @author ian.trimble@oracle.com + * + */ +public class ContextParamAdderForJavaEECommon implements Runnable +{ + private final IProject project; + private final String paramName; + private final String paramValue; + private final String description; + + + /** + * @param project + * @param paramName + * @param paramValue + * @param description + */ + public ContextParamAdderForJavaEECommon (final IProject project, + final String paramName, + final String paramValue, + final String description) + { + this.project = project; + this.paramName = paramName; + this.paramValue = paramValue; + this.description = description; + } + + + public void run () + { + final IWebCommon webApp = (IWebCommon) ModelProviderManager.getModelProvider(project).getModelObject(); + + WebXmlUtilsForJavaEECommon.addContextParam(webApp, paramName, paramValue, description); + } +} diff --git src/org/eclipse/jst/jsf/common/webxml/internal/operations/FilterAdderForJavaEECommon.java src/org/eclipse/jst/jsf/common/webxml/internal/operations/FilterAdderForJavaEECommon.java new file mode 100644 index 0000000..35414b9 --- /dev/null +++ src/org/eclipse/jst/jsf/common/webxml/internal/operations/FilterAdderForJavaEECommon.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2001, 2008 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation + *******************************************************************************/ + + +package org.eclipse.jst.jsf.common.webxml.internal.operations; + +import org.eclipse.core.resources.IProject; +import org.eclipse.jst.j2ee.model.ModelProviderManager; +import org.eclipse.jst.javaee.web.IWebCommon; +import org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon; + + +/** + * Runnable to add a filter to web.xml. + * + * @author ian.trimble@oracle.com + * + */ +public class FilterAdderForJavaEECommon implements Runnable +{ + private final IProject project; + private final String filterName; + private final String filterClass; + + + /** + * @param project + * @param filterName + * @param filterClass + */ + public FilterAdderForJavaEECommon (final IProject project, + final String filterName, + final String filterClass) + { + this.project = project; + this.filterName = filterName; + this.filterClass = filterClass; + } + + + public void run () + { + final IWebCommon webApp = (IWebCommon) ModelProviderManager.getModelProvider(project).getModelObject(); + + WebXmlUtilsForJavaEECommon.addFilter(webApp, filterName, filterClass); + } +} diff --git src/org/eclipse/jst/jsf/common/webxml/internal/operations/FilterMapperAdderForJavaEECommon.java src/org/eclipse/jst/jsf/common/webxml/internal/operations/FilterMapperAdderForJavaEECommon.java new file mode 100644 index 0000000..85b64ac --- /dev/null +++ src/org/eclipse/jst/jsf/common/webxml/internal/operations/FilterMapperAdderForJavaEECommon.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2001, 2008 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation + *******************************************************************************/ + + +package org.eclipse.jst.jsf.common.webxml.internal.operations; + +import org.eclipse.core.resources.IProject; +import org.eclipse.jst.j2ee.model.ModelProviderManager; +import org.eclipse.jst.javaee.web.IWebCommon; +import org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon; + + +/** + * Runnable to add a filter-mapping to web.xml. + * + * @author ian.trimble@oracle.com + * + */ +public class FilterMapperAdderForJavaEECommon implements Runnable +{ + private final IProject project; + private final String filterName; + private final String servletName; + + + /** + * @param project + * @param filterName + * @param servletName + */ + public FilterMapperAdderForJavaEECommon (final IProject project, + final String filterName, + final String servletName) + { + this.project = project; + this.filterName = filterName; + this.servletName = servletName; + } + + + public void run () + { + final IWebCommon webApp = (IWebCommon) ModelProviderManager.getModelProvider(project).getModelObject(); + + WebXmlUtilsForJavaEECommon.addFilterMapping(webApp, filterName, servletName); + } +} diff --git src/org/eclipse/jst/jsf/common/webxml/internal/operations/FilterRemoverForJavaEECommon.java src/org/eclipse/jst/jsf/common/webxml/internal/operations/FilterRemoverForJavaEECommon.java new file mode 100644 index 0000000..6118261 --- /dev/null +++ src/org/eclipse/jst/jsf/common/webxml/internal/operations/FilterRemoverForJavaEECommon.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2001, 2008 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation + *******************************************************************************/ + + +package org.eclipse.jst.jsf.common.webxml.internal.operations; + + +import static org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon.findFilter; +import static org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon.removeFilter; +import static org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon.removeFilterMappings; + +import org.eclipse.core.resources.IProject; +import org.eclipse.jst.j2ee.model.ModelProviderManager; +import org.eclipse.jst.javaee.web.Filter; +import org.eclipse.jst.javaee.web.IWebCommon; + + +/** + * Removes a filter and its associated mappings from web.xml + * + * @author ian.trimble@oracle.com + * + */ +public class FilterRemoverForJavaEECommon implements Runnable +{ + private final IProject project; + private final String filterClassName; + + + /** + * @param project + * @param filterClassName + */ + public FilterRemoverForJavaEECommon (final IProject project, + final String filterClassName) + { + this.project = project; + this.filterClassName = filterClassName; + } + + + public void run () + { + final IWebCommon webApp = (IWebCommon) ModelProviderManager.getModelProvider(project).getModelObject(); + final Filter filter = findFilter(webApp, filterClassName); + + removeFilterMappings(webApp, filter); + removeFilter(webApp, filter); + } +} diff --git src/org/eclipse/jst/jsf/common/webxml/internal/operations/ListenerAdderForJavaEECommon.java src/org/eclipse/jst/jsf/common/webxml/internal/operations/ListenerAdderForJavaEECommon.java new file mode 100644 index 0000000..3d945de --- /dev/null +++ src/org/eclipse/jst/jsf/common/webxml/internal/operations/ListenerAdderForJavaEECommon.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2001, 2008 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation + *******************************************************************************/ + + +package org.eclipse.jst.jsf.common.webxml.internal.operations; + +import org.eclipse.core.resources.IProject; +import org.eclipse.jst.j2ee.model.ModelProviderManager; +import org.eclipse.jst.javaee.web.IWebCommon; +import org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon; + + +/** + * Runnable to add a listener to web.xml. + * + * @author ian.trimble@oracle.com + * + */ +public class ListenerAdderForJavaEECommon implements Runnable +{ + private final IProject project; + private final String listenerClass; + + + /** + * @param project + * @param listenerClass + */ + public ListenerAdderForJavaEECommon (final IProject project, + final String listenerClass) + { + this.project = project; + this.listenerClass = listenerClass; + } + + + public void run () + { + final IWebCommon webApp = (IWebCommon) ModelProviderManager.getModelProvider(project).getModelObject(); + + WebXmlUtilsForJavaEECommon.addListener(webApp, listenerClass); + } +} diff --git src/org/eclipse/jst/jsf/common/webxml/internal/operations/ServletAdderForJavaEECommon.java src/org/eclipse/jst/jsf/common/webxml/internal/operations/ServletAdderForJavaEECommon.java new file mode 100644 index 0000000..5e1b214 --- /dev/null +++ src/org/eclipse/jst/jsf/common/webxml/internal/operations/ServletAdderForJavaEECommon.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright (c) 2001, 2008 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation + *******************************************************************************/ + + +package org.eclipse.jst.jsf.common.webxml.internal.operations; + +import org.eclipse.core.resources.IProject; +import org.eclipse.jst.j2ee.model.ModelProviderManager; +import org.eclipse.jst.javaee.web.IWebCommon; +import org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon; + + +/** + * Runnable to add a servlet to web.xml. + * + * @author ian.trimble@oracle.com + * + */ +public class ServletAdderForJavaEECommon implements Runnable +{ + private final IProject project; + private final String servletName; + private final String servletClass; + private final String loadOnStartup; + + + /** + * @param project + * @param servletName + * @param servletClass + * @param loadOnStartup + */ + public ServletAdderForJavaEECommon (final IProject project, + final String servletName, + final String servletClass, + final String loadOnStartup) + { + this.project = project; + this.servletName = servletName; + this.servletClass = servletClass; + this.loadOnStartup = loadOnStartup; + } + + + public void run () + { + final IWebCommon webApp = (IWebCommon) ModelProviderManager.getModelProvider(project).getModelObject(); + + WebXmlUtilsForJavaEECommon.addServlet(webApp, servletName, servletClass, loadOnStartup); + } +} diff --git src/org/eclipse/jst/jsf/common/webxml/internal/operations/ServletMappingAdderForJavaEECommon.java src/org/eclipse/jst/jsf/common/webxml/internal/operations/ServletMappingAdderForJavaEECommon.java new file mode 100644 index 0000000..f5e7401 --- /dev/null +++ src/org/eclipse/jst/jsf/common/webxml/internal/operations/ServletMappingAdderForJavaEECommon.java @@ -0,0 +1,59 @@ +/******************************************************************************* + * Copyright (c) 2001, 2008 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation + *******************************************************************************/ + + +package org.eclipse.jst.jsf.common.webxml.internal.operations; + +import org.eclipse.core.resources.IProject; +import org.eclipse.jst.j2ee.model.ModelProviderManager; +import org.eclipse.jst.javaee.web.IWebCommon; +import org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon; + + +/** + * Runnable to add a servlet-mapping to web.xml. + * + * @author ian.trimble@oracle.com + * + */ +public class ServletMappingAdderForJavaEECommon implements Runnable +{ + private final IProject project; + private final String servletName; + private final String servletClass; + private final String urlPatternString; + + + /** + * @param project + * @param servletName + * @param servletClass + * @param urlPatternString + */ + public ServletMappingAdderForJavaEECommon (final IProject project, + final String servletName, + final String servletClass, + final String urlPatternString) + { + this.project = project; + this.servletName = servletName; + this.servletClass = servletClass; + this.urlPatternString = urlPatternString; + } + + + public void run () + { + final IWebCommon webApp = (IWebCommon) ModelProviderManager.getModelProvider(project).getModelObject(); + + WebXmlUtilsForJavaEECommon.addServletMapping(webApp, servletName, servletClass, urlPatternString); + } +} diff --git src/org/eclipse/jst/jsf/common/webxml/internal/operations/ServletRemoverForJavaEECommon.java src/org/eclipse/jst/jsf/common/webxml/internal/operations/ServletRemoverForJavaEECommon.java new file mode 100644 index 0000000..8f4f33d --- /dev/null +++ src/org/eclipse/jst/jsf/common/webxml/internal/operations/ServletRemoverForJavaEECommon.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2001, 2008 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation + *******************************************************************************/ + + +package org.eclipse.jst.jsf.common.webxml.internal.operations; + +import org.eclipse.core.resources.IProject; +import org.eclipse.jst.j2ee.model.ModelProviderManager; +import org.eclipse.jst.javaee.web.IWebCommon; +import org.eclipse.jst.javaee.web.Servlet; +import org.eclipse.jst.jsf.common.webxml.WebXmlUtilsForJavaEECommon; + + +/** + * Removes a servlet and its associated mappings from web.xml + * + * @author ian.trimble@oracle.com + * + */ +public class ServletRemoverForJavaEECommon implements Runnable +{ + private final IProject project; + private final String servletClassName; + + + /** + * @param project + * @param servletClassName + */ + public ServletRemoverForJavaEECommon (final IProject project, + final String servletClassName) + { + this.project = project; + this.servletClassName = servletClassName; + } + + + public void run () + { + final IWebCommon webApp = (IWebCommon) ModelProviderManager.getModelProvider(project).getModelObject(); + final Servlet servlet = WebXmlUtilsForJavaEECommon.findServlet(servletClassName, webApp); + + WebXmlUtilsForJavaEECommon.removeServletMappings(webApp, servlet); + WebXmlUtilsForJavaEECommon.removeServlet(webApp, servlet); + } +} #P org.eclipse.jst.jsf.core diff --git plugin.xml plugin.xml index 954bdfd..ae24a02 100644 --- plugin.xml +++ plugin.xml @@ -46,7 +46,16 @@ - + + + + + + @@ -54,7 +63,16 @@ - + + + + + + @@ -62,7 +80,16 @@ - + + + + + + @@ -70,7 +97,16 @@ - + + + + + + @@ -79,10 +115,16 @@ version="2.2"> + + + + @@ -121,18 +163,6 @@ - - - - - - @@ -140,6 +170,18 @@ + + + + + + diff --git src/org/eclipse/jst/jsf/core/internal/project/facet/JEECommonUtils.java src/org/eclipse/jst/jsf/core/internal/project/facet/JEECommonUtils.java new file mode 100644 index 0000000..3a343f7 --- /dev/null +++ src/org/eclipse/jst/jsf/core/internal/project/facet/JEECommonUtils.java @@ -0,0 +1,344 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Oracle Corporation - initial API and implementation and/or initial documentation + *******************************************************************************/ + +package org.eclipse.jst.jsf.core.internal.project.facet; + +import java.util.Iterator; +import java.util.List; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.jst.javaee.core.JavaeeFactory; +import org.eclipse.jst.javaee.core.ParamValue; +import org.eclipse.jst.javaee.core.UrlPatternType; +import org.eclipse.jst.javaee.web.IWebCommon; +import org.eclipse.jst.javaee.web.Servlet; +import org.eclipse.jst.javaee.web.ServletMapping; +import org.eclipse.jst.javaee.web.WebFactory; + +/** + * Common web.xml access for JEE applications (web version 2.5 and above) and JEE web fragments + */ +public class JEECommonUtils { + /** + * Finds and returns a Servlet definition, or null if servlet is not defined. + * + * @param webApp + * @param servletName + * @return Servlet or null + */ + public static Servlet findServlet(final IWebCommon webApp, String servletName) + { + if (webApp == null) + { + return null; + } + + for (final Servlet servlet : webApp.getServlets()) + { + if (servlet != null && + servlet.getServletClass() != null + && servlet.getServletClass().trim().equals(servletName)) + { + return servlet; + } + } + + // if we get to here then we have finished the loop + // without finding the servlet we're looking for + return null; + } + + /** + * Creates servlet reference in WebApp if not present or updates servlet name if found + * using the passed configuration. + * + * @param webApp + * @param displayName + * @param className + * @param servlet + * @return Servlet servlet - if passed servlet was null, will return created servlet + */ + public static Servlet createOrUpdateServletRef(final IWebCommon webApp, + String displayName, String className, Servlet servlet) { + + if (servlet == null){ + // Create the servlet instance and set up the parameters from data + // model + servlet = WebFactory.eINSTANCE.createServlet(); + servlet.setServletName(displayName); + servlet.setServletClass(className); + servlet.setLoadOnStartup(Integer.valueOf(1)); + // Add the servlet to the web application model + webApp.getServlets().add(servlet); + + } else { + updateServletMappings(webApp, servlet, displayName); + servlet.setServletName(displayName); + servlet.setLoadOnStartup(Integer.valueOf(1)); + } + return servlet; + } + + /** + * Updates servlet mapping + * + * @param webApp + * @param servlet + * @param displayName + */ + public static void updateServletMappings(final IWebCommon webApp, + final Servlet servlet, final String displayName) + { + // update mappings for new name + ServletMapping mapping = findServletMapping(webApp, servlet); + if (mapping != null) + { + mapping.setServletName(displayName); + } + } + + /** + * Finds mapping for given servlet + * + * @param webApp + * @param servlet + * @return List of mappings + */ + public static ServletMapping findServletMapping(final IWebCommon webApp, final Servlet servlet) { + for (Iterator it=webApp.getServletMappings().iterator();it.hasNext();){ + ServletMapping mapping = (ServletMapping)it.next(); + if (mapping.getServletName() != null && + servlet.getServletName() != null && + mapping.getServletName().trim().equals(servlet.getServletName().trim())) + return mapping; + } + return null; + } + + /** + * Creates servlet-mappings for the servlet for 2.5 WebModules or greated + * + * @param webApp + * @param urlMappingList + * - list of string values to be used in url-pattern for + * servlet-mapping + * @param servlet + */ + public static void setUpURLMappings(final IWebCommon webApp, + final List urlMappingList, final Servlet servlet) + { + + if (urlMappingList.size() > 0) + { + ServletMapping mapping = findServletMapping(webApp, servlet); + if (mapping == null) + { + mapping = WebFactory.eINSTANCE + .createServletMapping(); + mapping.setServletName(servlet.getServletName()); + webApp.getServletMappings().add(mapping); + } + // Add patterns + for (final String pattern : urlMappingList) + { + if (!(doesServletMappingPatternExist(webApp, servlet, pattern))) + { + UrlPatternType urlPattern = JavaeeFactory.eINSTANCE + .createUrlPatternType(); + urlPattern.setValue(pattern); + mapping.getUrlPatterns().add(urlPattern); + } + } + } + } + + /** + * Checks whether given mapping exists for the given servlet + * @param webApp + * @param servlet + * @param pattern + * @return true or false + */ + public static boolean doesServletMappingPatternExist(final IWebCommon webApp, + final Servlet servlet, final String pattern) + { + List mappings = webApp.getServletMappings(); + String servletName = servlet.getServletName(); + if (servletName != null) + { + servletName = servletName.trim(); + for (final ServletMapping mapping : mappings) + { + if (mapping != null && + mapping.getServletName() != null && + servletName.equals(mapping.getServletName().trim())) + { + for (final UrlPatternType urlPattern : mapping.getUrlPatterns()) + { + String patternTypeValue = urlPattern.getValue(); + if (patternTypeValue != null + && pattern.equals(patternTypeValue.trim())) + return true; + } + } + } + } + return false; + } + + /** + * Removes servlet-mappings for servlet using servlet-name for >= 2.5 WebModules. + * @param webApp + * @param servlet + */ + public static void removeURLMappings(final IWebCommon webApp, final Servlet servlet) { + List mappings = webApp.getServletMappings(); + String servletName = servlet.getServletName(); + if (servletName != null) { + servletName = servletName.trim(); + for (int i=mappings.size()-1;i>=0;--i){ + ServletMapping mapping = (ServletMapping)mappings.get(i); + if (mapping != null && + mapping.getServletName() != null && + mapping.getServletName().trim() + .equals(servletName)) { + mappings.remove(mapping); + } + } + } + } + + /** + * Removes servlet definition + * @param webApp + * @param servlet + */ + public static void removeServlet(final IWebCommon webApp, final Servlet servlet) { + webApp.getServlets().remove(servlet); + } + + /** + * Removes context-param + * @param webApp + * @param paramName + */ + public static void removeContextParam(final IWebCommon webApp, String paramName) { + Iterator it = webApp.getContextParams().iterator(); + while (it.hasNext()) { + ParamValue cp = (ParamValue) it.next(); + if (cp.getParamName().equals(paramName)) { + webApp.getContextParams().remove(cp); + break; + } + } + } + + /** + * Creates or updates context-param + * @param webApp + * @param paramName + * @param paramValue + */ + public static void setupContextParam(final IWebCommon webApp, String paramName, String paramValue) { + // if not default name and location, then add context param + ParamValue foundCP = null; + ParamValue cp = null; + boolean found = false; + // check to see if present + Iterator it = webApp.getContextParams().iterator(); + while (it.hasNext()) { + cp = (org.eclipse.jst.javaee.core.ParamValue) it.next(); + if (cp != null && + cp.getParamName()!= null && + cp.getParamName().trim().equals(paramName)) { + foundCP = cp; + found = true; + } + } + if (!found) { + ParamValue pv = JavaeeFactory.eINSTANCE.createParamValue(); + pv.setParamName(paramName); + pv.setParamValue(paramValue); + webApp.getContextParams().add(pv); + } else { + cp = foundCP; + if (cp.getParamValue().indexOf(paramValue) < 0) { + String curVal = cp.getParamValue(); + String val = paramValue; + if (curVal != null && !"".equals(curVal.trim())) { //$NON-NLS-1$ + val = curVal + ",\n" + val; //$NON-NLS-1$ + } + cp.setParamValue(val); + } + } + } + + /** + * @param webApp + * @param paramName + * @return context-param value or null if context-param is not found + */ + public static String getContextParam(final IWebCommon webApp, String paramName) + { + for (Iterator it = webApp.getContextParams().iterator(); it.hasNext();) + { + ParamValue cp = (ParamValue) it.next(); + if (cp != null && + cp.getParamName()!= null && + cp.getParamName().trim().equals(paramName)) { + return cp.getParamValue(); + } + } + return null; + } + + /** + * @param map + * @return extension from map. Will return null if file extension not found in url patterns. + */ + public static String getFileExtensionFromMap(final ServletMapping map) { + List urls = map.getUrlPatterns(); + for (Iterator it=urls.iterator();it.hasNext();){ + IPath extPath = new Path(((UrlPatternType)it.next()).getValue()); + if (extPath != null){ + String ext = extPath.getFileExtension(); + if (ext != null && !ext.equals("")) //$NON-NLS-1$ + return ext; + } + } + return null; + } + + /** + * @param map + * @return prefix mapping + */ + public static String getPrefixMapping(final ServletMapping map) { + List urls = map.getUrlPatterns(); + for (Iterator it=urls.iterator();it.hasNext();){ + IPath extPath = new Path(((UrlPatternType)it.next()).getValue()); + if (extPath != null){ + String ext = extPath.getFileExtension(); + if (ext == null){ + String lastSeg = extPath.lastSegment(); + if (lastSeg.equals("*")) //$NON-NLS-1$ + { + return extPath.removeLastSegments(1).toString(); + } + + return extPath.toString(); + } + } + } + return null; + } +} diff --git src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDataModelProvider.java src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDataModelProvider.java index 33a8972..eecf699 100644 --- src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDataModelProvider.java +++ src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDataModelProvider.java @@ -47,6 +47,7 @@ import org.eclipse.wst.common.project.facet.core.IFacetedProject.Action; import org.eclipse.wst.common.project.facet.core.IFacetedProjectWorkingCopy; import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion; +import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager; /** * Provides a data model used by the JSF facet install. @@ -128,7 +129,7 @@ public Object getDefaultProperty(String propertyName) { if (propertyName.equals(CONFIG_PATH)) { - return JSFUtils.JSF_DEFAULT_CONFIG_PATH; + return getDefaultConfigPath(); } else if (propertyName.equals(SERVLET_NAME)) { return JSFUtils.JSF_DEFAULT_SERVLET_NAME; } else if (propertyName.equals(SERVLET_CLASSNAME)) { @@ -148,7 +149,20 @@ } return super.getDefaultProperty(propertyName); } - + + private String getDefaultConfigPath() { + String configPath = JSFUtils.JSF_DEFAULT_CONFIG_PATH; + Object objFPWC = model.getProperty(IFacetDataModelProperties.FACETED_PROJECT_WORKING_COPY); + if (objFPWC instanceof IFacetedProjectWorkingCopy) { + IFacetedProjectWorkingCopy fpwc = (IFacetedProjectWorkingCopy) objFPWC; + if (!fpwc.hasProjectFacet(ProjectFacetsManager.getProjectFacet("jst.web")) && //$NON-NLS-1$ + fpwc.hasProjectFacet(ProjectFacetsManager.getProjectFacet("jst.webfragment"))) { //$NON-NLS-1$ + configPath = JSFUtils.JSF_DEFAULT_CONFIG_PATH_FOR_WEB_FRAGMENT; + } + } + return configPath; + } + @Override public boolean propertySet( final String propertyName, final Object propertyValue ) diff --git src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java index d688a50..ffcceb8 100644 --- src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java +++ src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetInstallDelegate.java @@ -33,7 +33,6 @@ import org.eclipse.jst.common.project.facet.core.libprov.user.UserLibraryProviderInstallOperationConfig; import org.eclipse.jst.j2ee.model.IModelProvider; import org.eclipse.jst.j2ee.model.ModelProviderManager; -import org.eclipse.jst.javaee.web.WebApp; import org.eclipse.jst.jsf.common.facet.libraryprovider.jsf.JsfLibraryUtil; import org.eclipse.jst.jsf.common.webxml.WebXmlUpdater; import org.eclipse.jst.jsf.core.internal.JSFCorePlugin; @@ -364,9 +363,13 @@ if (shouldModify(jsfUtil)) { final IModelProvider provider = jsfUtil.getModelProvider(); - final IPath webXMLPath = new Path("WEB-INF").append("web.xml"); //$NON-NLS-1$ //$NON-NLS-2$ - if (jsfUtil.isJavaEE(provider.getModelObject())) + IPath webXMLPath = new Path("WEB-INF").append("web.xml"); //$NON-NLS-1$ //$NON-NLS-2$ + final Object modelObject = provider.getModelObject(); + if (jsfUtil.isJavaEE(modelObject) || jsfUtil.isWebFragment(modelObject)) { + if (jsfUtil.isWebFragment(modelObject)) { + webXMLPath = new Path("META-INF").append("web-fragment.xml"); //$NON-NLS-1$ //$NON-NLS-2$ + } provider.modify(new UpdateWebXMLForJavaEE(project, config, jsfUtil), doesDDFileExist(jsfUtil) ? webXMLPath : IModelProvider.FORCESAVE); @@ -446,8 +449,8 @@ } public void run() { - final WebApp webApp = (WebApp) ModelProviderManager.getModelProvider(project).getModelObject(); - jsfUtil.updateWebApp(webApp, config); + final Object webAppObj = ModelProviderManager.getModelProvider(project).getModelObject(); + jsfUtil.updateWebApp(webAppObj, config); } } diff --git src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetUninstallDelegate.java src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetUninstallDelegate.java index 29406fb..f60a88e 100644 --- src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetUninstallDelegate.java +++ src/org/eclipse/jst/jsf/core/internal/project/facet/JSFFacetUninstallDelegate.java @@ -14,12 +14,13 @@ import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.Path; import org.eclipse.jst.j2ee.model.IModelProvider; import org.eclipse.jst.j2ee.model.ModelProviderManager; +import org.eclipse.jst.javaee.web.IWebCommon; import org.eclipse.jst.javaee.web.WebApp; +import org.eclipse.jst.javaee.web.WebFragment; +import org.eclipse.jst.jsf.common.webxml.WebXmlUtils; import org.eclipse.jst.jsf.core.internal.Messages; import org.eclipse.osgi.util.NLS; import org.eclipse.wst.common.project.facet.core.IDelegate; @@ -95,15 +96,18 @@ Object webAppObj = provider.getModelObject(); if (webAppObj != null) { - IPath ddPath = new Path("WEB-INF").append("web.xml"); //$NON-NLS-1$ //$NON-NLS-2$ if (isJavaEEWebApp(webAppObj)) { provider.modify(new RemoveJSFFromJavaEEWebAppOperation(project, - jsfUtil), ddPath); + jsfUtil), WebXmlUtils.WEB_XML_PATH); + } else if (isWebFragment(webAppObj)) + { + provider.modify(new RemoveJSFFromJavaEECommonOperation(project, + jsfUtil), WebXmlUtils.WEB_FRAGMENT_XML_PATH); } else {// 2.3 or 2.4 web app provider.modify(new RemoveJSFFromJ2EEWebAppOperation(project, - jsfUtil), ddPath); + jsfUtil), WebXmlUtils.WEB_XML_PATH); } } } @@ -114,6 +118,10 @@ return false; + } + + private boolean isWebFragment(final Object webAppObj) { + return webAppObj instanceof WebFragment; } static class RemoveJSFFromJavaEEWebAppOperation implements Runnable { @@ -134,6 +142,24 @@ } + static class RemoveJSFFromJavaEECommonOperation implements Runnable { + private IProject _project; + private JSFUtils _jsfUtil; + + RemoveJSFFromJavaEECommonOperation(final IProject project, JSFUtils jsfUtil){ + this._project = project; + this._jsfUtil = jsfUtil; + } + + public void run() + { + IWebCommon webApp = (IWebCommon) ModelProviderManager.getModelProvider( + _project).getModelObject(); + _jsfUtil.rollbackWebApp(webApp); + } + + } + static class RemoveJSFFromJ2EEWebAppOperation implements Runnable { private IProject _project; private JSFUtils _jsfUtil; diff --git src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils.java src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils.java index 71fa94b..f907c81 100644 --- src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils.java +++ src/org/eclipse/jst/jsf/core/internal/project/facet/JSFUtils.java @@ -32,6 +32,7 @@ import org.eclipse.jst.j2ee.model.IModelProvider; import org.eclipse.jst.j2ee.webapplication.WebapplicationFactory; import org.eclipse.jst.javaee.core.JavaeeFactory; +import org.eclipse.jst.javaee.web.IWebCommon; import org.eclipse.jst.javaee.web.WebAppVersionType; import org.eclipse.jst.javaee.web.WebFactory; import org.eclipse.jst.jsf.core.JSFVersion; @@ -70,6 +71,12 @@ * The path to the default application configuration file */ public static final String JSF_DEFAULT_CONFIG_PATH = "/WEB-INF/faces-config.xml"; //$NON-NLS-1$ + + /** + * The path to the default application configuration file in a web fragment project + */ + public static final String JSF_DEFAULT_CONFIG_PATH_FOR_WEB_FRAGMENT = + "/META-INF/faces-config.xml"; //$NON-NLS-1$ /** * Default URL mapping to faces servlet @@ -224,6 +231,14 @@ } /** + * @param webAppObj + * @return true if webAppObj represents a web fragment + */ + public boolean isWebFragment(final Object webAppObj) { + return webAppObj instanceof org.eclipse.jst.javaee.web.WebFragment; + } + + /** * @param config * @return list of URL patterns from the datamodel */ @@ -303,8 +318,8 @@ protected String getDefaultSuffix(Object webApp) { String contextParam = null; if (webApp != null) { - if(isJavaEE(webApp)) { - contextParam = JEEUtils.getContextParam((org.eclipse.jst.javaee.web.WebApp) webApp, JSF_DEFAULT_SUFFIX_CONTEXT_PARAM); + if(isJavaEE(webApp) || isWebFragment(webApp)) { + contextParam = JEECommonUtils.getContextParam((IWebCommon) webApp, JSF_DEFAULT_SUFFIX_CONTEXT_PARAM); } else { contextParam = J2EEUtils.getContextParam((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, JSF_DEFAULT_SUFFIX_CONTEXT_PARAM); @@ -439,8 +454,8 @@ * @return Servlet or null */ protected Object findJSFServlet(Object webApp) { - if(isJavaEE(webApp)) { - return JEEUtils.findServlet((org.eclipse.jst.javaee.web.WebApp) webApp, JSF_SERVLET_CLASS); + if(isJavaEE(webApp) || isWebFragment(webApp)) { + return JEECommonUtils.findServlet((IWebCommon) webApp, JSF_SERVLET_CLASS); } return J2EEUtils.findServlet((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, JSF_SERVLET_CLASS); } @@ -460,8 +475,8 @@ { String displayName = getDisplayName(config); String className = getServletClassname(config); - if(isJavaEE(webApp)) { - return JEEUtils.createOrUpdateServletRef((org.eclipse.jst.javaee.web.WebApp) webApp, displayName, className, (org.eclipse.jst.javaee.web.Servlet) servlet); + if(isJavaEE(webApp) || isWebFragment(webApp)) { + return JEECommonUtils.createOrUpdateServletRef((IWebCommon) webApp, displayName, className, (org.eclipse.jst.javaee.web.Servlet) servlet); } return J2EEUtils.createOrUpdateServletRef((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, displayName, className, (org.eclipse.jst.j2ee.webapplication.Servlet) servlet); } @@ -478,8 +493,8 @@ protected void setUpURLMappings(final Object webApp, final List urlMappingList, final Object servlet) { - if(isJavaEE(webApp)) { - JEEUtils.setUpURLMappings((org.eclipse.jst.javaee.web.WebApp) webApp, urlMappingList, (org.eclipse.jst.javaee.web.Servlet) servlet); + if(isJavaEE(webApp) || isWebFragment(webApp)) { + JEECommonUtils.setUpURLMappings((IWebCommon) webApp, urlMappingList, (org.eclipse.jst.javaee.web.Servlet) servlet); } else { J2EEUtils.setUpURLMappings((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, urlMappingList, (org.eclipse.jst.j2ee.webapplication.Servlet) servlet); @@ -492,8 +507,8 @@ * @param servlet */ protected void removeURLMappings(final Object webApp, final Object servlet) { - if(isJavaEE(webApp)) { - JEEUtils.removeURLMappings((org.eclipse.jst.javaee.web.WebApp) webApp, (org.eclipse.jst.javaee.web.Servlet) servlet); + if(isJavaEE(webApp) || isWebFragment(webApp)) { + JEECommonUtils.removeURLMappings((IWebCommon) webApp, (org.eclipse.jst.javaee.web.Servlet) servlet); } else { J2EEUtils.removeURLMappings((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, (org.eclipse.jst.j2ee.webapplication.Servlet) servlet); @@ -506,8 +521,8 @@ * @param servlet */ protected void removeJSFServlet(final Object webApp, final Object servlet) { - if(isJavaEE(webApp)) { - JEEUtils.removeServlet((org.eclipse.jst.javaee.web.WebApp) webApp, (org.eclipse.jst.javaee.web.Servlet) servlet); + if(isJavaEE(webApp) || isWebFragment(webApp)) { + JEECommonUtils.removeServlet((IWebCommon) webApp, (org.eclipse.jst.javaee.web.Servlet) servlet); } else { J2EEUtils.removeServlet((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, (org.eclipse.jst.j2ee.webapplication.Servlet) servlet); @@ -519,8 +534,8 @@ * @param webApp */ protected void removeJSFContextParams(final Object webApp) { - if(isJavaEE(webApp)) { - JEEUtils.removeContextParam((org.eclipse.jst.javaee.web.WebApp) webApp, JSF_CONFIG_CONTEXT_PARAM); + if(isJavaEE(webApp) || isWebFragment(webApp)) { + JEECommonUtils.removeContextParam((IWebCommon) webApp, JSF_CONFIG_CONTEXT_PARAM); } else { J2EEUtils.removeContextParam((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, JSF_CONFIG_CONTEXT_PARAM); @@ -535,8 +550,8 @@ protected void setupContextParams(final Object webApp, final IDataModel config) { final String paramValue = config.getStringProperty(IJSFFacetInstallDataModelProperties.CONFIG_PATH); if (paramValue != null && !paramValue.equals(JSF_DEFAULT_CONFIG_PATH)) { - if(isJavaEE(webApp)) { - JEEUtils.setupContextParam((org.eclipse.jst.javaee.web.WebApp) webApp, JSF_CONFIG_CONTEXT_PARAM, paramValue); + if(isJavaEE(webApp) || isWebFragment(webApp)) { + JEECommonUtils.setupContextParam((IWebCommon) webApp, JSF_CONFIG_CONTEXT_PARAM, paramValue); } else { J2EEUtils.setupContextParam((org.eclipse.jst.j2ee.webapplication.WebApp) webApp, JSF_CONFIG_CONTEXT_PARAM, paramValue); @@ -552,8 +567,8 @@ * in url patterns. */ protected String getFileExtensionFromMap(final Object webApp, final Object map) { - if(isJavaEE(webApp)) { - return JEEUtils.getFileExtensionFromMap((org.eclipse.jst.javaee.web.ServletMapping) map); + if(isJavaEE(webApp) || isWebFragment(webApp)) { + return JEECommonUtils.getFileExtensionFromMap((org.eclipse.jst.javaee.web.ServletMapping) map); } return J2EEUtils.getFileExtensionFromMap((org.eclipse.jst.j2ee.webapplication.ServletMapping) map); } @@ -564,8 +579,8 @@ * @return prefix mapping. may return null. */ protected String getPrefixMapping(final Object webApp, final Object map) { - if(isJavaEE(webApp)) { - return JEEUtils.getPrefixMapping((org.eclipse.jst.javaee.web.ServletMapping) map); + if(isJavaEE(webApp) || isWebFragment(webApp)) { + return JEECommonUtils.getPrefixMapping((org.eclipse.jst.javaee.web.ServletMapping) map); } return J2EEUtils.getPrefixMapping((org.eclipse.jst.j2ee.webapplication.ServletMapping) map); } @@ -601,8 +616,8 @@ && !isValidKnownExtension(resource.getFileExtension())) return null; - if(isJavaEE(webAppObj)) { - org.eclipse.jst.javaee.web.WebApp webApp = (org.eclipse.jst.javaee.web.WebApp) webAppObj; + if(isJavaEE(webAppObj) || isWebFragment(webAppObj)) { + IWebCommon webApp = (IWebCommon) webAppObj; final String servletName = ((org.eclipse.jst.javaee.web.Servlet) servlet).getServletName(); @@ -704,7 +719,7 @@ if (sysPropServletMapping == null) { sysPropServletMapping = JSF_DEFAULT_URL_MAPPING; } - if (isJavaEE(webApp)) { + if (isJavaEE(webApp) || isWebFragment(webApp)) { servlet = WebFactory.eINSTANCE.createServlet(); ((org.eclipse.jst.javaee.web.Servlet)servlet).setServletName(JSF_DEFAULT_SERVLET_NAME); org.eclipse.jst.javaee.web.ServletMapping mapping = WebFactory.eINSTANCE.createServletMapping(); diff --git src/org/eclipse/jst/jsf/designtime/internal/view/model/jsp/registry/TLDRegistryManager.java src/org/eclipse/jst/jsf/designtime/internal/view/model/jsp/registry/TLDRegistryManager.java index 8bec065..6b591e9 100644 --- src/org/eclipse/jst/jsf/designtime/internal/view/model/jsp/registry/TLDRegistryManager.java +++ src/org/eclipse/jst/jsf/designtime/internal/view/model/jsp/registry/TLDRegistryManager.java @@ -40,6 +40,7 @@ { // STATIC private final static String JST_WEB_MODULE = "jst.web"; //$NON-NLS-1$ + private final static String JST_WEB_FRAGMENT = "jst.webfragment"; //$NON-NLS-1$ private static TLDRegistryManager INSTANCE; /** @@ -153,8 +154,10 @@ return false; } - // Check that this is a dynamic web project - // (I.E. the JST Web facet is installed) + /* + * Check that this is a dynamic web project (i.e. the JST Web facet is installed) or a + * web fragment project (i.e. the JST Web Fragment facet is installed) + */ try { if (ProjectFacetsManager.isProjectFacetDefined(JST_WEB_MODULE)) @@ -166,6 +169,15 @@ return true; } } + if (ProjectFacetsManager.isProjectFacetDefined(JST_WEB_FRAGMENT)) + { + IFacetedProject faceted = ProjectFacetsManager.create(project); + IProjectFacet webFragmentFacet = ProjectFacetsManager.getProjectFacet(JST_WEB_FRAGMENT); + if (faceted != null && faceted.hasProjectFacet(webFragmentFacet)) + { + return true; + } + } } catch (CoreException ce) { #P org.eclipse.jst.jsf.facesconfig.ui diff --git src/org/eclipse/jst/jsf/facesconfig/ui/FacesConfigEditor.java src/org/eclipse/jst/jsf/facesconfig/ui/FacesConfigEditor.java index 98310cf..0643bbb 100644 --- src/org/eclipse/jst/jsf/facesconfig/ui/FacesConfigEditor.java +++ src/org/eclipse/jst/jsf/facesconfig/ui/FacesConfigEditor.java @@ -1385,6 +1385,7 @@ private boolean matches(IEditorInput input) { final IResource file = (IResource) input.getAdapter(IResource.class); boolean hasWebFacet = false; + boolean hasWebFragmentFacet = false; boolean hasJSFFacet = false; if (file != null) { @@ -1408,6 +1409,8 @@ hasJSFFacet = true; } else if ("jst.web".equals(facet.getId())) { //$NON-NLS-1$ hasWebFacet = true; + } else if ("jst.webfragment".equals(facet.getId())) { //$NON-NLS-1$ + hasWebFragmentFacet = true; } } } @@ -1421,7 +1424,7 @@ } } - return hasWebFacet && hasJSFFacet; + return (hasWebFacet || hasWebFragmentFacet) && hasJSFFacet; } /** #P org.eclipse.jst.jsf.ui diff --git plugin.xml plugin.xml index 1e33adc..e1c4f20 100644 --- plugin.xml +++ plugin.xml @@ -172,6 +172,9 @@ + +