KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > webapps > JahiaWebAppsWarHandler


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
//
14
// JahiaWebAppsWarHandler
15
//
16
// NK 13.01.2001
17
//
18
//
19

20 package org.jahia.data.webapps;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.jahia.exceptions.JahiaException;
27 import org.jahia.utils.JahiaConsole;
28 import org.jahia.utils.zip.JahiaArchiveFileHandler;
29
30 /**
31  * This class is responsible for loading data from a WebApp War File
32  * The Information are loaded from the WEB-INF\web.xml file :
33  *
34  * -----------------------------------------------------------------
35  * IMPORTANT !!!!! Must call the method closeArchiveFile()
36  * to be able to delete the file later !!!
37  * -----------------------------------------------------------------
38  *
39  * @author Khue ng
40  * @version 1.0
41  *
42  */

43 public class JahiaWebAppsWarHandler {
44
45    /** The web.xml file **/
46    private static final String JavaDoc WEB_XML_FILE = "WEB-INF/web.xml";
47    /** The Servlet Type WebApp **/
48    private static final int SERVLET_TYPE = 1;
49    /** The JSP Type WebApp **/
50    private static final int JSP_TYPE = 2;
51    /** The Full path to the War File **/
52    private String JavaDoc m_FilePath;
53    /** The Archive File **/
54    private JahiaArchiveFileHandler m_ArchFile;
55    /** The Jahia Web App War Package **/
56    private JahiaWebAppsWarPackage m_WebAppsPackage;
57    /** The Web_App_Xml to store data extracted from web.xml file **/
58    private Web_App_Xml m_WebXmlDoc ;
59
60    /**
61     * Constructor is initialized with the war File
62     *
63     */

64    public JahiaWebAppsWarHandler (String JavaDoc filePath)
65    throws JahiaException {
66
67       m_FilePath = filePath;
68       File JavaDoc f = new File JavaDoc(filePath);
69       try {
70
71          m_ArchFile = new JahiaArchiveFileHandler( f.getAbsolutePath() );
72
73       } catch (IOException JavaDoc e ) {
74
75          String JavaDoc errMsg = "Failed creating an Archive File Handler " ;
76          JahiaConsole.println("JahiaWebAppsWarHandler:: Constructor", errMsg + "\n" + e.toString());
77          throw new JahiaException ("JahiaWebAppsWarHandler", errMsg ,
78                                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
79
80       }
81
82       try {
83          buildWebAppsWarPackage();
84       } catch ( JahiaException je ) {
85
86          if ( m_ArchFile != null ){
87             m_ArchFile.closeArchiveFile();
88          }
89
90          JahiaConsole.println("JahiaWebAppsWarHandler:: Constructor", "error building the WebApssWarPackage" + je.toString());
91          throw new JahiaException ("JahiaWebAppsWarPackage", "error building the WebApssWarPackage" ,
92                                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
93
94       }
95
96
97    }
98
99
100    /**
101     * Extract data from th web.xml file and build the JahiaWebAppsWarPackage
102     *
103     */

104    protected void buildWebAppsWarPackage() throws JahiaException {
105
106       // extract data from the web.xml file
107
try {
108          File JavaDoc tmpFile = m_ArchFile.extractFile(WEB_XML_FILE);
109          m_WebXmlDoc = new Web_App_Xml(tmpFile.getAbsolutePath());
110          m_WebXmlDoc.extractDocumentData();
111          tmpFile.deleteOnExit();
112          tmpFile.delete();
113       } catch (IOException JavaDoc ioe){
114
115          String JavaDoc errMsg = "Failed extracting web.xml file data " ;
116          JahiaConsole.println("JahiaWebAppsWarPackage:: Constructor", errMsg + "\n" + ioe.toString());
117          throw new JahiaException ("JahiaWebAppsWarPackage", errMsg ,
118                                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
119
120       }
121
122       // Actually the Context Root for the web application is the war filename without the .war extension
123
String JavaDoc contextRoot = removeFileExtension((new File JavaDoc(m_FilePath)).getName(),".war");
124
125       // build the list of the Web Apps Definition
126
m_WebAppsPackage = new JahiaWebAppsWarPackage(contextRoot);
127
128       Vector JavaDoc servlets = m_WebXmlDoc.getServlets();
129       int size = servlets.size();
130
131
132       Servlet_Element servlet = null;
133         
134       String JavaDoc webAppName = m_WebXmlDoc.getDisplayName();
135       if ( webAppName == null || webAppName.length()<=0 ){
136             webAppName = removeFileExtension((new File JavaDoc(m_FilePath)).getName(),".war");
137       }
138
139       JahiaWebAppDef webAppDef = new JahiaWebAppDef( webAppName,
140                                                      contextRoot
141                                                    );
142     
143       webAppDef.addRoles(m_WebXmlDoc.getRoles());
144             
145       for ( int i=0 ; i<size ; i++ ){
146
147          servlet = (Servlet_Element)servlets.get(i);
148
149          webAppDef.addServlet(servlet);
150         
151       }
152
153      m_WebAppsPackage.addWebAppDef(webAppDef);
154
155
156    }
157
158
159    /**
160     * Returns the WebApps Package Object
161     *
162     * @return (JahiaWebAppsPackage) the Jahia WebApps Package Object
163     */

164    public JahiaWebAppsWarPackage getWebAppsPackage(){
165
166       return m_WebAppsPackage;
167
168    }
169
170
171    /**
172     * Unzip the contents of the jar file in it's current folder
173     *
174     */

175    public void unzip() throws JahiaException {
176
177       // Unzip the file
178
m_ArchFile.unzip();
179
180    }
181
182
183    /**
184     * Unzip the contents of the jar file in a gived folder
185     *
186     * @param (String) path , the path where to extract file
187     */

188    public void unzip(String JavaDoc path) throws JahiaException {
189
190       // Unzip the file
191
m_ArchFile.unzip(path);
192
193    }
194
195
196    /**
197     * Unzip an entry in a gived folder
198     *
199     * @param (String) entryName , the name of the entry
200     * @param (String) path , the path where to extract file
201     */

202    public void extractEntry( String JavaDoc entryName,
203                              String JavaDoc path) throws JahiaException {
204
205       // Unzip the entry
206
m_ArchFile.extractEntry(entryName, path);
207
208    }
209
210
211    /**
212     * Close the Jar file
213     *
214     */

215    public void closeArchiveFile(){
216
217       if ( m_ArchFile != null ) {
218          m_ArchFile.closeArchiveFile();
219       }
220    }
221
222
223    /**
224     * Return the file name of the war file without the .war extension
225     *
226     * @param (String) filename , the complete file name with extension
227     * @param (String) ext , the extension to remove
228     * @return(String) the filename without a gived extension
229     */

230    protected String JavaDoc removeFileExtension(String JavaDoc filename, String JavaDoc ext){
231
232       String JavaDoc name = filename.toLowerCase(); // work on a copy
233
if ( name.endsWith( ext.toLowerCase() ) ){
234          return ( filename.substring(0,name.lastIndexOf(ext.toLowerCase())) );
235       }
236       return filename;
237    }
238
239
240 } // End Class JahiaWebAppsWarHandler
241
Popular Tags