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

Axis 2 example source code file (WSInfoList.java)

This example Axis 2 source code file (WSInfoList.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 - Axis 2 tags/keywords

arraylist, deployer, deploymentfiledata, deploymentfiledata, file, file, hashmap, io, iterator, list, string, string, util, wsinfo, wsinfo, wsinfolist

The Axis 2 WSInfoList.java source code

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */


package org.apache.axis2.deployment.repository.util;

import org.apache.axis2.deployment.DeploymentConstants;
import org.apache.axis2.deployment.DeploymentEngine;
import org.apache.axis2.deployment.Deployer;

import java.io.File;
import java.util.*;

public class WSInfoList implements DeploymentConstants {

    /**
     * This is to store all the jar files in a specified folder (WEB_INF)
     */
    private List jarList = new ArrayList();

    /**
     * All the currently updated jars
     */
    public Map currentJars = new HashMap();

    /**
     * Reference to DeploymentEngine to make update
     */

    private boolean locked = false;
    private final DeploymentEngine deploymentEngine;

    public WSInfoList(DeploymentEngine deploy_engine) {
        deploymentEngine = deploy_engine;
    }

    /**
     * First checks whether the file is already available by the
     * system call fileExists. If it is not deployed yet then adds to the jarList
     * and to the deployment engine as a new service or module.
     * While adding new item to jarList, first creates the WSInfo object and
     * then adds to the jarlist and actual jar file is added to DeploymentEngine.
     * <p/>
     * If the files already exists, then checks whether it has been updated
     * then changes the last update date of the wsInfo and adds two entries to
     * DeploymentEngine - one for new deployment and other for undeployment.
     *
     * @param file actual jar files for either Module or service
     */
    public synchronized void addWSInfoItem(File file, Deployer deployer , int type) {
        WSInfo info = (WSInfo) currentJars.get(file.getName());
        if (info != null) {
            if (deploymentEngine.isHotUpdate() && isModified(file, info)) {
//            info.setLastModifiedDate(file.lastModified());
                WSInfo wsInfo = new WSInfo(info.getFileName(), info.getLastModifiedDate(), deployer,type);
                deploymentEngine.addWSToUndeploy(wsInfo);           // add entry to undeploy list
                DeploymentFileData deploymentFileData = new DeploymentFileData(file, deployer);
                deploymentEngine.addWSToDeploy(deploymentFileData);    // add entry to deploylist
            }
        } else {
            info = getFileItem(file,deployer,type);
            setLastModifiedDate(file,info);
        }

        jarList.add(info.getFileName());
    }

    /**
     * Checks undeployed Services. Checks old jars files and current jars.
     * If name of the old jar file does not exist in the current jar
     * list then it is assumed that the jar file has been removed
     * and that is hot undeployment.
     */
    private synchronized void checkForUndeployedServices() {
        if(!locked) {
            locked = true;
        } else{
            return;
        }
        Iterator infoItems = currentJars.keySet().iterator();
        List tobeRemoved = new ArrayList();
        while (infoItems.hasNext()) {
            String  fileName = (String) infoItems.next();
            WSInfo infoItem = (WSInfo) currentJars.get(fileName);
            if (infoItem.getType() == WSInfo.TYPE_MODULE) {
                continue;
            }
            //seems like someone has deleted the file , so need to undeploy
            boolean found = false;
            for (int i = 0; i < jarList.size(); i++) {
                String s = (String) jarList.get(i);
                if(fileName.equals(s)){
                    found = true;
                }
            }
            if(!found){
                tobeRemoved.add(fileName);
                deploymentEngine.addWSToUndeploy(infoItem);
            }
        }

        for (int i = 0; i < tobeRemoved.size(); i++) {
            String fileName = (String) tobeRemoved.get(i);
            currentJars.remove(fileName);
        }
        tobeRemoved.clear();
        jarList.clear();
        locked = false;
    }

    /**
     * Clears the jarlist.
     */
    public void init() {
        jarList.clear();
    }

    /**
     *
     */
    public void update() {
        synchronized (deploymentEngine) {
            checkForUndeployedServices();
            deploymentEngine.unDeploy();
            deploymentEngine.doDeploy();
        }
    }

    /**
     * Gets the WSInfo object related to a file if it exists, null otherwise.
     *
     */
    private WSInfo getFileItem(File file , Deployer deployer , int type) {
        String fileName = file.getName();
        WSInfo info = (WSInfo) currentJars.get(fileName);
        if(info==null){
            info = new WSInfo(file.getName(), file.lastModified(), deployer ,type);
            currentJars.put(file.getName(),info);
            DeploymentFileData fileData = new DeploymentFileData(file, deployer);
            deploymentEngine.addWSToDeploy(fileData);
        }
        return info;
    }

    /**
     * Checks if a file has been modified by comparing the last update date of
     * both files and WSInfo. If they are different, the file is assumed to have
     * been modified.
     *
     * @param file
     * @param wsInfo
     */
    private boolean isModified(File file, WSInfo wsInfo) {
        if (file.isDirectory()) {
            if (isChanged(file, wsInfo.getLastModifiedDate(), wsInfo)) {
                setLastModifiedDate(file, wsInfo);
                return true;
            } else {
                return false;
            }
        } else {
            if(wsInfo.getLastModifiedDate() != file.lastModified()) {
                wsInfo.setLastModifiedDate(file.lastModified());
                return true;
            } else {
                return false;
            }
        }
    }

    private void setLastModifiedDate(File file, WSInfo wsInfo) {
        if (file.isDirectory()) {
            File files [] = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                File fileItem = files[i];
                if (fileItem.isDirectory()) {
                    setLastModifiedDate(fileItem, wsInfo);
                } else {
                    fileItem.setLastModified(wsInfo.getLastModifiedDate());
                }
            }
        } else {
            file.setLastModified(wsInfo.getLastModifiedDate());
        }
    }

    private boolean isChanged(File file, long lastModifedData, WSInfo wsInfo) {
        File files [] = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            File fileItem = files[i];
            if (fileItem.isDirectory()) {
                if (isChanged(fileItem, lastModifedData, wsInfo)) {
                    wsInfo.setLastModifiedDate(fileItem.lastModified());
                    return true;
                }
            } else {
                if (lastModifedData != fileItem.lastModified()) {
                    wsInfo.setLastModifiedDate(fileItem.lastModified());
                    return true;
                }
            }
        }
        return false;
    }


}

Other Axis 2 examples (source code examples)

Here is a short list of links related to this Axis 2 WSInfoList.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.