alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Glassfish example source code file (DeploymentElement.java)

This example Glassfish source code file (DeploymentElement.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Glassfish tags/keywords

arraylist, converted, deploymentelement, deploymentelement, ejbexception, file, file, io, list, log, logging, net, network, object, object, resultapplication, string, string, util, web-inf

The Glassfish DeploymentElement.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 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 org.glassfish.ejb.embedded;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;
import java.util.logging.Level;

import javax.ejb.EJBException;

import org.glassfish.internal.embedded.ScatteredArchive;
import org.glassfish.deployment.common.ModuleExploder;
import com.sun.enterprise.util.io.FileUtils;
import com.sun.logging.LogDomains;
import com.sun.ejb.containers.EjbContainerUtilImpl;

/**
 * Wrapper that allows to distinguish between an EJB module and a library reference.
 *
 * @author Marina Vatkina
 */
public class DeploymentElement {

    // Use Bundle from another package
    private static final Logger _logger =
            LogDomains.getLogger(EjbContainerUtilImpl.class, LogDomains.EJB_LOGGER);

    private File element;
    private boolean isEJBModule;
    private boolean isWebApp = false;

    DeploymentElement (File element, boolean isEJBModule) {
        this.element  = element;
        this.isEJBModule  = isEJBModule;
        if (element.isFile()) {
            isWebApp = element.getName().endsWith(".war");
        } else {
            List files = Arrays.asList(element.list());
            isWebApp = files.contains("WEB-INF");
        }
    }

    File getElement() {
        return element;
    }

    boolean isEJBModule() {
        return isEJBModule;
    }

    boolean isWebApp() {
        return isWebApp;
    }

    public static boolean hasEJBModule(Set<DeploymentElement> modules) {
        for (DeploymentElement module : modules) {
            if (module.isEJBModule()) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasWar(Set<DeploymentElement> modules) {
        for (DeploymentElement module : modules) {
            if (module.isWebApp()) {
                return true;
            }
        }
        return false;
    }

    public static DeploymentElement getWar(Set<DeploymentElement> modules) {
        for (DeploymentElement module : modules) {
            if (module.isWebApp()) {
                return module;
            }
        }
        return null;
    }

    public static boolean hasLibrary(Set<DeploymentElement> modules) {
        for (DeploymentElement module : modules) {
            if (!module.isEJBModule()) {
                return true;
            }
        }
        return false;
    }

    public static int countEJBModules(Set<DeploymentElement> modules) {
        int result = 0;
        for (DeploymentElement module : modules) {
            if (module.isEJBModule()) {
                ++result;
            }
        }
        return result;
    }


    /**
     * Create deployable application from a Set of DeploymentElements.
     * @param modules the Set of DeploymentElements.
     * @return deployable application.
     */
    public static ResultApplication getOrCreateApplication(Set<DeploymentElement> modules, String appName)
            throws EJBException, IOException {
        Object result = null;
        boolean deleteOnExit = false;
        if (modules == null || modules.size() == 0 || !DeploymentElement.hasEJBModule(modules)) {
            _logger.severe("[DeploymentElement] No modules found");
        } else if (appName == null && DeploymentElement.countEJBModules(modules) == 1) {
            // Use the single component as-is
            if (modules.size() == 1) {
                // Single EJB module
                result = modules.iterator().next().getElement();
            } else if(DeploymentElement.countEJBModules(modules) == 1 && DeploymentElement.hasWar(modules)) {
                // A WAR file with an EJB
                result = DeploymentElement.getWar(modules).getElement();
            } else {
                // EJB molule with libraries - create ScatteredArchive
                String aName = null;
                List<URL> archives = new ArrayList();
                for (DeploymentElement m : modules) {
                    boolean isEJBModule = m.isEJBModule();
                    File f = m.getElement();
                    String name = f.getName();
                    if (_logger.isLoggable(Level.INFO)) {
                        _logger.info("[DeploymentElement] adding " + ((isEJBModule)? "EJB module" : "library") + " to ScatteredArchive " + name);
                    }
        
                    if (isEJBModule) {
                        // Need to give archive some meaningful name
                        aName = name;

                        // workaround for bug 6975728: add the ejb module as the first element of
                        // urls in ScatteredArchive.  In the current ScatteredArchvie.getURI() method,
                        // if both topDir and resources are null, the first element in urls list is
                        // returned as this archvie's URI.
                        archives.add(0, f.toURI().toURL());
                    } else {
                        archives.add(f.toURI().toURL());
                    }
                }
                ScatteredArchive.Builder saBuilder = new ScatteredArchive.Builder(aName,
                        Collections.unmodifiableCollection(archives));
                result = saBuilder.buildJar();
            }
        } else {
            // Create an ear if appName is set or if there is more than 1 EJB module

            // Create a temp dir by creating a temp file first, then
            // delete the file and create a directory in its place.
            File resultFile = File.createTempFile("ejb-app", "");
            File lib = null;
            if (resultFile.delete() && resultFile.mkdirs()) {
                if (_logger.isLoggable(Level.FINE)) {
                    _logger.fine("[DeploymentElement] temp dir created at " + resultFile.getAbsolutePath());
                }

                // Create lib dir if there are library entries
                if (DeploymentElement.hasLibrary(modules)) {
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.fine("[DeploymentElement] lib dir added ... ");
                    }
                    lib = new File(resultFile, "lib");
                }

            } else {
                throw new EJBException("Not able to create temp dir " + resultFile.getAbsolutePath ());
            }

            if (System.getProperty(EJBContainerProviderImpl.KEEP_TEMPORARY_FILES) == null) {
                 deleteOnExit = true;
            }

            // Copy module directories and explode module jars
            int duplicate_dir_counter = 0;
            for (DeploymentElement m : modules) {
                File f = m.getElement();

                if (_logger.isLoggable(Level.INFO)) {
                    _logger.info("[DeploymentElement] adding " + f.getName() + " to exploded ear " +
                            " isEJBModule? " + m.isEJBModule() + " isWebApp? " + m.isWebApp());
                }

                String filename = f.toURI().getSchemeSpecificPart();
                if (filename.endsWith(File.separator) || filename.endsWith("/")) {
                    int length = filename.length();
                    filename = filename.substring(0, length - 1);
                }

                int lastpart = filename.lastIndexOf(File.separatorChar);
                if (lastpart == -1) {
                    lastpart = filename.lastIndexOf('/');
                }
                String name = filename.substring(lastpart + 1);
                if (_logger.isLoggable(Level.FINE)) {
                    _logger.fine("[DeploymentElement] Converted file name: " + filename + " to " + name);
                }

                File base = (m.isEJBModule())? resultFile : lib;
                if (!f.isDirectory() && m.isEJBModule()) { 
                    File out = new File(base, FileUtils.makeFriendlyFilename(name));
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.fine("[DeploymentElement] Exploding jar to: " + out);
                    }
                    ModuleExploder.explodeJar(f, out);
                } else {
                    if (f.isDirectory()) { 
                        name = name + (m.isWebApp()? "_war" : (m.isEJBModule()? "_jar" : ".jar"));
                    }
                    File out = new File(base, name);
                    if (out.exists()) {
                        out = new File(base, "d__" + ++duplicate_dir_counter + "__" + name);
                    }
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.fine("[DeploymentElement] Copying element to: " + out);
                    }
                    FileUtils.copy(f, out);
                }

                result = resultFile;
            }
        }
        return new ResultApplication(result, deleteOnExit);
    }

    protected static class ResultApplication {
        private boolean deleteOnExit = false;
        private Object app = null;

        ResultApplication (Object app) {
            this.app = app;
        }

        ResultApplication (Object app, boolean deleteOnExit) {
            this.app = app;
            this.deleteOnExit = deleteOnExit;
        }

        Object getApplication() {
            return app;
        }

        boolean deleteOnExit() {
            return deleteOnExit;
        }
    }

}

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish DeploymentElement.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.