KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > util > J2EEUtils


1 /*
2  * Copyright (c) 2005 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: J2EEUtils.java,v 1.12 2007/01/07 06:14:00 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.util;
23
24 import java.util.logging.Logger JavaDoc;
25
26 /**
27  * Collection of useful utilities to work with j2ee.
28  *
29  * @version $Id: J2EEUtils.java,v 1.12 2007/01/07 06:14:00 bastafidli Exp $
30  * @author Julo legeny
31  * @code.reviewer Miro Halas
32  * @code.reviewed 1.9 2005/07/29 07:36:23 bastafidli
33  */

34 public final class J2EEUtils
35 {
36    // Constants ////////////////////////////////////////////////////////////////
37

38    /**
39     * Constant defining j2ee server was not yet detected.
40     */

41    public static final int J2EE_SERVER_UNINITIALIZED = -1;
42
43    /**
44     * Constant defining no j2ee server.
45     */

46    public static final int J2EE_SERVER_NO = 0;
47
48    /**
49     * Constant defining JOnAS j2ee server.
50     */

51    public static final int J2EE_SERVER_JONAS = 1;
52
53    /**
54     * Constant defining JBoss j2ee server.
55     */

56    public static final int J2EE_SERVER_JBOSS = 2;
57
58    /**
59     * Constant defining WebLogic j2ee server.
60     */

61    public static final int J2EE_SERVER_WEBLOGIC = 3;
62
63    /**
64     * Constant defining WebSphere j2ee server.
65     */

66    public static final int J2EE_SERVER_WEBSPHERE = 4;
67
68    /**
69     * Constant identifying JOnAS j2ee server. There is retrieved
70     * parent of the classloader because it identify jonas server.
71     * Using just classloader there is retireved particular web
72     * server identifier (jetty, tomcat, ...).
73     * ClassLoader parent for JOnAS 4.2.3:
74     * org.objectweb.jonas_lib.loader.SimpleWebappClassLoader
75     */

76    public static final String JavaDoc JONAS_IDENTIFIER = ".objectweb.jonas";
77
78    /**
79     * Constant identifying JBoss j2ee server.
80     * ClassLoader parent for JBoss 3.2.6 and 4.0.1:
81     * org.jboss.system.server.NoAnnotationURLClassLoader
82     */

83    public static final String JavaDoc JBOSS_IDENTIFIER = ".jboss.";
84
85    /**
86     * Constant identifying WebLogic j2ee server.
87     * ClassLoader parent for BEA WebLogic 7.0 and 8.1:
88     * weblogic.utils.classloaders.GenericClassLoader
89     */

90    public static final String JavaDoc WEBLOGIC_IDENTIFIER = "weblogic.";
91
92    /**
93     * Constant identifying WebSphere j2ee server.
94     * ClassLoader parent for IBM WebSphere 6:
95     * com.ibm.ws.classloader.JarClassLoader
96     */

97    public static final String JavaDoc WEBSPHERE_IDENTIFIER = ".ibm.ws.";
98
99    // Cached values ////////////////////////////////////////////////////////////
100

101    /**
102     * Logger for this class
103     */

104    private static Logger JavaDoc s_logger = Log.getInstance(J2EEUtils.class);
105    
106    /**
107     * Since J2EE server doesn't changes during execution we can cache the
108     * value for detected server.
109     */

110    protected static int s_iDetectedServer = J2EE_SERVER_UNINITIALIZED;
111   
112    // Constructors /////////////////////////////////////////////////////////////
113

114    /**
115     * Private constructor since this class cannot be instantiated
116     */

117    private J2EEUtils(
118    )
119    {
120       // Do nothing
121
}
122
123    // Public methods ///////////////////////////////////////////////////////////
124

125    /**
126     * Method detects and returns type of the current running j2ee server.
127     * For detecting actual running j2ee server we will use ClassLoader
128     * because it is server specific.
129     *
130     * @return int - representation of the curernt running j2ee server
131     */

132    public static int getJ2EEServerType(
133    )
134    {
135       // No need to synchronize since in the worst case we execute this
136
// multiple times
137
if (s_iDetectedServer == J2EE_SERVER_UNINITIALIZED)
138       {
139          int iRetValue = J2EE_SERVER_NO;
140          String JavaDoc strClassLoader;
141    
142          strClassLoader = J2EEUtils.class.getClassLoader().getClass().getName();
143          iRetValue = detectJ2EEServerType(strClassLoader);
144          if (iRetValue == J2EE_SERVER_NO)
145          {
146             strClassLoader = J2EEUtils.class.getClassLoader().getParent().getClass().getName();
147             iRetValue = detectJ2EEServerType(strClassLoader);
148          }
149          
150          s_iDetectedServer = iRetValue;
151       }
152       
153
154       return s_iDetectedServer;
155    }
156    
157    // Helper methods ///////////////////////////////////////////////////////////
158

159    /**
160     * Detect current running j2ee server based on the specified string
161     * because it is server specific.
162     *
163     * @param strIdentifier - string which should uniquely identify the AS
164     * @return int - representation of the current running j2ee server
165     */

166    protected static int detectJ2EEServerType(
167       String JavaDoc strIdentifier
168    )
169    {
170       int iRetValue = J2EE_SERVER_NO;
171
172       if (strIdentifier != null)
173       {
174          s_logger.finest("Trying to detect J2EE application server using identifier "
175                          + strIdentifier);
176          if (strIdentifier.indexOf(JONAS_IDENTIFIER) > -1)
177          {
178             s_logger.fine("JOnAS application server detected.");
179             iRetValue = J2EE_SERVER_JONAS;
180          }
181          else if (strIdentifier.indexOf(JBOSS_IDENTIFIER) > -1)
182          {
183             s_logger.fine("JBoss application server detected.");
184             iRetValue = J2EE_SERVER_JBOSS;
185          }
186          else if (strIdentifier.indexOf(WEBLOGIC_IDENTIFIER) > -1)
187          {
188             s_logger.fine("Weblogic application server detected.");
189             iRetValue = J2EE_SERVER_WEBLOGIC;
190          }
191          else if (strIdentifier.indexOf(WEBSPHERE_IDENTIFIER) > -1)
192          {
193             s_logger.fine("Websphere application server detected.");
194             iRetValue = J2EE_SERVER_WEBSPHERE;
195          }
196       }
197       else
198       {
199          s_logger.finest("No J2EE application server detected since identifier" +
200                          " is null.");
201       }
202
203       return iRetValue;
204    }
205 }
206
Popular Tags