001: package com.sun.portal.community.mc.impl.manager;
002:
003: import java.util.Properties;
004: import java.util.Map;
005: import java.util.HashMap;
006: import java.util.Set;
007: import java.util.HashSet;
008: import java.util.Iterator;
009: import com.sun.portal.community.mc.impl.CMCProperties;
010: import com.sun.portal.community.mc.CMCException;
011: import com.sun.portal.community.mc.CMCPrincipal;
012: import com.sun.portal.community.mc.CMCNodeManager;
013: import java.util.regex.Pattern;
014:
015: public class CMCNodeManagerImpl extends CMCImpl implements
016: CMCNodeManager {
017: private Map contributors = new HashMap();
018: private String userId = null;
019:
020: public void init(Properties p) throws CMCException {
021: init(p, CMCProperties.getContributorNames());
022: }
023:
024: public void init(Properties p, Iterator types) throws CMCException {
025: loadContributors(types);
026: }
027:
028: protected Class getClass(String type) {
029: return CMCProperties.getNodeManagerClass(type);
030: }
031:
032: protected void initContributor(Object c) throws CMCException {
033: CMCNodeManager nm = (CMCNodeManager) c;
034: nm.init(CMCProperties.getProperties());
035: }
036:
037: private CMCNodeManager getNodeManagerContributor(String type)
038: throws CMCException {
039: CMCNodeManager nm = (CMCNodeManager) contributors.get(type);
040: if (nm == null) {
041: throw new CMCException("contributor not found for type: "
042: + type);
043: }
044:
045: return nm;
046: }
047:
048: /**
049: * Load and initialize all contributors.
050: */
051: private void loadContributors(Iterator types) throws CMCException {
052: while (types.hasNext()) {
053: String name = (String) types.next();
054: CMCNodeManager nm = (CMCNodeManager) createContributor(name);
055: contributors.put(name, nm);
056: }
057: }
058:
059: private CMCNodeManager getContributor(String type) {
060: return (CMCNodeManager) contributors.get(type);
061: }
062:
063: public Map getUserCounts(Set communityPrincipals)
064: throws CMCException {
065: Map counts = new HashMap();
066:
067: // Iterate over all contributor types, group communities by their type and
068: // call the getUserCounts once for each type.
069: for (Iterator typeIter = contributors.keySet().iterator(); typeIter
070: .hasNext();) {
071: String type = (String) typeIter.next();
072: CMCNodeManager nm = getContributor(type);
073:
074: // collect all communityprincipals of this type
075: Set sameTypeCommunityPrinicpals = new HashSet();
076: for (Iterator i = communityPrincipals.iterator(); i
077: .hasNext();) {
078: CMCPrincipal cmcp = (CMCPrincipal) i.next();
079: if (cmcp.getType().equals(type)) {
080: sameTypeCommunityPrinicpals.add(cmcp);
081: }
082: }
083: // get the user count for community principal of same type
084: // and add them in the total.
085: if (sameTypeCommunityPrinicpals.size() > 0) {
086: counts.putAll(nm
087: .getUserCounts(sameTypeCommunityPrinicpals));
088: }
089: }
090:
091: return counts;
092: }
093:
094: public Map getRoleUserCounts(Set communityPrincipals,
095: Set rolePrincipals) throws CMCException {
096: Map counts = new HashMap();
097: for (Iterator i = communityPrincipals.iterator(); i.hasNext();) {
098: CMCPrincipal cmcp = (CMCPrincipal) i.next();
099: CMCNodeManager nm = getContributor(cmcp.getType());
100: counts.putAll(nm.getRoleUserCounts(communityPrincipals,
101: rolePrincipals));
102: }
103:
104: return counts;
105: }
106:
107: public Set match(Pattern p, boolean nameOnly) throws CMCException {
108: Set principals = new HashSet();
109: for (Iterator typeIter = contributors.keySet().iterator(); typeIter
110: .hasNext();) {
111: String type = (String) typeIter.next();
112: CMCNodeManager nm = getContributor(type);
113: Set typedPrincipals = nm.match(p, nameOnly);
114: principals.addAll(typedPrincipals);
115: }
116:
117: return principals;
118: }
119: }
|