001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * Portions Copyrighted 2007 Sun Microsystems, Inc.
016: */
017: package org.netbeans.modules.sun.manager.jbi.nodes;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.jar.JarEntry;
023: import java.util.jar.JarFile;
024: import javax.xml.parsers.DocumentBuilder;
025: import javax.xml.parsers.DocumentBuilderFactory;
026: import javax.xml.parsers.ParserConfigurationException;
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029: import org.w3c.dom.NodeList;
030:
031: /**
032: *
033: * @author jqian
034: */
035: public abstract class JBIArtifactValidator {
036:
037: private static ComponentValidator serviceEngineValidator;
038: private static ComponentValidator bindingComponentValidator;
039: private static SharedLibraryValidator sharedLibraryValidator;
040: private static ServiceAssemblyValidator serviceAssemblyValidator;
041:
042: public static JBIArtifactValidator getServiceEngineValidator(
043: String name) {
044: if (serviceEngineValidator == null) {
045: serviceEngineValidator = new ServiceEngineValidator();
046: }
047: serviceEngineValidator.setComponentName(name);
048:
049: return serviceEngineValidator;
050: }
051:
052: public static JBIArtifactValidator getBindingComponentValidator(
053: String name) {
054: if (bindingComponentValidator == null) {
055: bindingComponentValidator = new BindingComponentValidator();
056: }
057: bindingComponentValidator.setComponentName(name);
058:
059: return bindingComponentValidator;
060: }
061:
062: public static JBIArtifactValidator getSharedLibraryValidator() {
063: if (sharedLibraryValidator == null) {
064: sharedLibraryValidator = new SharedLibraryValidator();
065: }
066:
067: return sharedLibraryValidator;
068: }
069:
070: public static JBIArtifactValidator getServiceAssemblyValidator() {
071: if (serviceAssemblyValidator == null) {
072: serviceAssemblyValidator = new ServiceAssemblyValidator();
073: }
074:
075: return serviceAssemblyValidator;
076: }
077:
078: public boolean validate(File zipFile) {
079: boolean isRightType = false;
080:
081: DocumentBuilderFactory factory = DocumentBuilderFactory
082: .newInstance();
083: DocumentBuilder docBuilder = null;
084: try {
085: docBuilder = factory.newDocumentBuilder();
086: } catch (ParserConfigurationException ex) {
087: ex.printStackTrace();
088: }
089:
090: if (docBuilder != null) {
091: JarFile jf = null;
092: try {
093: jf = new JarFile(zipFile);
094: JarEntry je = (JarEntry) jf
095: .getEntry("META-INF/jbi.xml"); // NOI18N
096: if (je != null) {
097: InputStream is = jf.getInputStream(je);
098: Document doc = docBuilder.parse(is);
099: isRightType = validate(doc); // very basic type checking
100: }
101: } catch (Exception e) {
102: e.printStackTrace();
103: } finally {
104: if (jf != null) {
105: try {
106: jf.close();
107: } catch (IOException e) {
108: }
109: }
110: }
111: }
112:
113: return isRightType;
114: }
115:
116: protected abstract boolean validate(Document jbiDoc);
117:
118: //==========================================================================
119:
120: private static class ComponentValidator extends
121: JBIArtifactValidator {
122:
123: private String componentType;
124: private String componentName;
125:
126: ComponentValidator(String componentType) {
127: this .componentType = componentType;
128: }
129:
130: public void setComponentName(String componentName) {
131: this .componentName = componentName;
132: }
133:
134: protected boolean validate(Document jbiDoc) {
135: NodeList ns = jbiDoc.getElementsByTagName("component"); // NOI18N
136: if (ns.getLength() > 0) {
137: Element e = (Element) ns.item(0);
138: String type = e.getAttribute("type"); // NOI18N
139: if (type != null && type.equals(componentType)) {
140: if (componentName == null) {
141: return true;
142: } else {
143: String name = null;
144: NodeList ids = e
145: .getElementsByTagName("identification"); // NOI18N
146: if (ids.getLength() > 0) {
147: Element id = (Element) ids.item(0);
148: NodeList names = id
149: .getElementsByTagName("name"); // NOI18N
150: if (names.getLength() > 0) {
151: Element n = (Element) names.item(0);
152: name = n.getFirstChild().getNodeValue();
153: }
154: }
155: if (componentName.equals(name)) {
156: return true;
157: }
158: }
159: }
160: }
161: return false;
162: }
163: }
164:
165: private static class ServiceEngineValidator extends
166: ComponentValidator {
167: ServiceEngineValidator() {
168: super ("service-engine"); // NOI18N
169: }
170: }
171:
172: private static class BindingComponentValidator extends
173: ComponentValidator {
174: BindingComponentValidator() {
175: super ("binding-component"); // NOI18N
176: }
177: }
178:
179: private static class SharedLibraryValidator extends
180: JBIArtifactValidator {
181:
182: protected boolean validate(Document jbiDoc) {
183: NodeList ns = jbiDoc.getElementsByTagName("shared-library"); // NOI18N
184: return ns.getLength() > 0;
185: }
186: }
187:
188: private static class ServiceAssemblyValidator extends
189: JBIArtifactValidator {
190:
191: protected boolean validate(Document jbiDoc) {
192: NodeList ns = jbiDoc
193: .getElementsByTagName("service-assembly"); // NOI18N
194: return ns.getLength() == 1;
195: }
196: }
197: }
|