FormsService.java :  » Web-Framework » calyxo » de » odysseus » calyxo » forms » Java Open Source

Java Open Source » Web Framework » calyxo 
calyxo » de » odysseus » calyxo » forms » FormsService.java
/*
 * 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.forms;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.StringTokenizer;

import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.forms.conf.FormsRootConfig;
import de.odysseus.calyxo.forms.conf.impl.FormsRootConfigParser;

/**
 * Utility class to parse several configuration files, initialize and
 * install {@link de.odysseus.calyxo.forms.FormsSupport} instance
 * 
 * @author Christoph Beck
 */
public class FormsService {
  private static final String VALIDATORS =
    "/de/odysseus/calyxo/forms/conf/impl/calyxo-forms-validators.xml";

  /**
   * Initialize forms service.
   * Parse the specified configuration files and pass the
   * configuration root element to the specified forms support.
   * Finally, save the support object in module scope.
   * @param context the module we're in
   * @param support the support instance to initialize and save
   * @param config comma-separated list of configuration files
   * @throws ConfigException
   */
  public void init(ModuleContext context, FormsSupport support, FormFactory factory, String config)
  throws ConfigException {
    ArrayList list = new ArrayList();
    // add predefined validators
    list.add(getClass().getResource(VALIDATORS));
    // add user-defined config-files
    StringTokenizer tokens = new StringTokenizer(config,",");
    while (tokens.hasMoreTokens()) {
      String token = tokens.nextToken().trim();
      URL url = null;
      try {
        url = context.getServletContext().getResource(token);
      } catch (MalformedURLException e) {
        throw new ConfigException(e);
      }
      if (url == null) {
        throw new ConfigException("Could not find resource " + token);
      } else {
        list.add(url);
      }
    }
    URL[] urls = (URL[])list.toArray(new URL[list.size()]);

    FormsRootConfig formsConfig = null;
    FormsRootConfigParser parser =
      new FormsRootConfigParser(context);
    formsConfig = parser.parse(urls);
    support.init(context, formsConfig, factory);

    // save support instance in application scope
    context.setAttribute(FormsSupport.FORMS_SUPPORT_KEY, support);
  }

  public void destroy(ModuleContext context) {
    context.removeAttribute(FormsSupport.FORMS_SUPPORT_KEY);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.