001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.sun.share.plan;
043:
044: import org.w3c.dom.*;
045: import org.netbeans.modules.schema2beans.*;
046:
047: import java.util.jar.JarOutputStream;
048: import java.io.InputStream;
049:
050: import java.util.jar.JarEntry;
051:
052: import org.netbeans.modules.j2ee.sun.dd.api.DDProvider;
053: import org.netbeans.modules.j2ee.sun.dd.api.DDException;
054: import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp;
055:
056: /** Utility functions for deployment plan objects
057: * @author vkraemer
058: */
059: public class Util {
060:
061: /** Creates a new instance of Util */
062: private Util() {
063: }
064:
065: /** Compile a textual plan into its jar file form
066: *
067: * Converts an xml file that conforms to the deployment-plan.dtd
068: * and changes it into a jar file, suitable for the SJS8.0PE
069: * DeploymentManager implementation.
070: * @param plan The textual deployment plan
071: * @param jar The resulting jar file
072: * @throws IOException in case of trouble
073: */
074: public static void convert(InputStream plan, JarOutputStream jar)
075: throws java.io.IOException {
076: DeploymentPlan dp = null;
077: Throwable cause = null;
078:
079: Document doc = null;
080: // read in the stream content as an xml document...
081: try {
082: doc = GraphManager.createXmlDocument(plan, false);
083: } catch (RuntimeException re) {
084: giveUp(re);
085: }
086:
087: // try to treat that document as a deployment-plan
088: try {
089: dp = DeploymentPlan.createGraph(doc);
090: } catch (org.netbeans.modules.schema2beans.Schema2BeansException s2be) {
091: // this may happen if the plan is from a a web app
092: cause = s2be;
093: }
094: if (null == dp) {
095: // try to correct for a webmod plan, which is just the sun-web.xml
096: SunWebApp swa = null;
097: try {
098: // treat the document as a sun-web-app
099: swa = DDProvider.getDefault().getWebDDRoot(doc);
100: dp = DeploymentPlan.createGraph();
101: FileEntry fe = new FileEntry();
102: fe.setName("sun-web.xml");
103: java.io.StringWriter strWriter = new java.io.StringWriter();
104: swa.write(strWriter);
105: fe.setContent(strWriter.toString());
106: dp.addFileEntry(fe);
107: } catch (DDException ex) {
108: giveUp(ex);
109: } catch (org.netbeans.modules.schema2beans.Schema2BeansException s2bX) {
110: giveUp(s2bX);
111: } catch (java.beans.PropertyVetoException pv) {
112: giveUp(pv);
113: }
114: }
115:
116: int index = dp.sizeFileEntry();
117: for (int i = 0; i < index; i++) {
118: FileEntry fe = dp.getFileEntry(i);
119: String name = fe.getUri();
120: if (null == name)
121: name = hashify(fe.getName());
122: else
123: name += "." + hashify(fe.getName());
124: JarEntry ent = new JarEntry(name);
125: jar.putNextEntry(ent);
126: String content = fe.getContent();
127: jar.write(content.getBytes());
128: jar.closeEntry();
129: }
130: }
131:
132: private static void giveUp(Throwable s2be)
133: throws java.io.IOException {
134: java.io.IOException ioe = new java.io.IOException(
135: "plan file issue");
136: ioe.initCause(s2be);
137: throw ioe;
138: }
139:
140: private static String hashify(String path) {
141: return path.replace('/', '#');
142: }
143:
144: }
|