001: /**
002: * $Id: TaskMembership.java,v 1.4 2005/09/21 11:02:08 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.ProviderMembershipEvent;
022: import com.sun.portal.providers.service.provision.ProviderMembershipException;
023: import com.sun.portal.providers.service.provision.ProviderMembershipListener;
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 TaskMembership {
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(TaskMembership.class);
046:
047: /** Creates a new instance of TaskMembership */
048: TaskMembership(ServletContext context) throws ServiceException {
049: _servletContext = context;
050: _providerProvisionDescriptor = ProviderProvisionDescriptorReader
051: .getProviderProvisionDescriptor();
052: _listenerMap = initializeListenerMap();
053: }
054:
055: void memberAdded(String providerName,
056: ProviderMembershipEvent membershipEvent)
057: throws ServiceException {
058: checkInitializationSuccess();
059:
060: // read a;; listeners
061:
062: ProviderMembershipListener listener = null;
063: List membershipListeners = (List) _listenerMap
064: .get(providerName);
065: if (membershipListeners != null) {
066: Iterator membershipListenersIter = membershipListeners
067: .iterator();
068: try {
069: while (membershipListenersIter.hasNext()) {
070: listener = (ProviderMembershipListener) membershipListenersIter
071: .next();
072: listener.memberAdded(membershipEvent);
073: }
074:
075: } catch (ProviderMembershipException pe) {
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 memberRemoved(String providerName,
087: ProviderMembershipEvent membershipEvent)
088: throws ServiceException {
089: checkInitializationSuccess();
090: // read a;; listeners
091:
092: ProviderMembershipListener listener = null;
093: List membershipListeners = (List) _listenerMap
094: .get(providerName);
095: if (membershipListeners != null) {
096: Iterator membershipListenersIter = membershipListeners
097: .iterator();
098: try {
099: while (membershipListenersIter.hasNext()) {
100: listener = (ProviderMembershipListener) membershipListenersIter
101: .next();
102: listener.memberRemoved(membershipEvent);
103: }
104:
105: } catch (ProviderMembershipException pe) {
106:
107: throw new ServiceException(
108: "Member Provisioning failed for provider:"
109: + providerName + "listener:"
110: + listener.getClass().getName(), pe);
111:
112: }
113: }
114: }
115:
116: protected Map initializeListenerMap() throws ServiceException {
117:
118: Map listenerNameMap = _providerProvisionDescriptor
119: .getMembershipListenerMap();
120: Map listenerInstanceMap = new HashMap();
121: if (listenerNameMap != null) {
122: Iterator providerNamesIter = listenerNameMap.keySet()
123: .iterator();
124: while (providerNamesIter.hasNext()) {
125: List listeners = new ArrayList();
126: String providerName = (String) providerNamesIter.next();
127: String listenerName = null;
128: try {
129:
130: List listenerNames = (List) listenerNameMap
131: .get(providerName);
132: if (listenerNames != null) {
133: Iterator listenerNamesIter = listenerNames
134: .iterator();
135: while (listenerNamesIter.hasNext()) {
136: listenerName = (String) listenerNamesIter
137: .next();
138: _logger.log(Level.FINEST, "Provider:"
139: + providerName
140: + ":listener configured:"
141: + listenerName);
142: ProviderMembershipListener listener = (ProviderMembershipListener) Class
143: .forName(listenerName)
144: .newInstance();
145: listener.init(_servletContext);
146: listeners.add(listener);
147: }
148: listenerInstanceMap
149: .put(providerName, listeners);
150: }
151: } catch (Exception ex) {
152: _initializationSuccessFul = false;
153: throw new ServiceException(
154: "Provider Provision: Initialization of listener failed:"
155: + listenerName, ex);
156: }
157: }
158: }
159: return listenerInstanceMap;
160:
161: }
162:
163: void checkInitializationSuccess() {
164: if (!_initializationSuccessFul) {
165: throw new IllegalStateException(
166: "Initialization of provisioning framework had failed. Check logs of the provider webapp");
167: }
168: }
169: }
|