001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013:
014: package com.sun.portal.subscriptions.context;
015:
016: import java.util.Map;
017: import java.util.HashMap;
018: import com.sun.portal.admin.common.util.AdminUtil;
019:
020: public class DSAMEMultiPortalConstants implements DSAMEConstants {
021:
022: // Attribute Prefix
023:
024: public String MP_ATTR_PREFIX = ATTR_PREFIX;
025:
026: //
027: // Service Names
028: //
029:
030: public String MP_SUN_SUBSCRIPTION_SERVICE = "SunPortalSubscriptionsService";
031:
032: //
033: // dynamic Attributes
034: //
035:
036: public String MP_ATTR_MAX_CATEGORY_SUBSCRIPTIONS = ATTR_PREFIX
037: + "MaxCategorySubscriptions";
038:
039: public String MP_ATTR_MAX_DISCUSSION_SUBSCRIPTIONS = ATTR_PREFIX
040: + "MaxDiscussionSubscriptions";
041:
042: public String MP_ATTR_MAX_SAVED_SEARCH = ATTR_PREFIX
043: + "MaxSavedSearch";
044:
045: //
046: // User Attributes
047: //
048:
049: public String MP_ATTR_CATEGORY_SUBSCRIPTIONS = ATTR_PREFIX
050: + "CategorySubscriptions";
051:
052: public String MP_ATTR_DISCUSSION_SUBSCRIPTIONS = ATTR_PREFIX
053: + "DiscussionSubscriptions";
054:
055: public String MP_ATTR_SAVED_SEARCH = ATTR_PREFIX + "SavedSearch";
056:
057: public String MP_ATTR_PROFILER_LAST_RUN = ATTR_PREFIX
058: + "ProfilerLastRun";
059:
060: public String MP_ATTR_PROFILER_ENABLED = ATTR_PREFIX
061: + "ProfilerEnabled";
062:
063: /*
064: * HashMap permits null values and the null key.
065: */
066: private static Map instances = new HashMap(2);
067:
068: /*
069: * Always holds the id of the first instance created.
070: * This is used only during the getInstance with no args.
071: * The only use this has, is to avoid the cost of getting the
072: * key set from the map and then iterating thru the set to
073: * get the poralId when only one entry exists.
074: */
075: private static String loneId = null;
076:
077: /*
078: * DSAMEMultiPortalConstants
079: */
080: private DSAMEMultiPortalConstants(String id) {
081: if (id != null && id.length() != 0) {
082: id = id.trim();
083: // Every portal MUST have a valid portal id
084: validateId(id);
085: // In case of an upgraded portal, the service and attrs do
086: // not have a portal id in their names
087: if (!id.equals(AdminUtil.UPGRADED_PORTAL)) {
088: MP_SUN_SUBSCRIPTION_SERVICE = "SunPortal" + id
089: + "SubscriptionsService";
090: MP_ATTR_PREFIX = "sunPortal" + id;
091: MP_ATTR_MAX_CATEGORY_SUBSCRIPTIONS = MP_ATTR_PREFIX
092: + "MaxCategorySubscriptions";
093: MP_ATTR_MAX_DISCUSSION_SUBSCRIPTIONS = MP_ATTR_PREFIX
094: + "MaxDiscussionSubscriptions";
095: MP_ATTR_MAX_SAVED_SEARCH = MP_ATTR_PREFIX
096: + "MaxSavedSearch";
097: MP_ATTR_CATEGORY_SUBSCRIPTIONS = MP_ATTR_PREFIX
098: + "CategorySubscriptions";
099: MP_ATTR_DISCUSSION_SUBSCRIPTIONS = MP_ATTR_PREFIX
100: + "DiscussionSubscriptions";
101: MP_ATTR_SAVED_SEARCH = MP_ATTR_PREFIX + "SavedSearch";
102: MP_ATTR_PROFILER_LAST_RUN = MP_ATTR_PREFIX
103: + "ProfilerLastRun";
104: MP_ATTR_PROFILER_ENABLED = MP_ATTR_PREFIX
105: + "ProfilerEnabled";
106: }
107: }
108: }
109:
110: /*
111: * createInstance
112: */
113: public static synchronized void createInstance(String id) {
114: if (!instances.containsKey(id)) {
115: instances.put(id, new DSAMEMultiPortalConstants(id));
116: if (instances.size() == 1) {
117: loneId = id;
118: }
119: }
120: }
121:
122: /*
123: * For the subscription code, there is always only one instance of
124: * this class since each portal runs in its own JVM. This method
125: * returns such instance which has been created using createInstance.
126: * It throws a ContextError if no instance exists or if more than one
127: * instances exist. So ideally, there will be only one instance in
128: * existance if this class is being properly used in the desktop
129: * code.
130: */
131: public static DSAMEMultiPortalConstants getInstance() {
132: int n = instances.size();
133: if (n == 0) {
134: throw new ContextError("Instance does not exist");
135: } else if (n > 1) {
136: throw new ContextError("Invalid use of getInstance: "
137: + " More than one instance exists");
138: }
139: return (DSAMEMultiPortalConstants) instances.get(loneId);
140: }
141:
142: /*
143: * The subscription code executes in VM in which there is only one
144: * portal. Thus there is a single instance of this
145: * class which gets initialized at the servlet init time.
146: * Once initialized, this never changes during the lifetime of that VM.
147: * But in case of admin mbeans, the situation is different. Different
148: * mbeans belonging to different portals run in the same JVM of the cacao
149: * server. Each mbean knows which portal it belongs to and hence can
150: * request an instance of this class initialized for a specific portal.
151: * So this method is used by the mbeans to get a specific instance. The rule
152: * to use this method still remains same, that is, one MUST first create
153: * an instance before trying to get it, otherwise a ContextError is
154: * thrown.
155: */
156: public static DSAMEMultiPortalConstants getInstance(String id) {
157: if (!instances.containsKey(id)) {
158: throw new ContextError("Instance does not exist");
159: }
160: return (DSAMEMultiPortalConstants) instances.get(id);
161: }
162:
163: /*
164: * portalId may contain only letters, digits and hyphen.
165: *
166: * @throws ContextError if the identifier is invalid
167: */
168: private void validateId(String id) {
169: if (id.matches(".*[^a-zA-Z0-9-].*")) {
170: throw new ContextError("Invalid portal identifier");
171: }
172: }
173:
174: }
|