001: /**
002: * $Id: AMUtil.java,v 1.10.2.1 2007/04/16 09:47:55 ns208321 Exp $
003: * Copyright 2004 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.fabric.util;
014:
015: import java.io.File;
016: import java.io.FileReader;
017: import java.io.FileInputStream;
018: import java.io.InputStream;
019: import java.io.InputStreamReader;
020: import java.io.BufferedReader;
021: import java.io.StringWriter;
022: import java.io.PrintWriter;
023: import java.io.IOException;
024: import java.net.URL;
025: import java.util.Properties;
026: import java.util.Set;
027: import java.util.Map;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.logging.Level;
031: import java.util.logging.Logger;
032:
033: import javax.security.auth.callback.Callback;
034: import javax.security.auth.callback.NameCallback;
035: import javax.security.auth.callback.PasswordCallback;
036:
037: import com.iplanet.am.sdk.AMException;
038: import com.iplanet.am.sdk.AMObject;
039: import com.iplanet.am.sdk.AMStoreConnection;
040: import com.iplanet.sso.SSOException;
041: import com.iplanet.sso.SSOToken;
042: import com.sun.identity.sm.ServiceConfig;
043: import com.sun.identity.sm.ServiceManager;
044: import com.sun.identity.sm.ServiceConfigManager;
045: import com.sun.identity.sm.SMSException;
046: import com.sun.identity.authentication.AuthContext;
047: import com.sun.identity.authentication.spi.AuthLoginException;
048: import com.sun.portal.log.common.PortalLogger;
049: import com.sun.portal.util.SSOUtil;
050:
051: import com.sun.portal.util.Platform;
052:
053: /**
054: * This is a utility class implements the functionality tasks related to
055: * configuring services and other portal server related that modify Access
056: * Manager data
057: */
058: public class AMUtil extends SSOUtil {
059:
060: private static final String FS = Platform.fs;
061:
062: private static String ORG_NAMING_ATTR = "o";
063: private static String ROLE_NAMING_ATTR = "cn";
064: private static String PEOPLE_CONTAINER = "ou=People";
065: private static String USER_NAMING_ATTR = "uid";
066:
067: private static final String LOCALE_SERVICE = "sunIdentityLocaleService";
068: private static final String VERSION = "1.0";
069: private static final String RB_CONFIG_ID = "ResourceBundleName";
070: private static final String LOCALE_CONFIG_ID = "Locale";
071:
072: private static Logger logger = PortalLogger.getLogger(AMUtil.class);
073:
074: public static void loadServiceSchema(Set filePaths,
075: SSOToken ssoToken) {
076: try {
077: ServiceManager sm = new ServiceManager(ssoToken);
078: if (sm != null && filePaths != null) {
079: // Extract all null values if any
080: filePaths.remove(null);
081: Iterator itr = filePaths.iterator();
082: while (itr.hasNext()) {
083: String schemaXMLPath = (String) itr.next();
084: try {
085: FileInputStream fis = new FileInputStream(
086: schemaXMLPath);
087: sm.registerServices(fis);
088: } catch (Exception e) {
089: if (e.getMessage().indexOf(
090: "Service already exists") != -1) {
091: logger.log(Level.SEVERE, e.getMessage());
092: } else {
093: logger.log(Level.SEVERE, "PSFB_CSPFU0045",
094: e);
095: }
096: }
097: }
098: }
099: } catch (SSOException ssoe) {
100: logger.log(Level.SEVERE, "PSFB_CSPFU0045", ssoe);
101: } catch (SMSException smse) {
102: logger.log(Level.SEVERE, "PSFB_CSPFU0045", smse);
103: }
104:
105: }
106:
107: public static void deleteServiceSchema(Set svcNames,
108: SSOToken ssoToken) {
109: try {
110: ServiceManager sm = new ServiceManager(ssoToken);
111: if (sm != null && svcNames != null) {
112: // Extract all null values if any
113: svcNames.remove(null);
114: Iterator itr = svcNames.iterator();
115: while (itr.hasNext()) {
116: String svcName = (String) itr.next();
117: try {
118: sm.removeService(svcName, VERSION);
119: } catch (Exception e) {
120: logger.log(Level.SEVERE, "PSFB_CSPFU0046", e);
121: }
122: }
123: }
124: } catch (SSOException ssoe) {
125: logger.log(Level.SEVERE, "PSFB_CSPFU0046", ssoe);
126: } catch (SMSException smse) {
127: logger.log(Level.SEVERE, "PSFB_CSPFU0046", smse);
128: }
129:
130: }
131:
132: public static String getOrgNamingAttribute(SSOToken AMSSOToken)
133: throws SSOException {
134: return getNamingAttribute(AMSSOToken, AMObject.ORGANIZATION,
135: ORG_NAMING_ATTR);
136: }
137:
138: public static String getRoleNamingAttribute(SSOToken AMSSOToken)
139: throws SSOException {
140: return getNamingAttribute(AMSSOToken, AMObject.ROLE,
141: ROLE_NAMING_ATTR);
142: }
143:
144: public static String getUserNamingAttribute(SSOToken AMSSOToken)
145: throws SSOException {
146: return getNamingAttribute(AMSSOToken, AMObject.USER,
147: USER_NAMING_ATTR);
148: }
149:
150: public static String getPeopleContainerNamingAttribute(
151: SSOToken AMSSOToken) throws SSOException {
152: return getNamingAttribute(AMSSOToken,
153: AMObject.PEOPLE_CONTAINER, PEOPLE_CONTAINER);
154: }
155:
156: /**
157: * Stores or creates the ResourceBundle in the directory.
158: * @param token Single sign-on token of user
159: * @param rbName Name of ResourceBundle
160: * @param locale Locale of ResourceBundle. If null, the default
161: * ResourceBundle is updated.
162: * @param attributes Map of key-value pairs defining the ResourceBundle.
163: * @throws SMSException If there is an error trying to modify the datastore
164: * @throws SSOException If this user's token has expired.
165: */
166: public static void addResourceBundle(String rbName, String locale,
167: Map resourceStrings, SSOToken token) {
168: if (rbName == null) {
169: return;
170: }
171:
172: try {
173: if (resourceStrings != null && !resourceStrings.isEmpty()) {
174: ServiceConfigManager scm = new ServiceConfigManager(
175: token, LOCALE_SERVICE, VERSION);
176: ServiceConfig globalConfig = scm.getGlobalConfig(null);
177: ServiceConfig rbConfig = globalConfig
178: .getSubConfig(rbName);
179: if (rbConfig == null) {
180: // create a sub config
181: globalConfig.addSubConfig(rbName, RB_CONFIG_ID, 0,
182: new HashMap());
183: rbConfig = globalConfig.getSubConfig(rbName);
184: }
185: if (locale == null) {
186: // store these as defaults
187: rbConfig.setAttributes(resourceStrings);
188: } else {
189: // get/create locale subconfig and set these attributes
190: ServiceConfig localeConfig = rbConfig
191: .getSubConfig(locale);
192: if (localeConfig == null) {
193: rbConfig.addSubConfig(locale, LOCALE_CONFIG_ID,
194: 0, resourceStrings);
195: localeConfig = rbConfig.getSubConfig(locale);
196: } else {
197: localeConfig.setAttributes(resourceStrings);
198: }
199: }
200: }
201: } catch (Exception e) {
202: logger.log(Level.SEVERE, "PSFB_CSPFU0047", e);
203: }
204: }
205:
206: /**
207: * Deletes the specified ResourceBundle from the directory.
208: * @param token Single sign-on token of user
209: * @param rbName Name of ResourceBundle
210: * @param locale String defining the locale. If null, then all the locales
211: * of this ResourceBundle, including the default one, are deleted.
212: */
213: public static void removeResourceBundle(String rbName,
214: String locale, SSOToken token) {
215: if (rbName == null) {
216: return;
217: }
218: try {
219: ServiceConfigManager scm = new ServiceConfigManager(token,
220: LOCALE_SERVICE, VERSION);
221: ServiceConfig globalConfig = scm.getGlobalConfig(null);
222: if (locale == null) {
223: // Delete the entire ResourceBundle tree
224: globalConfig.removeSubConfig(rbName);
225: } else {
226: ServiceConfig rbConfig = globalConfig
227: .getSubConfig(rbName);
228: if (rbConfig == null) {
229: // This will happen only when there is no config for
230: // the resource bundle provided.
231: } else {
232: rbConfig.removeSubConfig(locale);
233: }
234: }
235: } catch (Exception e) {
236: logger.log(Level.SEVERE, "PSFB_CSPFU0048", e);
237: }
238: }
239:
240: /**
241: * Retrieve the Access Manager naming attribute of <code>type</code> otherwise
242: * use the <code>def</code> value.
243: * script is fixed.
244: *
245: * @param store AMStoreConnection
246: * @param type AMObject typeMethod name to return
247: * @param def Default value to use if there is an exception
248: * return naming attribute
249: */
250: private static String getNamingAttribute(SSOToken ssoToken,
251: int type, String def) throws SSOException {
252:
253: AMStoreConnection amsc;
254: try {
255: amsc = new AMStoreConnection(ssoToken);
256: } catch (SSOException e) {
257: logger.log(Level.SEVERE, "PSFB_CSPFU0009", e);
258: throw e;
259: }
260:
261: String value = def;
262: if (amsc != null) {
263: try {
264: value = AMStoreConnection.getNamingAttribute(type);
265: } catch (AMException ame) {
266: value = def;
267: }
268: }
269: return value;
270: }
271:
272: public static boolean isRealmEnabled(final String sAMConfigDir,
273: final String sAMConfigFile) {
274:
275: boolean bIsRealmMode = true;
276: Properties AMConfigProps = new Properties();
277:
278: try {
279: String sAMConfileFilePath = sAMConfigDir + FS
280: + sAMConfigFile;
281: AMConfigProps.load(new FileInputStream(sAMConfileFilePath));
282:
283: String sProtocol = AMConfigProps
284: .getProperty("com.iplanet.am.server.protocol");
285: String sHostName = AMConfigProps
286: .getProperty("com.iplanet.am.server.host");
287: String sPortNum = AMConfigProps
288: .getProperty("com.iplanet.am.server.port");
289: String sDescriptor = AMConfigProps
290: .getProperty("com.iplanet.am.services.deploymentDescriptor");
291: String sServlet = "SMSServlet";
292: String sMethodName = "isRealmEnabled";
293:
294: if ((sDescriptor != null) && !sDescriptor.startsWith("/")) {
295: sDescriptor = "/" + sDescriptor;
296: }
297: StringBuffer sBuf = new StringBuffer(sDescriptor);
298: sBuf.append("/").append(sServlet).append("?").append(
299: "method=").append(sMethodName);
300: sDescriptor = sBuf.toString();
301:
302: URL url = new URL(sProtocol, sHostName, Integer
303: .parseInt(sPortNum), sDescriptor);
304: logger.log(Level.INFO, "PSFB_CSPFU0053", new String[] { url
305: .toString() });
306: InputStream str = (InputStream) url.getContent();
307: BufferedReader reader = new BufferedReader(
308: new InputStreamReader(str));
309: String sIsRealmMode = reader.readLine();
310: logger.log(Level.INFO, "PSFB_CSPFU0054",
311: new String[] { sIsRealmMode });
312: bIsRealmMode = sIsRealmMode.equalsIgnoreCase("true");
313: } catch (Exception e) {
314:
315: StringWriter sw = new StringWriter();
316: PrintWriter pw = new PrintWriter(sw);
317: e.printStackTrace(pw);
318: logger.log(Level.SEVERE, e.getMessage() + "\n"
319: + sw.toString());
320: }
321:
322: return bIsRealmMode;
323: }
324: }
|