001: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.groups;
007:
008: import java.util.ArrayList;
009: import java.util.HashMap;
010: import java.util.List;
011: import java.util.Map;
012:
013: import javax.xml.parsers.SAXParserFactory;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.jasig.portal.utils.ResourceLoader;
018: import org.xml.sax.Attributes;
019: import org.xml.sax.InputSource;
020: import org.xml.sax.XMLReader;
021:
022: /**
023: * Parses service descriptions found in the file found at SERVICES_XML. The
024: * elements of each service are stored in a ComponentGroupServiceDescriptor.
025: *
026: * @author Dan Ellentuck
027: * @version $Revision: 34758 $
028: */
029: public class GroupServiceConfiguration {
030: private static final Log log = LogFactory
031: .getLog(GroupServiceConfiguration.class);
032:
033: // The file containing the configuration:
034: private static String SERVICES_XML = "/properties/groups/compositeGroupServices.xml";
035:
036: // Singleton instance.
037: private static GroupServiceConfiguration configuration;
038:
039: private GroupConfigurationHandler serviceHandler;
040: private List serviceDescriptors = new ArrayList();
041: private Map attributes = new HashMap();
042:
043: // Handler for parsing the xml source.
044: class GroupConfigurationHandler extends
045: org.xml.sax.helpers.DefaultHandler {
046: ComponentGroupServiceDescriptor svcDescriptor;
047: String elementName;
048: StringBuffer elementValue;
049:
050: public void startElement(String namespaceURI, String localName,
051: String qName, Attributes atts) {
052: elementName = qName;
053: elementValue = new StringBuffer();
054:
055: if (qName.equals("servicelist")) {
056: infoMessage("Parsing group service configuration.");
057: parseAttributes(atts);
058: } else if (qName.equals("service")) {
059: debugMessage("Parsing configuration for component service.");
060: svcDescriptor = new ComponentGroupServiceDescriptor();
061: for (int i = 0; i < atts.getLength(); i++) {
062: String name = atts.getQName(i);
063: String value = atts.getValue(i);
064: svcDescriptor.put(name, value);
065: }
066: }
067: }
068:
069: public void endElement(String namespaceURI, String localName,
070: String qName) {
071: String val = elementValue.toString();
072: if (qName.equals("service")) {
073: serviceDescriptors.add(svcDescriptor);
074: debugMessage("Parsed configuration for "
075: + svcDescriptor.getName());
076: } else if (qName.equals("servicelist")) {
077: debugMessage("Done parsing group service configuration.");
078: } else if (qName.equals("internally_managed")) {
079: svcDescriptor.setInternallyManaged("TRUE"
080: .equalsIgnoreCase(val));
081: } else if (qName.equals("caching_enabled")) {
082: svcDescriptor.setCachingEnabled("TRUE"
083: .equalsIgnoreCase(val));
084: } else {
085: svcDescriptor.setAttribute(elementName, val);
086: }
087: }
088:
089: public void characters(char ch[], int start, int length) {
090: if (elementName == null || elementName.equals("service")
091: || elementName.equals("servicelist"))
092: return;
093: String chValue = new String(ch, start, length);
094: elementValue.append(chValue);
095: }
096: }
097:
098: public GroupServiceConfiguration() {
099: super ();
100: serviceHandler = new GroupConfigurationHandler();
101: }
102:
103: /**
104: * Record a message at "Debug" level.
105: * @deprecated see comment inside method
106: */
107: protected void debugMessage(String msg) {
108: /*
109: * This method is not in keeping with best practices.
110: * Prepending "Group services:" to the message is unnecessary because
111: * Commons Logging is capable of reporting from where the logging message
112: * originated when configured to do so. The org.jasig.portal.groups package
113: * in its entirety can be mapped to its own logger.
114: * Logging directly allows instutions wishing to take the performance hit of
115: * doing so to configure log4j to report from exactly what line of code
116: * each logging message originates. Going through this method causes all
117: * such messages to appear to come from here rather than from whereever
118: * this method is being invoked from.
119: */
120: log.debug("Group services: " + msg);
121: }
122:
123: /**
124: *
125: */
126: public Map getAttributes() {
127: return attributes;
128: }
129:
130: public static synchronized GroupServiceConfiguration getConfiguration()
131: throws Exception {
132: if (configuration == null) {
133: configuration = new GroupServiceConfiguration();
134: configuration.parseXml();
135: }
136: return configuration;
137: }
138:
139: /**
140: *
141: */
142: public String getDefaultService() {
143: return (String) getAttributes().get("defaultService");
144: }
145:
146: /**
147: *
148: */
149: public String getNodeSeparator() {
150: Object nodeSeparator = getAttributes().get("nodeSeparator");
151: return (nodeSeparator == null) ? IGroupConstants.NODE_SEPARATOR
152: : (String) nodeSeparator;
153: }
154:
155: public List getServiceDescriptors() {
156: return serviceDescriptors;
157: }
158:
159: /**
160: * Record a message at "info" level.
161: * @deprecated see comment inside method
162: */
163: protected void infoMessage(String msg) {
164: /*
165: * This method is not in keeping with best practices.
166: * Prepending "Group services:" to the message is unnecessary because
167: * Commons Logging is capable of reporting from where the logging message
168: * originated when configured to do so. The org.jasig.portal.groups package
169: * in its entirety can be mapped to its own logger.
170: * Logging directly allows instutions wishing to take the performance hit of
171: * doing so to configure log4j to report from exactly what line of code
172: * each logging message originates. Going through this method causes all
173: * such messages to appear to come from here rather than from whereever
174: * this method is being invoked from.
175: */
176: log.info("Group services: " + msg);
177: }
178:
179: /**
180: *
181: */
182: protected void parseAttributes(Attributes atts) {
183: String name, value;
184: for (int i = 0; i < atts.getLength(); i++) {
185: name = atts.getQName(i);
186: value = atts.getValue(i);
187: getAttributes().put(name, value);
188: }
189: }
190:
191: protected void parseXml() throws Exception {
192: InputSource xmlSource = new InputSource(ResourceLoader
193: .getResourceAsStream(GroupServiceConfiguration.class,
194: SERVICES_XML));
195:
196: if (xmlSource != null) {
197: XMLReader reader = SAXParserFactory.newInstance()
198: .newSAXParser().getXMLReader();
199: reader.setContentHandler(serviceHandler);
200: reader.parse(xmlSource);
201: }
202: }
203:
204: public static synchronized void reset() {
205: configuration = null;
206: }
207: }
|