001: /**
002: * $Id: TaskProvision.java,v 1.4 2005/09/21 11:02:11 dg154973 Exp $
003: * Copyright 2005 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.service.provider.impl;
014:
015: import java.util.Map;
016: import java.util.List;
017: import java.util.Iterator;
018: import java.util.ArrayList;
019: import java.util.HashMap;
020:
021: import com.sun.portal.providers.service.provision.ProviderProvisionEvent;
022: import com.sun.portal.providers.service.provision.ProviderProvisionException;
023: import com.sun.portal.providers.service.provision.ProviderProvisionListener;
024:
025: import java.util.logging.Logger;
026: import java.util.logging.Level;
027: import com.sun.portal.log.common.PortalLogger;
028: import com.sun.portal.service.ServiceException;
029: import com.sun.portal.service.provider.impl.descriptor.ProviderProvisionDescriptor;
030: import com.sun.portal.service.provider.impl.descriptor.ProviderProvisionDescriptorReader;
031:
032: import javax.servlet.ServletContext;
033:
034: /**
035: *
036: * @author mjain
037: */
038: class TaskProvision {
039:
040: private ProviderProvisionDescriptor _providerProvisionDescriptor;
041: private Map _listenerMap;
042: private boolean _initializationSuccessFul = true;
043: private ServletContext _servletContext;
044: private static Logger _logger = PortalLogger
045: .getLogger(TaskProvision.class);
046:
047: /** Creates a new instance of TaskServiceProvision */
048: TaskProvision(ServletContext context) throws ServiceException {
049: _servletContext = context;
050: _providerProvisionDescriptor = ProviderProvisionDescriptorReader
051: .getProviderProvisionDescriptor();
052: _listenerMap = initializeListenerMap();
053: }
054:
055: void provision(String providerName,
056: ProviderProvisionEvent provisionEvent)
057: throws ServiceException {
058: checkInitializationSuccess();
059:
060: // read a;; listeners
061:
062: ProviderProvisionListener listener = null;
063: List provisionListeners = (List) _listenerMap.get(providerName);
064: if (provisionListeners != null) {
065: Iterator provisionListenersIter = provisionListeners
066: .iterator();
067: try {
068: while (provisionListenersIter.hasNext()) {
069: listener = (ProviderProvisionListener) provisionListenersIter
070: .next();
071: listener.provision(provisionEvent);
072: }
073:
074: } catch (ProviderProvisionException pe) {
075:
076: throw new ServiceException(
077: "Provisioning failed for provider:"
078: + providerName + "listener:"
079: + listener.getClass().getName(), pe);
080:
081: }
082: }
083:
084: }
085:
086: void unprovision(String providerName,
087: ProviderProvisionEvent provisionEvent)
088: throws ServiceException {
089: checkInitializationSuccess();
090: // read a;; listeners
091:
092: ProviderProvisionListener listener = null;
093: List provisionListeners = (List) _listenerMap.get(providerName);
094: if (provisionListeners != null) {
095: Iterator provisionListenersIter = provisionListeners
096: .iterator();
097: try {
098: while (provisionListenersIter.hasNext()) {
099: listener = (ProviderProvisionListener) provisionListenersIter
100: .next();
101: listener.unprovision(provisionEvent);
102: }
103:
104: } catch (ProviderProvisionException pe) {
105:
106: throw new ServiceException(
107: "Provisioning failed for provider:"
108: + providerName + "listener:"
109: + listener.getClass().getName(), pe);
110:
111: }
112: }
113: }
114:
115: protected Map initializeListenerMap() throws ServiceException {
116:
117: Map listenerNameMap = _providerProvisionDescriptor
118: .getProvisionListenerMap();
119: Map listenerInstanceMap = new HashMap();
120: if (listenerNameMap != null) {
121: Iterator providerNamesIter = listenerNameMap.keySet()
122: .iterator();
123: while (providerNamesIter.hasNext()) {
124: List listeners = new ArrayList();
125: String providerName = (String) providerNamesIter.next();
126: String listenerName = null;
127: try {
128:
129: List listenerNames = (List) listenerNameMap
130: .get(providerName);
131: if (listenerNames != null) {
132: Iterator listenerNamesIter = listenerNames
133: .iterator();
134: while (listenerNamesIter.hasNext()) {
135: listenerName = (String) listenerNamesIter
136: .next();
137: _logger.log(Level.FINEST, "Provider:"
138: + providerName
139: + ":listener configured:"
140: + listenerName);
141: ProviderProvisionListener listener = (ProviderProvisionListener) Class
142: .forName(listenerName)
143: .newInstance();
144: listener.init(_servletContext);
145: listeners.add(listener);
146: }
147: listenerInstanceMap
148: .put(providerName, listeners);
149: }
150: } catch (Exception ex) {
151: _initializationSuccessFul = false;
152: throw new ServiceException(
153: "Provider Provision: Initialization of listener failed:"
154: + listenerName, ex);
155: }
156: }
157: }
158: return listenerInstanceMap;
159:
160: }
161:
162: void checkInitializationSuccess() {
163: if (!_initializationSuccessFul) {
164: throw new IllegalStateException(
165: "Initialization of provisioning framework had failed. Check logs of the provider webapp");
166: }
167: }
168: }
|