1 20 package org.jahia.data.webapps; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Vector ; 25 26 import org.jahia.exceptions.JahiaException; 27 import org.jahia.utils.JahiaConsole; 28 import org.jahia.utils.zip.JahiaArchiveFileHandler; 29 30 43 public class JahiaWebAppsWarHandler { 44 45 46 private static final String WEB_XML_FILE = "WEB-INF/web.xml"; 47 48 private static final int SERVLET_TYPE = 1; 49 50 private static final int JSP_TYPE = 2; 51 52 private String m_FilePath; 53 54 private JahiaArchiveFileHandler m_ArchFile; 55 56 private JahiaWebAppsWarPackage m_WebAppsPackage; 57 58 private Web_App_Xml m_WebXmlDoc ; 59 60 64 public JahiaWebAppsWarHandler (String filePath) 65 throws JahiaException { 66 67 m_FilePath = filePath; 68 File f = new File (filePath); 69 try { 70 71 m_ArchFile = new JahiaArchiveFileHandler( f.getAbsolutePath() ); 72 73 } catch (IOException e ) { 74 75 String 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 104 protected void buildWebAppsWarPackage() throws JahiaException { 105 106 try { 108 File 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 ioe){ 114 115 String 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 String contextRoot = removeFileExtension((new File (m_FilePath)).getName(),".war"); 124 125 m_WebAppsPackage = new JahiaWebAppsWarPackage(contextRoot); 127 128 Vector servlets = m_WebXmlDoc.getServlets(); 129 int size = servlets.size(); 130 131 132 Servlet_Element servlet = null; 133 134 String webAppName = m_WebXmlDoc.getDisplayName(); 135 if ( webAppName == null || webAppName.length()<=0 ){ 136 webAppName = removeFileExtension((new File (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 164 public JahiaWebAppsWarPackage getWebAppsPackage(){ 165 166 return m_WebAppsPackage; 167 168 } 169 170 171 175 public void unzip() throws JahiaException { 176 177 m_ArchFile.unzip(); 179 180 } 181 182 183 188 public void unzip(String path) throws JahiaException { 189 190 m_ArchFile.unzip(path); 192 193 } 194 195 196 202 public void extractEntry( String entryName, 203 String path) throws JahiaException { 204 205 m_ArchFile.extractEntry(entryName, path); 207 208 } 209 210 211 215 public void closeArchiveFile(){ 216 217 if ( m_ArchFile != null ) { 218 m_ArchFile.closeArchiveFile(); 219 } 220 } 221 222 223 230 protected String removeFileExtension(String filename, String ext){ 231 232 String name = filename.toLowerCase(); if ( name.endsWith( ext.toLowerCase() ) ){ 234 return ( filename.substring(0,name.lastIndexOf(ext.toLowerCase())) ); 235 } 236 return filename; 237 } 238 239 240 } | Popular Tags |