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

📄 portletservlet.java

📁 portal越来越流行了
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pluto.core;

import java.io.IOException;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.EventPortlet;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceServingPortlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.pluto.Constants;
import org.apache.pluto.ContainerInvocation;
import org.apache.pluto.PortletContainerException;
import org.apache.pluto.PortletWindow;
import org.apache.pluto.internal.InternalPortletConfig;
import org.apache.pluto.internal.InternalPortletContext;
import org.apache.pluto.internal.InternalPortletRequest;
import org.apache.pluto.internal.InternalPortletResponse;
import org.apache.pluto.om.portlet.PortletDefinition;
import org.apache.pluto.services.PlutoServices;
import org.apache.pluto.spi.ContainerInvocationService;
import org.apache.pluto.spi.FilterManager;
import org.apache.pluto.spi.optional.AdministrativeRequestListener;
import org.apache.pluto.spi.optional.PortalAdministrationService;
import org.apache.pluto.spi.optional.PortletContextService;
import org.apache.pluto.spi.optional.PortletInvocationEvent;
import org.apache.pluto.spi.optional.PortletInvocationListener;

/**
 * Portlet Invocation Servlet. This servlet recieves cross context requests from
 * the the container and services the portlet request for the specified method.
 * 
 * @version 1.1
 * @since 09/22/2004
 */
public class PortletServlet extends HttpServlet
{

    // Private Member Variables ------------------------------------------------
    /**
     * The portlet name as defined in the portlet app descriptor.
     */
    private String portletName;

    /**
     * The portlet instance wrapped by this servlet.
     */
    private Portlet portlet;

    /**
     * The internal portlet context instance.
     */
    private InternalPortletContext portletContext;

    /**
     * The internal portlet config instance.
     */
    private InternalPortletConfig portletConfig;

    /**
     * The Event Portlet instance (the same object as portlet) wrapped by this
     * servlet.
     */
    private EventPortlet eventPortlet = null;

    /** The resource serving portlet instance wrapped by this servlet. */
    private ResourceServingPortlet resourceServingPortlet = null;

    private PortletContextService contextService;

    private ContainerInvocationService containerInvocationService;

    private boolean started = false;
    private Timer   startTimer = null;
    
    // HttpServlet Impl --------------------------------------------------------

    public String getServletInfo()
    {
        return "Pluto PortletServlet [" + portletName + "]";
    }

    /**
     * Initialize the portlet invocation servlet.
     * 
     * @throws ServletException
     *             if an error occurs while loading portlet.
     */
    public void init(ServletConfig config) throws ServletException
    {

        // Call the super initialization method.
        super.init(config);

        // Retrieve portlet name as defined as an initialization parameter.
        portletName = getInitParameter("portlet-name");

        started = false;

        startTimer = new Timer(true);
        final ServletContext servletContext = getServletContext();
        final ClassLoader paClassLoader = Thread.currentThread().getContextClassLoader();
        startTimer.schedule(new TimerTask()
        {
            public void run()
            {
                synchronized(servletContext)
                {
                    if (startTimer != null)
                    {
                        if (attemptRegistration(servletContext, paClassLoader ))
                        {
                            startTimer.cancel();
                            startTimer = null;
                        }
                    }
                }
            }
        }, 1, 10000);
    }
    
    protected boolean attemptRegistration(ServletContext context, ClassLoader paClassLoader)
    {
        if (PlutoServices.getServices() != null) 
        {
            contextService = PlutoServices.getServices().getPortletContextService();
            containerInvocationService = PlutoServices.getServices().getContainerInvocationService();
            try
            {
                ServletConfig sConfig = getServletConfig();
                if (sConfig == null)
                {
                    String msg = "Problem obtaining servlet configuration(getServletConfig() returns null).";
                    context.log(msg);
                    return true;
                }

                String applicationName = contextService.register(sConfig);
                started = true;
                portletContext = (InternalPortletContext) contextService.getPortletContext(applicationName);
                portletConfig = (InternalPortletConfig) contextService.getPortletConfig(applicationName, portletName);

            }
            catch (PortletContainerException ex)
            {
                context.log(ex.getMessage(),ex);
                return true;
            }

            PortletDefinition portletDD = portletConfig.getPortletDefinition();

//          Create and initialize the portlet wrapped in the servlet.
            try
            {
                Class clazz = paClassLoader.loadClass((portletDD.getPortletClass()));
                portlet = (Portlet) clazz.newInstance();
                portlet.init(portletConfig);
                initializeEventPortlet();
                initializeResourceServingPortlet();
                return true;
            }
            catch (Exception ex)
            {
                context.log(ex.getMessage(),ex);
                // take out of service
                portlet = null;
                portletConfig = null;
                return true;
            }
        }
        return false;
    }

    public void destroy()
    {
        synchronized(getServletContext())
        {
            if ( startTimer != null )
            {
              startTimer.cancel();
              startTimer = null;
            }
            else if ( started && portletContext != null)
            {
              started = false;
              contextService.unregister(portletContext);
              if (portlet != null)
              {
                  try
                  {
                      portlet.destroy();
                  }
                  catch (Exception e)
                  {
                      // ignore
                  }
                  portlet = null;
              }
            }
            super.destroy();
        }
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        dispatch(request, response);

⌨️ 快捷键说明

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