001: /**
002: * Copyright 2003 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */package com.sun.portal.wsrp.consumer.producermanager;
013:
014: import java.util.logging.Level;
015: import java.util.logging.Logger;
016:
017: import javax.servlet.http.HttpServletRequest;
018:
019: import com.iplanet.sso.SSOToken;
020:
021: import com.sun.portal.wsrp.consumer.common.WSRPConsumerConfig;
022: import com.sun.portal.wsrp.consumer.common.WSRPConsumerException;
023: import com.sun.portal.log.common.PortalLogger;
024:
025: /**
026: * <code>ProducerEntityManagerFactory</code> is a singleton class which
027: * provides methods for obtaining the <code>ProducerEntityManager</code>.
028: */
029: public class ProducerEntityManagerFactory {
030:
031: private ProducerEntityManagerFactory() {
032: }
033:
034: //
035: // static instance
036: //
037: private static ProducerEntityManagerFactory pemFactory = null;
038:
039: private static Logger logger = PortalLogger
040: .getLogger(ProducerEntityManagerFactory.class);
041:
042: //
043: // TBD: caching of each PEM instances
044: //
045:
046: /**
047: * Initialize static instance of ProducerEntityManagerFactory.
048: */
049: public static void init() {
050: //
051: // init pemFactory instance
052: //
053: pemFactory = new ProducerEntityManagerFactory();
054:
055: }
056:
057: /**
058: * Get a static instance of ProducerEntityManagerFactory. It's assumed
059: * that init() has already been called.
060: *
061: * @return a <code>ProducerEntityManagerFactory</code> instance.
062: */
063: public static ProducerEntityManagerFactory getInstance() {
064: if (pemFactory == null) {
065: init();
066: }
067: return pemFactory;
068: }
069:
070: /**
071: * Get an instance of ProducerEntityManager from a servlet.
072: *
073: * @param consumerId a <code>String</code> value identifying the consumer entity.
074: * @param request a <code>HttpServletRequest</code> to be used for establishing user context.
075: * @return a <code>ProducerEntityManager</code> value
076: * @exception WSRPConsumerException if an error occurs
077: */
078: public ProducerEntityManager getProducerEntityManager(
079: String consumerId, HttpServletRequest request)
080: throws WSRPConsumerException {
081:
082: if (logger.isLoggable(Level.FINEST)) {
083: String[] param = { "consumerId", consumerId };
084: logger.log(Level.FINEST, "PSWS_CSPWCP00001", param);
085: }
086:
087: WSRPConsumerConfig config = WSRPConsumerConfig.getInstance();
088:
089: String pemClassname = config
090: .getProducerEntityManagerClassname();
091: ProducerEntityManager pem = loadProducerEntityManagerClass(pemClassname);
092: pem.init(consumerId, request);
093:
094: return pem;
095: }
096:
097: /**
098: * Get an instance of ProducerEntityManager using credentials supplied
099: * in a form of username and password.
100: *
101: * @param consumerId a <code>String</code> value identifying the consumer entity
102: * @param username user id for establishing user context
103: * @param password user password for establishing user context
104: * @return a <code>ProducerEntityManager</code> value
105: * @exception WSRPConsumerException if an error occurs
106: */
107: public ProducerEntityManager getProducerEntityManager(
108: String consumerId, String username, String password)
109: throws WSRPConsumerException {
110:
111: if (logger.isLoggable(Level.FINEST)) {
112: String[] param = { "consumerId", consumerId };
113: logger.log(Level.FINEST, "PSWS_CSPWCP00001", param);
114: param[0] = "username";
115: param[1] = username;
116: logger.log(Level.FINEST, "PSWS_CSPWCP00001", param);
117: }
118:
119: WSRPConsumerConfig config = WSRPConsumerConfig.getInstance();
120:
121: String pemClassname = config
122: .getProducerEntityManagerClassname();
123: ProducerEntityManager pem = loadProducerEntityManagerClass(pemClassname);
124: pem.init(consumerId, username, password);
125:
126: return pem;
127: }
128:
129: /**
130: * Get an instance of ProducerEntityManager using credentials supplied
131: * in a form of SSOToken.
132: *
133: * @param ssoToken a <code>SSOToken</code>
134: * @param portalId a <code>String</code> value identifying the portal
135: * @param consumerId value identifying the consumer entity
136: * @return a <code>ProducerEntityManager</code> value
137: * @exception WSRPConsumerException if an error occurs
138: */
139: public ProducerEntityManager getProducerEntityManager(
140: SSOToken ssoToken, String portalId, String consumerId)
141: throws WSRPConsumerException {
142:
143: if (logger.isLoggable(Level.FINEST)) {
144: String[] param = { "portalId", portalId };
145: logger.log(Level.FINEST, "PSWS_CSPWCP00001", param);
146: param[0] = "consumerId";
147: param[1] = consumerId;
148: logger.log(Level.FINEST, "PSWS_CSPWCP00001", param);
149: }
150:
151: WSRPConsumerConfig config = WSRPConsumerConfig.getInstance();
152:
153: String pemClassname = config
154: .getProducerEntityManagerClassname();
155: ProducerEntityManager pem = loadProducerEntityManagerClass(pemClassname);
156: pem.init(ssoToken, portalId, consumerId);
157:
158: return pem;
159: }
160:
161: protected ProducerEntityManager loadProducerEntityManagerClass(
162: String classname) throws WSRPConsumerException {
163:
164: ProducerEntityManager pem = null;
165: try {
166: pem = (ProducerEntityManager) (Class.forName(classname)
167: .newInstance());
168: } catch (ClassNotFoundException cnfe) {
169: throw new WSRPConsumerException(
170: "ProducerEntityManagerFactory.loadProducerEntityMangerClass(): failed to load PEM class.",
171: cnfe);
172: } catch (NoClassDefFoundError ncdfe) {
173: throw new WSRPConsumerException(
174: "ProducerEntityManagerFactory.loadProducerEntityMangerClass(): failed to load PEM class.",
175: ncdfe);
176: } catch (IllegalAccessException iae) {
177: throw new WSRPConsumerException(
178: "ProducerEntityManagerFactory.loadProducerEntityMangerClass(): failed to load PEM class.",
179: iae);
180: } catch (ClassCastException cce) {
181: throw new WSRPConsumerException(
182: "ProducerEntityManagerFactory.loadProducerEntityMangerClass(): failed to load PEM class.",
183: cce);
184: } catch (InstantiationException ie) {
185: throw new WSRPConsumerException(
186: "ProducerEntityManagerFactory.loadProducerEntityMangerClass(): failed to load PEM class.",
187: ie);
188: } catch (SecurityException se) {
189: throw new WSRPConsumerException(
190: "ProducerEntityManagerFactory.loadProducerEntityMangerClass(): failed to load PEM class.",
191: se);
192: }
193: return pem;
194: }
195:
196: }
|