package org.apache.struts.config.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ControllerConfig;
import org.apache.struts.config.DataSourceConfig;
import org.apache.struts.config.ExceptionConfig;
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.config.MessageResourcesConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.config.PlugInConfig;

public class ModuleConfigImpl implements Serializable, ModuleConfig {
  protected HashMap actionConfigs;
  
  protected HashMap dataSources;
  
  protected HashMap exceptions;
  
  protected HashMap formBeans;
  
  protected HashMap forwards;
  
  protected HashMap messageResources;
  
  protected ArrayList plugIns;
  
  protected boolean configured;
  
  protected ControllerConfig controllerConfig;
  
  protected String prefix;
  
  protected String actionMappingClass;
  
  public ModuleConfigImpl(String prefix) {
    this.actionConfigs = null;
    this.dataSources = null;
    this.exceptions = null;
    this.formBeans = null;
    this.forwards = null;
    this.messageResources = null;
    this.plugIns = null;
    this.configured = false;
    this.controllerConfig = null;
    this.prefix = null;
    this.actionMappingClass = "org.apache.struts.action.ActionMapping";
    this.prefix = prefix;
    this.actionConfigs = new HashMap();
    this.actionMappingClass = "org.apache.struts.action.ActionMapping";
    this.configured = false;
    this.controllerConfig = null;
    this.dataSources = new HashMap();
    this.exceptions = new HashMap();
    this.formBeans = new HashMap();
    this.forwards = new HashMap();
    this.messageResources = new HashMap();
    this.plugIns = new ArrayList();
  }
  
  public ModuleConfigImpl(ModuleConfigImpl moduleConfig) {
    this.actionConfigs = null;
    this.dataSources = null;
    this.exceptions = null;
    this.formBeans = null;
    this.forwards = null;
    this.messageResources = null;
    this.plugIns = null;
    this.configured = false;
    this.controllerConfig = null;
    this.prefix = null;
    this.actionMappingClass = "org.apache.struts.action.ActionMapping";
    this.actionConfigs = moduleConfig.actionConfigs;
    this.actionMappingClass = moduleConfig.actionMappingClass;
    this.configured = moduleConfig.configured;
    this.controllerConfig = moduleConfig.controllerConfig;
    this.dataSources = moduleConfig.dataSources;
    this.exceptions = moduleConfig.exceptions;
    this.formBeans = moduleConfig.formBeans;
    this.forwards = moduleConfig.forwards;
    this.messageResources = moduleConfig.messageResources;
    this.plugIns = moduleConfig.plugIns;
    this.prefix = moduleConfig.prefix;
  }
  
  public boolean getConfigured() {
    return this.configured;
  }
  
  public ControllerConfig getControllerConfig() {
    if (this.controllerConfig == null)
      this.controllerConfig = new ControllerConfig(); 
    return this.controllerConfig;
  }
  
  public void setControllerConfig(ControllerConfig cc) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.controllerConfig = cc;
  }
  
  public String getPrefix() {
    return this.prefix;
  }
  
  public void setPrefix(String prefix) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.prefix = prefix;
  }
  
  public String getActionMappingClass() {
    return this.actionMappingClass;
  }
  
  public void setActionMappingClass(String actionMappingClass) {
    this.actionMappingClass = actionMappingClass;
  }
  
  public void addActionConfig(ActionConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    config.setModuleConfig(this);
    this.actionConfigs.put(config.getPath(), config);
  }
  
  public void addDataSourceConfig(DataSourceConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.dataSources.put(config.getKey(), config);
  }
  
  public void addExceptionConfig(ExceptionConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.exceptions.put(config.getType(), config);
  }
  
  public void addFormBeanConfig(FormBeanConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    config.setModuleConfig(this);
    this.formBeans.put(config.getName(), config);
  }
  
  public void addForwardConfig(ForwardConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.forwards.put(config.getName(), config);
  }
  
  public void addMessageResourcesConfig(MessageResourcesConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.messageResources.put(config.getKey(), config);
  }
  
  public void addPlugInConfig(PlugInConfig plugInConfig) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.plugIns.add(plugInConfig);
  }
  
  public ActionConfig findActionConfig(String path) {
    return (ActionConfig)this.actionConfigs.get(path);
  }
  
  public ActionConfig[] findActionConfigs() {
    ActionConfig[] results = new ActionConfig[this.actionConfigs.size()];
    return (ActionConfig[])this.actionConfigs.values().toArray((Object[])results);
  }
  
  public DataSourceConfig findDataSourceConfig(String key) {
    return (DataSourceConfig)this.dataSources.get(key);
  }
  
  public DataSourceConfig[] findDataSourceConfigs() {
    DataSourceConfig[] results = new DataSourceConfig[this.dataSources.size()];
    return (DataSourceConfig[])this.dataSources.values().toArray((Object[])results);
  }
  
  public ExceptionConfig findExceptionConfig(String type) {
    return (ExceptionConfig)this.exceptions.get(type);
  }
  
  public ExceptionConfig[] findExceptionConfigs() {
    ExceptionConfig[] results = new ExceptionConfig[this.exceptions.size()];
    return (ExceptionConfig[])this.exceptions.values().toArray((Object[])results);
  }
  
  public FormBeanConfig findFormBeanConfig(String name) {
    return (FormBeanConfig)this.formBeans.get(name);
  }
  
  public FormBeanConfig[] findFormBeanConfigs() {
    FormBeanConfig[] results = new FormBeanConfig[this.formBeans.size()];
    return (FormBeanConfig[])this.formBeans.values().toArray((Object[])results);
  }
  
  public ForwardConfig findForwardConfig(String name) {
    return (ForwardConfig)this.forwards.get(name);
  }
  
  public ForwardConfig[] findForwardConfigs() {
    ForwardConfig[] results = new ForwardConfig[this.forwards.size()];
    return (ForwardConfig[])this.forwards.values().toArray((Object[])results);
  }
  
  public MessageResourcesConfig findMessageResourcesConfig(String key) {
    return (MessageResourcesConfig)this.messageResources.get(key);
  }
  
  public MessageResourcesConfig[] findMessageResourcesConfigs() {
    MessageResourcesConfig[] results = new MessageResourcesConfig[this.messageResources.size()];
    return (MessageResourcesConfig[])this.messageResources.values().toArray((Object[])results);
  }
  
  public PlugInConfig[] findPlugInConfigs() {
    PlugInConfig[] results = new PlugInConfig[this.plugIns.size()];
    return (PlugInConfig[])this.plugIns.toArray((Object[])results);
  }
  
  public void freeze() {
    this.configured = true;
    ActionConfig[] aconfigs = findActionConfigs();
    for (int i = 0; i < aconfigs.length; i++)
      aconfigs[i].freeze(); 
    getControllerConfig().freeze();
    DataSourceConfig[] dsconfigs = findDataSourceConfigs();
    for (int j = 0; j < dsconfigs.length; j++)
      dsconfigs[j].freeze(); 
    ExceptionConfig[] econfigs = findExceptionConfigs();
    for (int k = 0; k < econfigs.length; k++)
      econfigs[k].freeze(); 
    FormBeanConfig[] fbconfigs = findFormBeanConfigs();
    for (int m = 0; m < fbconfigs.length; m++)
      fbconfigs[m].freeze(); 
    ForwardConfig[] fconfigs = findForwardConfigs();
    for (int n = 0; n < fconfigs.length; n++)
      fconfigs[n].freeze(); 
    MessageResourcesConfig[] mrconfigs = findMessageResourcesConfigs();
    for (int i1 = 0; i1 < mrconfigs.length; i1++)
      mrconfigs[i1].freeze(); 
    PlugInConfig[] piconfigs = findPlugInConfigs();
    for (int i2 = 0; i2 < piconfigs.length; i2++)
      piconfigs[i2].freeze(); 
  }
  
  public void removeActionConfig(ActionConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    config.setModuleConfig(null);
    this.actionConfigs.remove(config.getPath());
  }
  
  public void removeExceptionConfig(ExceptionConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.exceptions.remove(config.getType());
  }
  
  public void removeDataSourceConfig(DataSourceConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.dataSources.remove(config.getKey());
  }
  
  public void removeFormBeanConfig(FormBeanConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    config.setModuleConfig(null);
    this.formBeans.remove(config.getName());
  }
  
  public void removeForwardConfig(ForwardConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.forwards.remove(config.getName());
  }
  
  public void removeMessageResourcesConfig(MessageResourcesConfig config) {
    if (this.configured)
      throw new IllegalStateException("Configuration is frozen"); 
    this.messageResources.remove(config.getKey());
  }
}
