001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.runtime.api;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.common.Environment;
027
028/**
029 * Helper class to get container related properties.
030 *
031 * @author Stephane Lacoin
032 * @author bstefanescu
033 * @author Florent Guillaume
034 */
035public enum J2EEContainerDescriptor {
036
037    JBOSS, TOMCAT, JETTY, GF3;
038
039    public static final Log log = LogFactory.getLog(J2EEContainerDescriptor.class);
040
041    static J2EEContainerDescriptor autodetect() {
042        String hostName = Environment.getDefault().getHostApplicationName();
043        if (hostName == null) {
044            return null;
045        }
046
047        if (Environment.JBOSS_HOST.equals(hostName)) {
048            log.info("Detected JBoss host");
049            return JBOSS;
050        } else if (Environment.TOMCAT_HOST.equals(hostName)) {
051            log.info("Detected Tomcat host");
052            return TOMCAT;
053        } else if (Environment.NXSERVER_HOST.equals(hostName)) {
054            // can be jetty or gf3 embedded
055            try {
056                Class.forName("com.sun.enterprise.glassfish.bootstrap.AbstractMain");
057                log.info("Detected GlassFish host");
058                return GF3;
059            } catch (ClassNotFoundException e) {
060                log.debug("Autodetect : not a glassfish host");
061            }
062            try {
063                Class.forName("org.mortbay.jetty.webapp.WebAppContext");
064                log.info("Detected Jetty host");
065                return JETTY;
066            } catch (ClassNotFoundException e) {
067                log.debug("Autodetect : not a jetty host");
068            }
069            return null; // unknown host
070        } else {
071            return null; // unknown host
072        }
073    }
074
075    static J2EEContainerDescriptor selected;
076
077    public static J2EEContainerDescriptor getSelected() {
078        if (selected == null) {
079            selected = autodetect();
080        }
081        return selected;
082    }
083
084}