⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericportlet.java

📁 portal越来越流行了
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

	/**
	 * Helper method to serve up the <code>edit</code> mode.
	 * <p>
	 * The default implementation throws an exception.
	 * 
	 * @param request
	 *            the portlet request
	 * @param response
	 *            the render response
	 * 
	 * @exception PortletException
	 *                if the portlet cannot fulfilling the request
	 * @exception UnavailableException
	 *                if the portlet is unavailable to perform render at this
	 *                time
	 * @exception PortletSecurityException
	 *                if the portlet cannot fullfill this request because of
	 *                security reasons
	 * @exception java.io.IOException
	 *                if the streaming causes an I/O problem
	 * 
	 */
	protected void doEdit(RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException {
		throw new PortletException("doEdit method not implemented");
	}

	/**
	 * Helper method to serve up the <code>help</code> mode.
	 * <p>
	 * The default implementation throws an exception.
	 * 
	 * @param request
	 *            the portlet request
	 * @param response
	 *            the render response
	 * 
	 * @exception PortletException
	 *                if the portlet cannot fulfilling the request
	 * @exception UnavailableException
	 *                if the portlet is unavailable to perform render at this
	 *                time
	 * @exception PortletSecurityException
	 *                if the portlet cannot fullfill this request because of
	 *                security reasons
	 * @exception java.io.IOException
	 *                if the streaming causes an I/O problem
	 */
	protected void doHelp(RenderRequest request, RenderResponse response) throws PortletException, java.io.IOException {
		throw new PortletException("doHelp method not implemented");
	}

	/**
	 * Returns the PortletConfig object of this portlet.
	 * 
	 * @return the PortletConfig object of this portlet
	 */
	public PortletConfig getPortletConfig() {
		return config;
	}

	/**
	 * Called by the portlet container to indicate to a portlet that the portlet
	 * is being taken out of service.
	 * <p>
	 * The default implementation does nothing.
	 * 
	 */
	public void destroy() {
		// do nothing
	}

	// -------------------------------------------------------------------------
	// implement PortletConfig
	// -------------------------------------------------------------------------

	/**
	 * Returns the name of this portlet.
	 * 
	 * @return the portlet name
	 * 
	 * @see PortletConfig#getPortletName()
	 */
	public String getPortletName() {
		if (config == null)
			throw new java.lang.IllegalStateException(
					"Config is null, please ensure that your init(config) method calls super.init(config)");

		return config.getPortletName();
	}

	/**
	 * Returns the <code>PortletContext</code> of the portlet application the
	 * portlet is in.
	 * 
	 * @return the portlet application context
	 */
	public PortletContext getPortletContext() {
		if (config == null)
			throw new java.lang.IllegalStateException(
					"Config is null, please ensure that your init(config) method calls super.init(config)");

		return config.getPortletContext();
	}

	/**
	 * Gets the resource bundle for the given locale based on the resource
	 * bundle defined in the deployment descriptor with
	 * <code>resource-bundle</code> tag or the inlined resources defined in
	 * the deployment descriptor.
	 * 
	 * @return the resource bundle for the given locale
	 */
	public java.util.ResourceBundle getResourceBundle(java.util.Locale locale) {
		if (config == null)
			throw new java.lang.IllegalStateException(
					"Config is null, please ensure that your init(config) method calls super.init(config)");

		return config.getResourceBundle(locale);
	}

	/**
	 * Returns a String containing the value of the named initialization	 * parameter, or null if the parameter does not exist.
	 * 
	 * @param name
	 *            a <code>String</code> specifying the name of the
	 *            initialization parameter
	 * 
	 * @return a <code>String</code> containing the value of the
	 *         initialization parameter
	 * 
	 * @exception java.lang.IllegalArgumentException
	 *                if name is <code>null</code>.
	 */
	public String getInitParameter(java.lang.String name) {
		if (config == null)
			throw new java.lang.IllegalStateException(
					"Config is null, please ensure that your init(config) method calls super.init(config)");

		return config.getInitParameter(name);
	}

	/**
	 * Returns the names of the portlet initialization parameters as an
	 * Enumeration of String objects, or an empty Enumeration if the portlet has
	 * no initialization parameters.
	 * 
	 * @return an <code>Enumeration</code> of <code>String</code> objects
	 *         containing the names of the portlet initialization parameters, or
	 *         an empty Enumeration if the portlet has no initialization
	 *         parameters.
	 */
	public java.util.Enumeration<String> getInitParameterNames() {
		if (config == null)
			throw new java.lang.IllegalStateException(
					"Config is null, please ensure that your init(config) method calls super.init(config)");

		return config.getInitParameterNames();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.portlet.PortletConfig#getProcessingEventQNames()
	 */
	public Enumeration<QName> getProcessingEventQNames() {
		if (config == null)
			throw new java.lang.IllegalStateException(
					"Config is null, please ensure that your init(config) method calls super.init(config)");

		return config.getProcessingEventQNames();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.portlet.PortletConfig#getPublishingEventQNames()
	 */
	public Enumeration<QName> getPublishingEventQNames() {
		if (config == null)
			throw new java.lang.IllegalStateException(
					"Config is null, please ensure that your init(config) method calls super.init(config)");

		return config.getPublishingEventQNames();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.portlet.PortletConfig#getSupportedLocales()
	 */
	public Enumeration<Locale> getSupportedLocales() {
		if (config == null)
			throw new java.lang.IllegalStateException(
					"Config is null, please ensure that your init(config) method calls super.init(config)");

		return config.getSupportedLocales();
	}

	/* (non-Javadoc)
	 * @see javax.portlet.PortletConfig#getContainerRuntimeOptions()
	 */
	public Map<String, String[]> getContainerRuntimeOptions() {
		return config.getContainerRuntimeOptions();
	}

	// -------------------------------------------------------------------------
	// V 2.0 additions
	// -------------------------------------------------------------------------

	/**
	 * Default resource serving.
	 * <p>
	 * The default implemention of this method is to call a
	 * RequestDispatcher.foward with the ResourceID of the ResourceRequest.
	 * <p>
	 * If no ResourceID is set on the resource URL the default implementation
	 * does nothing.
	 * 
	 * @since 2.0
	 */
	public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {
		if (request.getResourceID() != null) {
			PortletRequestDispatcher rd = getPortletConfig().getPortletContext().getRequestDispatcher(
					request.getResourceID());
			if (rd != null)
				rd.forward(request, response);
		}
	}

	/**
	 * The default implementation tries to dispatch to a method
	 * annotated with <code>@ProcessEvent</name> that matches the 
	 * event name or, if no
	 * such method is found just sets the current render parameters on
	 * the response.<br>
 	 * Note that the annotated methods needs to be public in order
	 * to be allowed to be called by <code>GenericPortlet</code>.
	 * 
	 * @see javax.portlet.EventPortlet#processEvent(javax.portlet.EventRequest,
	 *      javax.portlet.EventResponse)
	 * @since 2.0
	 */
	public void processEvent(EventRequest request, EventResponse response) throws PortletException, IOException {
		String eventName = request.getEvent().getQName().toString();

		try {
			// check for exact match
			Method eventMethod = processEventHandlingMethodsMap.get(eventName);
			if (eventMethod != null) {
				eventMethod.invoke(this, request, response);
				return;
			} else {
				// Search for the longest possible matching wildcard annotation
				int endPos = eventName.indexOf('}');
				int dotPos = eventName.lastIndexOf('.');
				while (dotPos > endPos) {
					String wildcardLookup = eventName.substring(0, dotPos + 1);
					eventMethod = processEventHandlingMethodsMap.get(wildcardLookup);
					if (eventMethod != null) {
						eventMethod.invoke(this, request, response);
						return;
					}
					if (dotPos == 0) {
						break;
					}
					dotPos = eventName.lastIndexOf('.', dotPos - 1);
				}
			}
		} catch (Exception e) {
			throw new PortletException(e);
		}

		// if no event processing method was found just keep render params
		response.setRenderParameters(request);
	}

	/**
	 * Used by the render method to set the response properties and headers.
	 * <p>
	 * The portlet should override this method and set its response header using
	 * this method in order to ensure that they are set before anything is
	 * written to the output stream.
	 * <p>
	 * The default implemention of this method is emtpy.
	 * 
	 * @param request  the render request
	 * @param response the render response
	 * @since 2.0
	 */
	protected void doHeaders(RenderRequest request, RenderResponse response) {
		return;
	}

	/**
	 * Used by the render method to set the next possible portlet modes.
	 * <p>
	 * The portlet should override this method and set the next possible portlet
	 * modes using this method in order to ensure that they are set before
	 * anything is written to the output stream.
	 * <p>
	 * The default implemention of this method returns <code>null</code>.
	 * 
	 * @since 2.0
	 */
	protected java.util.Collection<PortletMode> getNextPossiblePortletModes(RenderRequest request) {
		return null;
	}

	/**
	 * Returns the names of the public render parameters supported by the
	 * portlet as an <code>Enumeration</code> of String objects, or an empty
	 * <code>Enumeration</code> if the portlet has no public render
	 * parameters.
	 * 
	 * @return an <code>Enumeration</code> of <code>String</code> objects
	 *         containing the names of the public render parameters, or an empty
	 *         <code>Enumeration</code> if the portlet does not define any
	 *         public render parameters.
	 * 
	 * @see javax.portlet.PortletConfig#getPublicRenderParameterNames()
	 */
	public Enumeration<String> getPublicRenderParameterNames() {
		if (config == null)
			throw new java.lang.IllegalStateException(
					"Config is null, please ensure that your init(config) method calls super.init(config)");

		return config.getPublicRenderParameterNames();
	}

	/**
	 * Returns the default namespace for events and public parameters. This
	 * namespace is defined in the portlet deployment descriptor with the
	 * <code>default-namespace</code> element.
	 * <p>
	 * If no default namespace is defined in the portlet deployment descriptor
	 * this methods returns the XML default namespace
	 * <code>XMLConstants.NULL_NS_URI</code>.
	 * 
	 * @return the default namespace defined in the portlet deployment
	 *         descriptor, or <code>XMLConstants.NULL_NS_URI</code> is non is
	 *         defined.
	 * 
	 * @see javax.portlet.PortletConfig#getDefaultNamespace()
	 */
	public String getDefaultNamespace() {
		if (config == null)
			throw new java.lang.IllegalStateException(
					"Config is null, please ensure that your init(config) method calls super.init(config)");

		return config.getDefaultNamespace();
	}

	private void cacheAnnotatedMethods() {
		// cache all annotated and visible public methods
		for (Method method : this.getClass().getMethods()) {
			Annotation[] annotations = method.getAnnotations();
			if (annotations != null) {
				for (Annotation annotation : annotations) {
					Class<? extends Annotation> annotationType = annotation.annotationType();
					if (ProcessAction.class.equals(annotationType)) {
						String name = ((ProcessAction) annotation).name();
						if (name != null && name.length() > 0)
							processActionHandlingMethodsMap.put(name, method);
					} else if (ProcessEvent.class.equals(annotationType)) {
						String qname = ((ProcessEvent) annotation).qname();
						if (qname == null || qname.length() <= 0) {
							if (config == null)
								throw new java.lang.IllegalStateException(
										"Config is null, please ensure that your init(config) method calls super.init(config)");

							String name = ((ProcessEvent) annotation).name();
							if (name != null && name.length() > 0) {
								qname = new QName(config.getDefaultNamespace(), name).toString();
								processEventHandlingMethodsMap.put(qname, method);
							}
						} else
							processEventHandlingMethodsMap.put(qname, method);
					} else if (RenderMode.class.equals(annotationType)) {
						String name = ((RenderMode) annotation).name();
						if (name != null && name.length() > 0)
							renderModeHandlingMethodsMap.put(name.toLowerCase(), method);
					}
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -