001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.server.capabilities;
016    
017    import com.liferay.portal.kernel.util.MapUtil;
018    
019    import java.lang.reflect.Field;
020    
021    import java.util.Map;
022    
023    import javax.servlet.ServletContext;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Igor Spasic
028     */
029    public class GlassfishServerCapabilities implements ServerCapabilities {
030    
031            public void determine(ServletContext servletContext) throws Exception {
032                    determineSupportsHotDeploy(servletContext);
033            }
034    
035            public boolean isSupportsHotDeploy() {
036                    return _supportsHotDeploy;
037            }
038    
039            protected void determineSupportsHotDeploy(ServletContext servletContext)
040                    throws Exception {
041    
042                    // org.apache.catalina.core.ApplicationContext
043    
044                    Class<?> servletContextClass = servletContext.getClass();
045    
046                    Field contextField = servletContextClass.getDeclaredField("context");
047    
048                    contextField.setAccessible(true);
049    
050                    Object applicationContext = contextField.get(servletContext);
051    
052                    // com.sun.enterprise.web.WebModule
053    
054                    Class<?> applicationContextClass = applicationContext.getClass();
055    
056                    contextField = applicationContextClass.getDeclaredField("context");
057    
058                    contextField.setAccessible(true);
059    
060                    Object webModule = contextField.get(applicationContext);
061    
062                    // org.apache.catalina.core.ContainerBase
063    
064                    Class<?> containerBaseClass = webModule.getClass();
065    
066                    for (int i = 0; i < 3; i++) {
067                            containerBaseClass = containerBaseClass.getSuperclass();
068                    }
069    
070                    // com.sun.enterprise.web.VirtualServer
071    
072                    Field parentField = containerBaseClass.getDeclaredField("parent");
073    
074                    parentField.setAccessible(true);
075    
076                    Object virtualServer = parentField.get(webModule);
077    
078                    // com.sun.enterprise.web.WebContainer
079    
080                    Object webEngine = parentField.get(virtualServer);
081    
082                    Class<?> webEngineClass = webEngine.getClass();
083    
084                    Field webContainerField = webEngineClass.getDeclaredField(
085                            "webContainer");
086    
087                    webContainerField.setAccessible(true);
088    
089                    Object webContainer = webContainerField.get(webEngine);
090    
091                    // org.glassfish.config.support.TranslatedConfigView
092    
093                    Class<?> webContainerClass = webContainer.getClass();
094    
095                    Field dasConfigField = webContainerClass.getDeclaredField("dasConfig");
096    
097                    dasConfigField.setAccessible(true);
098    
099                    Object dasConfigProxy = dasConfigField.get(webContainer);
100    
101                    Class<?> proxyClass = dasConfigProxy.getClass().getSuperclass();
102    
103                    Field hField = proxyClass.getDeclaredField("h");
104    
105                    hField.setAccessible(true);
106    
107                    Object translatedConfigView = hField.get(dasConfigProxy);
108    
109                    // org.glassfish.config.support.GlassFishConfigBean
110    
111                    Class<?> translatedConfigViewClass = translatedConfigView.getClass();
112    
113                    Field masterViewField = translatedConfigViewClass.getDeclaredField(
114                            "masterView");
115    
116                    masterViewField.setAccessible(true);
117    
118                    Object masterView = masterViewField.get(translatedConfigView);
119    
120                    // org.jvnet.hk2.config.Dom
121    
122                    Class<?> domClass = masterView.getClass();
123    
124                    for (int i = 0; i < 2; i++) {
125                            domClass = domClass.getSuperclass();
126                    }
127    
128                    Field attributesField = domClass.getDeclaredField("attributes");
129    
130                    attributesField.setAccessible(true);
131    
132                    Map<String, String> attributes =
133                            (Map<String, String>)attributesField.get(masterView);
134    
135                    boolean autoDeployEnabled = MapUtil.getBoolean(
136                            attributes, "autodeploy-enabled", true);
137    
138                    _supportsHotDeploy = autoDeployEnabled;
139            }
140    
141            private boolean _supportsHotDeploy;
142    
143    }