KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > container > ContainerFactory


1 /*
2  * ========================================================================
3  *
4  * Copyright 2003 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ========================================================================
19  */

20 package org.apache.cactus.integration.ant.container;
21
22 import java.util.MissingResourceException JavaDoc;
23 import java.util.PropertyResourceBundle JavaDoc;
24 import java.util.ResourceBundle JavaDoc;
25
26 import org.apache.tools.ant.BuildException;
27
28 /**
29  * Factory for container support classes.
30  *
31  * @version $Id: ContainerFactory.java,v 1.5 2004/02/29 10:05:26 vmassol Exp $
32  */

33 public class ContainerFactory
34 {
35
36     // Constants ---------------------------------------------------------------
37

38     /**
39      * Name of the resource bundle that contains the definitions of the
40      * default containers supported by this element.
41      */

42     private static final String JavaDoc DEFAULT_CONTAINERS_BUNDLE =
43         "org.apache.cactus.integration.ant.container.default";
44
45     // Instance Variables ------------------------------------------------------
46

47     /**
48      * The list of nested container elements.
49      */

50     private ResourceBundle JavaDoc defaultContainers;
51
52     // Constructors ------------------------------------------------------------
53

54     /**
55      * Constructor.
56      */

57     public ContainerFactory()
58     {
59         defaultContainers =
60             PropertyResourceBundle.getBundle(DEFAULT_CONTAINERS_BUNDLE);
61     }
62
63     // Public Methods ----------------------------------------------------------
64

65     /**
66      * Creates and returns the implementation of the <code>Container</code>
67      * interface which is mapped to the specified name.
68      *
69      * @param theName The name of the container
70      * @return The instantiated container
71      * @throws BuildException If there was a problem creating the container
72      */

73     public final Container createContainer(String JavaDoc theName) throws BuildException
74     {
75         Container container = null;
76         try
77         {
78             String JavaDoc className = defaultContainers.getString(theName);
79             if (className == null)
80             {
81                 throw unsupportedContainer(theName, null);
82             }
83             Class JavaDoc clazz = Class.forName(className);
84             container = (Container) clazz.newInstance();
85         }
86         catch (MissingResourceException JavaDoc mre)
87         {
88             throw unsupportedContainer(theName, mre);
89         }
90         catch (ClassCastException JavaDoc cce)
91         {
92             throw unsupportedContainer(theName, cce);
93         }
94         catch (ClassNotFoundException JavaDoc cnfe)
95         {
96             throw unsupportedContainer(theName, cnfe);
97         }
98         catch (InstantiationException JavaDoc ie)
99         {
100             throw unsupportedContainer(theName, ie);
101         }
102         catch (IllegalAccessException JavaDoc iae)
103         {
104             throw unsupportedContainer(theName, iae);
105         }
106         return container;
107     }
108
109     // Private Methods ---------------------------------------------------------
110

111     /**
112      * Creates an exception that indicates that a specific container is not
113      * supported.
114      *
115      * @param theName The container name
116      * @param theCause The root cause of the exception
117      * @return The created exception
118      */

119     private BuildException unsupportedContainer(String JavaDoc theName,
120         Exception JavaDoc theCause)
121     {
122         if (theCause != null)
123         {
124             return new BuildException("The container '" + theName
125                 + "' is not supported", theCause);
126         }
127         else
128         {
129             return new BuildException("The container '" + theName
130                 + "' is not supported");
131         }
132     }
133
134 }
135
Popular Tags