001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Portal/Sources/es/udc/mypersonalizer/portal/controller/services/ServiceControllerRegistrySingleton.java,v 1.1.1.1 2004/03/25 12:08:39 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:39 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.portal.controller.services;
042:
043: import es.udc.mypersonalizer.portal.conventions.ServiceConfigurationConventions;
044: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceConfiguration;
045: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceConfigurationRegistrySingleton;
046: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
047: import java.util.*;
048:
049: /**
050: * This singleton class maintains a registry of service controller objects,
051: * which are service-specific. When a controller is requested for a service
052: * and this doesn't exist in the registry cache, the configuration parameter
053: * <tt>serviceControllerClassName</tt> is read and the class named by this
054: * parameter is instanced and pushed into the cache.
055: *
056: * @author Daniel Fernandez
057: * @since 1.0
058: */
059: public class ServiceControllerRegistrySingleton {
060:
061: /**
062: * The singleton object.
063: */
064: private static ServiceControllerRegistrySingleton singleton = null;
065:
066: /**
067: * The registry cache of service controllers.
068: */
069: private Map serviceControllers = null;
070:
071: /**
072: * Avoids the instantiation of this class.
073: */
074: private ServiceControllerRegistrySingleton() {
075: serviceControllers = new HashMap();
076: }
077:
078: /**
079: * Returns the singleton object.
080: *
081: * @return the singleton object.
082: */
083: public static ServiceControllerRegistrySingleton getInstance() {
084: if (singleton == null) {
085: singleton = new ServiceControllerRegistrySingleton();
086: }
087: return singleton;
088: }
089:
090: /**
091: * Returns the service controller specific for a service.
092: *
093: * @param serviceIdentifier the service identifier.
094: * @throws InternalErrorException if any fatal errors happen.
095: * @return the service controller.
096: */
097: public ServiceController getServiceController(
098: String serviceIdentifier) throws InternalErrorException {
099: ServiceController serviceController = (ServiceController) serviceControllers
100: .get(serviceIdentifier);
101:
102: if (serviceController == null) {
103:
104: /*
105: * The service controller for this service is not yet cached,
106: * and so we read its class name from the cache and instantiate
107: * it.
108: */
109: ServiceConfigurationRegistrySingleton serviceConfigurationRegistrySingleton = ServiceConfigurationRegistrySingleton
110: .getInstance();
111:
112: ServiceConfiguration serviceConfiguration = serviceConfigurationRegistrySingleton
113: .getServiceConfiguration(serviceIdentifier);
114:
115: String serviceControllerClassName = (String) serviceConfiguration
116: .getParameters()
117: .get(
118: ServiceConfigurationConventions.SERVICE_CONTROLLER_CLASS_NAME);
119:
120: if (serviceControllerClassName != null) {
121:
122: try {
123: serviceController = (ServiceController) (Class
124: .forName(serviceControllerClassName)
125: .newInstance());
126: } catch (Exception e) {
127: throw new InternalErrorException(e);
128: }
129:
130: serviceControllers.put(serviceIdentifier,
131: serviceController);
132:
133: } else {
134:
135: throw new InternalErrorException(
136: "No ServiceController registered for "
137: + serviceIdentifier);
138:
139: }
140:
141: }
142:
143: return serviceController;
144: }
145:
146: }
|