001: /*
002: * ========================================================================
003: *
004: * Copyright 2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant.container;
021:
022: import java.util.MissingResourceException;
023: import java.util.PropertyResourceBundle;
024: import java.util.ResourceBundle;
025:
026: import org.apache.tools.ant.BuildException;
027:
028: /**
029: * Factory for container support classes.
030: *
031: * @version $Id: ContainerFactory.java 238810 2004-02-29 10:05:26Z vmassol $
032: */
033: public class ContainerFactory {
034:
035: // Constants ---------------------------------------------------------------
036:
037: /**
038: * Name of the resource bundle that contains the definitions of the
039: * default containers supported by this element.
040: */
041: private static final String DEFAULT_CONTAINERS_BUNDLE = "org.apache.cactus.integration.ant.container.default";
042:
043: // Instance Variables ------------------------------------------------------
044:
045: /**
046: * The list of nested container elements.
047: */
048: private ResourceBundle defaultContainers;
049:
050: // Constructors ------------------------------------------------------------
051:
052: /**
053: * Constructor.
054: */
055: public ContainerFactory() {
056: defaultContainers = PropertyResourceBundle
057: .getBundle(DEFAULT_CONTAINERS_BUNDLE);
058: }
059:
060: // Public Methods ----------------------------------------------------------
061:
062: /**
063: * Creates and returns the implementation of the <code>Container</code>
064: * interface which is mapped to the specified name.
065: *
066: * @param theName The name of the container
067: * @return The instantiated container
068: * @throws BuildException If there was a problem creating the container
069: */
070: public final Container createContainer(String theName)
071: throws BuildException {
072: Container container = null;
073: try {
074: String className = defaultContainers.getString(theName);
075: if (className == null) {
076: throw unsupportedContainer(theName, null);
077: }
078: Class clazz = Class.forName(className);
079: container = (Container) clazz.newInstance();
080: } catch (MissingResourceException mre) {
081: throw unsupportedContainer(theName, mre);
082: } catch (ClassCastException cce) {
083: throw unsupportedContainer(theName, cce);
084: } catch (ClassNotFoundException cnfe) {
085: throw unsupportedContainer(theName, cnfe);
086: } catch (InstantiationException ie) {
087: throw unsupportedContainer(theName, ie);
088: } catch (IllegalAccessException iae) {
089: throw unsupportedContainer(theName, iae);
090: }
091: return container;
092: }
093:
094: // Private Methods ---------------------------------------------------------
095:
096: /**
097: * Creates an exception that indicates that a specific container is not
098: * supported.
099: *
100: * @param theName The container name
101: * @param theCause The root cause of the exception
102: * @return The created exception
103: */
104: private BuildException unsupportedContainer(String theName,
105: Exception theCause) {
106: if (theCause != null) {
107: return new BuildException("The container '" + theName
108: + "' is not supported", theCause);
109: } else {
110: return new BuildException("The container '" + theName
111: + "' is not supported");
112: }
113: }
114:
115: }
|