📄 jahiawebappswarhandler.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//////// JahiaWebAppsWarHandler//// NK 13.01.2001////package org.jahia.data.webapps;import java.io.*;import java.util.*;import java.util.jar.*;import java.util.zip.*;//import com.sun.xml.tree.*;//import com.sun.xml.parser.Parser;import org.w3c.dom.*;//import org.xml.sax.*;import org.jahia.exceptions.*;import org.jahia.utils.zip.*; // JahiaarHandlerimport org.jahia.utils.*; // JahiaCmdExecimport org.jahia.utils.xml.*; // JahiaConsole, XMLParserimport org.jahia.registries.ServicesRegistry;/** * This class is responsible for loading data from a WebApp War File * The Information are loaded from the WEB-INF\web.xml file : * * ----------------------------------------------------------------- * IMPORTANT !!!!! Must call the method closeArchiveFile() * to be able to delete the file later !!! * ----------------------------------------------------------------- * * @author Khue ng * @version 1.0 * */public class JahiaWebAppsWarHandler { /** The web.xml file **/ private static final String WEB_XML_FILE = "WEB-INF/web.xml"; /** The Servlet Type WebApp **/ private static final int SERVLET_TYPE = 1; /** The JSP Type WebApp **/ private static final int JSP_TYPE = 2; /** The Full path to the War File **/ private String m_FilePath; /** The Archive File **/ private JahiaArchiveFileHandler m_ArchFile; /** The Jahia Web App War Package **/ private JahiaWebAppsWarPackage m_WebAppsPackage; /** The Web_App_Xml to store data extracted from web.xml file **/ private Web_App_Xml m_WebXmlDoc ; /** * Constructor is initialized with the war File * */ public JahiaWebAppsWarHandler (String filePath) throws JahiaException { m_FilePath = filePath; File f = new File(filePath); try { m_ArchFile = new JahiaArchiveFileHandler( f.getAbsolutePath() ); } catch (IOException e ) { String errMsg = "Failed creating an Archive File Handler " ; JahiaConsole.println("JahiaWebAppsWarHandler:: Constructor", errMsg + "\n" + e.toString()); throw new JahiaException ("JahiaWebAppsWarHandler", errMsg , JahiaException.SERVICE_ERROR, JahiaException.ERROR); } try { buildWebAppsWarPackage(); } catch ( JahiaException je ) { if ( m_ArchFile != null ){ m_ArchFile.closeArchiveFile(); } JahiaConsole.println("JahiaWebAppsWarHandler:: Constructor", "error building the WebApssWarPackage" + je.toString()); throw new JahiaException ("JahiaWebAppsWarPackage", "error building the WebApssWarPackage" , JahiaException.SERVICE_ERROR, JahiaException.ERROR); } } /** * Extract data from th web.xml file and build the JahiaWebAppsWarPackage * */ protected void buildWebAppsWarPackage() throws JahiaException { // extract data from the web.xml file try { File tmpFile = m_ArchFile.extractFile(WEB_XML_FILE); m_WebXmlDoc = new Web_App_Xml(tmpFile.getAbsolutePath()); m_WebXmlDoc.extractDocumentData(); tmpFile.deleteOnExit(); tmpFile.delete(); } catch (IOException ioe){ String errMsg = "Failed extracting web.xml file data " ; JahiaConsole.println("JahiaWebAppsWarPackage:: Constructor", errMsg + "\n" + ioe.toString()); throw new JahiaException ("JahiaWebAppsWarPackage", errMsg , JahiaException.SERVICE_ERROR, JahiaException.ERROR); } // Actually the Context Root for the web application is the war filename without the .war extension String contextRoot = removeFileExtension((new File(m_FilePath)).getName(),".war"); // build the list of the Web Apps Definition m_WebAppsPackage = new JahiaWebAppsWarPackage(contextRoot); Vector servlets = m_WebXmlDoc.getServlets(); int size = servlets.size(); Servlet_Element servlet = null; String webAppName = m_WebXmlDoc.getDisplayName(); if ( webAppName == null || webAppName.length()<=0 ){ webAppName = removeFileExtension((new File(m_FilePath)).getName(),".war"); } JahiaWebAppDef webAppDef = new JahiaWebAppDef( webAppName, contextRoot ); webAppDef.addRoles(m_WebXmlDoc.getRoles()); for ( int i=0 ; i<size ; i++ ){ servlet = (Servlet_Element)servlets.get(i); webAppDef.addServlet(servlet); } m_WebAppsPackage.addWebAppDef(webAppDef); } /** * Returns the WebApps Package Object * * @return (JahiaWebAppsPackage) the Jahia WebApps Package Object */ public JahiaWebAppsWarPackage getWebAppsPackage(){ return m_WebAppsPackage; } /** * Unzip the contents of the jar file in it's current folder * */ public void unzip() throws JahiaException { // Unzip the file m_ArchFile.unzip(); } /** * Unzip the contents of the jar file in a gived folder * * @param (String) path , the path where to extract file */ public void unzip(String path) throws JahiaException { // Unzip the file m_ArchFile.unzip(path); } /** * Unzip an entry in a gived folder * * @param (String) entryName , the name of the entry * @param (String) path , the path where to extract file */ public void extractEntry( String entryName, String path) throws JahiaException { // Unzip the entry m_ArchFile.extractEntry(entryName, path); } /** * Close the Jar file * */ public void closeArchiveFile(){ if ( m_ArchFile != null ) { m_ArchFile.closeArchiveFile(); } } /** * Return the file name of the war file without the .war extension * * @param (String) filename , the complete file name with extension * @param (String) ext , the extension to remove * @return(String) the filename without a gived extension */ protected String removeFileExtension(String filename, String ext){ String name = filename.toLowerCase(); // work on a copy if ( name.endsWith( ext.toLowerCase() ) ){ return ( filename.substring(0,name.lastIndexOf(ext.toLowerCase())) ); } return filename; }} // End Class JahiaWebAppsWarHandler
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -