Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2012 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package com.sun.enterprise.web;
 
 
 import java.util.*;
 
 
     private static final Logger logger = .....;
 
     protected static final ResourceBundle rb = .getResourceBundle();
 
     @LogMessageInfo(
             message = "Unknown error, loadWebModule returned null, file a bug",
             level = "SEVERE",
             cause = "An exception occurred writing to access log file",
             action = "Check the exception for the error")
     public static final String WEBAPP_UNKNOWN_ERROR = "AS-WEB-00323";
 
     @LogMessageInfo(
             message = "Loading application [{0}] at [{1}]",
             level = "INFO")
     public static final String LOADING_APP = "AS-WEB-00324";
 
     @LogMessageInfo(
             message = "App config customization specified to ignore descriptor's {0} {1} " +
                     "so it will not be present for the application",
             level = "FINER")
     public static final String IGNORE_DESCRIPTOR = "AS-WEB-00325";
 
     @LogMessageInfo(
             message = "Overriding descriptor {0}",
             level = "FINER")
     public static final String OVERIDE_DESCRIPTOR = "AS-WEB-00326";
 
     @LogMessageInfo(
            message = "Creating new {0}",
            level = "FINER")
    public static final String CREATE_DESCRIPTOR = "AS-WEB-00327";
            message = "Exception during Coherence*Web shutdown for application [{0}]",
            level = "WARNING")
    public static final String EXCEPTION_SHUTDOWN_COHERENCE_WEB = "AS-WEB-00328";
    private final WebContainer container;
    private final WebModuleConfig wmInfo;
    private Set<WebModulewebModules = new HashSet<WebModule>();
    public WebApplication(WebContainer containerWebModuleConfig config
            final ApplicationConfigInfo appConfigInfo) {
        this. = container;
        this. = config;
        this. = extractCustomizations(appConfigInfo);
    }
    @Override
    public boolean start(ApplicationContext appContextthrows Exception {
        .clear();
        Properties props = null;
        if (appContext!=null) {
            .setAppClassLoader(appContext.getClassLoader());
            if (appContext instanceof DeploymentContext) {
                DeploymentContext deployContext = (DeploymentContext)appContext;
                .setDeploymentContext(deployContext);
                if (isKeepState(deployContexttrue)) {
                    props = deployContext.getAppProps();
                }
            }
            applyApplicationConfig(appContext);
        }
        List<Result<WebModule>> results = .loadWebModule(
            "null"props);
        // release DeploymentContext in memory
        .setDeploymentContext(null);
        if (results.size() == 0) {
            .log(."webApplication.unknownError");
            return false;
        }
        boolean isFailure = false;
        StringBuilder sb = null;
        for (Result<WebModuleresult : results) {
            if (result.isFailure()) {
                if (sb == null) {
                    sb = new StringBuilder(result.exception().toString());
                } else {
                    sb.append(result.exception().toString());
                }
                .log(.result.exception().toString(),
                           result.exception());
                isFailure = true;
            } else {
                .add(result.result());
            }
        }
        if (isFailure) {
            .clear();
            throw new Exception(sb.toString());
        }
        if (.isLoggable(.)) {
        }
        
        return true;
    }
    @Override
    public boolean stop(ApplicationContext stopContext) {
        if (stopContext instanceof DeploymentContext) {
            DeploymentContext deployContext = (DeploymentContext)stopContext;
            Properties props = null;
            boolean keepSessions = isKeepState(deployContextfalse);
            if (keepSessions) {
                props = new Properties();
            }
                                      getDescriptor().getApplication().getRegistrationName(),
                                      .getVirtualServers(), props);
            if (keepSessions) {
                Properties actionReportProps = getActionReportProperties(deployContext);
                // should not be null here
                if (actionReportProps != null) {
                    actionReportProps.putAll(props);
                }
            }
        }
        stopCoherenceWeb();
        return true;
    }

    
Suspends this application on all virtual servers.
    @Override
    public boolean suspend() {
        return .suspendWebModule(
            .getDescriptor().getContextRoot(), "null"null);
    }

    
Resumes this application on all virtual servers.
    @Override
    public boolean resume() throws Exception {
        // WebContainer.loadWebModule(), which is called by start(),
        // already checks if the web module has been suspended, and if so,
        // just resumes it and returns
        return start(null);
    }

    
Returns the class loader associated with this application

Returns:
ClassLoader for this app
    @Override
    public ClassLoader getClassLoader() {
        return .getAppClassLoader();
    }

    
Gets a set of all the WebModule instances (one per virtual server deployment) of this WebApplication.

For each WebModule in the returned set, the corresponding ServletContext may be obtained by calling WebModule#getServletContext

    public Set<WebModulegetWebModules() {
        return ;
    }

    
Returns the deployment descriptor associated with this application

Returns:
deployment descriptor if they exist or null if not
    @Override
        return .getDescriptor();
    }
    private boolean isKeepState(DeploymentContext deployContextboolean isDeploy) {
        Boolean keepState = null;
        if (isDeploy) {
            DeployCommandParameters dcp = deployContext.getCommandParameters(DeployCommandParameters.class);
            if (dcp != null) {
                keepState = dcp.keepstate;
            }
        } else {
            UndeployCommandParameters ucp = deployContext.getCommandParameters(UndeployCommandParameters.class);
            if (ucp != null) {
                keepState = ucp.keepstate;
            }
        }
        if (keepState == null) {
            String keepSessionsString = deployContext.getAppProps().getProperty(.);
            if (keepSessionsString != null && keepSessionsString.trim().length() > 0) {
                keepState = Boolean.valueOf(keepSessionsString);
            } else {
                keepState = getDescriptor().getApplication().getKeepState();
            }
        }
        return ((keepState != null) ? keepState : false);
    }

    
Extracts the application config information for the web container from the saved config info. The saved config info is from the in-memory configuration (domain.xml) if this app was already deployed and is being redeployed.

Parameters:
appConfigInfo
Returns:
            final ApplicationConfigInfo appConfigInfo) {
        return appConfigInfo.get(trimmedModuleName(.getName()), "web");
    }
    private String trimmedModuleName(String moduleName) {
        final int hash = moduleName.indexOf('#');
        if (hash == -1) {
            return moduleName;
        }
        return moduleName.substring(hash + 1);
    }
    
Applies application config customization (stored temporarily in the start-up context's start-up parameters) to the web app's descriptor.

Parameters:
appContext
    private void applyApplicationConfig(ApplicationContext appContext) {
        WebBundleDescriptorImpl descriptor = .getDescriptor();
        try {
            if ( != null) {
                EnvEntryCustomizer envEntryCustomizer =
                        new EnvEntryCustomizer(
                            descriptor.getEnvironmentEntrySet(),
                            .getEnvEntry());
                ContextParamCustomizer contextParamCustomizer =
                        new ContextParamCustomizer(
                            descriptor.getContextParametersSet(),
                            .getContextParam());
                envEntryCustomizer.applyCustomizations();
                contextParamCustomizer.applyCustomizations();
            }
        } catch (ClassCastException ex) {
            /*
             * If the user specified an env-entry value that does not
             * work with the env-entry type it can cause a class cast
             * exception.  Log the warning but continue working.
             */
            .log(.""ex);
        }
    }
    @SuppressWarnings("unchecked")
    private Properties getActionReportProperties(DeploymentContext deployContext) {
        if (!.getDescriptor().getApplication().isVirtual()) {
            deployContext = ((ExtendedDeploymentContext)deployContext).getParentContext();
        }
        return deployContext.getActionReport().getExtraProperties();
    }
    /*
     * Convenience class for applying customizations to descriptor items.
     * <p>
     * Much of the logic is the same for the different types of customizations -
     * and this class abstracts all the common behavior.  This may seem like
     * overkill, factoring this logic out like this, but the applyCustomizations
     * logic is not something we want to have two copies of.
     */
    private abstract class Customizer<T,U> {
        protected Set<T> descriptorItems;
        protected List<U> customizations;
        private String descriptorItemName;
        private Customizer(Set<T> descriptorItemsList<U> customizationsString descriptorItemName) {
            this. = descriptorItems;
            this. = customizations;
            this. = descriptorItemName;
        }

        
Indicates whether the customization says to ignore any corresponding descriptor entry.

Parameters:
customization the customization
Returns:
true if the user wants to ignore any corresponding descriptor entry; false otherwise
        protected abstract boolean isIgnoreDescriptorItem(U customization);

        
Creates a new descriptor item using the information from the customization.

Parameters:
customization the customization the gives the value(s) for the new descriptor
Returns:
the new descriptor item
        protected abstract T newDescriptorItem(U customization);

        
Assigns the values from the customization to the existing descriptor item.

Parameters:
descriptorItem descriptor item to change
customization customization containing the new values to be set in the descriptor item
        protected abstract void setDescriptorItemValue(T descriptorItem, U customization);

        
Returns the name from the descriptor item

Parameters:
descriptorItem
Returns:
name from the descriptor item
        protected abstract String getName(T descriptorItem);

        
Returns the value from the descriptor item

Parameters:
descriptorItem
Returns:
value from the descriptor item
        protected abstract String getValue(T descriptorItem);

        
Returns the name from the customization

Parameters:
customization
Returns:
name from the customization
        protected abstract String getCustomizationName(U customization);

        
Represents the customization as a String for logging.

Parameters:
customization
Returns:
        protected abstract String toString(U customization);


        
Removes the descriptor item from the descriptor's collection of this type of item.

Parameters:
descriptorItem the item to remove
        protected void removeDescriptorItem(T descriptorItem) {
            .remove(descriptorItem);
        }

        
Adds a new descriptor item to the descriptor's collection of items, basing the new one on the customization the user created.

Parameters:
customization
Returns:
the newly-created item
        protected T addDescriptorItem(U customization) {
            T newItem = newDescriptorItem(customization);
            .add(newItem);
            return newItem;
        }

        
Applies the set of customizations to the descriptor's set of items.
        void applyCustomizations () {
            boolean isFiner = .isLoggable(.);
          nextCustomization:
            for (U customization : ) {
                /*
                 * For each customization try to find a descriptor item with
                 * the same name.  If there is one, either ignore the descriptor
                 * item (if that is what the customization specifies) or override
                 * the descriptor items'a value with the value from the
                 * customization.
                 */
                for (Iterator<T> it = .iterator(); it.hasNext();) {
                    T descriptorItem = it.next();
                    String dItemName = getName(descriptorItem);
                    String customizationItemName = getCustomizationName(customization);
                    if (dItemName.equals(customizationItemName)) {
                        /*
                         * We found a descriptor item that matches this
                         * customization's name.
                         */
                        if (isIgnoreDescriptorItem(customization)) {
                            /*
                             * The user wants to ignore this descriptor item
                             * so remove it from the descriptor's collection
                             * of items.
                             */
                            it.remove();
                            if (isFiner) {
                                .log(.,
                                        ,
                                        new Object[]{getName(descriptorItem)});
                            }
                        } else {
                            /*
                             * The user wants to override the setting of this
                             * descriptor item using the customized settings.
                             */
                            String oldValue = getValue(descriptorItem); // for logging purposes only
                            try {
                                setDescriptorItemValue(descriptorItemcustomization);
                                if (isFiner) {
                                    .log(.,
                                             + " " +
                                            getName(descriptorItem) + "=" +
                                            oldValue +
                                            " with " + toString(customization));
                                }
                            } catch (Exception e) {
                                .warning(toString(customization) + " " + e.getLocalizedMessage());
                            }
                        }
                        /*
                         * We have matched this customization with a descriptor
                         * item, so we can skip to the next customization.
                         */
                        continue nextCustomization;
                    }
                }
                /*
                 * The customization matched no existing descriptor item, so
                 * add a new descriptor item.
                 */
                try {
                    T newItem = addDescriptorItem(customization);
                    if (isFiner) {
                        .log(.,
                                ,
                                 + getName(newItem) + "=" + getValue(newItem));
                    }
                } catch (Exception e) {
                    .warning(toString(customization) + " " + e.getLocalizedMessage());
                }
            }
        }
    }

    
Concrete implementation of the context-parameter customizer.
        private ContextParamCustomizer(Set<ContextParameterdescriptorItemsList<ContextParamcustomizations) {
            super(descriptorItemscustomizations"context-param"); // NOI18N
        }
        
        @Override
        protected boolean isIgnoreDescriptorItem(ContextParam customization) {
            return Boolean.parseBoolean(customization.getIgnoreDescriptorItem());
        }
        @Override
        protected void setDescriptorItemValue(ContextParameter descriptorItemContextParam customization) {
            descriptorItem.setValue(customization.getParamValue());
        }
        @Override
        protected ContextParameter newDescriptorItem(ContextParam customization) {
            ContextParameter newItem = 
                    new EnvironmentProperty(
                        customization.getParamName(), 
                        customization.getParamValue(), 
                        "" /* description */);
            return newItem;
        }
        @Override
        protected String getName(ContextParameter descriptorItem) {
            return descriptorItem.getName();
        }
        @Override
        protected String getCustomizationName(ContextParam customization) {
            return customization.getParamName();
        }
        @Override
        protected String getValue(ContextParameter descriptorItem) {
            return descriptorItem.getValue();
        }
        @Override
        protected String toString(ContextParam customization) {
            return "Context-param: name=" + customization.getParamName() + ", value=" + customization.getParamValue();
        }
            
    }

    
Concrete implementation for the EnvEntry customizer.
    private class EnvEntryCustomizer extends Customizer<EnvironmentEntry,EnvEntry> {
        
        private EnvEntryCustomizer(Set<EnvironmentEntrydescriptorItemsList<EnvEntrycustomizations) {
            super(descriptorItemscustomizations"env-entry"); // NOI18N
        }
        @Override
        protected boolean isIgnoreDescriptorItem(EnvEntry customization) {
            return Boolean.parseBoolean(customization.getIgnoreDescriptorItem());
        }
        @Override
        protected void setDescriptorItemValue(EnvironmentEntry descriptorItemEnvEntry customization) {
            customization.validateValue();
            descriptorItem.setValue(customization.getEnvEntryValue());
            descriptorItem.setType(customization.getEnvEntryType());
        }
        @Override
        protected EnvironmentEntry newDescriptorItem(EnvEntry customization) {
            customization.validateValue();
            EnvironmentEntry newItem = 
                    new EnvironmentProperty(
                        customization.getEnvEntryName(), 
                        customization.getEnvEntryValue(), 
                        customization.getDescription(), 
                        customization.getEnvEntryType());
            /*
             * Invoke setValue which records that the value has been set.
             * Otherwise naming does not bind the name.
             */
            newItem.setValue(customization.getEnvEntryValue());
            return newItem;
        }
        @Override
        protected String getName(EnvironmentEntry descriptorItem) {
            return descriptorItem.getName();
        }
        @Override
        protected String getCustomizationName(EnvEntry customization) {
            return customization.getEnvEntryName();
        }
        @Override
        protected String getValue(EnvironmentEntry descriptorItem) {
            return descriptorItem.getValue();
        }
        @Override
        protected String toString(EnvEntry customization) {
            return "EnvEntry: name=" + customization.getEnvEntryName() +
                    ", type=" + customization.getEnvEntryType() +
                    ", value=" + customization.getEnvEntryValue() +
                    ", desc=" + customization.getDescription();
        }
    }
    private void stopCoherenceWeb() {
        if (.getDescriptor() != null && 
                .getDescriptor().getSunDescriptor() != null) {
            SunWebAppImpl sunWebApp = (SunWebAppImpl.getDescriptor().getSunDescriptor();
            if (sunWebApp.getSessionConfig() != null &&
                    sunWebApp.getSessionConfig().getSessionManager() != null) {
                SessionManager sessionManager =
                    sunWebApp.getSessionConfig().getSessionManager();
                String persistenceType = sessionManager.getAttributeValue(
                    .);
                if (..getType().equals(persistenceType)) {
                    ClassLoader cloader = .getAppClassLoader();
                    try {
                        Class<?> cacheFactoryClass = cloader.loadClass(
                                "com.tangosol.net.CacheFactory");
                        if (cacheFactoryClass != null) {
                            Method shutdownMethod = cacheFactoryClass.getMethod("shutdown");
                            if (shutdownMethod != null) {
                                shutdownMethod.invoke(null);
                            }
                        }
                    } catch(Exception ex) {
                        if (.isLoggable(.)) {
                            String msg = .getString();
                            msg = MessageFormat.format(msg.getDescriptor().getName());
                            .log(.msgex);
                        }
                    }
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X