Package de.odysseus.calyxo.struts.forms

Source Code of de.odysseus.calyxo.struts.forms.FormsPlugIn

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed 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 de.odysseus.calyxo.struts.forms;

import javax.servlet.ServletException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.MessageResourcesConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.MessageResourcesFactory;
import org.apache.struts.util.ModuleUtils;
import org.apache.struts.util.PropertyMessageResourcesFactory;

import de.odysseus.calyxo.base.I18nSupport;
import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.access.AccessSupport;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.forms.FormFactory;
import de.odysseus.calyxo.forms.FormsService;
import de.odysseus.calyxo.forms.impl.Factory;
import de.odysseus.calyxo.forms.misc.FormsAccessor;
import de.odysseus.calyxo.struts.base.StrutsModuleGroup;

/**
* Struts Forms plugin class.
*
* @author Christoph Beck
*/
public class FormsPlugIn implements PlugIn {
  private Log log = LogFactory.getLog(FormsPlugIn.class);

  private String config;

  private ModuleContext context;
  private FormsService service;

  public void init(ActionServlet servlet, ModuleConfig moduleConfig) throws ServletException {
    StrutsModuleGroup modules = StrutsModuleGroup.getInstance(servlet);
    context = modules.getStrutsModuleContext(moduleConfig);
    if (context == null) {
      throw new ServletException("No module context! BasePlugIn installed?");
    }

    log.debug("Initializing Calyxo Forms for Struts module '" + context.getName() + "'.");

    service = new FormsService();
    FormFactory factory = new Factory();
    factory.init(context);
    try {
      service.init(context, new StrutsFormsSupport(moduleConfig), factory, config);
    } catch (ConfigException e) {
      throw new ServletException(e);
    }

    AccessSupport access = AccessSupport.getInstance(context);
    access.put("forms", new FormsAccessor(context));

    // If there's no message-resources declared for the validation message, we do this here.
    // However, we don't install messages if they have been declared in the default module
    // or the module attribute is already in use.
    String bundle = I18nSupport.getInstance(context).resolveBundle("calyxo-forms-validators");
    if (context.getAttribute(bundle) == null) {  // do not overwrite attribute in use
      MessageResourcesConfig messages = moduleConfig.findMessageResourcesConfig(bundle);
      if (messages == null) { // messages might be declared in the default module
        ModuleConfig defaultConfig = ModuleUtils.getInstance().getModuleConfig("", servlet.getServletContext());
        if (moduleConfig != defaultConfig && defaultConfig != null) {
          messages = defaultConfig.findMessageResourcesConfig(bundle);
        }
        if (messages == null) {  // no messages declared --> install message resources
          MessageResourcesFactory.setFactoryClass(PropertyMessageResourcesFactory.class.getName());
          MessageResources resources = MessageResourcesFactory.createFactory().createResources(bundle);
          context.setAttribute(bundle, resources);
        }
      }   
    }
    }

  public void destroy() {
    service.destroy(context);
  }

  /**
   * @return commma separated list of configuration files
   */
  public String getConfig() {
    return config;
  }

  /**
   * Set comma-separated list of configuration files
   *
   * @param string
   */
  public void setConfig(String string) {
    config = string;
  }
}
TOP

Related Classes of de.odysseus.calyxo.struts.forms.FormsPlugIn

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.