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

Glassfish example source code file (AutoUndeploymentOperation.java)

This example Glassfish source code file (AutoUndeploymentOperation.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

application, autoundeploymentoperation, autoundeploymentoperation, command_name, deleting, deleting, deploymentproperties, exception, file, file, inject, io, log, logging, properties, string, string, util

The Glassfish AutoUndeploymentOperation.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2008-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.deployment.autodeploy;

import com.sun.enterprise.util.io.FileUtils;
import com.sun.enterprise.config.serverbeans.Applications;
import com.sun.enterprise.config.serverbeans.Application;
import java.io.File;
import java.util.Properties;
import java.util.logging.Level;
import java.util.List;
import org.glassfish.api.admin.AdminCommand;
import org.glassfish.deployment.autodeploy.AutoDeployer.AutodeploymentStatus;
import org.glassfish.deployment.common.DeploymentProperties;
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.annotations.Scoped;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.component.Habitat;
import org.jvnet.hk2.component.PerLookup;

/**
 * Performs a single auto-undeploy operation for a single file.
 * <p>
 * Note - Use the newInstance static method to obtain a fully-injected operation;
 * it is safer and more convenient than using the no-arg constructor and then 
 * invoking init yourself.
 * 
 * @author tjquinn
 */
@Service
@Scoped(PerLookup.class)
public class AutoUndeploymentOperation extends AutoOperation {

    @Inject
    Applications apps;

    /**
     * Creates a new, injected, and initialized AutoUndeploymentOperation object.
     * 
     * @param habitat
     * @param appFile
     * @param name
     * @param target
     * @return the AutoUndeploymentOperation object
     */
    static AutoUndeploymentOperation newInstance(
            Habitat habitat,
            File appFile, 
            String name,
            String target) {
        AutoUndeploymentOperation o = 
                (AutoUndeploymentOperation) habitat.getComponent(AutoUndeploymentOperation.class);
        
        o.init(appFile, name, target);
        return o;
    }

    private String name;
    
    private static final String COMMAND_NAME = "undeploy";
    
    @Inject
    private AutodeployRetryManager retryManager;

    @Inject(name=COMMAND_NAME)
    private AdminCommand undeployCommand;
    
    /**
     * Completes the intialization of the object.
     * @param appFile
     * @param name
     * @param target
     * @return the AutoUndeployOperation for convenience
     */
    protected AutoUndeploymentOperation init(
            File appFile, 
            String name,
            String target) {
        super.init(
                appFile, 
                prepareUndeployActionProperties(name, target), 
                COMMAND_NAME, 
                undeployCommand);
        this.name = name;
        return this;
    }
    
    private Properties prepareUndeployActionProperties(String archiveName, String target) {
        DeploymentProperties dProps = new DeploymentProperties();

        // we need to find the application registration name
        // which is not always the same as archive name
        String appName = archiveName;
        List<Application> applications = apps.getApplications();
        for (Application app : applications) {
            if (app.getDeployProperties().getProperty
                (DeploymentProperties.DEFAULT_APP_NAME).equals(archiveName)) {
                appName = app.getName();
            }
        }

        dProps.setName(appName);
//        dProps.setResourceAction(DeploymentProperties.RES_UNDEPLOYMENT);
//        dProps.setResourceTargetList(target);
        return (Properties)dProps;
    }

    
    /**
     * {@inheritDoc}
     */
    protected String getMessageString(AutodeploymentStatus ds, File file) {
        return localStrings.getLocalString(
                ds.undeploymentMessageKey, 
                ds.undeploymentDefaultMessage, 
                file);
    }

    /**
     * {@inheritDoc}
     */
    protected void markFiles(AutodeploymentStatus ds, File file) {
        /*
         * Before managing the marker file for the app, see if 
         * the autodeployer is responsible for deleting this app
         * file and, if so, delete it.
         * 
         * Normally users will delete the application file themselves.  Especially
         * in the case of directories, though, users may create the file
         * ${fileName}_undeployRequested and have the autodeployer delete the
         * file.  
         * <p>
         * This avoids problems if the user-initiated deletion of a large
         * file or directory takes longer than the autodeployer cycle time.  If
         * a file has been removed from the top-level directory, the autodeployer
         * will see the updated timestamp on the directory and can only decide
         * that this is a new file - at least a newer file - to be autodeployed.
         * <p>
         * By allowing the auto-deployer to manage the deletion of the file the 
         * user can avoid this whole scenario and, thereby, avoid accidental
         * attempts to deploy an application that the user wants gone.
         * 
         */
        if (undeployedByRequestFile(file)) {
            cleanupAppAndRequest(file);
        }

        if (ds.status) {
            markUndeployed(file);
            retryManager.recordSuccessfulUndeployment(file);
        } else {
            markUndeployFailed(file);
            retryManager.recordFailedUndeployment(file);
        }
    }
    
    private void markUndeployed(File f) {
        try {
            deleteAllMarks(f);
            getUndeployedFile(f).createNewFile();
        } catch (Exception e) { 
            //ignore 
        }
    }
    
    private void markUndeployFailed(File f) {
        try {
            deleteAllMarks(f);
            getUndeployFailedFile(f).createNewFile();
        } catch (Exception e) { 
            //ignore 
        }
    }
    
    private boolean undeployedByRequestFile(File f) {
        return f instanceof AutoDeployedFilesManager.UndeployRequestedFile;
    }
    
    private void cleanupAppAndRequest(File f) {
        boolean logFine = sLogger.isLoggable(Level.FINE);

        /*
         * Clean up the application file or directory.
         */
        if (f.isDirectory()) {
            if (logFine) {
                sLogger.fine("Deleting autodeployed directory " + f.getAbsolutePath() + " by request");
            }
            FileUtils.liquidate(f);
        } else {
            if (logFine) {
                sLogger.fine("Deleting autodeployed file " + f.getAbsolutePath() + " by request");
            }
            FileUtils.deleteFile(f);
        }
        
        /*
         * Remove the undeploy request file.
         */
        File requestFile = AutoDeployedFilesManager.appToUndeployRequestFile(f);
        if (logFine) {
            sLogger.fine("Deleting autodeploy request file " + requestFile.getAbsolutePath());
        }
        FileUtils.deleteFile(requestFile);
    }
    
    
}

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish AutoUndeploymentOperation.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.