1 | /* |
2 | * Copyright 2004 The Apache Software Foundation. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
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 org.apache.pluto.driver.config; |
17 | |
18 | import javax.servlet.ServletContext; |
19 | |
20 | import org.apache.commons.logging.Log; |
21 | import org.apache.commons.logging.LogFactory; |
22 | import org.springframework.beans.factory.BeanFactory; |
23 | import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
24 | import org.springframework.beans.factory.access.BeanFactoryLocator; |
25 | import org.springframework.beans.factory.access.BeanFactoryReference; |
26 | import org.springframework.beans.factory.access.SingletonBeanFactoryLocator; |
27 | |
28 | /** |
29 | * |
30 | * @see DriverConfiguration |
31 | * @see AdminConfiguration |
32 | * @author <a href="mailto:ddewolf@apache.org">David H. DeWolf</a> |
33 | * @author <a href="mailto:zheng@apache.org">ZHENG Zhong</a> |
34 | * @version 1.0 |
35 | * @since Sep 23, 2004 |
36 | */ |
37 | public class DriverConfigurationFactory { |
38 | |
39 | /** Logger. */ |
40 | private static final Log LOG = LogFactory.getLog(DriverConfigurationFactory.class); |
41 | |
42 | /** Portal driver services configuration file name. */ |
43 | private static final String DRIVER_CONFIG_FILE = "/WEB-INF/pluto-portal-driver-services-config.xml"; |
44 | |
45 | /** The singleton factory instance. */ |
46 | private static final DriverConfigurationFactory factory = new DriverConfigurationFactory(); |
47 | |
48 | // Private Member Variables ------------------------------------------------ |
49 | |
50 | private BeanFactory beanFactory = null; |
51 | |
52 | // Constructor ------------------------------------------------------------- |
53 | |
54 | /** |
55 | * Private constructor that prevents external instantiation. |
56 | */ |
57 | private DriverConfigurationFactory() { |
58 | // Do nothing. |
59 | } |
60 | |
61 | /** |
62 | * Returns the singleton factory instance. |
63 | * |
64 | * @return the singleton factory instance. |
65 | */ |
66 | public static DriverConfigurationFactory getFactory() { |
67 | return factory; |
68 | } |
69 | |
70 | // Public Methods ---------------------------------------------------------- |
71 | |
72 | public DriverConfiguration getConfig(ServletContext servletContext) { |
73 | if (LOG.isDebugEnabled()) { |
74 | LOG.debug("Retrieving driver config from: " + DRIVER_CONFIG_FILE); |
75 | } |
76 | try { |
77 | DriverConfiguration driverConfig = (DriverConfiguration) getBeanFactory(servletContext).getBean("DriverConfiguration"); |
78 | driverConfig.init(servletContext); |
79 | if (LOG.isDebugEnabled()) { |
80 | LOG.debug("Driver config of type " + driverConfig.getClass() + " Initialized and Ready For Service."); |
81 | } |
82 | return driverConfig; |
83 | } catch (NoSuchBeanDefinitionException ex) { |
84 | throw new DriverConfigurationException("Unable to find Driver Configuration.", ex); |
85 | } |
86 | } |
87 | |
88 | public AdminConfiguration getAdminConfig(ServletContext servletContext) { |
89 | if (LOG.isDebugEnabled()) { |
90 | LOG.debug("Retrieving admin config from: " + DRIVER_CONFIG_FILE); |
91 | } |
92 | try { |
93 | AdminConfiguration adminConfig = (AdminConfiguration) getBeanFactory(servletContext).getBean("AdminConfiguration"); |
94 | adminConfig.init(servletContext); |
95 | if (LOG.isDebugEnabled()) { |
96 | LOG.debug("Admin config of type " + adminConfig.getClass() + "Initialized and ready for service."); |
97 | } |
98 | return adminConfig; |
99 | } catch (NoSuchBeanDefinitionException ex) { |
100 | return null; |
101 | } |
102 | } |
103 | |
104 | // Private Methods --------------------------------------------------------- |
105 | |
106 | private BeanFactory getBeanFactory(ServletContext servletContext) { |
107 | BeanFactoryLocator bfLocator = SingletonBeanFactoryLocator.getInstance("Spring.xml"); |
108 | BeanFactoryReference bfReference = bfLocator.useBeanFactory("appContext"); |
109 | BeanFactory factory = bfReference.getFactory(); |
110 | return factory; |
111 | // |
112 | // if (beanFactory == null) { |
113 | // InputStream is = servletContext.getResourceAsStream( |
114 | // DRIVER_CONFIG_FILE); |
115 | // beanFactory = new XmlBeanFactory(new InputStreamResource( |
116 | // is, "Driver Configuration")); |
117 | // } |
118 | // return beanFactory; |
119 | } |
120 | |
121 | } |