View Javadoc

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;
19  import java.io.File;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.io.Reader;
25  import java.io.StreamTokenizer;
26  import java.util.Enumeration;
27  import java.util.jar.JarEntry;
28  import java.util.jar.JarFile;
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 destination;
38  
39      private boolean validateJsp;
40  
41      /***
42       *
43       * @param destination
44       */
45      public PortletApplicationExploder(File destination) {
46          this.destination = destination;
47      }
48  
49      public PortletApplicationExploder(File destination, boolean validate) {
50          this(destination);
51          this.validateJsp = validate;
52      }
53  
54      public void explode(File war) throws IOException {
55          File destination = getDestinationDirectory(war.getName());
56          destination.delete();
57          destination.mkdirs();
58          JarFile jarFile = new JarFile(war);
59  
60          Enumeration files = jarFile.entries();
61          while (files.hasMoreElements()) {
62              JarEntry entry = (JarEntry) files.nextElement();
63              String fileName = entry.getName();
64              InputStream is = jarFile.getInputStream(entry);
65              if(validateJsp && !entry.isDirectory() && entry.getName().endsWith(".jsp")) {
66  
67                  validateJsp(is);
68              }
69  
70                  File file = new File(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 fos = new FileOutputStream(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 is) throws IOException {
89          Reader r = new BufferedReader(new InputStreamReader(is));
90          StreamTokenizer st = new StreamTokenizer(r);
91          st.quoteChar('\'');
92          st.quoteChar('"');
93          while(st.nextToken()!=StreamTokenizer.TT_EOF) {
94              if(st.ttype=='\'' || st.ttype=='"'){
95                  String sval = st.sval;
96                  String 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 getDestinationDirectory(String warName) {
106         int extLocation = warName.indexOf(".");
107         warName = warName.substring(0, extLocation);
108         return new File(destination, warName);
109     }
110 
111 }
112