com.athena.chameleon.engine.core.analyzer.AbstractAnalyzer.java Source code

Java tutorial

Introduction

Here is the source code for com.athena.chameleon.engine.core.analyzer.AbstractAnalyzer.java

Source

/* 
 * Copyright 2012 the original author or authors.
 *
 * Licensed 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.
 *
 * Revision History
 * Author         Date            Description
 * ---------------   ----------------   ------------
 * Sang-cheon Park   2012. 9. 24.      First Draft.
 */
package com.athena.chameleon.engine.core.analyzer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.athena.chameleon.common.utils.ClasspathUtil;
import com.athena.chameleon.common.utils.ThreadLocalUtil;
import com.athena.chameleon.engine.core.analyzer.parser.ApplicationXMLParser;
import com.athena.chameleon.engine.core.analyzer.parser.EjbJarXMLParser;
import com.athena.chameleon.engine.core.analyzer.parser.IbmApplicationBndXMIParser;
import com.athena.chameleon.engine.core.analyzer.parser.IbmApplicationExtXMIParser;
import com.athena.chameleon.engine.core.analyzer.parser.IbmEjbJarBndXMIParser;
import com.athena.chameleon.engine.core.analyzer.parser.IbmEjbJarExtXMIParser;
import com.athena.chameleon.engine.core.analyzer.parser.IbmWebBndXMIParser;
import com.athena.chameleon.engine.core.analyzer.parser.IbmWebExtXMIParser;
import com.athena.chameleon.engine.core.analyzer.parser.JeusApplicationDDXMLParser;
import com.athena.chameleon.engine.core.analyzer.parser.JeusEjbDDXMLParser;
import com.athena.chameleon.engine.core.analyzer.parser.JeusWebDDXMLParser;
import com.athena.chameleon.engine.core.analyzer.parser.PomXMLParser;
import com.athena.chameleon.engine.core.analyzer.parser.WebXMLParser;
import com.athena.chameleon.engine.core.analyzer.parser.WeblogicApplicationXMLParser;
import com.athena.chameleon.engine.core.analyzer.parser.WeblogicEjbJarXMLParser;
import com.athena.chameleon.engine.core.analyzer.parser.WeblogicXMLParser;
import com.athena.chameleon.engine.core.analyzer.support.EarAnalyzer;
import com.athena.chameleon.engine.core.analyzer.support.JarAnalyzer;
import com.athena.chameleon.engine.core.analyzer.support.WarAnalyzer;
import com.athena.chameleon.engine.core.analyzer.support.ZipAnalyzer;
import com.athena.chameleon.engine.core.converter.FileEncodingConverter;
import com.athena.chameleon.engine.entity.pdf.AnalyzeDefinition;
import com.athena.chameleon.engine.policy.Policy;
import com.athena.chameleon.engine.threadpool.executor.ChameleonThreadPoolExecutor;
import com.athena.chameleon.engine.threadpool.task.ClassFileDependencyCheckTask;
import com.athena.chameleon.engine.threadpool.task.RegularFileDependencyCheckTask;

/**
 * <pre>
 * 
 * </pre>
 * @author Sang-cheon Park
 * @version 1.0
 */
public abstract class AbstractAnalyzer implements Analyzer {

    protected static final Logger logger = LoggerFactory.getLogger(AbstractAnalyzer.class);

    protected Policy policy;
    protected FileEncodingConverter converter;
    protected ChameleonThreadPoolExecutor executor;
    protected AnalyzeDefinition analyzeDefinition;

    protected List<String> libFileList;
    protected List<String> deleteFileList;

    protected List<File> warFileList;
    protected List<File> jarFileList;

    public AbstractAnalyzer(Policy policy, FileEncodingConverter converter, ChameleonThreadPoolExecutor executor,
            AnalyzeDefinition analyzeDefinition) {
        this.policy = policy;
        this.converter = converter;
        this.executor = executor;
        this.analyzeDefinition = analyzeDefinition;
    }

    public String analyze(String path) {
        File file = new File(path);
        analyzeDefinition.setFileName(file.getName());

        return analyze(file);
    }//end of analyze()

    /**
     * <pre>
     *
     * </pre>
     * @param file
     * @param rootPath
     */
    protected void analyze(File file, String rootPath) {
        defaultAnalyze(file, rootPath);
        executor.getExecutor().shutdown();

        try {
            while (!executor.getExecutor().isTerminated()) {
                Thread.sleep(100);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }//end of analyze()

    /**
     * <pre>
     *
     * </pre>
     * @param file
     * @param rootPath
     */
    private void defaultAnalyze(File file, String rootPath) {
        File[] fileList = file.listFiles();

        String extension = null;
        for (File f : fileList) {
            if (f.isDirectory()) {

                logger.debug("[jwchoi] [{}]  ?.", f.getAbsolutePath());

                // EJB Archive ? Exploded   WEB Directory ? EJB Directory?  ? .
                if (this instanceof EarAnalyzer && (warFileList.contains(file) || jarFileList.contains(file))) {
                    return;
                }

                // classpath ?  ?    . 
                if (f.getAbsolutePath().startsWith(ClasspathUtil.lastAddedPath)) {
                    analyzeDefinition.addClassDirCount();
                }
                defaultAnalyze(f, rootPath);
            } else {
                logger.debug("[jwchoi] [{}] ?? ?.", f.getAbsolutePath());
                extension = f.getName().substring(f.getName().lastIndexOf(".") + 1).toLowerCase();

                if (extension.equals("java") || extension.equals("jsp") || extension.equals("properties")) {
                    executor.execute(new RegularFileDependencyCheckTask(f, rootPath, policy, analyzeDefinition));
                } else if (extension.equals("class")) {
                    // classpath ?  class ?? ? ?  .
                    if (f.getAbsolutePath().startsWith(ClasspathUtil.lastAddedPath)) {
                        analyzeDefinition.addClassFileCount();
                        executor.execute(new ClassFileDependencyCheckTask(f, ClasspathUtil.lastAddedPath, policy,
                                analyzeDefinition));
                    }
                } else if (extension.equals("xml") || extension.equals("xmi")) {
                    // [war] WEB-INF/web.xml, WEB-INF/weblogic.xml, WEB-INF/jeus-web-dd.xml, WEB-INF/ibm-web-bnd.xmi, WEB-INF/ibm-web-ext.xmi
                    // [ear] META-INF/application.xml, META-INF/weblogic-application.xml, META-INF/jeus-application-dd.xml, META-INF/ibm-ejb-jar-bnd.xmi, META-INF/ibm-ejb-jar-ext.xmi
                    // [jar] META-INF/ejb-jar.xml, META-INF/weblogic-ejb-jar.xml, META-INF/jeus-ejb-dd.xml, META-INF/ibm-application-bnd.xmi, META-INF/ibm-application-ext.xmi
                    if (f.getParent().endsWith("WEB-INF")) {
                        if (f.getName().equals("web.xml")) {
                            if (this instanceof ZipAnalyzer || this instanceof WarAnalyzer) {
                                new WebXMLParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("weblogic.xml")) {
                            if (this instanceof ZipAnalyzer || this instanceof WarAnalyzer) {
                                new WeblogicXMLParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("jeus-web-dd.xml")) {
                            if (this instanceof ZipAnalyzer || this instanceof WarAnalyzer) {
                                new JeusWebDDXMLParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("ibm-web-bnd.xmi")) {
                            if (this instanceof ZipAnalyzer || this instanceof WarAnalyzer) {
                                new IbmWebBndXMIParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("ibm-web-ext.xmi")) {
                            if (this instanceof ZipAnalyzer || this instanceof WarAnalyzer) {
                                new IbmWebExtXMIParser().parse(f, analyzeDefinition);
                            }
                        }
                    } else if (f.getParent().endsWith("META-INF")) {
                        if (f.getName().equals("application.xml")) {
                            // EarAnalyzer application.xml ??  .
                            if (this instanceof ZipAnalyzer) {
                                new ApplicationXMLParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("weblogic-application.xml")) {
                            if (this instanceof ZipAnalyzer || this instanceof EarAnalyzer) {
                                new WeblogicApplicationXMLParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("jeus-application-dd.xml")
                                || f.getName().equals("JEUSMain.xml")) {
                            if (this instanceof ZipAnalyzer || this instanceof EarAnalyzer) {
                                new JeusApplicationDDXMLParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("ibm-application-bnd.xmi")) {
                            if (this instanceof ZipAnalyzer || this instanceof EarAnalyzer) {
                                new IbmApplicationBndXMIParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("ibm-application-ext.xmi")) {
                            if (this instanceof ZipAnalyzer || this instanceof EarAnalyzer) {
                                new IbmApplicationExtXMIParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("ejb-jar.xml")) {
                            if (this instanceof ZipAnalyzer || this instanceof JarAnalyzer) {
                                new EjbJarXMLParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("weblogic-ejb-jar.xml")) {
                            if (this instanceof ZipAnalyzer || this instanceof JarAnalyzer) {
                                new WeblogicEjbJarXMLParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("jeus-ejb-dd.xml")) {
                            if (this instanceof ZipAnalyzer || this instanceof JarAnalyzer) {
                                new JeusEjbDDXMLParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("ibm-ejb-jar-bnd.xmi")) {
                            if (this instanceof ZipAnalyzer || this instanceof JarAnalyzer) {
                                new IbmEjbJarBndXMIParser().parse(f, analyzeDefinition);
                            }
                        } else if (f.getName().equals("ibm-ejb-jar-ext.xmi")) {
                            if (this instanceof ZipAnalyzer || this instanceof JarAnalyzer) {
                                new IbmEjbJarExtXMIParser().parse(f, analyzeDefinition);
                            }
                        }
                    } else {
                        if (f.getName().equals("pom.xml")) {
                            if (this instanceof ZipAnalyzer) {
                                new PomXMLParser().parse(f, analyzeDefinition);
                            }
                        }
                    }
                }
            }
        }
    }//end of defaultAnalyzer()

    /**
     * <pre>
     * ??   .
     * </pre>
     * @param path
     * @return
     */
    protected boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();

            for (File f : files) {
                if (f.isDirectory()) {
                    deleteDirectory(f);
                } else {
                    f.delete();
                }
            }
        }

        return path.delete();
    }//end of deleteDirectory()

    /**
     * <pre>
     * classpath   classes  ?.
     * </pre>
     * @param dir
     * @return
     */
    protected String getClassesDirPath(File dir) {
        String path = null;
        if (dir.exists()) {
            File[] files = dir.listFiles();

            for (File f : files) {
                if (f.isDirectory()) {
                    // APP-INF/classes ? WEB-INF/classes ?
                    if (f.getName().equals("classes") && f.getParent().endsWith("INF")) {
                        path = f.getAbsolutePath();
                        break;
                    } else {
                        path = getClassesDirPath(f);
                    }
                }
            }
        }

        return path;
    }//end of getClassesDirPath()

    /**
     * <pre>
     * WEB-INF  ?.
     * </pre>
     * @param dir
     * @return
     */
    protected File getWebInfDirPath(File dir) {
        File path = null;
        if (dir.exists()) {
            File[] files = dir.listFiles();

            for (File f : files) {
                if (f.isDirectory()) {
                    // APP-INF/classes ? WEB-INF/classes ?
                    if (f.getName().endsWith("WEB-INF")) {
                        path = f;
                        break;
                    } else {
                        path = getWebInfDirPath(f);
                    }
                }
            }
        }

        return path;
    }//end of getWebInfDirPath()

    /**
     * <pre>
     * META-INF  ?.
     * </pre>
     * @param dir
     * @return
     */
    protected File getMetaInfDirPath(File dir) {
        File path = null;
        if (dir.exists()) {
            File[] files = dir.listFiles();

            for (File f : files) {
                if (f.isDirectory()) {
                    // APP-INF/classes ? WEB-INF/classes ?
                    if (f.getName().endsWith("META-INF")) {
                        path = f;
                        break;
                    } else {
                        path = getMetaInfDirPath(f);
                    }
                }
            }
        }

        return path;
    }//end of getMetaInfDirPath()

    /**
     * <pre>
     * Migration   ${fileName}-result.${archiveType}     ?? ?.
     * </pre>
     * @param file
     * @return
     */
    protected String getResultFile(File file) {
        String resultFile = file.getAbsolutePath();
        resultFile = resultFile.substring(0, resultFile.lastIndexOf(".")) + "-result"
                + resultFile.substring(resultFile.lastIndexOf("."));

        return resultFile;
    }//end of getResultFile()

    /**
     * <pre>
     * jboss-classloading.xml ?? ?.
     * </pre>
     * @param parentPath
     * @param domain
     * @param patentDomain
     */
    protected void makeClassLoading(File parentPath, String domain, String patentDomain) {
        if (parentPath != null && domain != null) {
            StringBuilder sb = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");

            if (StringUtils.isEmpty(patentDomain)) {
                sb.append("<classloading xmlns=\"urn:jboss:classloading:1.0\"\r\n").append("        domain=\"")
                        .append(domain).append("\"\r\n").append("        export-all=\"NON_EMPTY\"\r\n")
                        .append("        import-all=\"true\"\r\n").append("        parent-first=\"false\">\r\n")
                        .append("</classloading>");
            } else {
                /*
                sb.append("<classloading xmlns=\"urn:jboss:classloading:1.0\"\r\n")
                   .append("        domain=\"").append(domain).append("\"\r\n")
                   .append("        parent-domain=\"").append(patentDomain).append("\"\r\n")
                   .append("        export-all=\"NON_EMPTY\"\r\n")
                   .append("        import-all=\"true\">\r\n")
                   .append("</classloading>");
                */
                sb.append("<classloading xmlns=\"urn:jboss:classloading:1.0\"\r\n").append("        name=\"")
                        .append(domain).append("\"\r\n").append("        domain=\"DefaultDomain\"\r\n")
                        .append("        parent-domain=\"Ignored\"\r\n")
                        .append("        parent-first=\"false\"\r\n").append("        export-all=\"NON_EMPTY\"\r\n")
                        .append("        import-all=\"true\">\r\n").append("</classloading>");
            }

            try {
                File file = new File(parentPath, "jboss-classloading.xml");
                FileWriter fw = new FileWriter(file);
                fw.write(sb.toString());
                IOUtils.closeQuietly(fw);
            } catch (IOException e) {
                logger.error("IOException has occurred.", e);
            }
        }
    }//end of makeClassLoading()

    /**
     * <pre>
     *  ??    ?   ?  .
     * </pre>
     * @param fullPath
     * @param key
     * @return
     */
    protected String removeTempDir(String fullPath, String key) {
        String tempPath = (String) ThreadLocalUtil.get(key);

        if (StringUtils.isEmpty(tempPath)) {
            return fullPath;
        } else {
            return fullPath.substring(tempPath.length() + 1);
        }
    }//end of removeTempDir()

}//end of DependencyAnalyzer.java