001: /**
002: * $Id: CMCFactory.java,v 1.2 2005/07/25 23:02:33 jtb 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;
014:
015: import com.sun.portal.community.mc.impl.CMCProperties;
016: import com.sun.portal.community.mc.impl.CMCUserManager;
017: import java.util.Iterator;
018:
019: /**
020: * Returns instances of <CODE>CommunityUser</CODE>
021: * and <CODE>CommunityNode</CODE> objects.
022: * <br><br>
023: * The community MC (membership and configuration)
024: * system is implemented using the
025: * <I>group of responsibility</I> (GoR) pattern. This is a variation on the
026: * <I>chain of responsibility</I> (CoR) pattern. CoR defines an ordered
027: * set of "handlers", each with the same interface.
028: * Clients have a reference to a head handler,
029: * and make calls into its interface.
030: * To answer question and perform operations,
031: * the handler can either return a response,
032: * or defer to the next handler (successor) to answer the question.
033: * Clients do not know which handler answered the question,
034: * only that it was answered.
035: * Applying the GoR pattern to this class,
036: * there is a set of unordered
037: * <B>contributors</B>, and one <B>manager</B>,
038: * all of which implement the same interface.
039: * This class returns references to the manager,
040: * which has references to the contributors. Clients call into the manager,
041: * and the manager delegates to one or more of the contributors to
042: * answer the question.
043: * Clients are unaware of the pattern.
044: * They only see the <CODE>CommunityNode</CODE> or
045: * <CODE>CommunityUser</CODE> interface.
046: * <br><br>
047: * This class is configured via a properties file,
048: * communitymc.properties. This file must
049: * exist on the classpath. communitymc.properties contains
050: * configuration for this factory,
051: * the manager implementation, and all contributor implementations.
052: * The properties are
053: * read from the classpath by this class, and passed to the manager,
054: * which passes it to
055: * each contributor upon initialization.
056: * <br><br>
057: * This class requires the communitymc.properties file to contain the
058: * property <CODE>manager.package</CODE>
059: * that defines the Java package name where the manager
060: * <CODE>CommunityUserImpl</CODE> and
061: * <CODE>CommunityNodeImpl</CODE> classes can be found. For example:
062: * <PRE>
063: * manager.package=com.sun.portal.community.impl.manager
064: * </PRE>
065: * @see com.sun.portal.community.mc.CommunityUser
066: * @see com.sun.portal.community.mc.CommunityNode
067: */
068: public class CMCFactory {
069: /**
070: * The relative class name that implements the <CODE>CommunityUser</CODE> interface.
071: *
072: * Community C+M contributor implementations must include a class with this relative name under the package name of
073: * the contributor specified in the community properties file. For example, for a contributor named <I>foo</I>,
074: * the community properties file must specify a property <CODE>foo.package</CODE> that
075: * indicates the package name of the
076: * class <CODE>CommunityUserImpl</CODE> that implements the foo community user contributor, as such:
077: * <PRE>foo.package=some.pkg</PRE>
078: * Accordingly, the Java class <CODE>some.pkg.CommunityUserImpl</CODE> must be present and
079: * implement the <CODE>CommunityUser</CODE> interface.
080: */
081: public static final String USER_CLASS_NAME = "CMCUserImpl";
082:
083: /**
084: * The relative class name that implements the <CODE>CommunityNode</CODE> interface.
085: *
086: * Community C+M contributor implementations must include a class with this name under the package name of
087: * the contributor specified in the community properties file. For example, for a contributor named <I>foo</I>,
088: * the community properties file must specify a property <CODE>foo.package</CODE> that
089: * indicates the package name of the
090: * class <CODE>CommunityNodeImpl</CODE> that implements the foo community node contributor, as such:
091: * <PRE>foo.package=some.pkg</PRE>
092: * Accordingly, the Java class <CODE>some.pkg.CommunityNodeImpl</CODE> must be present and
093: * implement the <CODE>CommunityNode</CODE> interface.
094: */
095: public static final String NODE_CLASS_NAME = "CMCNodeImpl";
096:
097: /**
098: * The relative class name that implements the
099: * <CODE>CommunityNodeManager</CODE> interface.
100: *
101: * Community MC contributor implementations must include a class with
102: * this name under the package name of
103: * the contributor specified in the community properties file.
104: * For example, for a contributor named <I>foo</I>,
105: * the community properties file must specify a property
106: * <CODE>foo.package</CODE> that
107: * indicates the package name of the
108: * class <CODE>CommunityNodeManagerImpl</CODE> that implements the foo
109: * community node contributor, as such:
110: * <PRE>foo.package=some.pkg</PRE>
111: * Accordingly, the Java class
112: * <CODE>some.pkg.CommunityNodeManagerImpl</CODE> must be present and
113: * implement the <CODE>CommunityNodeManager</CODE> interface.
114: */
115: public static final String NODE_MANAGER_CLASS_NAME = "CMCNodeManagerImpl";
116:
117: private static CMCFactory factory;
118:
119: static {
120: try {
121: factory = new CMCFactory();
122: } catch (CMCException ce) {
123: throw new Error(ce);
124: }
125: }
126:
127: private CMCFactory() throws CMCException {
128: // nothing
129: }
130:
131: /**
132: * Get a community factory instance.
133: *
134: * CommunityFactory is a singleton. Instances of this class may only be obtained through this method.
135: * @throws com.sun.portal.community.mc.CommunityException If there is a problem obtaining the factory instance.
136: * @return the factory object.
137: */
138: public static CMCFactory getInstance() throws CMCException {
139: return factory;
140: }
141:
142: /**
143: * Get a <CODE>CommunityNode</CODE> object.
144: * <br><br>
145: * This method never returns null. The return value of this method has no
146: * bearing on whether the community node exists persistently. To verify
147: * existence, clients should call <CODE>exists()</CODE> on the
148: * <CODE>CommunityNode</CODE> after obtaining
149: * a refernece via this method. Community nodes are created by calling
150: * <CODE>create()</CODE> on the <CODE>CommunityNode</CODE>.
151: * Community nodes are removed by
152: * calling <CODE>remove()</CODE> on the <CODE>CommunityNode</CODE>.
153: * @return A <CODE>CommunityNode</CODE> object corresponding to the <CODE>CommunityPrincipal</CODE> parameter.
154: * @param communityPrincipal The <CODE>CommunityPrincipal</CODE> that identifies the <CODE>CommunityNode</CODE>
155: * @throws com.sun.portal.community.mc.CommunityException If there was a problem obtaining the community node object for the given community principal parameter
156: * @see com.sun.portal.community.mc.CommunityNode#create()
157: */
158: public CMCNode getCMCNode(CMCPrincipal communityPrincipal)
159: throws CMCException {
160: Class nodeClass = CMCProperties.getNodeClass("manager");
161: if (nodeClass == null) {
162: throw new CMCException("node class not found for manager");
163: }
164: CMCNode node = (CMCNode) getObject(nodeClass);
165:
166: node.init(CMCProperties.getProperties(), communityPrincipal);
167:
168: return node;
169: }
170:
171: /**
172: * Get a <CODE>CommunityNodeManager</CODE> object.
173:
174: * @return A <CODE>CommunityNodeManager</CODE> object.
175: * @throws com.sun.portal.community.mc.CMCException If there was a problem
176: * obtaining the node manager object.
177: */
178: public CMCNodeManager getCMCNodeManager() throws CMCException {
179: Class nodeManagerClass = CMCProperties
180: .getNodeManagerClass("manager");
181: if (nodeManagerClass == null) {
182: throw new CMCException(
183: "node manager class not found for manager");
184: }
185: CMCNodeManager nodeMgr = (CMCNodeManager) getObject(nodeManagerClass);
186:
187: nodeMgr.init(CMCProperties.getProperties());
188:
189: return nodeMgr;
190: }
191:
192: /**
193: * Get a <CODE>CommunityUser</CODE> object.
194: * <p>
195: * The resulting <CODE>CommunityUser</CODE> manager
196: * object will load all configured <CODE>CommunityUser</CODE>
197: * contributor types.
198: * @param userId The user identifier that identified the <CODE>CommunityUser</CODE> object
199: * @return A <CODE>CommunityUser</CODE> object corresponding to the given user ID parameter.
200: * @throws com.sun.portal.community.mc.CommunityException If there was a problem obtaing the <CODE>CommunityUser</CODE> object
201: */
202: public CMCUser getCMCUser(String userId) throws CMCException {
203: Class userClass = CMCProperties.getUserClass("manager");
204: if (userClass == null) {
205: throw new CMCException("user class not found for manager");
206: }
207: CMCUserManager user = (CMCUserManager) getObject(userClass);
208:
209: user.init(CMCProperties.getProperties(), userId);
210:
211: return user;
212: }
213:
214: /**
215: * Get a <CODE>CommunityUser</CODE> object.
216: * <p>
217: * The resulting <CODE>CommunityUser</CODE> manager
218: * object will load only the named <CODE>CommunityUser</CODE>
219: * contributor types. The named contributors must be a subset
220: * of the configured contributors.
221: * @return A <CODE>CommunityUser</CODE> object corresponding to the given user ID parameter.
222: * @param types A <CODE>Set</CODE> of <CODE>Strings</CODE>, the contributor types to load.
223: * @param userId The user identifier that identified the <CODE>CommunityUser</CODE> object
224: * @throws com.sun.portal.community.mc.CommunityException If there was a problem obtaing the <CODE>CommunityUser</CODE> object
225: */
226: public CMCUser getCMCUser(String userId, Iterator types)
227: throws CMCException {
228: Class userClass = CMCProperties.getUserClass("manager");
229: if (userClass == null) {
230: throw new CMCException("user class not found for manager");
231: }
232: CMCUserManager user = (CMCUserManager) getObject(userClass);
233:
234: user.init(CMCProperties.getProperties(), userId, types);
235:
236: return user;
237: }
238:
239: /**
240: * Get an object instance based on the given class name.
241: *
242: * This is a convenience wrapper to avoid have multiple blocks
243: * that catch the varioues exceptions and errors than can
244: * result from this process.
245: */
246: private Object getObject(Class c) throws CMCException {
247: Object o = null;
248:
249: try {
250: o = c.newInstance();
251: } catch (NoClassDefFoundError ncdfe) {
252: throw new CMCException(ncdfe);
253: } catch (IllegalAccessException iae) {
254: throw new CMCException(iae);
255: } catch (ClassCastException cce) {
256: throw new CMCException(cce);
257: } catch (InstantiationException ie) {
258: throw new CMCException(ie);
259: }
260:
261: return o;
262: }
263: }
|