KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > ServletContainerUtils


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 package org.jahia.utils;
14
15 import java.io.File JavaDoc;
16 import java.util.HashMap JavaDoc;
17
18 import javax.naming.Context JavaDoc;
19 import javax.naming.InitialContext JavaDoc;
20 import javax.servlet.ServletConfig JavaDoc;
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.jahia.data.constants.JahiaConstants;
24
25
26
27
28 public class ServletContainerUtils
29 {
30
31     public static HashMap JavaDoc infos = null;
32
33
34     //-------------------------------------------------------------------------
35
/**
36      * Default constructor.
37      */

38     public ServletContainerUtils ()
39     {
40         // nothing to do...
41
}
42
43
44    /**
45      * Get server specific informations, like it's name (Orion, Tomcat, etc.)
46      * and it's server home disk path. This method returns the result in a
47      * HashMap with these keys: <code>type</code> and <code>home</code>.
48      *
49      * @author Khue N'Guyen
50      * @author Alexandre Kraft
51      */

52     public static HashMap JavaDoc getServerInformations( ServletConfig JavaDoc config )
53     {
54         ServletContext JavaDoc context = config.getServletContext();
55
56         if( infos != null ) {
57             return infos;
58         } else {
59
60             // initialize the result hashmap...
61
infos = new HashMap JavaDoc();
62             infos.put("info", context.getServerInfo() );
63
64             JahiaConsole.println("getServerInformations","serverinfo = " + context.getServerInfo() );
65
66             // try to get the server type
67
if ( context.getServerInfo().toLowerCase().indexOf(JahiaConstants.SERVER_ORION)>=0 )
68             {
69                 infos.put("type", JahiaConstants.SERVER_ORION);
70
71                 // try to get the server home disk path
72
String JavaDoc contextRealPath = context.getRealPath("." + File.separator);
73
74                 // last occurence of the server name
75
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                 // try to get the server home disk path
90
String JavaDoc contextRealPath = context.getRealPath("/" + config.getServletName());
91                 String JavaDoc jahiaRealPath = JahiaTools.replacePattern(contextRealPath,config.getServletName(),"");
92                 jahiaRealPath = JahiaTools.replacePattern(jahiaRealPath,File.separator+"."+File.separator,File.separator);
93
94                 File JavaDoc jahiaDir = new File JavaDoc(jahiaRealPath); // ../tomcat/webapps/jahia
95
JahiaConsole.println("getServerInformations", "jahia dir name = " + jahiaDir.getAbsolutePath() );
96
97                 String JavaDoc webAppPath = jahiaDir.getParent();
98                 File JavaDoc webAppDir = new File JavaDoc(webAppPath);
99                 JahiaConsole.println("getServerInformations", "webapp dir = " + webAppDir.getAbsolutePath() );
100
101                 String JavaDoc tomcatPath = webAppDir.getParent();
102                 File JavaDoc tomcatDir = new File JavaDoc(tomcatPath);
103
104                 JahiaConsole.println("getServerInformations", "tomcat dir name = " + tomcatDir.getName() );
105
106                 /*
107                 File tomcatParentDir = tomcatDir.getParentFile();
108                 File[] files = tomcatParentDir.listFiles();
109                 for ( int i=0 ; i<files.length ; i++ ){
110                     if ( files[i].isDirectory() && (files[i].getName().indexOf(JahiaConstants.SERVER_TOMCAT) != -1) ){
111                         JahiaConsole.println("getServerInformations",
112                         "Files[i] = " + files[i].getAbsolutePath() );
113                         tomcatDir = files[i];
114                         break;
115                     }
116                 }
117                 */

118
119                 JahiaConsole.println("getServerInformations", "tomcat real path = " + tomcatDir.getAbsolutePath() );
120
121                 String JavaDoc 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                 // try to get the server home disk path
132
String JavaDoc 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 JavaDoc ctx = new InitialContext JavaDoc();
147                 if (((String JavaDoc)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                     // try to get the server home disk path
152
String JavaDoc contextRealPath = context.getRealPath("/" + config.getServletName());
153                     String JavaDoc jahiaRealPath = JahiaTools.replacePattern(contextRealPath,config.getServletName(),"");
154                     jahiaRealPath = JahiaTools.replacePattern(jahiaRealPath,File.separator+"."+File.separator,File.separator);
155
156                     File JavaDoc jahiaDir = new File JavaDoc(jahiaRealPath); // ../tomcat/webapps/jahia
157
JahiaConsole.println("getServerInformations", "jboss dir name = " + jahiaDir.getAbsolutePath() );
158
159                     String JavaDoc webAppPath = jahiaDir.getParent();
160                     File JavaDoc webAppDir = new File JavaDoc(webAppPath);
161                     JahiaConsole.println("getServerInformations", "webapp dir = " + webAppDir.getAbsolutePath() );
162
163                     String JavaDoc tomcatPath = webAppDir.getParent();
164                     File JavaDoc tomcatDir = new File JavaDoc(tomcatPath);
165
166                     String JavaDoc 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 JavaDoc e) {
176             }
177
178             return infos;
179         }
180     } // end getServerInformations
181

182
183
184 } // end ServletContainerUtils
Popular Tags