0001: /*
0002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0003: *
0004: * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
0005: *
0006: * The contents of this file are subject to the terms of either the GNU
0007: * General Public License Version 2 only ("GPL") or the Common Development
0008: * and Distribution License("CDDL") (collectively, the "License"). You
0009: * may not use this file except in compliance with the License. You can obtain
0010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
0011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
0012: * language governing permissions and limitations under the License.
0013: *
0014: * When distributing the software, include this License Header Notice in each
0015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
0016: * Sun designates this particular file as subject to the "Classpath" exception
0017: * as provided by Sun in the GPL Version 2 section of the License file that
0018: * accompanied this code. If applicable, add the following below the License
0019: * Header, with the fields enclosed by brackets [] replaced by your own
0020: * identifying information: "Portions Copyrighted [year]
0021: * [name of copyright owner]"
0022: *
0023: * Contributor(s):
0024: *
0025: * If you wish your version of this file to be governed by only the CDDL or
0026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
0027: * elects to include this software in this distribution under the [CDDL or GPL
0028: * Version 2] license." If you don't indicate a single choice of license, a
0029: * recipient has the option to distribute your version of this file under
0030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
0031: * its licensees as provided above. However, if you add GPL Version 2 code
0032: * and therefore, elected the GPL Version 2 license, then the option applies
0033: * only if the new code is made subject to such option by the copyright
0034: * holder.
0035: */
0036: /*
0037: * ValidationUtilities.java
0038: */
0039: package com.sun.jbi.jsf.util;
0040:
0041: import com.sun.jbi.jsf.bean.ArchiveBean;
0042: import com.sun.jbi.jsf.bean.ConfigProperty;
0043: import com.sun.jbi.jsf.bean.ConfigPropertyGroup;
0044: import com.sun.org.apache.xerces.internal.parsers.DOMParser;
0045: import com.sun.org.apache.xpath.internal.XPathAPI;
0046: import java.io.ByteArrayInputStream;
0047: import java.io.ByteArrayOutputStream;
0048: import java.io.FileInputStream;
0049: import java.io.InputStream;
0050: import java.util.ArrayList;
0051: import java.util.List;
0052: import java.util.logging.Level;
0053: import java.util.logging.Logger;
0054: import java.util.zip.ZipEntry;
0055: import java.util.zip.ZipInputStream;
0056: import javax.xml.parsers.DocumentBuilder;
0057: import javax.xml.parsers.DocumentBuilderFactory;
0058: import org.w3c.dom.Document;
0059: import org.w3c.dom.NamedNodeMap;
0060: import org.w3c.dom.Node;
0061: import org.w3c.dom.NodeList;
0062: import org.xml.sax.InputSource;
0063:
0064: /**
0065: * XML, XSD, XPath related utilities
0066: *
0067: * @author Sun Microsystems Inc.
0068: */
0069:
0070: public final class ValidationUtilities {
0071:
0072: /**
0073: * Gets the ConfigExtensions attribute of the ValidationUtilities class
0074: *
0075: * @param aJbiDocument Description of Parameter
0076: * @return The ConfigExtensions value
0077: */
0078: public static List<ConfigPropertyGroup> getConfigExtensions(
0079: Document aJbiDocument) {
0080: List<ConfigPropertyGroup> result = null;
0081: // if no valid Configuration extension found
0082:
0083: if (null != aJbiDocument) {
0084: NodeList configElements = aJbiDocument
0085: .getElementsByTagNameNS(ESBConstants.NS_CFG,
0086: ESBConstants.ELT_CFG);
0087:
0088: if ((null != configElements)
0089: && (1 == configElements.getLength())) {
0090: sLog
0091: .fine("ValidationUtilities.getConfigExtensions(), found Configuration element");
0092:
0093: ConfigPropertyGroup defaultPropertyGroup = createDefaultPropertyGroup();
0094: result = new ArrayList();
0095: result.add(defaultPropertyGroup);
0096:
0097: for (int i = 0; i < configElements.getLength(); ++i) {
0098: Node configNode = configElements.item(i);
0099: NodeList groupOrPropertyElements = configNode
0100: .getChildNodes();
0101:
0102: // add ungrouped properties to the default group
0103: // add property groups as found
0104: // (adding grouped properties under them)
0105:
0106: if ((null != groupOrPropertyElements)
0107: && (0 < groupOrPropertyElements.getLength())) {
0108: for (int j = 0; j < groupOrPropertyElements
0109: .getLength(); ++j) {
0110: Node groupOrPropNode = groupOrPropertyElements
0111: .item(j);
0112:
0113: String nodePrefix = null;
0114: String nodeName = null;
0115: String groupOrPropNodeTagName = null;
0116:
0117: if (null != groupOrPropNode) {
0118: short nodeType = groupOrPropNode
0119: .getNodeType();
0120: // 1==element
0121: if (Node.ELEMENT_NODE == nodeType) {
0122: nodePrefix = groupOrPropNode
0123: .getPrefix();
0124: // e.g. "config" (without ":")
0125: nodeName = groupOrPropNode
0126: .getNodeName();
0127: // e.g. "config:PropertyGroup"
0128: if (null != nodePrefix) {
0129: // e.g. "PropertyGroup"
0130: groupOrPropNodeTagName = nodeName
0131: .substring((ESBConstants.NS_PREFIX_SEP
0132: .length() + nodePrefix
0133: .length()));
0134: }
0135: }
0136: sLog
0137: .fine("ValidationUtilities.getConfigExtensions()"
0138: + ", nodeType="
0139: + nodeType
0140: + ", nodePrefix="
0141: + nodePrefix
0142: + ", nodeName="
0143: + nodeName
0144: + ", groupOrPropNodeTagName="
0145: + groupOrPropNodeTagName);
0146: }
0147: if (ESBConstants.ELT_PROP_GROUP
0148: .equals(groupOrPropNodeTagName)) {
0149: sLog
0150: .fine("ValidationUtilities.getConfigExtensions()"
0151: + ", found PropertyGroup element");
0152: ConfigPropertyGroup group = createPropertyGroup(groupOrPropNode);
0153: if (null == result) {
0154: result = new ArrayList();
0155: }
0156: result.add(group);
0157: } else if (ESBConstants.ELT_PROP
0158: .equals(groupOrPropNodeTagName)) {
0159: sLog
0160: .fine("ValidationUtilities.getConfigExtensions()"
0161: + ", found ungrouped Property element");
0162: addProperty(groupOrPropNode,
0163: defaultPropertyGroup);
0164: } else {
0165: sLog
0166: .fine("ValidationUtilities.getConfigExtensions()"
0167: + ", found unexpected \""
0168: + groupOrPropNodeTagName
0169: + "\" element");
0170: }
0171: }
0172: }
0173: }
0174: }
0175: }
0176:
0177: return result;
0178: }
0179:
0180: /**
0181: * Gets the ConfigExtensionJbiDocument attribute of the
0182: * ValidationUtilities class
0183: *
0184: * @return The ConfigExtensionJbiDocument value
0185: */
0186: public static Document getConfigExtensionJbiDocument() {
0187: return getJbiDocument(IS_NS_AWARE);
0188: }
0189:
0190: /**
0191: * Gets the JbiDocument attribute of the ValidationUtilities class
0192: *
0193: * @return The JbiDocument value
0194: */
0195: public static Document getJbiDocument() {
0196: return getJbiDocument(IS_NOT_NS_AWARE);
0197: }
0198:
0199: /**
0200: * determines the JBI Type for a given JBI metadata descriptor (jbi.xml)
0201: *
0202: * @param aJbiDescriptor an XML document representing a valid jbi.xml
0203: * file
0204: * @return the JBI type <code> binding-component</code>,
0205: * <code>service-assembly</code>, <code>service-engine</code>, or
0206: * <code>shared-library</code>
0207: */
0208: public static String getJbiType(Document aJbiDescriptor) {
0209: String result = null;
0210:
0211: if (null != aJbiDescriptor) {
0212: if (tryXpathExpInDescriptor(XPATH_BC_NAME, XPATH_BC_DESCR,
0213: aJbiDescriptor)) {
0214: result = JBIConstants.JBI_BINDING_COMPONENT_TYPE;
0215: } else if (tryXpathExpInDescriptor(XPATH_SA_NAME,
0216: XPATH_SA_DESCR, aJbiDescriptor)) {
0217: result = JBIConstants.JBI_SERVICE_ASSEMBLY_TYPE;
0218: } else if (tryXpathExpInDescriptor(XPATH_SE_NAME,
0219: XPATH_SE_DESCR, aJbiDescriptor)) {
0220: result = JBIConstants.JBI_SERVICE_ENGINE_TYPE;
0221: } else if (tryXpathExpInDescriptor(XPATH_SL_NAME,
0222: XPATH_SL_DESCR, aJbiDescriptor)) {
0223: result = JBIConstants.JBI_SHARED_LIBRARY_TYPE;
0224: } else {
0225: sLog
0226: .fine("ValidationUtilities.getJbiType() "
0227: + "cannot find an expected type in supplied descriptor");
0228:
0229: return "unknown jbi type";
0230: // not i18n
0231: }
0232: }
0233:
0234: sLog.fine("ValidationUtilities.getJbiType(), result=" + result);
0235: return result;
0236: }
0237:
0238: /**
0239: * Get the jbi.xml file from given jar/zip file if present.
0240: *
0241: * @return result InputStream containing jbi.xml data, or empty stream
0242: * if no jbi.xml
0243: */
0244: public static InputStream getMetaDataEntry() {
0245: ArchiveBean archiveBean = BeanUtilities.getArchiveBean();
0246: InputStream result = new ByteArrayInputStream(new byte[0]);
0247:
0248: int count = 0;
0249: String name = "";
0250: ZipEntry zipEntry;
0251:
0252: String filePath = archiveBean.getArchiveAbsolutePath();
0253: sLog.fine("ValidationUtilities.getMetaDataEntry(), filePath="
0254: + filePath);
0255: ZipInputStream zipFile = null;
0256:
0257: if (null != filePath) {
0258: try {
0259: zipFile = new ZipInputStream(new FileInputStream(
0260: filePath));
0261: sLog
0262: .fine("ValidationUtilities.getMetaDataEntry(), zipFile="
0263: + zipFile);
0264:
0265: if (null != zipFile) {
0266: zipEntry = zipFile.getNextEntry();
0267: sLog
0268: .fine("ValidationUtilities.getMetaDataEntry(), zipEntry="
0269: + zipEntry);
0270: if (null != zipEntry) {
0271: name = zipEntry.getName();
0272: }
0273: while (null != zipEntry
0274: && (!name.equals(JBI_MANIFEST))) {
0275: ++count;
0276: zipEntry = zipFile.getNextEntry();
0277: if (zipEntry != null) {
0278: name = zipEntry.getName();
0279: sLog
0280: .finer("ValidationUtilities.getMetaDataEntry(), name="
0281: + name);
0282: }
0283: }
0284: }
0285: //zipArchive has jbi.xml
0286: sLog
0287: .fine("ValidationUtilities.getMetaDataEntry(), after loop, name="
0288: + name);
0289:
0290: if (name.equals(JBI_MANIFEST)) {
0291: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0292: sLog
0293: .fine("ValidationUtilities.getMetaDataEntry(), baos="
0294: + baos);
0295:
0296: byte[] buffer = new byte[BLOCK_SIZE];
0297: int len = zipFile.read(buffer);
0298: while (0 < len) {
0299: baos.write(buffer, 0, len);
0300: len = zipFile.read(buffer);
0301: }
0302: result = new ByteArrayInputStream(baos
0303: .toByteArray());
0304: sLog
0305: .fine("ValidationUtilities.getMetaDataEntry(), jbi.xml found");
0306: } else {
0307: archiveBean.setHasJbiXml(false);
0308: sLog
0309: .fine("ValidationUtilities.getMetaDataEntry(), jbi.xml not found");
0310: }
0311: } catch (java.util.zip.ZipException zex) {
0312: sLog
0313: .fine("ValidationUtilities.getMetaDataEntry(), caught zex="
0314: + zex);
0315: archiveBean.setZipFileReadError(true);
0316: } catch (java.io.FileNotFoundException fnfex) {
0317: sLog
0318: .fine("ValidationUtilities.getMetaDataEntry(), caught fnfex="
0319: + fnfex);
0320: archiveBean.setFileReadError(true);
0321: } catch (java.io.IOException ioex) {
0322: sLog
0323: .fine("ValidationUtilities.getMetaDataEntry(), caught ioex="
0324: + ioex);
0325: archiveBean.setFileReadError(true);
0326: } finally {
0327: close(zipFile);
0328: }
0329: } else {
0330: sLog
0331: .fine("ValidationUtilities.getMetaDataEntry(), unable to read archive");
0332: archiveBean.setFileReadError(true);
0333: // no file specified
0334: }
0335: sLog.fine("ValidationUtilities.getMetaDataEntry(), result="
0336: + result);
0337: return result;
0338: }
0339:
0340: /**
0341: * Check zip file entry.
0342: *
0343: * @return true iff the zipfile is empty
0344: */
0345: public static boolean isArchiveEmptyOrInValid() {
0346: ArchiveBean archiveBean = BeanUtilities.getArchiveBean();
0347: boolean result = false;
0348: // assume zip file has some entries
0349:
0350: String filePath = archiveBean.getArchiveAbsolutePath();
0351: // TBD log.fine("isArchiveEmptyOrInValid(), filePath=" + filePath);
0352:
0353: ZipInputStream zipArchive = null;
0354: if (null != filePath) {
0355: try {
0356: zipArchive = new ZipInputStream(new FileInputStream(
0357: filePath));
0358: boolean done = false;
0359: boolean empty = true;
0360: while (!done) {
0361: ZipEntry entry = zipArchive.getNextEntry();
0362:
0363: if (null != entry) {
0364: empty = false;
0365: // TBD log.fine("reading: " + entry.getName());
0366: } else {
0367: done = true;
0368: }
0369: }
0370: if (empty) {
0371: // TBD log.fine("isArchiveEmptyOrInValid() empty");
0372: result = true;
0373: } else {
0374: // TBD log.fine("isArchiveEmptyOrInValid() not empty");
0375: }
0376: } catch (java.util.zip.ZipException zex) {
0377: // not a valid archive
0378:
0379: archiveBean.setZipFileReadError(true);
0380: sLog.fine("isArchiveEmptyOrInValid() bad archive, zex="
0381: + zex);
0382: result = true;
0383: } catch (java.io.IOException ioex) {
0384: // not a valid file
0385:
0386: archiveBean.setFileReadError(true);
0387: sLog.fine("isArchiveEmptyOrInValid() bad file, ioex="
0388: + ioex);
0389: result = true;
0390: } finally {
0391: close(zipArchive);
0392: }
0393: } else {
0394: // no file specified
0395:
0396: sLog.fine("isArchiveEmptyOrInValid() no file");
0397: archiveBean.setFileReadError(true);
0398: result = true;
0399: }
0400:
0401: // TBD log.fine("isArchiveEmptyOrInValid(), filePath=" + filePath + ", result=" + result);
0402: return result;
0403: }
0404:
0405: /**
0406: * Validate the jbi.xml file for well-formedness.
0407: *
0408: * @return true iff the file passes validation
0409: */
0410: public static boolean isJbiXmlWellformed() {
0411: // TBD log.fine("isJbiXmlWellformed()");
0412: boolean result = false;
0413: try {
0414: DOMParser parser = new DOMParser();
0415: InputStream istr = getMetaDataEntry();
0416: InputSource inSrc = new InputSource(istr);
0417: sLog.fine("Created InputSOurce");
0418: parser.parse(inSrc);
0419: istr.close();
0420: result = true;
0421: } catch (org.xml.sax.SAXParseException spe) {
0422: sLog.fine("isJbiXmlWellformed(), caught spe=" + spe);
0423: result = false;
0424: } catch (Exception e) {
0425: sLog.fine("isJbiXmlWellformed(), caught e=" + e);
0426: result = false;
0427: }
0428:
0429: // TBD log.fine("isJbiXmlWellformed(), result=" + result);
0430: return result;
0431: }
0432:
0433: /**
0434: * Description of the Field
0435: */
0436: public static final String GLASSFISH_INSTALL_ROOT = "com.sun.aas.installRoot";
0437: //not i18n
0438:
0439: /*
0440: * Scaffolded location of jbi.xsd schema file for scaffolding testing purpose
0441: */
0442: /**
0443: * Description of the Field
0444: */
0445: public static final String SCAF_JBI_SCHEMA_LOC = "../generated/xml/j2ee-modules/admin-jsf"
0446: + "/WEB-INF/classes/com/sun/jbi/jsf/scaf/jbi.xsd";
0447:
0448: /**
0449: * Gets the JbiDocument attribute of the ValidationUtilities class
0450: *
0451: * @param isNsAware Description of Parameter
0452: * @return The JbiDocument value
0453: */
0454: private static Document getJbiDocument(boolean isNsAware) {
0455: Document doc;
0456: try {
0457: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
0458: .newInstance();
0459: docBuilderFactory.setNamespaceAware(isNsAware);
0460: docBuilderFactory.setValidating(false);
0461: docBuilderFactory.setExpandEntityReferences(false);
0462: DocumentBuilder docBuilder = docBuilderFactory
0463: .newDocumentBuilder();
0464: doc = docBuilder.parse(getMetaDataEntry());
0465: } catch (javax.xml.parsers.ParserConfigurationException pcEx) {
0466: sLog.fine("getJbiDocument(), caught pcEx.getMessage()="
0467: + pcEx.getMessage());
0468: doc = null;
0469: } catch (org.xml.sax.SAXException saXex) {
0470: sLog.fine("getJbiDocument(), caught saXex.getMessage()="
0471: + saXex.getMessage());
0472: doc = null;
0473: } catch (java.io.IOException ioEx) {
0474: sLog.fine("getJbiDocument(), caught ioEx.getMessage()="
0475: + ioEx.getMessage());
0476: doc = null;
0477: }
0478: return doc;
0479: }
0480:
0481: /**
0482: * Gets the BooleanAttr attribute of the ValidationUtilities class
0483: *
0484: * @param aBooleanAttrName Description of Parameter
0485: * @param aNode Description of Parameter
0486: * @param aDefault Description of Parameter
0487: * @return The BooleanAttr value
0488: */
0489: private static boolean getBooleanAttr(String aBooleanAttrName,
0490: Node aNode, boolean aDefault) {
0491: NamedNodeMap attrs = aNode.getAttributes();
0492: Node booleanAttrNode = attrs.getNamedItem(aBooleanAttrName);
0493: boolean result = aDefault;
0494: if (null != booleanAttrNode) {
0495: String booleanAttrValue = booleanAttrNode.getNodeValue();
0496: if (Boolean.toString(true).equalsIgnoreCase(
0497: booleanAttrValue)) {
0498: result = true;
0499: }
0500: }
0501: return result;
0502: }
0503:
0504: /**
0505: * Sets the BooleanAttrs attribute of the ValidationUtilities class
0506: *
0507: * @param aPropertyNode The new BooleanAttrs value
0508: * @param aProperty The new BooleanAttrs value
0509: */
0510: private static void setBooleanAttrs(Node aPropertyNode,
0511: ConfigProperty aProperty) {
0512: NamedNodeMap attrs = aPropertyNode.getAttributes();
0513: if (null != attrs) {
0514: boolean isEncrypted = getBooleanAttr(
0515: ESBConstants.ATTR_IS_VALUE_ENCRYPTED,
0516: aPropertyNode, false);
0517: aProperty.setIsEncrypted(isEncrypted);
0518:
0519: boolean isRequired = getBooleanAttr(
0520: ESBConstants.ATTR_IS_VALUE_REQUIRED, aPropertyNode,
0521: false);
0522: aProperty.setIsRequired(isRequired);
0523: }
0524: }
0525:
0526: /**
0527: * Adds a feature to the Property attribute of the ValidationUtilities
0528: * class
0529: *
0530: * @param aPropertyNode The feature to be added to the Property
0531: * attribute
0532: * @param aConfigPropertyGroup The feature to be added to the Property
0533: * attribute
0534: */
0535: private static void addProperty(Node aPropertyNode,
0536: ConfigPropertyGroup aConfigPropertyGroup) {
0537: ConfigProperty prop = new ConfigProperty();
0538:
0539: // parse property info
0540:
0541: String nameValue = parsePropertyNode(aPropertyNode,
0542: ESBConstants.ATTR_NAME, "*name unknown*");
0543: prop.setName(nameValue);
0544:
0545: String displayNameValue = parsePropertyNode(aPropertyNode,
0546: ESBConstants.ATTR_DISPLAY_NAME, nameValue);
0547: prop.setDisplayName(displayNameValue);
0548:
0549: String displayDescriptionValue = parsePropertyNode(
0550: aPropertyNode, ESBConstants.ATTR_DISPLAY_DESCRIPTION,
0551: null);
0552: prop.setDisplayDescription(displayDescriptionValue);
0553:
0554: // TBD set type
0555: String xsdType = parsePropertyNode(aPropertyNode,
0556: ESBConstants.ATTR_TYPE, XSD_DEFAULT_TYPE);
0557: if ((ESBConstants.XSD_TYPE_BOOLEAN.equals(xsdType))
0558: || (ESBConstants.XSD_TYPE_INT.equals(xsdType))
0559: || (ESBConstants.XSD_TYPE_STRING.equals(xsdType))) {
0560: prop.setXSDType(xsdType);
0561: } else {
0562: // unsupported type
0563:
0564: // TBD warning for this property
0565: // and treat it as String?
0566: prop.setXSDType(XSD_DEFAULT_TYPE);
0567: }
0568:
0569: // TBD set (default) value
0570: String defaultValue = parsePropertyNode(aPropertyNode,
0571: ESBConstants.ATTR_DEFAULT_VALUE, null);
0572: prop.setValue(defaultValue);
0573:
0574: // check for required field, encrypted field,
0575: setBooleanAttrs(aPropertyNode, prop);
0576:
0577: // and type/range constraints
0578: addConstraints(aPropertyNode, prop);
0579:
0580: // TBD add message indicators (for component runtime config only)
0581:
0582: // add to group
0583: List<ConfigProperty> props = aConfigPropertyGroup
0584: .getConfigProperties();
0585: if (null == props) {
0586: props = new ArrayList<ConfigProperty>();
0587: aConfigPropertyGroup.setConfigProperties(props);
0588: }
0589: props.add(prop);
0590: }
0591:
0592: /**
0593: * Description of the Method
0594: *
0595: * @return Description of the Returned Value
0596: */
0597: private static ConfigPropertyGroup createDefaultPropertyGroup() {
0598: ConfigPropertyGroup result = new ConfigPropertyGroup();
0599:
0600: // Initialize default PropertyGroup
0601: result.setName("ungroupedProperties");
0602:
0603: String displayName = I18nUtilities
0604: .getResourceString("jbi.config.props.general.section.display.name");
0605: result.setDisplayName(displayName);
0606:
0607: String displayDescription = I18nUtilities
0608: .getResourceString("jbi.config.props.general.section.display.description");
0609: result.setDisplayDescription(displayDescription);
0610:
0611: // Children, if any, are added after the group is returned
0612:
0613: return result;
0614: }
0615:
0616: /**
0617: * Adds a feature to the Constraints attribute of the ValidationUtilities
0618: * class
0619: *
0620: * @param aPropertyNode The feature to be added to the Constraints
0621: * attribute
0622: * @param aProperty The feature to be added to the Constraints
0623: * attribute
0624: */
0625: private static void addConstraints(Node aPropertyNode,
0626: ConfigProperty aProperty) {
0627: NodeList propertyChildren = aPropertyNode.getChildNodes();
0628:
0629: if ((null != propertyChildren)
0630: && (0 < propertyChildren.getLength())) {
0631: for (int j = 0; j < propertyChildren.getLength(); ++j) {
0632: Node constraintOrOtherNode = propertyChildren.item(j);
0633:
0634: String nodePrefix = null;
0635: String nodeName = null;
0636: String constraintOrOtherNodeTagName = null;
0637:
0638: if (null != constraintOrOtherNode) {
0639: short nodeType = constraintOrOtherNode
0640: .getNodeType();
0641: // 1==element
0642: if (Node.ELEMENT_NODE == nodeType) {
0643: // e.g. "config" (without ":")
0644: nodePrefix = constraintOrOtherNode.getPrefix();
0645:
0646: // e.g. "config:Constraint"
0647: nodeName = constraintOrOtherNode.getNodeName();
0648:
0649: if (null != nodePrefix) {
0650: // e.g. "Constraint"
0651: constraintOrOtherNodeTagName = nodeName
0652: .substring((ESBConstants.NS_PREFIX_SEP
0653: .length() + nodePrefix
0654: .length()));
0655: }
0656:
0657: if (ESBConstants.ELT_CONSTRAINT
0658: .equals(constraintOrOtherNodeTagName)) {
0659: NamedNodeMap attrs = constraintOrOtherNode
0660: .getAttributes();
0661: if (null != attrs) {
0662: Node facetAttrNode = attrs
0663: .getNamedItem(ESBConstants.ATTR_FACET);
0664: String facetAttrValue = null;
0665: if (null != facetAttrNode) {
0666: facetAttrValue = facetAttrNode
0667: .getNodeValue();
0668:
0669: }
0670: Node valueAttrNode = attrs
0671: .getNamedItem(ESBConstants.ATTR_VALUE);
0672: String valueAttrValue = null;
0673: if (null != valueAttrNode) {
0674: valueAttrValue = valueAttrNode
0675: .getNodeValue();
0676: }
0677:
0678: // if we have a maxInclusive numeric value,
0679: // then setConstraintMaxInclusive
0680: if (ESBConstants.CONSTRAINT_FACET_ATTR_MAX_INCLUSIVE
0681: .equals(facetAttrValue)) {
0682: int max = Integer.MAX_VALUE;
0683: try {
0684: max = Integer
0685: .parseInt(valueAttrValue);
0686: } catch (Exception ex) {
0687: sLog
0688: .log(
0689: Level.FINE,
0690: ("ValidationUtils.addConstraints()"
0691: + ", maxInclusive caught ex=" + ex),
0692: ex);
0693: }
0694: aProperty
0695: .setConstraintMaxInclusive(max);
0696: sLog
0697: .fine("ValidationUtilities.addConstraints()"
0698: + ", maxInclusive="
0699: + max);
0700: }
0701:
0702: // else if we have a minInclusive numeric value,
0703: // then setConstraintMinInclusive
0704: else if (ESBConstants.CONSTRAINT_FACET_ATTR_MIN_INCLUSIVE
0705: .equals(facetAttrValue)) {
0706: int min = Integer.MIN_VALUE;
0707: try {
0708: min = Integer
0709: .parseInt(valueAttrValue);
0710: } catch (Exception ex) {
0711: sLog
0712: .log(
0713: Level.FINE,
0714: ("ValidationUtils.addConstraints()"
0715: + ", minInclusive caughght ex=" + ex),
0716: ex);
0717: }
0718: aProperty
0719: .setConstraintMinInclusive(min);
0720: sLog
0721: .fine("ValidationUtilities.addConstraints()"
0722: + ", minInclusive="
0723: + min);
0724: }
0725:
0726: // else if we have an enumeration value,
0727: // add it to the list
0728: else if (ESBConstants.CONSTRAINT_FACET_ATTR_ENUMERATION
0729: .equals(facetAttrValue)) {
0730: // (if the list is empty, create it)
0731: List<String> choices = aProperty
0732: .getConstraintEnumeration();
0733: if (null == choices) {
0734: choices = new ArrayList<String>();
0735:
0736: aProperty
0737: .setConstraintEnumeration(choices);
0738: }
0739: choices.add(valueAttrValue);
0740: sLog
0741: .fine("ValidationUtilities.addConstraints()"
0742: + ", enumeration="
0743: + valueAttrValue);
0744:
0745: }
0746:
0747: // else what?
0748: else {
0749: sLog
0750: .fine("ValidationUtilities.addConstraints()"
0751: + ", skipping unsupported facetAttrValue="
0752: + facetAttrValue);
0753: }
0754: }
0755: }
0756: }
0757: }
0758: }
0759: }
0760: }
0761:
0762: /**
0763: * Description of the Method
0764: *
0765: * @param aPropertyGroupNode Description of Parameter
0766: * @return Description of the Returned Value
0767: */
0768: private static ConfigPropertyGroup createPropertyGroup(
0769: Node aPropertyGroupNode) {
0770: ConfigPropertyGroup result = new ConfigPropertyGroup();
0771:
0772: // Initialize from PropertyGroup node attributes
0773: String groupName = parsePropertyNode(aPropertyGroupNode,
0774: ESBConstants.ATTR_NAME, "*group name unknown*");
0775: result.setName(groupName);
0776:
0777: String groupDisplayName = parsePropertyNode(aPropertyGroupNode,
0778: ESBConstants.ATTR_DISPLAY_NAME, groupName);
0779: result.setDisplayName(groupDisplayName);
0780:
0781: String groupDisplayDescription = parsePropertyNode(
0782: aPropertyGroupNode,
0783: ESBConstants.ATTR_DISPLAY_DESCRIPTION, null);
0784: result.setDisplayDescription(groupDisplayDescription);
0785:
0786: // add children configuration properties
0787: NodeList propertyElements = aPropertyGroupNode.getChildNodes();
0788: if ((null != propertyElements)
0789: && (0 < propertyElements.getLength())) {
0790: for (int j = 0; j < propertyElements.getLength(); ++j) {
0791: Node propNode = propertyElements.item(j);
0792:
0793: String nodePrefix = null;
0794: String nodeName = null;
0795: String propNodeTagName = null;
0796: if (null != propNode) {
0797: short nodeType = propNode.getNodeType();
0798: // if 1==element
0799: if (Node.ELEMENT_NODE == nodeType) {
0800: nodePrefix = propNode.getPrefix();
0801:
0802: // e.g. "config" (without ":")
0803: nodeName = propNode.getNodeName();
0804:
0805: // e.g. "config:Property"
0806: if (null != nodePrefix) {
0807: propNodeTagName = nodeName
0808: .substring((ESBConstants.NS_PREFIX_SEP
0809: .length() + nodePrefix
0810: .length()));
0811: }
0812: }
0813: sLog
0814: .fine("ValidationUtilities.createPropertyGroup()"
0815: + ", nodeType="
0816: + nodeType
0817: + ", nodePrefix="
0818: + nodePrefix
0819: + ", nodeName="
0820: + nodeName
0821: + ", propNodeTagName="
0822: + propNodeTagName);
0823: }
0824:
0825: if (ESBConstants.ELT_PROP.equals(propNodeTagName)) {
0826: sLog
0827: .fine("ValidationUtilities.createPropertyGroup() found Property element");
0828: addProperty(propNode, result);
0829: } else {
0830: sLog
0831: .fine("ValidationUtilities.createPropertyGroup() skipping unsupported \""
0832: + propNodeTagName + "\" element");
0833: }
0834: }
0835: }
0836:
0837: return result;
0838: }
0839:
0840: /**
0841: * determines if a given XPATH expression is found in a given JBI
0842: * descriptor.
0843: *
0844: * @param anXpathNameExpression Description of Parameter
0845: * @param anXpathDescExpression Description of Parameter
0846: * @param aJbiDescriptor The jbi.xml DOM Document
0847: * @return true if the XPATH expression is found,
0848: * else false
0849: */
0850: private static boolean tryXpathExpInDescriptor(
0851: String anXpathNameExpression, String anXpathDescExpression,
0852: Document aJbiDescriptor) {
0853: boolean result = false;
0854:
0855: ArchiveBean archiveBean = BeanUtilities.getArchiveBean();
0856: sLog.fine("ValidationUtilities.tryXpathExpInDescriptor("
0857: + anXpathNameExpression + ", " + anXpathDescExpression
0858: + "," + aJbiDescriptor + ")");
0859:
0860: try {
0861: NodeList nodeList = XPathAPI.selectNodeList(aJbiDescriptor,
0862: anXpathNameExpression);
0863:
0864: if (0 != nodeList.getLength()) {
0865: NodeList descNodeList = XPathAPI.selectNodeList(
0866: aJbiDescriptor, anXpathDescExpression);
0867: if ((null != descNodeList.item(0))
0868: && (null != descNodeList.item(0)
0869: .getFirstChild())) {
0870: archiveBean.setDescription(descNodeList.item(0)
0871: .getFirstChild().getNodeValue());
0872: } else {
0873: archiveBean.setDescription("");
0874: // none provided (BT CR 6529558)
0875: }
0876: if ((null != nodeList.item(0))
0877: && (null != nodeList.item(0).getFirstChild())) {
0878: archiveBean.setJbiName(nodeList.item(0)
0879: .getFirstChild().getNodeValue());
0880: result = true;
0881: } else {
0882: // result = false; // required name missing
0883: }
0884: }
0885: } catch (Exception ex) {
0886: sLog.log(Level.FINE,
0887: ("ValidationUtilities.tryXpathExpInDescriptor(...)"
0888: + ", while parsing jbi.xml, caught ex="),
0889: ex);
0890: }
0891:
0892: sLog
0893: .fine("ValidationUtilities.tryXpathExpInDescriptor(...), result="
0894: + result);
0895: return result;
0896: }
0897:
0898: /**
0899: * Description of the Method
0900: *
0901: * @param is Description of Parameter
0902: */
0903: private static void close(InputStream is) {
0904: if (is != null) {
0905: try {
0906: is.close();
0907: } catch (java.io.IOException ioe) {
0908: sLog
0909: .fine("IO Exception error in closing the zip archive"
0910: + ioe.getMessage());
0911: }
0912: }
0913: }
0914:
0915: /**
0916: * Description of the Method
0917: *
0918: * @param aPropertyNode Description of Parameter
0919: * @param anAttrName Description of Parameter
0920: * @param aDefaultValue Description of Parameter
0921: * @return Description of the Returned Value
0922: */
0923: private static String parsePropertyNode(Node aPropertyNode,
0924: String anAttrName, String aDefaultValue) {
0925: NamedNodeMap attrs = aPropertyNode.getAttributes();
0926: String attrValue = aDefaultValue;
0927: if (null != attrs) {
0928: Node attrNode = attrs.getNamedItem(anAttrName);
0929: if (null != attrNode) {
0930: attrValue = attrNode.getNodeValue();
0931: }
0932: }
0933: return attrValue;
0934: }
0935:
0936: /**
0937: * Controls printing of diagnostic messages to the log
0938: */
0939: private static Logger sLog = JBILogger.getInstance();
0940:
0941: /*
0942: * Expected location of jbi.xml descriptor file in jbi archive
0943: */
0944: private static final String JBI_MANIFEST = "META-INF/jbi.xml";
0945: // not i18n
0946:
0947: /**
0948: * XPATH expression to extract a binding-component name from a JBI Binding
0949: * Component archive's /META-INF/jbi.xml
0950: */
0951: private static final String XPATH_BC_NAME = "/jbi/component[@type='binding-component']/identification/name";
0952:
0953: /**
0954: * XPATH expression to extract a binding-component description from a JBI
0955: * Binding Component archive's /META-INF/jbi.xml
0956: */
0957: private static final String XPATH_BC_DESCR = "/jbi/component[@type='binding-component']/identification/description";
0958:
0959: /**
0960: * XPATH expression to extract a service-assembly name from a JBI Service
0961: * Assembly archive's /META-INF/jbi.xml
0962: */
0963: private static final String XPATH_SA_NAME = "/jbi/service-assembly/identification/name";
0964:
0965: /**
0966: * XPATH expression to extract a service-assembly description from a JBI
0967: * Service Assembly archive's /META-INF/jbi.xml
0968: */
0969: private static final String XPATH_SA_DESCR = "/jbi/service-assembly/identification/description";
0970:
0971: /**
0972: * XPATH expression to extract a service-engine name from a JBI Service
0973: * Engine archive's /META-INF/jbi.xml
0974: */
0975: private static final String XPATH_SE_NAME = "/jbi/component[@type='service-engine']/identification/name";
0976:
0977: /**
0978: * XPATH expression to extract a service-engine description from a JBI
0979: * Service Engine archive's /META-INF/jbi.xml
0980: */
0981: private static final String XPATH_SE_DESCR = "/jbi/component[@type='service-engine']/identification/description";
0982:
0983: /**
0984: * XPATH expression to extract a shared-library name from a JBI Shared
0985: * Library archive's /META-INF/jbi.xml
0986: */
0987: private static final String XPATH_SL_NAME = "/jbi/shared-library/identification/name";
0988:
0989: /**
0990: * XPATH expression to extract a shared-library description from a JBI
0991: * Shared Library archive's /META-INF/jbi.xml
0992: */
0993: private static final String XPATH_SL_DESCR = "/jbi/shared-library/identification/description";
0994:
0995: private static final String IDENT_EXT_NS = "http://www.sun.com/jbi/descriptor/identification";
0996: private static final String IDENT_EXT_VERS_INFOG_ELT_NAME = "VersionInfo";
0997: private static final String LOGGING_EXT_LOGGING_ELT_NAME = "Logging";
0998: private static final String LOGGING_EXT_LOGGER_ELT_NAME = "logger";
0999: private static final String LOGGING_EXT_NS = "http://www.sun.com/jbi/descriptor/logging";
1000:
1001: private static final int BLOCK_SIZE = 4096;
1002: private static final boolean IS_NS_AWARE = true;
1003: private static final boolean IS_NOT_NS_AWARE = false;
1004: private static final String XSD_DEFAULT_TYPE = ESBConstants.XSD_TYPE_STRING;
1005:
1006: // xsd:string
1007:
1008: /**
1009: * prevents instantiation
1010: */
1011: private ValidationUtilities() {
1012: }
1013:
1014: }
|