001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.runtime.api;
016
017import org.apache.commons.logging.Log;
018import org.apache.commons.logging.LogFactory;
019import org.nuxeo.common.Environment;
020
021/**
022 * Helper class to get container related properties.
023 *
024 * @author Stephane Lacoin
025 * @author bstefanescu
026 * @author Florent Guillaume
027 */
028public enum J2EEContainerDescriptor {
029
030    JBOSS, TOMCAT, JETTY, GF3;
031
032    public static final Log log = LogFactory.getLog(J2EEContainerDescriptor.class);
033
034    static J2EEContainerDescriptor autodetect() {
035        String hostName = Environment.getDefault().getHostApplicationName();
036        if (hostName == null) {
037            return null;
038        }
039
040        if (Environment.JBOSS_HOST.equals(hostName)) {
041            log.info("Detected JBoss host");
042            return JBOSS;
043        } else if (Environment.TOMCAT_HOST.equals(hostName)) {
044            log.info("Detected Tomcat host");
045            return TOMCAT;
046        } else if (Environment.NXSERVER_HOST.equals(hostName)) {
047            // can be jetty or gf3 embedded
048            try {
049                Class.forName("com.sun.enterprise.glassfish.bootstrap.AbstractMain");
050                log.info("Detected GlassFish host");
051                return GF3;
052            } catch (ClassNotFoundException e) {
053                log.debug("Autodetect : not a glassfish host");
054            }
055            try {
056                Class.forName("org.mortbay.jetty.webapp.WebAppContext");
057                log.info("Detected Jetty host");
058                return JETTY;
059            } catch (ClassNotFoundException e) {
060                log.debug("Autodetect : not a jetty host");
061            }
062            return null; // unknown host
063        } else {
064            return null; // unknown host
065        }
066    }
067
068    static J2EEContainerDescriptor selected;
069
070    public static J2EEContainerDescriptor getSelected() {
071        if (selected == null) {
072            selected = autodetect();
073        }
074        return selected;
075    }
076
077}