예제 #1
0
  @Override
  public void init(PortletConfig config) throws PortletException {
    CurrentInstance.clearAll();
    super.init(config);
    Properties initParameters = new Properties();

    // Read default parameters from the context
    final PortletContext context = config.getPortletContext();
    for (final Enumeration<String> e = context.getInitParameterNames(); e.hasMoreElements(); ) {
      final String name = e.nextElement();
      initParameters.setProperty(name, context.getInitParameter(name));
    }

    // Override with application settings from portlet.xml
    for (final Enumeration<String> e = config.getInitParameterNames(); e.hasMoreElements(); ) {
      final String name = e.nextElement();
      initParameters.setProperty(name, config.getInitParameter(name));
    }

    DeploymentConfiguration deploymentConfiguration = createDeploymentConfiguration(initParameters);
    try {
      vaadinService = createPortletService(deploymentConfiguration);
    } catch (ServiceException e) {
      throw new PortletException("Could not initialized VaadinPortlet", e);
    }
    // Sets current service even though there are no request and response
    vaadinService.setCurrentInstances(null, null);

    portletInitialized();

    CurrentInstance.clearAll();
  }
  /**
   * Register web-specific environment beans ("contextParameters", "contextAttributes") with the
   * given BeanFactory, as used by the Portlet ApplicationContext.
   *
   * @param bf the BeanFactory to configure
   * @param sc the ServletContext that we're running within
   * @param pc the PortletContext that we're running within
   * @param config the PortletConfig of the containing Portlet
   */
  static void registerEnvironmentBeans(
      ConfigurableListableBeanFactory bf,
      ServletContext sc,
      PortletContext pc,
      PortletConfig config) {

    if (sc != null && !bf.containsBean(WebApplicationContext.SERVLET_CONTEXT_BEAN_NAME)) {
      bf.registerSingleton(WebApplicationContext.SERVLET_CONTEXT_BEAN_NAME, sc);
    }

    if (pc != null
        && !bf.containsBean(ConfigurablePortletApplicationContext.PORTLET_CONTEXT_BEAN_NAME)) {
      bf.registerSingleton(ConfigurablePortletApplicationContext.PORTLET_CONTEXT_BEAN_NAME, pc);
    }

    if (config != null
        && !bf.containsBean(ConfigurablePortletApplicationContext.PORTLET_CONFIG_BEAN_NAME)) {
      bf.registerSingleton(ConfigurablePortletApplicationContext.PORTLET_CONFIG_BEAN_NAME, config);
    }

    if (!bf.containsBean(WebApplicationContext.CONTEXT_PARAMETERS_BEAN_NAME)) {
      Map<String, String> parameterMap = new HashMap<String, String>();
      if (pc != null) {
        Enumeration paramNameEnum = pc.getInitParameterNames();
        while (paramNameEnum.hasMoreElements()) {
          String paramName = (String) paramNameEnum.nextElement();
          parameterMap.put(paramName, pc.getInitParameter(paramName));
        }
      }
      if (config != null) {
        Enumeration paramNameEnum = config.getInitParameterNames();
        while (paramNameEnum.hasMoreElements()) {
          String paramName = (String) paramNameEnum.nextElement();
          parameterMap.put(paramName, config.getInitParameter(paramName));
        }
      }
      bf.registerSingleton(
          WebApplicationContext.CONTEXT_PARAMETERS_BEAN_NAME,
          Collections.unmodifiableMap(parameterMap));
    }

    if (!bf.containsBean(WebApplicationContext.CONTEXT_ATTRIBUTES_BEAN_NAME)) {
      Map<String, Object> attributeMap = new HashMap<String, Object>();
      if (pc != null) {
        Enumeration attrNameEnum = pc.getAttributeNames();
        while (attrNameEnum.hasMoreElements()) {
          String attrName = (String) attrNameEnum.nextElement();
          attributeMap.put(attrName, pc.getAttribute(attrName));
        }
      }
      bf.registerSingleton(
          WebApplicationContext.CONTEXT_ATTRIBUTES_BEAN_NAME,
          Collections.unmodifiableMap(attributeMap));
    }
  }