001: /**
002: * $Id: CMCImplBase.java,v 1.3 2005/08/23 04:47:42 as133206 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.community.mc.impl.am;
014:
015: import com.iplanet.am.sdk.AMException;
016: import com.iplanet.am.sdk.AMFilteredRole;
017: import com.iplanet.am.sdk.AMObject;
018: import com.iplanet.am.sdk.AMOrganization;
019: import com.iplanet.am.sdk.AMRole;
020: import com.iplanet.am.sdk.AMStoreConnection;
021: import com.iplanet.am.sdk.AMTemplate;
022: import com.iplanet.sso.SSOException;
023: import com.iplanet.sso.SSOToken;
024: import java.security.AccessController;
025: import com.sun.identity.security.AdminTokenAction;
026: import com.iplanet.sso.SSOTokenManager;
027: import com.sun.identity.sm.SMSException;
028: import com.sun.identity.sm.ServiceSchema;
029: import com.sun.identity.sm.ServiceSchemaManager;
030: import com.sun.portal.community.mc.CMCException;
031: import com.sun.portal.community.mc.CMCPrincipal;
032: import java.util.Iterator;
033: import java.util.Map;
034: import java.util.Set;
035: import java.util.HashSet;
036: import com.sun.portal.util.ResourceLoader;
037:
038: /**
039: * AM contributor base class, base class
040: *
041: * This class is extended by the AM contributor base classes.
042: */
043: public abstract class CMCImplBase {
044: /**
045: * Community name for AM "global" community
046: */
047: public static final String GLOBAL_NAME = "!global!";
048:
049: //
050: // TBD: retrieve w/ approved method
051: // current plan is that portal ID will be set as
052: // a system property. this should be available on the trunk
053: //
054: private static final String portalId = ResourceLoader.getInstance(
055: System.getProperties()).getPortalId();
056:
057: private static final SSOTokenManager tokenManager;
058: protected static final AMStoreConnection adminConnection;
059: private static final ServiceSchema schema;
060: private static final ServiceSchemaManager schemaManager;
061:
062: //
063: // used to access aM attributes in a portal-specific manner
064: //
065: protected static final DSAMEMultiPortalConstants dmpc;
066:
067: //
068: // initialize AM connections
069: // these are static and shared between all object instances
070: //
071: static {
072: //Debug.log("CommunityImplBase", "<static init>", "called");
073: if (portalId == null) {
074: throw new Error("portal ID was null");
075: }
076:
077: DSAMEMultiPortalConstants.createInstance(portalId);
078: dmpc = DSAMEMultiPortalConstants.getInstance();
079:
080: try {
081: tokenManager = SSOTokenManager.getInstance();
082:
083: //
084: // get the admin connection
085: // this impl uses the AM admin connection, always
086: // this is necessary because we are accessing attributes from
087: // roles and orgs. a user token does not allow use to do this
088: //
089: SSOToken ssot = (SSOToken) AccessController
090: .doPrivileged(AdminTokenAction.getInstance());
091: adminConnection = new AMStoreConnection(ssot);
092:
093: //
094: // prepare to access global (schema) attributes
095: // to read "global" DP
096: //
097: schemaManager = new ServiceSchemaManager(
098: dmpc.MP_SUN_DESKTOP_SERVICE, ssot);
099: schema = schemaManager.getGlobalSchema();
100: } catch (SSOException ssoe) {
101: throw new Error("error in static initializer", ssoe);
102: } catch (SMSException smse) {
103: throw new Error("error in static initializer", smse);
104: }
105:
106: //Debug.log("CommunityImplBase", "<static init>", "returned");
107: }
108:
109: public abstract String getType();
110:
111: protected int getAMObjectType(String dn) throws SSOException,
112: AMException {
113: int objType = adminConnection.getAMObjectType(dn);
114: return objType;
115: }
116:
117: protected String getGlobalStringAttribute(String attributeName) {
118: Set vals = getGlobalAttribute(attributeName);
119: if (vals == null || vals.size() < 1) {
120: return null;
121: }
122:
123: Iterator iter = vals.iterator();
124: String val = (String) iter.next();
125:
126: return val;
127: }
128:
129: private Set getGlobalAttribute(String attributeName) {
130: Map attrs = getGlobalAttributes();
131: Set vals = (Set) attrs.get(attributeName);
132: return vals;
133: }
134:
135: private Map getGlobalAttributes() {
136: Map attrs = null;
137: attrs = schema.getAttributeDefaults();
138:
139: return attrs;
140: }
141:
142: protected boolean isValidEntry(String dn) throws CMCException {
143: try {
144: if (!adminConnection.isValidEntry(dn)) {
145: return false;
146: }
147:
148: return true;
149: } catch (SSOException ssoe) {
150: throw new CMCException(ssoe);
151: }
152: }
153:
154: private Set getTemplateAttribute(String dn, String serviceName,
155: String attributeName) throws AMException, SSOException,
156: CMCException {
157: Set vals = null;
158:
159: AMTemplate temp = getTemplate(dn, serviceName);
160: if (temp == null) {
161: // this is okay, just means that the template did not exist
162: // at the node
163: return null;
164: } else {
165: vals = temp.getAttribute(attributeName);
166: }
167:
168: return vals;
169: }
170:
171: private byte[][] getTemplateAttributeByteArray(String dn,
172: String serviceName, String attributeName)
173: throws AMException, SSOException, CMCException {
174: byte[][] vals = null;
175:
176: AMTemplate temp = getTemplate(dn, serviceName);
177: if (temp == null) {
178: // this is okay, just means that the template did not exist
179: // at the node
180: return null;
181: } else {
182: vals = temp.getAttributeByteArray(attributeName);
183: }
184:
185: return vals;
186: }
187:
188: protected String getTemplateStringAttribute(String dn,
189: String serviceName, String attributeName)
190: throws AMException, SSOException, CMCException {
191: Set vals = getTemplateAttribute(dn, serviceName, attributeName);
192: if (vals == null || vals.size() == 0) {
193: return null;
194: }
195: return (String) vals.iterator().next();
196: }
197:
198: protected byte[] getTemplateStringAttributeByteArray(String dn,
199: String serviceName, String attributeName)
200: throws AMException, SSOException, CMCException {
201: byte[][] vals = getTemplateAttributeByteArray(dn, serviceName,
202: attributeName);
203: if (vals == null || vals.length == 0) {
204: return null;
205: }
206: return vals[0];
207: }
208:
209: private AMTemplate getTemplate(String dn, String serviceName)
210: throws AMException, SSOException, CMCException {
211: int objType = getAMObjectType(dn);
212: AMTemplate temp = null;
213:
214: switch (objType) {
215: case AMObject.ORGANIZATION:
216: AMOrganization org = adminConnection.getOrganization(dn);
217: temp = org.getTemplate(serviceName,
218: AMTemplate.DYNAMIC_TEMPLATE);
219: break;
220:
221: case AMObject.ROLE:
222: AMRole role = adminConnection.getRole(dn);
223: temp = role.getTemplate(serviceName,
224: AMTemplate.DYNAMIC_TEMPLATE);
225: break;
226:
227: case AMObject.FILTERED_ROLE:
228: AMFilteredRole frole = adminConnection.getFilteredRole(dn);
229: temp = frole.getTemplate(serviceName,
230: AMTemplate.DYNAMIC_TEMPLATE);
231: break;
232:
233: default:
234: throw new CMCException("unhandled AM object type for DN: "
235: + dn);
236: }
237:
238: if (!temp.isExists()) {
239: temp = null;
240: }
241:
242: return temp;
243: }
244:
245: protected Set filterPrincipals(Set principals) {
246: Set filtered = new HashSet();
247: for (Iterator i = principals.iterator(); i.hasNext();) {
248: CMCPrincipal cmcp = (CMCPrincipal) i.next();
249: if (!cmcp.getType().equals(getType())) {
250: continue;
251: }
252: filtered.add(cmcp);
253: }
254:
255: return filtered;
256: }
257: }
|