001: /* Copyright 2005 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.layout.dlm;
007:
008: import java.io.InputStream;
009: import java.io.InputStreamReader;
010: import java.io.PrintWriter;
011: import java.io.StringWriter;
012: import java.net.URL;
013: import java.util.Properties;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.jasig.portal.utils.DocumentFactory;
018: import org.jasig.portal.utils.XML;
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Element;
021: import org.w3c.dom.NamedNodeMap;
022: import org.w3c.dom.Node;
023: import org.w3c.dom.NodeList;
024:
025: /**
026: * @version $Revision: 36074 $ $Date: 2005-09-13 11:31:57 -0700 (Tue, 13 Sep 2005) $
027: * @since uPortal 2.5
028: */
029: public class ConfigurationLoader {
030: public static final String RCS_ID = "@(#) $Header$";
031:
032: private static final String CONFIG_FILE_NAME = "/properties/dlm.xml";
033: private static URL configFileURL = null;
034: private static final Log LOG = LogFactory
035: .getLog(ConfigurationLoader.class);
036:
037: /**
038: * Load the distributed layout configuration.
039: */
040: public static void load(RDBMDistributedLayoutStore dlManager) {
041: try {
042: Class c = ConfigurationLoader.class;
043: configFileURL = c.getResource(CONFIG_FILE_NAME);
044: logConfigFileInfo();
045:
046: InputStream inputStream = configFileURL.openStream();
047: Document doc = DocumentFactory.getDocumentFromStream(
048: inputStream, configFileURL.toExternalForm());
049:
050: NodeList properties = doc
051: .getElementsByTagName("dlm:property");
052: NodeList definitions = doc
053: .getElementsByTagName("dlm:fragment");
054:
055: dlManager.setProperties(getProperties(properties));
056: dlManager.setDefinitions(getFragments(definitions));
057: } catch (Exception e) {
058: throw new RuntimeException(ConfigurationLoader.class
059: .getName()
060: + " could not load distributed layout "
061: + "configuration.", e);
062: }
063: }
064:
065: private static void logConfigFileInfo() {
066: if (LOG.isInfoEnabled()) {
067: try {
068: char[] buf = new char[4096];
069: InputStream is = configFileURL.openStream();
070: InputStreamReader isr = new InputStreamReader(is);
071: StringWriter sw = new StringWriter();
072: PrintWriter p = new PrintWriter(sw);
073:
074: p
075: .println("\n\n---- Distributed Layout Management ----");
076: p.println("config file: " + configFileURL.toString());
077: p.println("\n---- CONTENTS ----\n");
078: p.flush();
079:
080: int i = -1;
081: while ((i = isr.read(buf, 0, 4096)) != -1)
082: sw.write(buf, 0, i);
083: LOG.info(sw.toString() + "\n------------------\n");
084: } catch (Exception IOException) {
085: // ignore. if we can't open here runtime will be thrown soon
086: // after returning showing the same information.
087: }
088: }
089: }
090:
091: private static Properties getProperties(NodeList props) {
092: if (props == null || props.getLength() == 0)
093: return null;
094:
095: Properties properties = new Properties();
096:
097: for (int i = 0; i < props.getLength(); i++) {
098: Node node = props.item(i);
099: NamedNodeMap atts = node.getAttributes();
100: Node name = atts.getNamedItem("name");
101: Node value = atts.getNamedItem("value");
102: if (name == null || name.equals("")) {
103: if (LOG.isInfoEnabled())
104: LOG
105: .info("\n\n---------- Warning ----------\nThe 'name'"
106: + " attribute of the "
107: + "property element is required and must not be empty "
108: + "in \n'"
109: + XML.serializeNode(node)
110: + "'\nfrom distributed layout managment configuration "
111: + "file \n"
112: + configFileURL.toString()
113: + " \n-----------------------------\n");
114: continue;
115: }
116: if (value == null)
117: properties.put(name.getNodeValue(), "");
118: else
119: properties.put(name.getNodeValue(), value
120: .getNodeValue());
121: }
122: return properties;
123: }
124:
125: private static FragmentDefinition[] getFragments(NodeList frags) {
126: if (frags == null || frags.getLength() == 0)
127: return null;
128:
129: FragmentDefinition[] fragments = null;
130:
131: for (int i = 0; i < frags.getLength(); i++) {
132: try {
133: FragmentDefinition f = new FragmentDefinition(
134: (Element) frags.item(i));
135: fragments = appendDef(f, fragments);
136:
137: if (LOG.isInfoEnabled())
138: LOG
139: .info("\n\nDLM loaded fragment definition '"
140: + f.name
141: + "' owned by '"
142: + f.ownerID
143: + "' with precedence "
144: + f.precedence
145: + (f.noAudienceIncluded ? " and no specified audience"
146: + ". It will be editable by '"
147: + f.ownerID
148: + "' but "
149: + "not included in any user's layout."
150: : (f.evaluators == null ? " with no audience. It will be editable by '"
151: + f.ownerID
152: + "' but "
153: + "not included in any user's layout."
154: : " with "
155: + f.evaluators.length
156: + " audiences")));
157: } catch (Exception e) {
158: LOG
159: .error(
160: "\n\n---------- Warning ---------\nUnable to load "
161: + "distributed layout fragment "
162: + "definition from configuration file\n"
163: + configFileURL.toString()
164: + "\n Details: "
165: + e.getMessage()
166: + " \n----------------------------\n",
167: e);
168: }
169: }
170: return fragments;
171: }
172:
173: private static FragmentDefinition[] appendDef(FragmentDefinition f,
174: FragmentDefinition[] frags) {
175: if (frags == null) {
176: f.index = 0;
177: return new FragmentDefinition[] { f };
178: }
179: f.index = frags.length;
180: FragmentDefinition[] newArr = new FragmentDefinition[frags.length + 1];
181: System.arraycopy(frags, 0, newArr, 0, frags.length);
182: newArr[frags.length] = f;
183: return newArr;
184: }
185: }
|