KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > plan > Util


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.share.plan;
21
22 import org.w3c.dom.*;
23 import org.netbeans.modules.schema2beans.*;
24
25 import java.util.jar.JarOutputStream JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28 import java.util.jar.JarEntry JavaDoc;
29
30 import org.netbeans.modules.j2ee.sun.dd.api.DDProvider;
31 import org.netbeans.modules.j2ee.sun.dd.api.DDException;
32 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp;
33
34 /** Utility functions for deployment plan objects
35  * @author vkraemer
36  */

37 public class Util {
38
39     /** Creates a new instance of Util */
40     private Util() {
41     }
42     
43     /** Compile a textual plan into its jar file form
44      *
45      * Converts an xml file that conforms to the deployment-plan.dtd
46      * and changes it into a jar file, suitable for the SJS8.0PE
47      * DeploymentManager implementation.
48      * @param plan The textual deployment plan
49      * @param jar The resulting jar file
50      * @throws IOException in case of trouble
51      */

52     public static void convert(InputStream JavaDoc plan, JarOutputStream JavaDoc jar) throws java.io.IOException JavaDoc {
53         DeploymentPlan dp = null;
54         Throwable JavaDoc cause = null;
55
56         Document doc = null;
57         // read in the stream content as an xml document...
58
try {
59              doc = GraphManager.createXmlDocument(plan, false);
60         }
61         catch (RuntimeException JavaDoc re) {
62             giveUp(re);
63         }
64
65         // try to treat that document as a deployment-plan
66
try {
67             dp = DeploymentPlan.createGraph(doc);
68         }
69         catch (org.netbeans.modules.schema2beans.Schema2BeansException s2be) {
70             // this may happen if the plan is from a a web app
71
cause = s2be;
72         }
73         if (null == dp) {
74             // try to correct for a webmod plan, which is just the sun-web.xml
75
SunWebApp swa = null;
76             try {
77                 // treat the document as a sun-web-app
78
swa = DDProvider.getDefault().getWebDDRoot(doc);
79                 dp = DeploymentPlan.createGraph();
80                 FileEntry fe = new FileEntry();
81                 fe.setName("sun-web.xml");
82                 String JavaDoc s = new String JavaDoc();
83                 java.io.StringWriter JavaDoc strWriter = new java.io.StringWriter JavaDoc();
84                 swa.write(strWriter);
85                 fe.setContent(strWriter.toString());
86                 dp.addFileEntry(fe);
87             } catch(DDException ex) {
88                 giveUp(ex);
89             } catch (org.netbeans.modules.schema2beans.Schema2BeansException s2bX) {
90                 giveUp(s2bX);
91             } catch (java.beans.PropertyVetoException JavaDoc pv) {
92                 giveUp(pv);
93             }
94         }
95         
96         int index = dp.sizeFileEntry();
97         for (int i = 0; i < index; i++) {
98             FileEntry fe = dp.getFileEntry(i);
99             String JavaDoc name = fe.getUri();
100             if (null == name)
101                 name = hashify(fe.getName());
102             else
103                 name += "." + hashify(fe.getName());
104             JarEntry JavaDoc ent = new JarEntry JavaDoc(name);
105             jar.putNextEntry(ent);
106             String JavaDoc content = fe.getContent();
107             jar.write(content.getBytes());
108             jar.closeEntry();
109         }
110     }
111     
112     private static void giveUp(Throwable JavaDoc s2be) throws java.io.IOException JavaDoc {
113         java.io.IOException JavaDoc ioe = new java.io.IOException JavaDoc("plan file issue");
114         ioe.initCause(s2be);
115         throw ioe;
116     }
117     
118     private static String JavaDoc hashify(String JavaDoc path) {
119         return path.replace('/','#');
120     }
121             
122 }
123
Popular Tags