01: /*
02: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package de.odysseus.calyxo.forms;
17:
18: import java.net.MalformedURLException;
19: import java.net.URL;
20: import java.util.ArrayList;
21: import java.util.StringTokenizer;
22:
23: import de.odysseus.calyxo.base.ModuleContext;
24: import de.odysseus.calyxo.base.conf.ConfigException;
25: import de.odysseus.calyxo.forms.conf.FormsRootConfig;
26: import de.odysseus.calyxo.forms.conf.impl.FormsRootConfigParser;
27:
28: /**
29: * Utility class to parse several configuration files, initialize and
30: * install {@link de.odysseus.calyxo.forms.FormsSupport} instance
31: *
32: * @author Christoph Beck
33: */
34: public class FormsService {
35: private static final String VALIDATORS = "/de/odysseus/calyxo/forms/conf/impl/calyxo-forms-validators.xml";
36:
37: /**
38: * Initialize forms service.
39: * Parse the specified configuration files and pass the
40: * configuration root element to the specified forms support.
41: * Finally, save the support object in module scope.
42: * @param context the module we're in
43: * @param support the support instance to initialize and save
44: * @param config comma-separated list of configuration files
45: * @throws ConfigException
46: */
47: public void init(ModuleContext context, FormsSupport support,
48: FormFactory factory, String config) throws ConfigException {
49: ArrayList list = new ArrayList();
50: // add predefined validators
51: list.add(getClass().getResource(VALIDATORS));
52: // add user-defined config-files
53: StringTokenizer tokens = new StringTokenizer(config, ",");
54: while (tokens.hasMoreTokens()) {
55: String token = tokens.nextToken().trim();
56: URL url = null;
57: try {
58: url = context.getServletContext().getResource(token);
59: } catch (MalformedURLException e) {
60: throw new ConfigException(e);
61: }
62: if (url == null) {
63: throw new ConfigException("Could not find resource "
64: + token);
65: } else {
66: list.add(url);
67: }
68: }
69: URL[] urls = (URL[]) list.toArray(new URL[list.size()]);
70:
71: FormsRootConfig formsConfig = null;
72: FormsRootConfigParser parser = new FormsRootConfigParser(
73: context);
74: formsConfig = parser.parse(urls);
75: support.init(context, formsConfig, factory);
76:
77: // save support instance in application scope
78: context.setAttribute(FormsSupport.FORMS_SUPPORT_KEY, support);
79: }
80:
81: public void destroy(ModuleContext context) {
82: context.removeAttribute(FormsSupport.FORMS_SUPPORT_KEY);
83: }
84: }
|