1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.pluto.portlet.admin.services;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStreamWriter;
21 import java.io.PrintWriter;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.parsers.ParserConfigurationException;
28
29 import org.apache.pluto.descriptors.portlet.PortletDD;
30 import org.apache.pluto.portlet.admin.PlutoAdminConstants;
31 import org.apache.pluto.portlet.admin.PlutoAdminException;
32 import org.apache.pluto.portlet.admin.PlutoAdminLogger;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35 import org.w3c.dom.NodeList;
36 import org.xml.sax.ErrorHandler;
37 import org.xml.sax.SAXException;
38 import org.xml.sax.SAXParseException;
39
40 /***
41 *
42 * Parses out info from portlet.xml, putting them in a list
43 * of PortletDD objects to be used to update web.xml during deployment.
44 *
45 * @author Craig Doremus
46 *
47 */
48 public class PortletConfigService {
49 private Document _doc;
50 private List portletDDList;
51
52 public static final String CLASS_NAME = "PortletConfigService";
53
54 private PortletConfigService() {
55 }
56
57 public PortletConfigService(InputStream ios) {
58 _doc = getDocument(ios);
59 createPortletDDList();
60 }
61
62
63 private static Document getDocument(InputStream ios) {
64 final String METHOD_NAME = "getDocument(InputStream)";
65 boolean validation = false;
66
67 boolean ignoreWhitespace = false;
68 boolean ignoreComments = false;
69 boolean putCDATAIntoText = false;
70 boolean createEntityRefs = false;
71
72 DocumentBuilderFactory dbf =
73 DocumentBuilderFactory.newInstance();
74
75 dbf.setValidating(validation);
76 dbf.setIgnoringComments(ignoreComments);
77 dbf.setIgnoringElementContentWhitespace(ignoreWhitespace);
78 dbf.setCoalescing(putCDATAIntoText);
79 dbf.setExpandEntityReferences(!createEntityRefs);
80
81 DocumentBuilder db = null;
82 Document doc = null;
83 try {
84 db = dbf.newDocumentBuilder();
85
86 OutputStreamWriter errorWriter =
87 new OutputStreamWriter(System.err, PlutoAdminConstants.ENCODING);
88 db.setErrorHandler(
89 new MyErrorHandler(new PrintWriter(errorWriter, true)));
90
91 doc = db.parse(ios);
92 } catch (ParserConfigurationException e) {
93 PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
94 throw new PlutoAdminException(e);
95 } catch (SAXException e) {
96 PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
97 throw new PlutoAdminException(e);
98 } catch (IOException e) {
99 PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
100 throw new PlutoAdminException(e);
101 }
102 return doc;
103 }
104
105
106 /***
107 *
108 */
109 private void createPortletDDList() {
110 final String METHOD_NAME = "createPortletDDList";
111 portletDDList = new ArrayList();
112 NodeList portlets = _doc.getElementsByTagName("portlet");
113 Element portlet = null;
114 PortletDD portletDD = null;
115 NodeList roleRefs = null;
116 ArrayList refList = null;
117 String name = null;
118 String className = null;
119 Element ref = null;
120 String refName = null;
121 String refLink = null;
122 if (portlets != null) {
123 for (int i = 0; i < portlets.getLength(); i++) {
124 portletDD = new PortletDD();
125 portlet = (Element)portlets.item(i);
126
127 name = portlet.getElementsByTagName("portlet-name").item(0).getChildNodes().item(0).getNodeValue();
128 if (name == null) {
129 IllegalStateException e =
130 new IllegalStateException("The portlet-name element is required in portlet.xml");
131 PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
132 throw e;
133 }
134 portletDD.setPortletName(name);
135
136 className = portlet.getElementsByTagName("portlet-class").item(0).getChildNodes().item(0).getNodeValue();
137 if (className == null) {
138 IllegalStateException e =
139 new IllegalStateException("The portlet-class element is required in portlet.xml");
140 PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
141 throw e;
142 }
143 portletDD.setPortletClass(className);
144 roleRefs = portlet.getElementsByTagName("security-role-ref");
145 refList = new ArrayList();
146 if (roleRefs != null && roleRefs.getLength() != 0) {
147 for (int j = 0; j < roleRefs.getLength(); j++) {
148 ref = (Element)roleRefs.item(j);
149 refName = ref.getElementsByTagName("role-name").item(0).getChildNodes().item(0).getNodeValue();
150
151 if (ref.getElementsByTagName("role-link").item(0) != null) {
152 refLink = ref.getElementsByTagName("role-link").item(0).getChildNodes().item(0).getNodeValue();
153 }
154 refList.add(new RoleRef(refName, refLink));
155 }
156 }
157
158 portletDD.setSecurityRoleRefs(refList);
159 portletDDList.add(portletDD);
160
161 }
162 } else {
163 IllegalStateException e =
164 new IllegalStateException("The portlet.xml file does not contain portlet elements.");
165 PlutoAdminLogger.logError(CLASS_NAME, METHOD_NAME, e);
166 throw e;
167 }
168
169 }
170
171 /***
172 * Small Value Object holding a security-role-ref name and link
173 * values in the portletDDList List.
174 *
175 * @author cdoremus
176 *
177 */
178 public class RoleRef {
179 public String roleName = null;
180 public String roleLink = null;
181 private RoleRef(){}
182 public RoleRef(String p_roleName, String p_roleLink) {
183 roleName = p_roleName;
184 roleLink = p_roleLink;
185 }
186 }
187
188 /***
189 *
190 * Inner class that handles errors
191 *
192 * @author Ken Atherton
193 *
194 */
195 private static class MyErrorHandler implements ErrorHandler {
196 private static final String INNER_CLASS_NAME = "MyErrorHandler";
197 PrintWriter out = null;
198 MyErrorHandler(PrintWriter p_out) {
199 out = p_out;
200 }
201
202
203 private String getParseExceptionInfo(SAXParseException e) {
204 String systemId = e.getSystemId();
205 if (systemId == null) {
206 systemId = "null";
207 }
208 String info = "URI=" + systemId +
209 " Line=" + e.getLineNumber() +
210 ": " + e.getMessage();
211 return info;
212 }
213
214
215 public void warning(SAXParseException e) throws SAXException {
216 String METHOD_NAME = "warning(e)";
217 String msg = "Warning: " + getParseExceptionInfo(e);
218 PlutoAdminLogger.logWarn(INNER_CLASS_NAME, METHOD_NAME, msg);
219 }
220
221 public void error(SAXParseException e) throws SAXException {
222 String METHOD_NAME = "error(e)";
223 String message = "Error: " + getParseExceptionInfo(e);
224 PlutoAdminLogger.logError(INNER_CLASS_NAME, METHOD_NAME, message, e);
225 throw new SAXException(message);
226 }
227
228 public void fatalError(SAXParseException e) throws SAXException {
229 String METHOD_NAME = "fatalError(e)";
230 String message = "Fatal Error: " + getParseExceptionInfo(e);
231 PlutoAdminLogger.logError(INNER_CLASS_NAME, METHOD_NAME, message, e);
232 throw new SAXException(message);
233 }
234 }
235
236 public List getPortletDDList() {
237 return portletDDList;
238 }
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 }