001: package com.sun.portal.community.mc.impl;
002:
003: import java.util.Properties;
004: import java.util.Set;
005: import java.util.HashSet;
006: import java.util.StringTokenizer;
007:
008: import java.io.InputStream;
009: import java.io.FileInputStream;
010: import java.io.File;
011: import java.io.FileNotFoundException;
012: import java.io.IOException;
013: import com.sun.portal.community.mc.*;
014: import com.sun.portal.util.ResourceLoader;
015: import java.util.Map;
016: import java.util.HashMap;
017: import java.util.Iterator;
018:
019: /**
020: * Utility class that wraps a Java <CODE>Properties</CODE> object and provides
021: * community MC related convenience methods.
022: */
023: public abstract class CMCProperties {
024: /**
025: * Factory class for contributor Class objects.
026: *
027: * This class pre-loads all CMCUser and CMCNode
028: * implementation Class objects based on the package names
029: * defined in the community MC properties file. This avoids
030: * calling Class.forName() for each new CU and CN object,
031: * for each contributor and also the manager class.
032: */
033: private abstract static class ClassFactory {
034: protected Map classes = new HashMap();
035:
036: public ClassFactory(Properties p, Set types)
037: throws CMCException {
038: init(p, types);
039: }
040:
041: /**
042: * Return the correct class name based on the package name.
043: */
044: protected abstract String getClassName(String pkgName);
045:
046: protected void init(Properties p, Set types)
047: throws CMCException {
048: //
049: // scan properties looking for keys that end in .package
050: // these define the contributor's package name. use the
051: // pkg name to build a class name, and then a Class
052: // object
053: //
054: for (Iterator i = types.iterator(); i.hasNext();) {
055: String type = (String) i.next();
056: String pkgKey = type + ".package";
057: String pkg = p.getProperty(pkgKey);
058: if (pkg == null) {
059: throw new CMCException(
060: "package name is null for properties type: "
061: + type);
062: }
063:
064: String typeClassName = getClassName(pkg);
065: Class c;
066:
067: try {
068: c = Class.forName(typeClassName);
069: } catch (ClassNotFoundException cnfe) {
070: throw new CMCException(cnfe);
071: } catch (NoClassDefFoundError ncdfe) {
072: throw new CMCException(ncdfe);
073: } catch (ClassCastException cce) {
074: throw new CMCException(cce);
075: }
076:
077: classes.put(type, c);
078: }
079: }
080:
081: public Class getClass(String type) {
082: return (Class) classes.get(type);
083: }
084: }
085:
086: /**
087: * Factory class for contributor user Class objects.
088: */
089: private static class CMCUserClassFactory extends ClassFactory {
090: CMCUserClassFactory(Properties p, Set types)
091: throws CMCException {
092: super (p, types);
093: }
094:
095: protected String getClassName(String pkg) {
096: return pkg + "." + CMCFactory.USER_CLASS_NAME;
097: }
098: }
099:
100: /**
101: * Factory class for contributor node Class objects.
102: */
103: private static class CMCNodeClassFactory extends ClassFactory {
104: CMCNodeClassFactory(Properties p, Set types)
105: throws CMCException {
106: super (p, types);
107: }
108:
109: protected String getClassName(String pkg) {
110: return pkg + "." + CMCFactory.NODE_CLASS_NAME;
111: }
112: }
113:
114: /**
115: * Factory class for contributor node Class objects.
116: */
117: private static class CMCNodeManagerClassFactory extends
118: ClassFactory {
119: CMCNodeManagerClassFactory(Properties p, Set types)
120: throws CMCException {
121: super (p, types);
122: }
123:
124: protected String getClassName(String pkg) {
125: return pkg + "." + CMCFactory.NODE_MANAGER_CLASS_NAME;
126: }
127: }
128:
129: private static final String COMMUNITYMC_PROPERTIES = "communitymc.properties";
130: private static Properties properties;
131: private static ClassFactory userClassFactory;
132: private static ClassFactory nodeClassFactory;
133: private static ClassFactory nodeManagerClassFactory;
134: private static Set contributors;
135:
136: static {
137: try {
138: loadProperties();
139: initContributorNames();
140:
141: // pre-load Class objects
142:
143: // add manager type, even though it is not
144: // officially a contributor
145: Set types = new HashSet(contributors);
146: types.add("manager");
147:
148: userClassFactory = new CMCUserClassFactory(properties,
149: types);
150: nodeClassFactory = new CMCNodeClassFactory(properties,
151: types);
152: nodeManagerClassFactory = new CMCNodeManagerClassFactory(
153: properties, types);
154: } catch (CMCException ce) {
155: throw new Error(ce);
156: }
157: }
158:
159: /**
160: * Property key for the community MC contributor types.
161: */
162: public static final String CONTRIBUTOR_KEY = "manager.contributors";
163:
164: private CMCProperties() throws CMCException {
165: }
166:
167: private static void loadProperties() throws CMCException {
168: try {
169: properties = ResourceLoader.getInstance(
170: System.getProperties()).getProperties(
171: COMMUNITYMC_PROPERTIES);
172: } catch (FileNotFoundException fnfe) {
173: throw new CMCException(fnfe);
174: } catch (IOException ioe) {
175: throw new CMCException(ioe);
176: }
177:
178: }
179:
180: /**
181: * Get the original Java <CODE>Properties</CODE> object.
182: */
183: public static Properties getProperties() {
184: return properties;
185: }
186:
187: /**
188: * Get the contributor types.
189: */
190: public static Iterator getContributorNames() {
191: return contributors.iterator();
192: }
193:
194: private static void initContributorNames() {
195: String ts = properties.getProperty(CONTRIBUTOR_KEY);
196: StringTokenizer tokens = new StringTokenizer(ts, "|");
197: contributors = new HashSet();
198: while (tokens.hasMoreTokens()) {
199: contributors.add(tokens.nextToken());
200: }
201: }
202:
203: /**
204: * Get the Java class name that implements the CMCUser
205: * interface, for the given type.
206: */
207: public static Class getUserClass(String type) {
208: return userClassFactory.getClass(type);
209: }
210:
211: /**
212: * Get the Java class name that implements the CMCNode
213: * interface, for the given type.
214: */
215: public static Class getNodeClass(String type) {
216: return nodeClassFactory.getClass(type);
217: }
218:
219: /**
220: * Get the Java class name that implements the CMCNodeManager
221: * interface, for the given type.
222: */
223: public static Class getNodeManagerClass(String type) {
224: return nodeManagerClassFactory.getClass(type);
225: }
226:
227: /**
228: * Get the type-specific property value for the given type
229: * and key.
230: */
231: public static String getTypeProperty(String type, String key) {
232: return properties.getProperty(type + "." + key);
233: }
234: }
|