KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > container > factory > ContainerBuilderFactory


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

16 package com.jdon.container.factory;
17
18 import javax.servlet.ServletContext JavaDoc;
19
20 import com.jdon.container.ContainerWrapper;
21 import com.jdon.container.builder.ContainerBuilder;
22 import com.jdon.container.builder.DefaultContainerBuilder;
23 import com.jdon.container.config.ContainerComponents;
24 import com.jdon.util.Debug;
25
26 /**
27  * fetch the all components configures, and create
28  * ContainerBuilder Instance.
29  *
30  * @author <a HREF="mailto:banqiao@jdon.com">banq</a>
31  *
32  */

33 public class ContainerBuilderFactory {
34     public final static String JavaDoc module = ContainerBuilderFactory.class.getName();
35     
36     private static final String JavaDoc DEFAULT_CONTAINER_CONFIGURE_FILENAME = "container.xml";
37     private static final String JavaDoc DEFAULT_ASPECT_CONFIGURE_FILENAME = "aspect.xml";
38
39     private static final String JavaDoc USER_CONTAINER_CONFIGURE_FILENAME = "mycontainer.xml";
40     private static final String JavaDoc USER_ASPECT_CONFIGURE_FILENAME = "myaspect.xml";
41     
42     
43     private static final String JavaDoc CONTAINER_CONFIG_PARAM = "containerConfigure";
44     private static final String JavaDoc ASPECT_CONFIG_PARAM = "aspectConfigure";
45     
46     /**
47      * the main method in this class,
48      * read all components include interceptors from Xml configure file.
49      *
50      * create a micro container instance.
51      * and then returen a ContainerBuilder instance
52      *
53      * @param context
54      * @return
55      */

56     public ContainerBuilder createContainerBuilder(ServletContext JavaDoc context){
57         ContainerFactory containerFactory = new ContainerFactory();
58         ContainerComponents configComponents = fetchContainerConfig(context);
59         ContainerComponents aspectConfigComponents = fecthAspectConfig(context);
60             
61             ContainerWrapper cw = containerFactory.create();
62             return new DefaultContainerBuilder(cw, configComponents, aspectConfigComponents);
63     }
64     
65     /**
66      * read the configure from container.xml/ServletContext initParameter/mycontainer.xml
67      * @param context
68      * @return
69      */

70     private ContainerComponents fetchContainerConfig(ServletContext JavaDoc context){
71         ContainerFactory containerFactory = new ContainerFactory();
72         Debug.logVerbose("[JdonFramework] 1. read container components from:" + DEFAULT_CONTAINER_CONFIGURE_FILENAME, module);
73         ContainerComponents configComponents = containerFactory.fetchBasicComponents(DEFAULT_CONTAINER_CONFIGURE_FILENAME);
74         //read the configure in web.xml
75
String JavaDoc container_configFile = context.getInitParameter(CONTAINER_CONFIG_PARAM);
76             if (container_configFile != null){
77                 Debug.logVerbose("[JdonFramework] 2. read container components from:" + container_configFile, module);
78                 ContainerComponents appconfigComponents = containerFactory.fetchBasicComponents(container_configFile);
79             configComponents.addComponents(appconfigComponents.getComponents());
80             }
81             
82             //read user fraemwork's configure
83
Debug.logVerbose("[JdonFramework] 3. read container components from:" + USER_CONTAINER_CONFIGURE_FILENAME, module);
84             ContainerComponents userconfigComponents = containerFactory.fetchBasicComponents(USER_CONTAINER_CONFIGURE_FILENAME);
85             configComponents.addComponents(userconfigComponents.getComponents());
86             
87             return configComponents;
88     }
89     
90     /**
91      * read the configure from aspect.xml /ServletContext initParameter/myaspect.xml
92      * @param context
93      * @return
94      */

95     private ContainerComponents fecthAspectConfig(ServletContext JavaDoc context){
96         ContainerFactory containerFactory = new ContainerFactory();
97         
98             //read user fraemwork's configure
99
Debug.logVerbose("[JdonFramework]1. read apspect interceptors from:" + USER_ASPECT_CONFIGURE_FILENAME, module);
100             ContainerComponents aspectConfigComponents = containerFactory.fetchAspectComponents(USER_ASPECT_CONFIGURE_FILENAME);
101             
102         String JavaDoc aspect_configFile = context.getInitParameter(ASPECT_CONFIG_PARAM);
103             if (aspect_configFile != null){
104                 Debug.logVerbose("[JdonFramework]2. read apspect interceptors from:" + aspect_configFile, module);
105                 ContainerComponents appaspectConfigComponents2 = containerFactory.fetchAspectComponents(aspect_configFile);
106                 aspectConfigComponents.addComponents(appaspectConfigComponents2.getComponents());
107             }
108         
109         Debug.logVerbose("[JdonFramework] 3. read apspect interceptors from:" + DEFAULT_ASPECT_CONFIGURE_FILENAME, module);
110         ContainerComponents aspectConfigComponents3 =containerFactory.fetchAspectComponents(DEFAULT_ASPECT_CONFIGURE_FILENAME);
111         aspectConfigComponents.addComponents(aspectConfigComponents3.getComponents());
112             
113             
114             Debug.logVerbose("[JdonFramework] aspectConfigComponents size:" + aspectConfigComponents.size(), module);
115             return aspectConfigComponents;
116         
117     }
118     
119     public ContainerBuilder createContainerBuilder(String JavaDoc container_configFile, String JavaDoc aspect_configFile){
120         ContainerFactory containerFactory = new ContainerFactory();
121         ContainerWrapper cw = containerFactory.create();
122         
123         ContainerComponents configComponents = containerFactory.fetchBasicComponents(container_configFile);
124         ContainerComponents aspectConfigComponents = containerFactory.fetchAspectComponents(aspect_configFile);
125         
126         return new DefaultContainerBuilder(cw, configComponents, aspectConfigComponents);
127     }
128     
129 }
130
Popular Tags