001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.services.idsame;
006:
007: import com.iplanet.sso.SSOException;
008: import com.iplanet.sso.SSOToken;
009: import com.iplanet.sso.SSOTokenManager;
010: import com.sun.identity.sm.SMSException;
011: import com.sun.identity.sm.ServiceConfig;
012: import com.sun.identity.sm.ServiceConfigManager;
013: import com.sun.portal.rewriter.services.DataService;
014: import com.sun.portal.rewriter.services.DataServiceException;
015: import com.sun.portal.rewriter.services.DataServiceHelper;
016: import com.sun.portal.rewriter.util.StringHelper;
017:
018: import java.util.Collections;
019: import java.util.HashMap;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Observable;
024: import java.util.Properties;
025: import java.util.Set;
026:
027: /**
028: * Implements DataService Interface, uses IDSAME as the server
029: * to store/retrive data
030: *
031: * @version 1.0 12/15/2001
032: * @author sanjib.ghosh@sun.com
033: */
034: public final class IDSAMEDataService implements DataService {
035: private static final IDSAMEEventListenerImpl registar = new IDSAMEEventListenerImpl();
036: private static final String SERVICE_NAME = "SunPortalRewriterService";
037: private static final String SERVICE_SCHEMA_NAME = "SunPortalRewriterGlobal";
038: private static final String ATTRIBUTE_NAME = "sunPortalRewriterRuleset";
039: private static final String SUBCONFIG_ID = "SunPortalRewriterRuleSets";
040:
041: static {
042: DataServiceHelper
043: .initLogSystem(com.sun.portal.rewriter.services.idsame.IDSAMELogHandler.class);
044: }
045:
046: private final ServiceConfig config; // holds the config node SERVICE_SCHEMA_NAME
047: private ServiceConfigManager configManager;
048:
049: public IDSAMEDataService(final Properties aProps)
050: throws DataServiceException {
051: this (createSSOToken(aProps));
052: }//constructor
053:
054: public IDSAMEDataService(final SSOToken aSSOToken)
055: throws DataServiceException {
056: config = getConfig(aSSOToken);
057: }//constructor
058:
059: /**
060: * Delete the entry corresponding to the key from the IDSAME repository
061: * and returns the XML ruleset that just got deleted.
062: * If the entry corresponding to the key does not exist in the repository,
063: * it returns null
064: *
065: * @param aRuleSetIDKey The key to the entry
066: * @return String the value that just deleted or null if not existed.
067: * @exception DataServiceException - If it is not able to retrieve
068: * the value from the repository.
069: * Use getCause() to find out
070: * specific exception.
071: * @exception IllegalArgumentException - If the key is null or contains
072: * only empty spaces
073: */
074: public String deleteKey(final String aRuleSetIDKey)
075: throws DataServiceException {
076: checkRuleSetIDKeyInput(aRuleSetIDKey);
077:
078: final String lMatchedRuleSetID = StringHelper
079: .findMatchByIgnoringCase(retrieveKeys(), aRuleSetIDKey);
080: final String result = optimisticDeleteKey(lMatchedRuleSetID);
081: return result;
082: }//deleteKey()
083:
084: /**
085: * Retrieve the ruleset(XML) correponding to the key from the IDSAME
086: * repository. If there is no entry for that key, it returns null
087: *
088: * @param aRuleSetIDKey The key to the entry
089: * @return String the ruleset value for the key or null if does not exist.
090: * @exception DataServiceException - If it is not able to retrieve the value
091: * find out specific exception.
092: * @exception IllegalArgumentException - If the key is null or contains
093: * only empty spaces
094: */
095: public String retrieveXML(final String aRuleSetIDKey)
096: throws DataServiceException {
097: checkRuleSetIDKeyInput(aRuleSetIDKey);
098: return optimisticRetrieveXML(aRuleSetIDKey.toLowerCase());
099: } //retrieveXML()
100:
101: /**
102: * Store the ruleset(XML) against the key in the IDSAME repository. If any
103: * ruleset was already stored against the key, it overwrites that with the
104: * new value and returns the old value.
105: *
106: * @param aRuleSetIDKey The key to the entry
107: * @param aXMLRuleSet The ruleset
108: * @return String the old ruleset value for the key or
109: * null if did not exist.
110: * @exception DataServiceException - If it is not able to retrieve
111: * the value from the repository.
112: * Use getCause() to find out
113: * specific exception.
114: * @exception IllegalArgumentException - If the key/ruleset is null or
115: * contains only empty spaces
116: */
117: public String storeXML(final String aRuleSetIDKey,
118: final String aXMLRuleSet) throws DataServiceException {
119: try {
120: checkRuleSetIDKeyInput(aRuleSetIDKey);
121: checkRuleSetInput(aXMLRuleSet);
122:
123: String oldXML = optimisticDeleteKey(aRuleSetIDKey);
124: config.addSubConfig(aRuleSetIDKey.toLowerCase(),
125: SUBCONFIG_ID, 0, // priority ???;
126: prepareAttributeValue(aXMLRuleSet));
127: return oldXML;
128: } catch (Exception e) {
129: throw new DataServiceException(e.getMessage(), e);
130: }
131: } //storeXML()
132:
133: /**
134: * Retrieve the set of all the ruleset keys
135: *
136: * @return Set A ruleset keys ( readonly)
137: * @exception DataServiceException - If it is not able to retrieve the value
138: */
139: public Set retrieveKeys() throws DataServiceException {
140: try {
141: return Collections.unmodifiableSet(config
142: .getSubConfigNames());
143: } catch (SMSException smse) {
144: throw new DataServiceException(smse.getMessage(), smse);
145: } //try-catch
146: } //retrieveKeys()
147:
148: // prepares the ruleset to be added to the repository
149: private static Map prepareAttributeValue(final String aXMLRuleSet) {
150: final HashSet set = new HashSet(1);
151: final Map map = new HashMap(1);
152: set.add(aXMLRuleSet);
153: map.put(ATTRIBUTE_NAME, set);
154: return map;
155: }
156:
157: // delete the entry for the key
158: private String optimisticDeleteKey(final String aRuleSetIDKey)
159: throws DataServiceException {
160: try {
161: String oldXML = null;
162: if (matchesWithID(aRuleSetIDKey) != null) {
163: oldXML = optimisticRetrieveXML(aRuleSetIDKey);
164: config.removeSubConfig(aRuleSetIDKey);
165: } //if
166: return oldXML;
167: } catch (SMSException smse) {
168: throw new DataServiceException(smse.getMessage(), smse);
169: } catch (SSOException ssoe) {
170: throw new DataServiceException(ssoe.getMessage(), ssoe);
171: }
172: }//optimisticDeleteKey()
173:
174: // retrieves the ruleset corresponsing to the key
175: private String optimisticRetrieveXML(final String aRuleSetIDKey)
176: throws DataServiceException {
177: try {
178: final ServiceConfig subConfig = config
179: .getSubConfig(aRuleSetIDKey);
180:
181: if (null == subConfig) {
182: return null;
183: }
184:
185: Map attributes = subConfig.getAttributes();
186: Set values = (Set) attributes.get(ATTRIBUTE_NAME);
187: Iterator itr = values.iterator();
188: return (String) itr.next();
189: } catch (SMSException smse) {
190: throw new DataServiceException(smse.getMessage(), smse);
191: } catch (SSOException ssoe) {
192: throw new DataServiceException(ssoe.getMessage(), ssoe);
193: }
194: } //optimisticRetrieveXML()
195:
196: // returns if the Key does exist
197: public String matchesWithID(final String aRuleSetIDKey)
198: throws DataServiceException {
199:
200: final String match = StringHelper.findMatchByIgnoringCase(
201: retrieveKeys(), aRuleSetIDKey);
202: return match;
203: }//matchesWithID()
204:
205: // Gets the config node for "SunPortalRewriterGlobal"
206: private ServiceConfig getConfig(final SSOToken token)
207: throws DataServiceException {
208: try {
209: configManager = new ServiceConfigManager(SERVICE_NAME,
210: token);
211:
212: registerForIDSMAEEvents(configManager);
213:
214: ServiceConfig globalConfig = configManager
215: .getGlobalConfig(null);
216: return globalConfig.getSubConfig(SERVICE_SCHEMA_NAME);
217: } catch (SSOException te) {
218: throw new DataServiceException(
219: "Not able to get ServiceConfigManager - ", te);
220: } catch (SMSException smse) {
221: throw new DataServiceException(smse.getMessage(), smse);
222: } // end try-catch block
223: }//getConfig()
224:
225: /**
226: * This is provided to make the external users use observer interface rather
227: * than IDSAMEEvent notification mechanisam.
228: */
229: public Observable getChangeNotifier() {
230: return registar;
231: }//getChangeNotifier()
232:
233: /**
234: * This method can be completed only when IDSAME provide a way to register
235: * for succonfig change events, make sure all the various constructors will
236: * call this method during intialisation time
237: */
238: private static void registerForIDSMAEEvents(
239: final ServiceConfigManager aConfigManager) {
240: aConfigManager.addListener(registar);
241: }//registerForIDSMAEEvents()
242:
243: // checks the validity of RuleSetKey input
244: private static void checkRuleSetIDKeyInput(
245: final String aRuleSetIDKey) {
246: if ((null == aRuleSetIDKey)
247: || (0 == aRuleSetIDKey.trim().length())) {
248: throw new IllegalArgumentException(
249: "aRuleSetIDKey cannot be null or empty");
250: }
251: }//checkRuleSetIDKeyInput()
252:
253: // checks the validity of RuleSet input
254: private static void checkRuleSetInput(final String aXMLRuleSet) {
255: if ((null == aXMLRuleSet) || (0 == aXMLRuleSet.trim().length())) {
256: throw new IllegalArgumentException(
257: "aXMLRuleSet cannot be nullor empty");
258: }
259: }//checkRuleSetInput()
260:
261: private static SSOToken createSSOToken(final Properties aProps)
262: throws DataServiceException {
263: final String lBindDN = aProps.getProperty(
264: DataService.PROPERTY_DATA_SERIVCE_USER, "");
265: final String lPassword = aProps.getProperty(
266: DataService.PROPERTY_DATA_SERIVCE_USER_PASSWORD, "");
267:
268: try {
269: return SSOTokenManager.getInstance().createSSOToken(
270: new java.security.Principal() {
271: public String getName() {
272: return lBindDN;
273: }
274: }, lPassword);
275: } catch (Exception e) {
276: throw new DataServiceException(e.getMessage(), e,
277: DataServiceException.AUTHORIZATION_FAILED);
278: }
279: }//createSSOToken()
280:
281: }//class IDSAMEDataService
|