1 13 package org.jahia.utils; 14 15 import java.io.File ; 16 import java.util.HashMap ; 17 18 import javax.naming.Context ; 19 import javax.naming.InitialContext ; 20 import javax.servlet.ServletConfig ; 21 import javax.servlet.ServletContext ; 22 23 import org.jahia.data.constants.JahiaConstants; 24 25 26 27 28 public class ServletContainerUtils 29 { 30 31 public static HashMap infos = null; 32 33 34 38 public ServletContainerUtils () 39 { 40 } 42 43 44 52 public static HashMap getServerInformations( ServletConfig config ) 53 { 54 ServletContext context = config.getServletContext(); 55 56 if( infos != null ) { 57 return infos; 58 } else { 59 60 infos = new HashMap (); 62 infos.put("info", context.getServerInfo() ); 63 64 JahiaConsole.println("getServerInformations","serverinfo = " + context.getServerInfo() ); 65 66 if ( context.getServerInfo().toLowerCase().indexOf(JahiaConstants.SERVER_ORION)>=0 ) 68 { 69 infos.put("type", JahiaConstants.SERVER_ORION); 70 71 String contextRealPath = context.getRealPath("." + File.separator); 73 74 int pos = contextRealPath.toLowerCase().lastIndexOf("orion"); 76 if ( pos>=0 ){ 77 int pos2 = contextRealPath.toLowerCase().indexOf(File.separator,pos); 78 if ( pos2 >=0 ){ 79 infos.put("home", contextRealPath.substring(0, pos2) + File.separator); 80 } 81 } 82 } 83 else if ( context.getServerInfo().toLowerCase().indexOf("tomcat")>=0 ) 84 { 85 infos.put("type",JahiaConstants.SERVER_TOMCAT); 86 87 JahiaConsole.println("getServerInformations", "servertype = " +infos.get("type") ); 88 89 String contextRealPath = context.getRealPath("/" + config.getServletName()); 91 String jahiaRealPath = JahiaTools.replacePattern(contextRealPath,config.getServletName(),""); 92 jahiaRealPath = JahiaTools.replacePattern(jahiaRealPath,File.separator+"."+File.separator,File.separator); 93 94 File jahiaDir = new File (jahiaRealPath); JahiaConsole.println("getServerInformations", "jahia dir name = " + jahiaDir.getAbsolutePath() ); 96 97 String webAppPath = jahiaDir.getParent(); 98 File webAppDir = new File (webAppPath); 99 JahiaConsole.println("getServerInformations", "webapp dir = " + webAppDir.getAbsolutePath() ); 100 101 String tomcatPath = webAppDir.getParent(); 102 File tomcatDir = new File (tomcatPath); 103 104 JahiaConsole.println("getServerInformations", "tomcat dir name = " + tomcatDir.getName() ); 105 106 118 119 JahiaConsole.println("getServerInformations", "tomcat real path = " + tomcatDir.getAbsolutePath() ); 120 121 String serverPath = tomcatDir.getAbsolutePath(); 122 123 if ( !serverPath.endsWith(File.separator) ){ 124 serverPath+=File.separator; 125 } 126 127 infos.put("home", serverPath); 128 129 } else if ( context.getServerInfo().toLowerCase().indexOf("weblogic")>=0 ) { 130 infos.put("type",JahiaConstants.SERVER_WEBLOGIC); 131 String contextRealPath = context.getRealPath("." + File.separator); 133 int pos = contextRealPath.toLowerCase().lastIndexOf("server"); 134 if ( pos>=0 ){ 135 int pos2 = contextRealPath.toLowerCase().indexOf(File.separator,pos); 136 if ( pos2 >=0 ){ 137 infos.put("home", contextRealPath.substring(0, pos2) + File.separator); 138 } 139 } 140 } else { 141 infos.put("type", "unknown"); 142 infos.put("home", "unknown"); 143 } 144 145 try { 146 Context ctx = new InitialContext (); 147 if (((String )ctx.getEnvironment().get("java.naming.factory.url.pkgs")).indexOf("org.jboss") > -1) { 148 infos.put("type",JahiaConstants.SERVER_JBOSS); 149 infos.put("info", infos.get("info") + " (JBoss integration)" ); 150 151 String contextRealPath = context.getRealPath("/" + config.getServletName()); 153 String jahiaRealPath = JahiaTools.replacePattern(contextRealPath,config.getServletName(),""); 154 jahiaRealPath = JahiaTools.replacePattern(jahiaRealPath,File.separator+"."+File.separator,File.separator); 155 156 File jahiaDir = new File (jahiaRealPath); JahiaConsole.println("getServerInformations", "jboss dir name = " + jahiaDir.getAbsolutePath() ); 158 159 String webAppPath = jahiaDir.getParent(); 160 File webAppDir = new File (webAppPath); 161 JahiaConsole.println("getServerInformations", "webapp dir = " + webAppDir.getAbsolutePath() ); 162 163 String tomcatPath = webAppDir.getParent(); 164 File tomcatDir = new File (tomcatPath); 165 166 String serverPath = tomcatDir.getAbsolutePath(); 167 168 if ( !serverPath.endsWith(File.separator) ){ 169 serverPath+=File.separator; 170 } 171 172 infos.put("home", serverPath); 173 174 } 175 } catch (Exception e) { 176 } 177 178 return infos; 179 } 180 } 182 183 184 } | Popular Tags |