`
yianpuodiaotu
  • 浏览: 240298 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

WTP Facet 之 AddFilter

阅读更多

做插件开发的人员都知道,如果你不晓得该使用那个Eclipse提供的api工具类,你做起来很痛苦。

今天做facet的时候,发现j2ee 和 jee 是有区别的,自己琢磨着把程序功能做了出来,费了好大劲。

 

今天偶尔看到这一个facet,顿时感慨,如果早看到它,也不用那么费神了。

/*******************************************************************************
 * Copyright (c) 2007, 2008 SAP AG 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:
 * Kaloyan Raev, kaloyan.raev@sap.com - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.j2ee.internal.web.operations;

import static org.eclipse.jst.j2ee.internal.web.operations.INewFilterClassDataModelProperties.FILTER_MAPPINGS;
import static org.eclipse.jst.j2ee.internal.web.operations.INewFilterClassDataModelProperties.INIT_PARAM;
import static org.eclipse.jst.j2ee.internal.web.operations.INewWebClassDataModelProperties.DESCRIPTION;
import static org.eclipse.jst.j2ee.internal.web.operations.INewWebClassDataModelProperties.DISPLAY_NAME;

import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.jst.j2ee.common.CommonFactory;
import org.eclipse.jst.j2ee.common.Description;
import org.eclipse.jst.j2ee.common.ParamValue;
import org.eclipse.jst.j2ee.internal.J2EEVersionConstants;
import org.eclipse.jst.j2ee.internal.common.operations.NewJavaEEArtifactClassOperation;
import org.eclipse.jst.j2ee.webapplication.DispatcherType;
import org.eclipse.jst.j2ee.webapplication.Filter;
import org.eclipse.jst.j2ee.webapplication.FilterMapping;
import org.eclipse.jst.j2ee.webapplication.InitParam;
import org.eclipse.jst.j2ee.webapplication.Servlet;
import org.eclipse.jst.j2ee.webapplication.WebApp;
import org.eclipse.jst.j2ee.webapplication.WebapplicationFactory;
import org.eclipse.jst.javaee.core.DisplayName;
import org.eclipse.jst.javaee.core.JavaeeFactory;
import org.eclipse.jst.javaee.core.UrlPatternType;
import org.eclipse.jst.javaee.web.WebFactory;
import org.eclipse.wst.common.componentcore.internal.operation.ArtifactEditProviderOperation;
import org.eclipse.wst.common.frameworks.datamodel.IDataModel;

/**
 * This class, AddFilter Operation is a IDataModelOperation following the
 * IDataModel wizard and operation framework.
 * 
 * @see org.eclipse.wst.common.frameworks.datamodel.IDataModelOperation
 * @see org.eclipse.wst.common.frameworks.datamodel.IDataModelProvider
 * 
 * This operation subclasses the ArtifactEditProviderOperation so the changes
 * made to the deployment descriptor models are saved to the artifact edit
 * model.
 * @see org.eclipse.wst.common.componentcore.internal.operation.ArtifactEditProviderOperation
 * 
 * It is the operation which should be used when adding a new filter to a web
 * app. This uses the NewFilterClassDataModelProvider to retrieve properties set by the
 * user in order to create the custom filter.
 * @see org.eclipse.jst.j2ee.internal.web.operations.NewFilterClassDataModelProvider
 * 
 * This operation will add the metadata necessary into the web deployment descriptor. 
 * To actually create the java class for the filter, the operation uses the NewFilterClassOperation. 
 * The NewFilterClassOperation shares the same data model provider.
 * @see org.eclipse.jst.j2ee.internal.web.operations.NewFilterClassOperation
 * 
 * Clients may subclass this operation to provide their own behavior on filter
 * creation. The execute method can be extended to do so. Also,
 * generateFilterMetaData and creteFilterClass are exposed.
 * 
 * The use of this class is EXPERIMENTAL and is subject to substantial changes.
 */
public class AddFilterOperation extends AddWebClassOperation {

	/**
	 * This is the constructor which should be used when creating the operation.
	 * It will not accept null parameter. It will not return null.
	 * 
	 * @see ArtifactEditProviderOperation#ArtifactEditProviderOperation(IDataModel)
	 * 
	 * @param dataModel
	 * @return AddFilterOperation
	 */
	public AddFilterOperation(IDataModel dataModel) {
		super(dataModel);
	}
	
	@Override
	protected NewJavaEEArtifactClassOperation getNewClassOperation() {
		return new NewFilterClassOperation(getDataModel());
	}

	/**
	 * Subclasses may extend this method to add their own generation steps for
	 * the creation of the metadata for the web deployment descriptor. This
	 * implementation uses the J2EE models to create the Filter model instance,
	 * any init params specified, and any filter mappings. It then adds these
	 * to the web application model. This will then be written out to the
	 * deployment descriptor file. This method does not accept null parameters.
	 * 
	 * @see Filter
	 * @see AddFilterOperation#createFilter(String)
	 * @see AddFilterOperation#setUpInitParams(List, Filter)
	 * @see AddFilterOperation#setUpURLMappings(List, Filter)
	 * 
	 * @param aModel
	 * @param qualifiedClassName
	 */
	@Override
	protected void generateMetaData(IDataModel aModel, String qualifiedClassName) {
		// Set up the filter modelled object
		Object filter = createFilter(qualifiedClassName);

		// Set up the InitParams if any
		List initParamList = 
		    (List) aModel.getProperty(INIT_PARAM);
		if (initParamList != null)
			setUpInitParams(initParamList, filter);

		// Set up the filter mappings if any
		 List filterMappingsList = 
		     (List) aModel.getProperty(FILTER_MAPPINGS);

         if (filterMappingsList != null && !filterMappingsList.isEmpty())
             setUpMappings(filterMappingsList, filter);
	}

	/**
	 * This method is intended for private use only. This method is used to
	 * create the filter modeled object, to set any parameters specified in
	 * the data model, and then to add the filter instance to the web
	 * application model. This method does not accept null parameters. It will
	 * not return null.
	 * 
	 * @see AddFilterOperation#generateFilterMetaData(NewFilterClassDataModel,
	 *      String)
	 * @see WebapplicationFactory#createFilter()
	 * @see Filter
	 * 
	 * @param qualifiedClassName
	 * @return Filter instance
	 */
	/**
	 * @param qualifiedClassName
	 * @return
	 */
	private Object createFilter(String qualifiedClassName) {
		// Get values from data model
		String displayName = 
		    model.getStringProperty(DISPLAY_NAME);
		String description = 
		    model.getStringProperty(DESCRIPTION);

		// Create the filter instance and set up the parameters from data model
		Object modelObject = provider.getModelObject(WEB_APP_XML_PATH);
		if (modelObject instanceof org.eclipse.jst.j2ee.webapplication.WebApp) {
			Filter filter = WebapplicationFactory.eINSTANCE.createFilter();
			filter.setName(displayName);
			filter.setDisplayName(displayName);
			filter.setDescription(description);
			filter.setFilterClassName(qualifiedClassName);

			// Add the filter to the web application model
			WebApp webApp = (WebApp) modelObject;
			webApp.getFilters().add(filter);
			return filter;
		} else if (modelObject instanceof org.eclipse.jst.javaee.web.WebApp) {
			org.eclipse.jst.javaee.web.WebApp webApp = (org.eclipse.jst.javaee.web.WebApp) modelObject;
			org.eclipse.jst.javaee.web.Filter filter = WebFactory.eINSTANCE.createFilter();
			DisplayName displayNameObj = JavaeeFactory.eINSTANCE.createDisplayName();
            displayNameObj.setValue(displayName);
            filter.getDisplayNames().add(displayNameObj);
			filter.setFilterName(displayName);
			filter.setFilterClass(qualifiedClassName);
			webApp.getFilters().add(filter);
			// Should be return Filter's instance
			return filter;
		}
		// Return the filter instance
		return null;
	}

	/**
	 * This method is intended for internal use only. This is used to create any
	 * init params for the new filter metadata. It will not accept null
	 * parameters. The init params are set on the filter modeled object.
	 * 
	 * @see AddFilterOperation#generateFilterMetaData(NewFilterClassDataModel,
	 *      String)
	 * @see WebapplicationFactory#createInitParam()
	 * 
	 * @param initParamList
	 * @param filter
	 */
	private void setUpInitParams(List initParamList, Object filterObj) {
		// Get the web app instance from the data model
		Object modelObject = provider.getModelObject();
		if (modelObject instanceof org.eclipse.jst.j2ee.webapplication.WebApp) {
			WebApp webApp = (WebApp) modelObject;
			Filter filter = (Filter) filterObj;

			// If J2EE 1.4, add the param value and description info instances
			// to the filter init params
			if (webApp.getJ2EEVersionID() >= J2EEVersionConstants.J2EE_1_4_ID) {
				for (int iP = 0; iP < initParamList.size(); iP++) {
					String[] stringArray = (String[]) initParamList.get(iP);
					// Create 1.4 common param value
					ParamValue param = CommonFactory.eINSTANCE.createParamValue();
					param.setName(stringArray[0]);
					param.setValue(stringArray[1]);
					// Create 1.4 common descripton value
					Description descriptionObj = CommonFactory.eINSTANCE.createDescription();
					descriptionObj.setValue(stringArray[2]);
					// Set the description on the param
					param.getDescriptions().add(descriptionObj);
					param.setDescription(stringArray[2]);
					// Set the param to the filter model list of init params
					filter.getInitParamValues().add(param);
				}
			}
			// If J2EE 1.2 or 1.3, use the filter specific init param instances
			else {
				for (int iP = 0; iP < initParamList.size(); iP++) {
					String[] stringArray = (String[]) initParamList.get(iP);
					// Create the web init param
					InitParam ip = WebapplicationFactory.eINSTANCE.createInitParam();
					// Set the param name
					ip.setParamName(stringArray[0]);
					// Set the param value
					ip.setParamValue(stringArray[1]);
					// Set the param description
					ip.setDescription(stringArray[2]);
					// Add the init param to the filter model list of params
					filter.getInitParams().add(ip);
				}
			}
		} else if (modelObject instanceof org.eclipse.jst.javaee.web.WebApp) {
			org.eclipse.jst.javaee.web.Filter filter = (org.eclipse.jst.javaee.web.Filter) filterObj;

			for (int iP = 0; iP < initParamList.size(); iP++) {
				String[] stringArray = (String[]) initParamList.get(iP);
				// Create 1.4 common param value
				org.eclipse.jst.javaee.core.ParamValue param = 
				    JavaeeFactory.eINSTANCE.createParamValue();
				param.setParamName(stringArray[0]);
				param.setParamValue(stringArray[1]);

				org.eclipse.jst.javaee.core.Description descriptionObj = 
				    JavaeeFactory.eINSTANCE.createDescription();
				descriptionObj.setValue(stringArray[2]);
				// Set the description on the param
				param.getDescriptions().add(descriptionObj);
				// Add the param to the filter model list of init params
				filter.getInitParams().add(param);
			}
		}
	}

	/**
	 * This method is intended for internal use only. This method is used to
	 * create the filter mapping modelled objects so the metadata for the
	 * filter mappings is store in the web deployment descriptor. This method
	 * will not accept null parameters. The filter mappings are added to the
	 * web application modeled object.
	 * 
	 * @see AddFilterOperation#generateFilterMetaData(NewFilterClassDataModel,
	 *      String)
	 * @see WebapplicationFactory#createFilterMapping()
	 * 
	 * @param urlMappingList
	 * @param filter
	 */
	private void setUpMappings(List filterMappingsList, Object filterObj) {
		// Get the web app modelled object from the data model
		// WebApp webApp = (WebApp) artifactEdit.getContentModelRoot();
		Object modelObject = provider.getModelObject(WEB_APP_XML_PATH);

		// Create the filter mappings if any
		if (modelObject instanceof org.eclipse.jst.j2ee.webapplication.WebApp) {
			WebApp webApp = (WebApp) modelObject;
			Filter filter = (Filter) filterObj;
			if (filterMappingsList != null) 
			    for (int iM = 0; iM < filterMappingsList.size(); iM++) {
			        IFilterMappingItem filterMapping = (IFilterMappingItem) filterMappingsList.get(iM);
			        // Create the filter mapping instance from the web factory
			        FilterMapping mapping = WebapplicationFactory.eINSTANCE.createFilterMapping();
			        // Set the filter
			        mapping.setFilter(filter);
			        if (filterMapping.isUrlPatternType()) {
			            // Set the URL pattern to map the filter to
			            mapping.setUrlPattern(filterMapping.getName());
			        } else {
                        // Set the Servlet Name to map the filter to
			        	Servlet servlet = webApp.getServletNamed(filterMapping.getName());
                        mapping.setServlet(servlet);
			        }
			        //Set dispatcher options for the filter mapping if any.
			        int dispatchers = filterMapping.getDispatchers();
			        EList dispatcherTypes = mapping.getDispatcherType();
                    if ((dispatchers & IFilterMappingItem.REQUEST) > 0) {
                        dispatcherTypes.add(DispatcherType.REQUEST_LITERAL);
                    }
                    if ((dispatchers & IFilterMappingItem.FORWARD) > 0) {
                        dispatcherTypes.add(DispatcherType.FORWARD_LITERAL);
                    }
                    if ((dispatchers & IFilterMappingItem.INCLUDE) > 0) {
                        dispatcherTypes.add(DispatcherType.INCLUDE_LITERAL);
                    }
                    if ((dispatchers & IFilterMappingItem.ERROR) > 0) {
                        dispatcherTypes.add(DispatcherType.ERROR_LITERAL);
                    }
			        // Add the filter mapping to the web application modelled list
			        webApp.getFilterMappings().add(mapping);
			    }
		} else if (modelObject instanceof org.eclipse.jst.javaee.web.WebApp) {
		    org.eclipse.jst.javaee.web.WebApp webApp = (org.eclipse.jst.javaee.web.WebApp) modelObject;
		    org.eclipse.jst.javaee.web.Filter filter = (org.eclipse.jst.javaee.web.Filter) filterObj;

		    // Create the filter mapping instance from the web factory
            org.eclipse.jst.javaee.web.FilterMapping mapping = null;
			// Create the filter mappings if any
			if (filterMappingsList != null) {
				for (int i = 0; i < filterMappingsList.size(); i++) {
	                mapping = WebFactory.eINSTANCE.createFilterMapping();
	                mapping.setFilterName(filter.getFilterName());
	                IFilterMappingItem filterMapping = (IFilterMappingItem) filterMappingsList.get(i);
	                if (filterMapping.getMappingType() == IFilterMappingItem.URL_PATTERN) {
	                    // Set the URL pattern to map the filter to
	                    UrlPatternType url = JavaeeFactory.eINSTANCE.createUrlPatternType();
	                    url.setValue(filterMapping.getName());
	                    mapping.getUrlPatterns().add(url);
	                } else {
	                    mapping.getServletNames().add(filterMapping.getName());
	                }
  			        //Set dispatcher options for the filter mapping if any.	
                    int dispatchers = filterMapping.getDispatchers();
                    if ((dispatchers & IFilterMappingItem.REQUEST) > 0) {
                        mapping.getDispatchers().add(org.eclipse.jst.javaee.web.DispatcherType.REQUEST_LITERAL);
                    }
                    if ((dispatchers & IFilterMappingItem.FORWARD) > 0) {
                        mapping.getDispatchers().add(org.eclipse.jst.javaee.web.DispatcherType.FORWARD_LITERAL);
                    }
                    if ((dispatchers & IFilterMappingItem.INCLUDE) > 0) {
                        mapping.getDispatchers().add(org.eclipse.jst.javaee.web.DispatcherType.INCLUDE_LITERAL);
                    }
                    if ((dispatchers & IFilterMappingItem.ERROR) > 0) {
                        mapping.getDispatchers().add(org.eclipse.jst.javaee.web.DispatcherType.ERROR_LITERAL);
                    }
                    // Add the filter mapping to the web application model list
                    webApp.getFilterMappings().add(mapping);
				}
			}
		}
	}

}

 

顺便送一个好的Java code 搜索引擎:http://www.krugle.org

分享到:
评论

相关推荐

    eclipse wtp plugs 3

    eclipse wtp-R-3.0.5-20090521045405 请将文件名改为 wtp-R-3.03.fss 因为文件太大,Eclipse WTP Plugs 使用文件分割工具处理后上传的,先说明如下: 1. eclipse wtp plugs 1 ~ eclipse wtp plugs 4 是一份完整的...

    eclipse wtp plugs 2

    eclipse wtp-R-3.0.5-20090521045405 请将文件名改为 wtp-R-3.02.fss 因为文件太大,Eclipse WTP Plugs 使用文件分割工具处理后上传的,先说明如下: 1. eclipse wtp plugs 1 ~ eclipse wtp plugs 4 是一份完整的...

    wtp.rar_web project_wtp_wtp系统_项目管理_项目管理 java

    Web Top Project(WTP)是一个基于Java开发的开源项目管理系统,专为项目管理和协作而设计。这个系统,正如其名称所示,提供了Web界面以便用户轻松地进行项目管理和任务分配。WTP旨在帮助团队成员跟踪项目进度,管理...

    Eclipse_wtp_tomcat

    Eclipse_wtp_tomcatEclipse_wtp_tomcatEclipse_wtp_tomcatEclipse_wtp_tomcatEclipse_wtp_tomcatEclipse_wtp_tomcatEclipse_wtp_tomcatEclipse_wtp_tomcatEclipse_wtp_tomcat

    wtp-common-fproj-enablement-jdt-I-3.2.0-20100107113913.zip

    The component includes the Java facet, modeling of the JVM-based runtimes and tools for simplifying Java library management for facet authors. Note: you only need this zip file(s) if you want to use...

    wtp-jem-sdk-R-1.5.4-200705021353.zip

    标题 "wtp-jem-sdk-R-1.5.4-200705021353.zip" 暗示了这是一个与Web Tools Platform (WTP) 和 Java Enterprise Edition (Java EE) 开发相关的软件包。WTP 是一个 Eclipse 基金会项目,用于提供开发 web 应用程序和 ...

    Eclipse 3.3配置WTP插件

    ### Eclipse 3.3配置WTP插件 #### 一、引言 Eclipse是一款流行的开源集成开发环境(IDE),广泛应用于Java应用开发以及其他多种语言的项目开发中。Web Tools Platform (WTP) 是Eclipse的一个插件集,用于支持Web和...

    WTP.zip_wtp_zip

    【WTP.zip_wtp_zip】是一个关于Web Tools Platform(WTP)的压缩文件,其中包含了一个名为"WTP.ppt"的演示文稿。WTP是Eclipse基金会的一个项目,主要目的是提供一组工具来支持Web和Java EE应用程序的开发。这个...

    使用WTP来构建你的WEB应用程序

    **使用WTP来构建你的WEB应用程序** Web工具平台(Web Tools Platform,简称WTP)是Eclipse IDE的一个扩展,专为开发、测试和部署...结合Eclipse的强大功能和丰富的插件生态,WTP是Java Web开发者不可或缺的工具之一。

    使用Eclipse及WTP插件开发JSP应用程序

    ### 使用Eclipse及WTP插件开发JSP应用程序 #### 一、安装Eclipse及WTP插件 在本文档中,我们详细介绍了如何在Eclipse环境下安装并配置WTP插件来支持JSP应用程序的开发。以下是安装过程的具体步骤: 1. **安装JDK*...

    基于WTP开发自定义的JSP编辑器

    ### 基于WTP开发自定义的JSP编辑器:深入解析与实践 #### 一、整体概览:WTP及其在插件开发中的地位 Web Tools Platform(WTP)是Eclipse平台上的一个重要组件,专为Java EE和Web应用开发而设计。它不仅提供了强大...

    WTP1.5.3 开发ejb步骤

    【WTP1.5.3 开发ejb步骤】 Web Tools Platform (WTP) 是一个由Eclipse基金会维护的开源项目,它为Java Web应用程序和Java EE(企业版)应用程序的开发提供了强大的集成开发环境(IDE)支持。在WTP1.5.3版本中,开发...

    MyEclipse Web工程 完美移植到Eclipse WTP

    ### MyEclipse Web工程完美移植至Eclipse WTP:详细步骤与解析 #### 背景与挑战 在软件开发领域,开发工具的选择对项目的效率和团队协作有着至关重要的影响。MyEclipse作为一款功能丰富的集成开发环境(IDE),...

    Eclipse WTP Web应用开发

    Eclipse WTP Web应用开发,(曼德尔),姚军等译。

    eclipse wtp plugs 4

    wtp-R-3.0.5-20090521045405 请将文件名改为 wtp-R-3.04.fss 因为文件太大,Eclipse WTP Plugs 使用文件分割工具处理后上传的,先说明如下: 1. eclipse wtp plugs 1 ~ eclipse wtp plugs 4 是一份完整的 Eclipse ...

    eclipse wtp plugs 0

    eclipse wtp-R-3.0.5-20090521045405 plugs 请将文件名改为 wtp-R-3.00.fsm 因为文件太大,Eclipse WTP Plugs 使用文件分割工具处理后上传的,先说明如下: 1. eclipse wtp plugs 1 ~ eclipse wtp plugs 4 是一份...

    eclipse wtp plugs 1

    eclipse wtp-R-3.0.5-20090521045405 plugs 请将文件名改为 wtp-R-3.01.fss 因为文件太大,Eclipse WTP Plugs 使用文件分割工具处理后上传的,先说明如下: 1. eclipse wtp plugs 1 ~ eclipse wtp plugs 4 是一份...

    eclipse WTP 1.2 插件 支持 jquery 1.6

    jQueryWTP一个让Eclipse WTP支持jQuery Javascript代码自动补全功能的Eclipse插件。 支持jquery 1.6

    wtp1.5.x +eclipse3.2.x的中文语言包

    标题 "wtp1.5.x +eclipse3.2.x的中文语言包" 指的是针对Web Tools Platform (WTP) 1.5.x版本和Eclipse集成开发环境(IDE) 3.2.x版本的中文语言翻译包。这个语言包的目的是为了帮助中文用户更方便地理解和操作这两个...

    wtp-jsdt-R-3.0

    "WTP-jsdt-R-3.0" 是一个专门为JavaScript开发设计的插件,它能够无缝地集成到Eclipse集成开发环境中。这个插件极大地提升了Eclipse对于JavaScript项目的开发、调试和管理能力,使得开发者能够在熟悉的Eclipse环境下...

Global site tag (gtag.js) - Google Analytics