001: /**
002: * $Id: AMObjectSearch.java,v 1.10 2006/04/06 22:39:45 rt94277 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.fabric.mbeans;
014:
015: import java.security.Principal;
016: import java.util.ArrayList;
017: import java.util.List;
018: import java.util.Iterator;
019: import java.util.Map;
020: import java.util.HashMap;
021: import java.util.HashSet;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024: import java.util.Set;
025:
026: import com.iplanet.sso.SSOException;
027: import com.iplanet.sso.SSOTokenManager;
028: import com.iplanet.sso.SSOToken;
029: import com.iplanet.am.sdk.AMConstants;
030: import com.iplanet.am.sdk.AMException;
031: import com.iplanet.am.sdk.AMObject;
032: import com.iplanet.am.sdk.AMOrganization;
033: import com.iplanet.am.sdk.AMPeopleContainer;
034: import com.iplanet.am.sdk.AMSearchResults;
035: import com.iplanet.am.sdk.AMSearchControl;
036: import com.iplanet.am.sdk.AMStoreConnection;
037: import com.iplanet.am.sdk.AMUser;
038:
039: import com.sun.portal.admin.common.PSMBeanException;
040: import com.sun.portal.admin.server.AdminServerUtil;
041: import com.sun.portal.admin.server.mbeans.PSResource;
042: import com.sun.portal.admin.common.context.PortalDomainContext;
043: import com.sun.portal.admin.common.context.PSConfigContext;
044:
045: import com.sun.portal.log.common.PortalLogger;
046:
047: public class AMObjectSearch extends PSResource implements
048: AMObjectSearchMBean {
049:
050: private static Logger logger = PortalLogger
051: .getLogger(AMObjectSearch.class);
052:
053: private PSConfigContext cc = null;
054:
055: public void init(PSConfigContext cc, PortalDomainContext pdc,
056: List path) {
057: super .init(cc, pdc, path);
058: this .cc = cc;
059: }
060:
061: /**
062: * Query AM for label and object type identifier that correspond
063: * to USER, ROLE and ORGANIZATION.
064: *
065: * @return A map object whose key represents the label and
066: * the value corresponding to the key in the map is
067: * the object type identifier. The results are not
068: * in any particular order in the map.
069: */
070: public Map queryObjectTypes() throws PSMBeanException {
071: Map result = null;
072: AMStoreConnection conn = null;
073: try {
074: SSOTokenManager manager = SSOTokenManager.getInstance();
075: SSOToken token = AdminServerUtil.getSSOToken();
076: if ((token != null) && (manager.isValidToken(token))) {
077: // open a store connection
078: conn = new AMStoreConnection(token);
079: // store the result using a HashMap
080: result = new HashMap();
081: synchronized (result) {
082: String oname = conn
083: .getAMObjectName(AMObject.ORGANIZATION);
084: result.put(oname,
085: new Integer(AMObject.ORGANIZATION));
086: oname = conn.getAMObjectName(AMObject.ROLE);
087: result.put(oname, new Integer(AMObject.ROLE));
088: oname = conn.getAMObjectName(AMObject.USER);
089: result.put(oname, new Integer(AMObject.USER));
090: oname = conn
091: .getAMObjectName(AMObject.FILTERED_ROLE);
092: result.put(oname, new Integer(
093: AMObject.FILTERED_ROLE));
094: }
095: }
096: } catch (SSOException ssoe) {
097: if (logger.isLoggable(Level.INFO)) {
098: logger.log(Level.INFO, "PSFB_CSPFM0001", ssoe);
099: }
100: throw new PSMBeanException("amObjectSearch.ssoTokenError");
101: } finally {
102: if (conn != null) {
103: // help garbage collect
104: conn = null;
105: }
106: }
107: return result;
108: }
109:
110: /**
111: * Query AM for object type given a DN
112: *
113: * @return An Integer object representing the AMObject
114: */
115: public Integer queryObjectType(String dn) throws PSMBeanException {
116: Integer result = null;
117: AMStoreConnection conn = null;
118: try {
119: SSOTokenManager manager = SSOTokenManager.getInstance();
120: SSOToken token = AdminServerUtil.getSSOToken();
121: if ((token != null) && (manager.isValidToken(token))) {
122: // open a store connection
123: conn = new AMStoreConnection(token);
124: int otype = conn.getAMObjectType(dn);
125: switch (otype) {
126: case AMObject.USER:
127: result = new Integer(0);
128: break;
129: case AMObject.ROLE:
130: result = new Integer(1);
131: break;
132: case AMObject.ORGANIZATION:
133: result = new Integer(2);
134: break;
135: case AMObject.FILTERED_ROLE:
136: result = new Integer(3);
137: break;
138: }
139: if (result == null) {
140: result = new Integer(4);
141: }
142: }
143: } catch (AMException ame) {
144: if (logger.isLoggable(Level.INFO)) {
145: logger.log(Level.INFO, "PSFB_CSPFM0002", ame);
146: }
147: throw new PSMBeanException("amObjectSearch.amException");
148: } catch (SSOException ssoe) {
149: if (logger.isLoggable(Level.INFO)) {
150: logger.log(Level.INFO, "PSFB_CSPFM0001", ssoe);
151: }
152: throw new PSMBeanException("amObjectSearch.ssoTokenError");
153: } finally {
154: if (conn != null) {
155: // help garbage collect
156: conn = null;
157: }
158: }
159: return result;
160: }
161:
162: protected int findScope(int jmxscope) {
163: // jmxscope 0,1,2 BASE,ONE,SUB
164: int result = AMConstants.SCOPE_BASE;
165: switch (jmxscope) {
166: case 1:
167: result = AMConstants.SCOPE_ONE;
168: break;
169: case 2:
170: result = AMConstants.SCOPE_SUB;
171: break;
172: }
173: return result;
174: }
175:
176: /**
177: * Query AM for default ORG DN for the current userDN .
178: *
179: * @return A String object which represents the default ORGDN
180: */
181: public String queryDefaultOrgDN() throws PSMBeanException {
182: return cc.getDefaultOrganization();
183: }
184:
185: /**
186: * Query AM for Root Suffix.
187: *
188: * @return A String object which represents the Root Suffix
189: */
190: public String queryRootSuffix() throws PSMBeanException {
191: return cc.getRootSuffix();
192: }
193:
194: protected Map setToMap(AMSearchResults results, String namingAttr) {
195: HashMap rs = new HashMap();
196: Map rmap = results.getResultAttributes();
197: Set keys = rmap.keySet();
198: Iterator i = keys.iterator();
199: while (i.hasNext()) {
200: Object key = i.next(); // key is the DN
201: Object values = rmap.get(key);
202: if (values instanceof Map) {
203: Map vmap = (Map) values;
204: Object obj = vmap.get(namingAttr);
205: if ((obj != null) && (obj instanceof Set)) {
206: Set vset = (Set) obj;
207: Iterator j = vset.iterator();
208: // we want only one value
209: synchronized (rs) {
210: rs.put(key, j.next());
211: }
212: }
213: }
214: }
215: return rs;
216: }
217:
218: public Map searchObjects(AMStoreConnection conn,
219: AMOrganization startOrg, int objType, int scope,
220: String filter) throws SSOException, AMException {
221: Map resultMap = null;
222: // prepare search control
223: AMSearchControl scontrol = new AMSearchControl();
224: HashSet wantattr = new HashSet();
225: String namingAttr = conn.getNamingAttribute(objType);
226: wantattr.add(namingAttr);
227: scontrol.setReturnAttributes(wantattr);
228: scontrol.setSearchScope(scope);
229: AMSearchResults results = null;
230:
231: switch (objType) {
232: case AMObject.ORGANIZATION:
233: results = startOrg.searchSubOrganizations(filter, scontrol);
234: if (results.getErrorCode() == AMSearchResults.SUCCESS) {
235: resultMap = setToMap(results, namingAttr);
236: if (!resultMap.containsKey(startOrg)) {
237: Set attrs = startOrg.getAttribute(namingAttr);
238: String attr = startOrg.getDN();
239: if (attrs != null && !attrs.isEmpty()) {
240: Iterator i = attrs.iterator();
241: attr = (String) i.next();
242: }
243: resultMap.put(startOrg.getDN(), attr);
244: }
245: }
246: break;
247:
248: case AMObject.ROLE:
249: results = startOrg.searchRoles(filter, scontrol);
250: if (results.getErrorCode() == AMSearchResults.SUCCESS) {
251: resultMap = setToMap(results, namingAttr);
252: }
253: break;
254:
255: case AMObject.USER:
256: Map m = new HashMap();
257: ;
258: if (scope == AMConstants.SCOPE_ONE) {
259: // try to navigate into the people container(s) and do a
260: // SCOPE_ONE search there
261: Set s = startOrg
262: .getPeopleContainers(AMConstants.SCOPE_ONE);
263: Iterator i = s.iterator();
264: while (i.hasNext()) {
265: String pcDN = (String) i.next();
266: if (conn.isValidEntry(pcDN)) {
267: AMPeopleContainer pc = conn
268: .getPeopleContainer(pcDN);
269: scontrol.setSearchScope(AMConstants.SCOPE_ONE);
270: results = pc.searchUsers(filter, scontrol);
271: if (results.getErrorCode() == AMSearchResults.SUCCESS) {
272: m.putAll(setToMap(results, namingAttr));
273: }
274: }
275: }
276: resultMap = m;
277: } else {
278: results = startOrg.searchUsers(filter, scontrol);
279: if (results.getErrorCode() == AMSearchResults.SUCCESS) {
280: resultMap = setToMap(results, namingAttr);
281: }
282: }
283: break;
284: case AMObject.FILTERED_ROLE:
285: results = startOrg.searchFilteredRoles(filter, scontrol);
286: if (results.getErrorCode() == AMSearchResults.SUCCESS) {
287: resultMap = setToMap(results, namingAttr);
288: }
289: break;
290: }
291:
292: return resultMap;
293: }
294:
295: /**
296: * Perform a search for a given AM object type using the filter within
297: * a given scope starting from a particular DN.
298: *
299: * @param baseDN the starting DN of the search
300: * @param filter the filter against the naming attribute of the object
301: * @param objType the object type of the search
302: * @param scope the scope of the search
303: * BASE(0), ONE(1) or SUB(2).
304: *
305: * @return a map of keys and values pair where keys are DNs of objects
306: * found and values are their short names.
307: *
308: */
309: public Map searchObjects(String baseDN, String filter,
310: Integer objType, Integer jmxscope) throws PSMBeanException {
311: Map result = null;
312: AMStoreConnection conn = null;
313: try {
314: SSOTokenManager manager = SSOTokenManager.getInstance();
315: SSOToken token = AdminServerUtil.getSSOToken();
316: java.security.Principal principal = token.getPrincipal();
317: if ((token != null) && (manager.isValidToken(token))) {
318: // open a store connection
319: conn = new AMStoreConnection(token);
320: AMOrganization startOrg = null;
321: if (conn.isValidEntry(baseDN)) {
322: startOrg = conn.getOrganization(baseDN);
323: }
324: // get the search scope
325: int scope = findScope(jmxscope.intValue());
326: // do the search
327: result = searchObjects(conn, startOrg, objType
328: .intValue(), scope, filter);
329: }
330: } catch (SSOException ssoe) {
331: if (logger.isLoggable(Level.INFO)) {
332: logger.log(Level.INFO, "PSFB_CSPFM0001", ssoe);
333: }
334: throw new PSMBeanException("amObjectSearch.ssoTokenError");
335: } catch (AMException ame) {
336: if (logger.isLoggable(Level.INFO)) {
337: logger.log(Level.INFO, "PSFB_CSPFM0002", ame);
338: }
339: throw new PSMBeanException("amObjectSearch.amException");
340: } finally {
341: if (conn != null) {
342: // help garbage collect
343: conn = null;
344: }
345: }
346: return result;
347: }
348:
349: /**
350: * Check if the DN passed in is a valid DN.
351: *
352: * @param dn String
353: * @return boolean
354: */
355: public boolean isValidDN(String dn) throws PSMBeanException {
356: boolean valid = false;
357: AMStoreConnection conn = null;
358: try {
359: SSOTokenManager manager = SSOTokenManager.getInstance();
360: SSOToken token = AdminServerUtil.getSSOToken();
361: java.security.Principal principal = token.getPrincipal();
362: if ((token != null) && (manager.isValidToken(token))) {
363: // open a store connection
364: conn = new AMStoreConnection(token);
365: valid = conn.isValidEntry(dn);
366: }
367: } catch (SSOException ssoe) {
368: if (logger.isLoggable(Level.INFO)) {
369: logger.log(Level.INFO, "PSFB_CSPFM0001", ssoe);
370: }
371: throw new PSMBeanException("amObjectSearch.ssoTokenError");
372: } finally {
373: if (conn != null) {
374: // help garbage collect
375: conn = null;
376: }
377: }
378: return valid;
379: }
380:
381: }
|