📄 servletcontainerutils.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//package org.jahia.utils;import java.io.*; // Fileimport java.util.*; // HashMapimport javax.servlet.*; // ServletContextimport org.jahia.data.constants.JahiaConstants; // JahiaConstantspublic class ServletContainerUtils{ public static HashMap infos = null; //------------------------------------------------------------------------- /** * Default constructor. */ public ServletContainerUtils () { // nothing to do... } /** * Get server specific informations, like it's name (Orion, Tomcat, etc.) * and it's server home disk path. This method returns the result in a * HashMap with these keys: <code>type</code> and <code>home</code>. * * @author Khue N'Guyen * @author Alexandre Kraft */ public static HashMap getServerInformations( ServletConfig config ) { ServletContext context = config.getServletContext(); if( infos != null ) { return infos; } else { // initialize the result hashmap... infos = new HashMap(); infos.put("info", context.getServerInfo() ); JahiaConsole.println("getServerInformations","serverinfo = " + context.getServerInfo() ); // try to get the server type if ( context.getServerInfo().toLowerCase().indexOf(JahiaConstants.SERVER_ORION)>=0 ) { infos.put("type", JahiaConstants.SERVER_ORION); // try to get the server home disk path String contextRealPath = context.getRealPath("." + File.separator); // last occurence of the server name int pos = contextRealPath.toLowerCase().lastIndexOf("orion"); if ( pos>=0 ){ int pos2 = contextRealPath.toLowerCase().indexOf(File.separator,pos); if ( pos2 >=0 ){ infos.put("home", contextRealPath.substring(0, pos2) + File.separator); } } } else if ( context.getServerInfo().toLowerCase().indexOf("tomcat")>=0 ) { infos.put("type",JahiaConstants.SERVER_TOMCAT); JahiaConsole.println("getServerInformations", "servertype = " +JahiaConstants.SERVER_TOMCAT ); // try to get the server home disk path String contextRealPath = context.getRealPath("/" + config.getServletName()); String jahiaRealPath = JahiaTools.replacePattern(contextRealPath,config.getServletName(),""); jahiaRealPath = JahiaTools.replacePattern(jahiaRealPath,File.separator+"."+File.separator,File.separator); File jahiaDir = new File(jahiaRealPath); // ../tomcat/webapps/jahia JahiaConsole.println("getServerInformations", "jahia dir name = " + jahiaDir.getAbsolutePath() ); String webAppPath = jahiaDir.getParent(); File webAppDir = new File(webAppPath); JahiaConsole.println("getServerInformations", "webapp dir = " + webAppDir.getAbsolutePath() ); String tomcatPath = webAppDir.getParent(); File tomcatDir = new File(tomcatPath); JahiaConsole.println("getServerInformations", "tomcat dir name = " + tomcatDir.getName() ); /* File tomcatParentDir = tomcatDir.getParentFile(); File[] files = tomcatParentDir.listFiles(); for ( int i=0 ; i<files.length ; i++ ){ if ( files[i].isDirectory() && (files[i].getName().indexOf(JahiaConstants.SERVER_TOMCAT) != -1) ){ JahiaConsole.println("getServerInformations", "Files[i] = " + files[i].getAbsolutePath() ); tomcatDir = files[i]; break; } } */ JahiaConsole.println("getServerInformations", "tomcat real path = " + tomcatDir.getAbsolutePath() ); String serverPath = tomcatDir.getAbsolutePath(); if ( !serverPath.endsWith(File.separator) ){ serverPath+=File.separator; } infos.put("home", serverPath); } else { infos.put("type", "unknown"); infos.put("home", "unknown"); } return infos; } } // end getServerInformations} // end ServletContainerUtils
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -