KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > pluto > driver > deploy > PortletApplicationExploder


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.pluto.driver.deploy;
17
18 import java.io.BufferedReader JavaDoc;
19 import java.io.File JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.io.StreamTokenizer JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.jar.JarEntry JavaDoc;
28 import java.util.jar.JarFile JavaDoc;
29
30 /**
31  * @author <a HREF="ddewolf@apache.org">David H. DeWolf</a>
32  * @version 1.0
33  * @since Mar 6, 2005
34  */

35 class PortletApplicationExploder {
36
37     private File JavaDoc destination;
38
39     private boolean validateJsp;
40
41     /**
42      *
43      * @param destination
44      */

45     public PortletApplicationExploder(File JavaDoc destination) {
46         this.destination = destination;
47     }
48
49     public PortletApplicationExploder(File JavaDoc destination, boolean validate) {
50         this(destination);
51         this.validateJsp = validate;
52     }
53
54     public void explode(File JavaDoc war) throws IOException JavaDoc {
55         File JavaDoc destination = getDestinationDirectory(war.getName());
56         destination.delete();
57         destination.mkdirs();
58         JarFile JavaDoc jarFile = new JarFile JavaDoc(war);
59
60         Enumeration JavaDoc files = jarFile.entries();
61         while (files.hasMoreElements()) {
62             JarEntry JavaDoc entry = (JarEntry JavaDoc) files.nextElement();
63             String JavaDoc fileName = entry.getName();
64             InputStream JavaDoc is = jarFile.getInputStream(entry);
65             if(validateJsp && !entry.isDirectory() && entry.getName().endsWith(".jsp")) {
66
67                 validateJsp(is);
68             }
69
70                 File JavaDoc file = new File JavaDoc(destination, fileName);
71                 if (entry.isDirectory()) {
72                     file.mkdirs();
73                 } else {
74                     file.getParentFile().mkdirs();
75                     byte[] buffer = new byte[1024];
76                     int length = 0;
77                     is = jarFile.getInputStream(entry);
78                     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
79                     while ((length = is.read(buffer)) >= 0) {
80                         fos.write(buffer, 0, length);
81                     }
82                     fos.close();
83             }
84         }
85
86     }
87
88     private void validateJsp(InputStream JavaDoc is) throws IOException JavaDoc {
89         Reader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
90         StreamTokenizer JavaDoc st = new StreamTokenizer JavaDoc(r);
91         st.quoteChar('\'');
92         st.quoteChar('"');
93         while(st.nextToken()!=StreamTokenizer.TT_EOF) {
94             if(st.ttype=='\'' || st.ttype=='"'){
95                 String JavaDoc sval = st.sval;
96                 String JavaDoc sqc = Character.toString((char)st.ttype);
97                 if(sval.equals("/WEB-INF/tld/portlet.tld")){
98                     System.out.println("Warning: " + sqc+st.sval+sqc + " has been found in file " + ". Use instead " +sqc+"http://java.sun.com/portlet"+sqc+" with your portlet taglib declaration!\n");
99                     break;
100                 }
101             }
102         }
103     }
104
105     private File JavaDoc getDestinationDirectory(String JavaDoc warName) {
106         int extLocation = warName.indexOf(".");
107         warName = warName.substring(0, extLocation);
108         return new File JavaDoc(destination, warName);
109     }
110
111 }
112
113
Popular Tags