| /******************************************************************************* |
| * Copyright (c) 2009, 2010 IBM 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: |
| * IBM Corporation - initial API and implementation |
| * yyyymmdd bug Email and other contact information |
| * -------- -------- ----------------------------------------------------------- |
| * 20091109 291954 ericdp@ca.ibm.com - Eric D. Peters, JAX-RS: Implement JAX-RS Facet |
| * 20100303 291954 kchong@ca.ibm.com - Keith Chong, JAX-RS: Implement JAX-RS Facet |
| * 20100319 306594 ericdp@ca.ibm.com - Eric D. Peters, JAX-RS facet install fails for Web 2.3 & 2.4 |
| * 20100324 306937 ericdp@ca.ibm.com - Eric D. Peters, JAX-RS Properties page- NPE after pressing OK |
| * 20100325 307059 ericdp@ca.ibm.com - Eric D. Peters, JAX-RS properties page- fields empty or incorrect |
| *******************************************************************************/ |
| package org.eclipse.jst.ws.jaxrs.ui.internal.project.facet; |
| |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.Iterator; |
| 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.core.runtime.NullProgressMonitor; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.jst.common.project.facet.core.libprov.ILibraryProvider; |
| import org.eclipse.jst.common.project.facet.core.libprov.LibraryInstallDelegate; |
| import org.eclipse.jst.common.project.facet.ui.libprov.LibraryFacetPropertyPage; |
| import org.eclipse.jst.j2ee.model.IModelProvider; |
| import org.eclipse.jst.javaee.core.UrlPatternType; |
| import org.eclipse.jst.javaee.web.Servlet; |
| import org.eclipse.jst.javaee.web.ServletMapping; |
| import org.eclipse.jst.javaee.web.WebApp; |
| import org.eclipse.jst.ws.jaxrs.core.internal.IJAXRSCoreConstants; |
| import org.eclipse.jst.ws.jaxrs.core.internal.project.facet.IJAXRSFacetInstallDataModelProperties; |
| import org.eclipse.jst.ws.jaxrs.core.internal.project.facet.JAXRSJ2EEUtils; |
| import org.eclipse.jst.ws.jaxrs.core.internal.project.facet.JAXRSJEEUtils; |
| import org.eclipse.jst.ws.jaxrs.core.internal.project.facet.JAXRSUtils; |
| import org.eclipse.jst.ws.jaxrs.ui.internal.IJAXRSUIConstants; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.widgets.Composite; |
| import org.eclipse.swt.widgets.Control; |
| import org.eclipse.wst.common.frameworks.datamodel.IDataModel; |
| import org.eclipse.wst.common.project.facet.core.IFacetedProject; |
| import org.eclipse.wst.common.project.facet.core.IProjectFacet; |
| import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion; |
| import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager; |
| |
| @SuppressWarnings("restriction") |
| public final class JAXRSLibraryPropertyPage |
| |
| extends LibraryFacetPropertyPage |
| |
| implements IJAXRSFacetInstallDataModelProperties |
| |
| { |
| |
| private static final String SETTINGS_SERVLET = "servletName"; //$NON-NLS-1$ |
| private static final String SETTINGS_SERVLET_CLASSNAME = "servletClassname"; //$NON-NLS-1$ |
| private static final String SETTINGS_URL_MAPPINGS = "urlMappings"; //$NON-NLS-1$ |
| private static final String SETTINGS_URL_PATTERN = "pattern"; //$NON-NLS-1$ |
| |
| private ServletInformationGroup servletInfoGroup; |
| |
| // Java EE |
| private Servlet servlet = null; |
| private ServletMapping servletMapping = null; |
| // J2EE |
| private org.eclipse.jst.j2ee.webapplication.Servlet j2eeServlet = null; |
| private org.eclipse.jst.j2ee.webapplication.ServletMapping j2eeServletMapping = null; |
| private String initialInstallDelegateLibraryProviderID =""; //$NON-NLS-1$ |
| @Override |
| protected Control createPageContents(Composite parent) |
| { |
| Control c = super.createPageContents(parent); |
| LibraryInstallDelegate initialInstallDelegate = super.getLibraryInstallDelegate(); |
| if ( initialInstallDelegate != null ) { |
| ILibraryProvider initialLibraryProvider = initialInstallDelegate.getLibraryProvider(); |
| if (initialLibraryProvider != null) { |
| String initID = initialLibraryProvider.getId(); |
| initialInstallDelegateLibraryProviderID = (initID == null) ? initialInstallDelegateLibraryProviderID : initID; |
| } |
| } |
| servletInfoGroup = new ServletInformationGroup((Composite) c, SWT.NONE); |
| initializeValues(); |
| |
| return c; |
| } |
| |
| public IProjectFacetVersion getProjectFacetVersion() |
| { |
| final IProjectFacet jaxrsFacet = ProjectFacetsManager.getProjectFacet(IJAXRSCoreConstants.JAXRS_FACET_ID); |
| final IFacetedProject fproj = getFacetedProject(); |
| return fproj.getInstalledVersion(jaxrsFacet); |
| } |
| |
| @SuppressWarnings({ "unchecked", "rawtypes" }) |
| protected void initializeValues() |
| { |
| |
| IModelProvider provider = JAXRSUtils.getModelProvider(getProject()); |
| Object webAppObj = provider.getModelObject(); |
| List<ServletMapping> servletMappings = new ArrayList<ServletMapping>(); |
| if (webAppObj != null) |
| { |
| String servletName = JAXRSUtils.JAXRS_DEFAULT_SERVLET_NAME; |
| String servletClass = ""; //$NON-NLS-1$ |
| if (JAXRSJEEUtils.isWebApp25or30(webAppObj)) { |
| WebApp webApp = (WebApp) webAppObj; |
| Servlet servlet = JAXRSJEEUtils.findJAXRSServlet(webApp); |
| if (servlet != null) { |
| this.servlet = servlet; |
| servletMappings = webApp.getServletMappings(); |
| servletName = (servlet.getServletName() == null) ? servletName : servlet.getServletName(); |
| servletClass =(servlet.getServletClass() == null) ? servletClass : servlet.getServletClass(); |
| } |
| } else { |
| // 2.3 or 2.4 web app |
| org.eclipse.jst.j2ee.webapplication.WebApp webApp = (org.eclipse.jst.j2ee.webapplication.WebApp) webAppObj; |
| org.eclipse.jst.j2ee.webapplication.Servlet servlet = JAXRSJ2EEUtils |
| .findJAXRSServlet(webApp); |
| if (servlet != null) { |
| this.j2eeServlet = servlet; |
| servletMappings = webApp.getServletMappings(); |
| servletName = (servlet.getServletName() == null) ? servletName : servlet.getServletName(); |
| if (servlet.getServletClass() != null) { |
| servletClass =(servlet.getServletClass().getQualifiedName() == null) ? servletClass : servlet.getServletClass().getQualifiedName(); |
| } |
| } |
| |
| } |
| servletInfoGroup.txtJAXRSServletName.setText(servletName); |
| servletInfoGroup.txtJAXRSServletClassName.setText(servletClass); |
| // Find the servletMapping that corresponds to the servletName |
| if (JAXRSJEEUtils.isWebApp25or30(webAppObj)) { |
| servletInfoGroup.lstJAXRSServletURLPatterns.removeAll(); |
| |
| for (Iterator<ServletMapping> i = servletMappings.iterator(); i.hasNext();) |
| { |
| Object o = i.next(); |
| if (o instanceof ServletMapping) |
| { |
| // init the servletMapping |
| ServletMapping next = (ServletMapping) o; |
| if (servletName.equals(next.getServletName())) |
| { |
| this.servletMapping = next; |
| for (Iterator p = servletMapping.getUrlPatterns().iterator(); p.hasNext();) |
| { |
| UrlPatternType pattern = (UrlPatternType) p.next(); |
| servletInfoGroup.lstJAXRSServletURLPatterns.add(pattern.getValue()); |
| } |
| } |
| } |
| } |
| } else { |
| servletInfoGroup.lstJAXRSServletURLPatterns.removeAll(); |
| for (Iterator<ServletMapping> i = servletMappings.iterator(); i.hasNext();) |
| { |
| Object o = i.next(); |
| if (o instanceof org.eclipse.jst.j2ee.webapplication.ServletMapping) |
| { |
| // init the servletMapping |
| org.eclipse.jst.j2ee.webapplication.ServletMapping next = (org.eclipse.jst.j2ee.webapplication.ServletMapping) o; |
| |
| if (servletName.equals(next.getServlet().getServletName())) |
| { |
| this.j2eeServletMapping = next; |
| |
| |
| String pattern = next.getUrlPattern(); |
| servletInfoGroup.lstJAXRSServletURLPatterns.add(new String(pattern)); |
| |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| public boolean performOk() |
| { |
| LibraryInstallDelegate installDelegate = super.getLibraryInstallDelegate(); |
| if ( installDelegate != null ) { |
| ILibraryProvider libraryProvider = installDelegate.getLibraryProvider(); |
| if (libraryProvider != null) { |
| String id = libraryProvider.getId(); |
| if (!initialInstallDelegateLibraryProviderID.equals(id) || IJAXRSUIConstants.USER_LIBRARY_ID.equals(id)) { |
| // This will update the libraries by calling the library provider delegate |
| super.performOk(); |
| } |
| } |
| } |
| |
| |
| // Update the servlet properties |
| createServletAndModifyWebXML(getProject(), null, new NullProgressMonitor()); |
| return true; |
| } |
| |
| private void createServletAndModifyWebXML(final IProject project, final IDataModel config, final IProgressMonitor monitor) |
| { |
| IModelProvider provider = JAXRSUtils.getModelProvider(project); |
| IPath webXMLPath = new Path("WEB-INF").append("web.xml"); //$NON-NLS-1$ //$NON-NLS-2$ |
| List<String> listOfMappings = Arrays.asList(servletInfoGroup.lstJAXRSServletURLPatterns.getItems()); |
| |
| if (JAXRSJEEUtils.isWebApp25or30(provider.getModelObject())) |
| { |
| provider.modify(new UpdateWebXMLForJavaEE(project, this.servlet, this.servletMapping, servletInfoGroup.txtJAXRSServletName.getText(), servletInfoGroup.txtJAXRSServletClassName.getText(), listOfMappings), |
| doesDDFileExist(project, webXMLPath) ? webXMLPath : IModelProvider.FORCESAVE); |
| } |
| else |
| // must be 2.3 or 2.4 |
| { |
| provider.modify(new UpdateWebXMLForJ2EE(project, this.j2eeServlet, this.j2eeServletMapping, servletInfoGroup.txtJAXRSServletName.getText(), servletInfoGroup.txtJAXRSServletClassName.getText(), listOfMappings), webXMLPath); |
| } |
| } |
| |
| private boolean doesDDFileExist(IProject project, IPath webXMLPath) |
| { |
| return project.getProjectRelativePath().append(webXMLPath).toFile().exists(); |
| } |
| |
| } |