01: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.container.services;
07:
08: import java.util.HashMap;
09: import java.util.Map;
10: import java.util.Properties;
11: import java.util.Enumeration;
12:
13: import javax.servlet.ServletConfig;
14:
15: import org.apache.pluto.factory.Factory;
16: import org.apache.pluto.services.factory.FactoryManagerService;
17: import org.jasig.portal.utils.ResourceLoader;
18:
19: /**
20: * Implementation of Apache Pluto object model.
21: * @author Michael Ivanov, mvi@immagic.com
22: * @version $Revision: 34757 $
23: */
24: public class FactoryManagerServiceImpl implements
25: PortletContainerService, FactoryManagerService {
26:
27: private ServletConfig servletConfig = null;
28: private Properties properties = null;
29: private Map factories = null;
30: protected final static String FACTORY_PROPERTIES = "/properties/container/factory.properties";
31:
32: public FactoryManagerServiceImpl() {
33: factories = new HashMap();
34: }
35:
36: // PortletContainerService methods
37:
38: public void init(ServletConfig servletConfig, Properties properties)
39: throws Exception {
40: this .servletConfig = servletConfig;
41: this .properties = properties;
42: Properties factoryProperties = null;
43: try {
44: factoryProperties = ResourceLoader
45: .getResourceAsProperties(
46: FactoryManagerServiceImpl.class,
47: FACTORY_PROPERTIES);
48: } catch (Exception e) {
49: throw new IllegalAccessException("Could not load "
50: + FACTORY_PROPERTIES + " file");
51: }
52: for (Enumeration names = factoryProperties.propertyNames(); names
53: .hasMoreElements();) {
54: String name = (String) names.nextElement();
55: addFactory(name, factoryProperties.getProperty(name));
56: }
57:
58: }
59:
60: public void destroy() throws Exception {
61: factories = null;
62: }
63:
64: // FactoryManagerService methods
65:
66: public Factory getFactory(Class theClass) {
67: return (Factory) factories.get(theClass);
68: }
69:
70: // Additional methods
71:
72: private void addFactory(String factoryInterfaceName,
73: String factoryImplName) throws Exception {
74: Class factoryInterface = Class.forName(factoryInterfaceName);
75: Class factoryImpl = Class.forName(factoryImplName);
76: Factory factory = (Factory) factoryImpl.newInstance();
77: factory.init(servletConfig, properties);
78: addFactory(factoryInterface, factory);
79: }
80:
81: private void addFactory(Class factoryInterface, Factory factory) {
82: factories.put(factoryInterface, factory);
83: }
84:
85: }
|