001: /*
002: * Copyright 2002 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: package com.sun.portal.portletcontainercommon.descriptor;
014:
015: import java.io.InputStream;
016: import java.io.IOException;
017: import java.util.logging.Logger;
018: import java.util.logging.Level;
019:
020: import org.jdom.input.SAXBuilder;
021: import org.jdom.Document;
022: import org.jdom.Element;
023: import org.jdom.Namespace;
024: import org.jdom.JDOMException;
025: import com.sun.portal.log.common.PortalLogger;
026:
027: /**
028: * The deployment descriptor reader reads the portlet deployment descriptor
029: * file and parses the elements of the XML file.
030: */
031: public class DeploymentDescriptorReader {
032: // Schema location and namespace
033: public static String PORTLET_SCHEMA_LOCATION = "DDSchemaLocation";
034: public static String PORTLET_SCHEMA_FILE = "/portlet.xsd";
035: public static String SUN_PORTLET_SCHEMA_FILE = "/sun-portlet.xsd";
036:
037: public static String PORTLET_NAMESPACE = "http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd";
038:
039: public static String SUN_PORTAL_NAMESPACE = "http://www.sun.com/software/xml/ns/portal_server";
040:
041: // SAXBuilder related constants
042: public static String BUILDER_SAX_PARSER = "org.apache.xerces.parsers.SAXParser";
043:
044: public static String BUILDER_SAX_PARSER_SUN_INTERNAL = "com.sun.org.apache.xerces.internal.parsers.SAXParser";
045:
046: public static String BUILDER_VALIDATION_SCHEMA_FEATURE = "http://apache.org/xml/features/validation/schema";
047:
048: public static String BUILDER_SCHEMA_LOC_PROPERTY = "http://apache.org/xml/properties/schema/external-schemaLocation";
049:
050: public static final String VALIDATE_SCHEMA = "validate_schema";
051:
052: // Create a logger for this class
053: private static Logger debugLogger = PortalLogger
054: .getLogger(DeploymentDescriptorReader.class);
055:
056: //global variables
057: private String _portletSchemaLocation;
058:
059: public DeploymentDescriptorReader(Logger logger,
060: String schemaLocation) {
061: _portletSchemaLocation = schemaLocation;
062: debugLogger.log(Level.INFO, "PSPL_PCCCSPPCCD0001",
063: schemaLocation);
064: }
065:
066: /**
067: * Reads the deployment descriptor's xml file, and returns the root
068: * element.
069: * <P>
070: * In the case of unable to read the deployment descriptor, an error
071: * is logged.
072: * <P>
073: * @param portletStream The input stream of the deployment
074: * descriptor's xml file
075: * @return The root Element of the document, null if the resource does not
076: * exists.
077: */
078: private Element readDeploymentDescriptor(InputStream portletStream,
079: String schemaLocation) throws DeploymentDescriptorException {
080: SAXBuilder builder = null;
081: String validateString = System.getProperty(VALIDATE_SCHEMA);
082:
083: // validating the schema
084: if (validateString != null
085: && validateString.equalsIgnoreCase("true")) {
086: String parser = BUILDER_SAX_PARSER_SUN_INTERNAL;
087: if (!isParserAvailable(parser)) {
088: parser = BUILDER_SAX_PARSER;
089: }
090: builder = new SAXBuilder(parser, true);
091: builder.setFeature(BUILDER_VALIDATION_SCHEMA_FEATURE, true);
092: builder.setProperty(BUILDER_SCHEMA_LOC_PROPERTY,
093: schemaLocation);
094: debugLogger.log(Level.INFO, "PSPL_PCCCSPPCCD0002",
095: validateString);
096: } else {
097: builder = new SAXBuilder();
098: }
099:
100: Element element = null;
101:
102: try {
103: Document doc = builder.build(portletStream);
104: element = doc.getRootElement();
105: } catch (IOException e) {
106: throw new DeploymentDescriptorException(
107: "error reading stream", e);
108: } catch (JDOMException je) {
109: debugLogger.log(Level.SEVERE, "PSPL_PCCCSPPCCD0003", je);
110: throw new DeploymentDescriptorException(
111: "Can not read deployment descriptor", je);
112: }
113:
114: debugLogger.log(Level.INFO, "PSPL_PCCCSPPCCD0004", element);
115: return element;
116: }
117:
118: private boolean isParserAvailable(String parser) {
119: boolean result = true;
120: try {
121: Class.forName(parser);
122: } catch (ClassNotFoundException e) {
123: result = false;
124: }
125:
126: return result;
127: }
128:
129: /**
130: * Loads the deployment descriptor into PortletAppDescriptor structure.
131: * <P>
132: * @param The root Element of the deployment descriptor document
133: * @return The PortletAppDescriptor object, null if portlet app descriptor
134: * can not be read or can not be instantiated.
135: */
136: public PortletAppDescriptor loadPortletAppDescriptor(
137: InputStream portletStream, InputStream extStream)
138: throws DeploymentDescriptorException {
139:
140: PortletAppDescriptor appDescriptor = null;
141:
142: if (portletStream != null) {
143: Element root = readDeploymentDescriptor(portletStream,
144: PORTLET_NAMESPACE + " " + _portletSchemaLocation
145: + PORTLET_SCHEMA_FILE);
146: if (root != null) {
147: Namespace namespace = Namespace
148: .getNamespace(PORTLET_NAMESPACE);
149: appDescriptor = new PortletAppDescriptor(debugLogger,
150: extStream);
151: appDescriptor.load(root, namespace, this );
152: }
153: }
154:
155: return appDescriptor;
156: }
157:
158: public DeploymentExtensionDescriptor loadDeploymentExtensionDescriptor(
159: InputStream extStream) throws DeploymentDescriptorException {
160:
161: DeploymentExtensionDescriptor exDescriptor = null;
162:
163: if (extStream != null) {
164: Element root = readDeploymentDescriptor(extStream,
165: SUN_PORTAL_NAMESPACE + " " + _portletSchemaLocation
166: + SUN_PORTLET_SCHEMA_FILE);
167: if (root != null) {
168: Namespace namespace = Namespace
169: .getNamespace(SUN_PORTAL_NAMESPACE);
170: exDescriptor = new DeploymentExtensionDescriptor(
171: debugLogger);
172: exDescriptor.load(root, namespace);
173: }
174: }
175:
176: return exDescriptor;
177: }
178:
179: }
|