001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005:
006: package com.sun.portal.harness;
007:
008: import java.io.FileInputStream;
009:
010: import java.util.Vector;
011: import java.util.HashMap;
012: import java.util.Iterator;
013: import java.util.Locale;
014:
015: import org.w3c.dom.Document;
016: import org.w3c.dom.Element;
017: import org.w3c.dom.NodeList;
018: import org.w3c.dom.Node;
019:
020: import javax.xml.parsers.DocumentBuilder;
021: import javax.xml.parsers.DocumentBuilderFactory;
022:
023: class UsersFile {
024:
025: public static final String DEFAULT_USER = "default";
026:
027: // We make the UsersFile an actual object rather than just a static switchboard mainly
028: // to avoid repeating the top level child scanning.
029:
030: private UsersFile(Document doc) throws ProviderHarnessException {
031:
032: m_UserTab = new HashMap();
033: m_DefaultUser = null;
034: m_Global = null;
035:
036: Element e = doc.getDocumentElement();
037: if (e == null) {
038: throw new ProviderHarnessException(
039: "No root node in Users.xml document");
040: }
041: if (!e.getNodeName().equals("Users")) {
042: throw new ProviderHarnessException(
043: "Root node in Users.xml document is not Users");
044: }
045:
046: NodeList kids = e.getChildNodes();
047: for (int i = 0; i < kids.getLength(); ++i) {
048: Node child = kids.item(i);
049: if (child instanceof Element) {
050: Element chelt = (Element) child;
051: if (chelt.getNodeName().equals("User")) {
052: String nm = chelt.getAttribute("Name");
053: m_UserTab.put(nm, chelt);
054: continue;
055: }
056: if (chelt.getNodeName().equals("Global")) {
057: m_Global = chelt;
058: continue;
059: }
060: if (chelt.getNodeName().equals("DefaultUser")) {
061: m_DefaultUser = chelt;
062: }
063: }
064: }
065: }
066:
067: static UsersFile makeUsersFile(Document doc)
068: throws ProviderHarnessException {
069: return new UsersFile(doc);
070: }
071:
072: static void getUserNames(String file, Vector v)
073: throws ProviderHarnessException {
074: DocumentBuilder db = null;
075:
076: try {
077: DocumentBuilderFactory dbf = DocumentBuilderFactory
078: .newInstance();
079: db = dbf.newDocumentBuilder();
080: } catch (Exception ex) {
081: throw new ProviderHarnessException(
082: "Failure creating document builder", ex);
083: }
084:
085: Document doc = null;
086:
087: // process compfile first. This sets up our channel name. Also,
088: // any parameters entered from compfile will then take precedence over
089: // the more global ones (PropHashMap() uses the FIRST entered.
090:
091: try {
092: db.setEntityResolver(new ParEntryEntityResolver());
093: doc = db.parse(new FileInputStream(file));
094: } catch (Exception ex) {
095: throw new ProviderHarnessException(
096: "Failure reading users xml - " + file, ex);
097: }
098:
099: UsersFile uf = new UsersFile(doc);
100: uf.getUserNames(v);
101: }
102:
103: void getUserNames(Vector v) {
104: v.clear();
105:
106: if (m_DefaultUser != null) {
107: v.add(DEFAULT_USER);
108: }
109:
110: Iterator it = m_UserTab.keySet().iterator();
111: while (it.hasNext()) {
112: v.add((String) it.next());
113: }
114: }
115:
116: String getLocale(String user) throws ProviderHarnessException {
117: String ls = getUserElement(user).getAttribute("Locale");
118: if (ls.equals("")) {
119: return Locale.getDefault().toString();
120: }
121: return ls;
122: }
123:
124: Element getUserProps(String user) throws ProviderHarnessException {
125: return subElement(getUserElement(user), "Properties", false);
126: }
127:
128: Element getUserChannelProps(String user, String chan)
129: throws ProviderHarnessException {
130: return getChannelProperties(getUserElement(user), chan);
131: }
132:
133: Element getGlobalProps() throws ProviderHarnessException {
134: return subElement(checkGlobal(), "Properties", false);
135: }
136:
137: Element getGlobalChannelProps(String chan)
138: throws ProviderHarnessException {
139: return getChannelProperties(checkGlobal(), chan);
140: }
141:
142: void getUserChannelList(String user, Vector v)
143: throws ProviderHarnessException {
144: getChannelList(getUserElement(user), v);
145: }
146:
147: void getGlobalChannelList(Vector v) throws ProviderHarnessException {
148: getChannelList(checkGlobal(), v);
149: }
150:
151: private void getChannelList(Element elt, Vector v)
152: throws ProviderHarnessException {
153: v.clear();
154:
155: Element chans = subElement(elt, "Channels", true);
156: if (chans == null) {
157: return;
158: }
159:
160: NodeList kids = chans.getChildNodes();
161: for (int i = 0; i < kids.getLength(); ++i) {
162: Node child = kids.item(i);
163: if (child instanceof Element
164: && child.getNodeName().equals("Channel")) {
165: Element chelt = (Element) child;
166: v.add(chelt.getAttribute("Name"));
167: }
168: }
169: }
170:
171: private Element getChannelProperties(Element elt, String chan)
172: throws ProviderHarnessException {
173: Element chans = subElement(elt, "Channels", false);
174:
175: NodeList kids = chans.getChildNodes();
176: for (int i = 0; i < kids.getLength(); ++i) {
177: Node child = kids.item(i);
178: if (child instanceof Element
179: && child.getNodeName().equals("Channel")) {
180: Element chelt = (Element) child;
181: if (chelt.getAttribute("Name").equals(chan)) {
182: return subElement(chelt, "Properties", false);
183: }
184: }
185: }
186:
187: throw new ProviderHarnessException("Cannot find channel - "
188: + chan);
189: }
190:
191: private Element getUserElement(String user)
192: throws ProviderHarnessException {
193:
194: if (user.equals(DEFAULT_USER)) {
195: if (m_DefaultUser == null) {
196: throw new ProviderHarnessException(
197: "Users.xml has no default user.");
198: }
199: return m_DefaultUser;
200: }
201:
202: Element elt = (Element) m_UserTab.get(user);
203: if (elt == null) {
204: throw new ProviderHarnessException(
205: "Users.xml does not contain user - " + user);
206: }
207: return elt;
208: }
209:
210: private Element checkGlobal() throws ProviderHarnessException {
211: if (m_Global == null) {
212: throw new ProviderHarnessException(
213: "Users.xml does not contain <Global>.");
214: }
215: return m_Global;
216: }
217:
218: private Element subElement(Element e, String nm, boolean nullok)
219: throws ProviderHarnessException {
220: NodeList kids = e.getChildNodes();
221:
222: for (int i = 0; i < kids.getLength(); ++i) {
223: Node child = kids.item(i);
224: if (child instanceof Element
225: && child.getNodeName().equals(nm)) {
226: return (Element) child;
227: }
228: }
229:
230: if (nullok) {
231: return null;
232: }
233:
234: throw new ProviderHarnessException("Did not find expected "
235: + nm + " element in " + e.getNodeName());
236: }
237:
238: private HashMap m_UserTab;
239: private Element m_DefaultUser;
240: private Element m_Global;
241: }
|