com.huawei.streaming.cql.executor.mergeuserdefinds.Merger.java Source code

Java tutorial

Introduction

Here is the source code for com.huawei.streaming.cql.executor.mergeuserdefinds.Merger.java

Source

/**
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 com.huawei.streaming.cql.executor.mergeuserdefinds;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Strings;
import com.huawei.streaming.api.Application;

/**
 * ?
 * <p>
 * Storm????jar
 * ?jarstorm??
 */
public class Merger {

    private static final Logger LOG = LoggerFactory.getLogger(Merger.class);

    /**
     * jar??
     */
    private static final String JAR_UNZIP_DIR_NAME = "jartmp";

    /**
     * jar
     */
    private File tmpOutputDir = null;

    /**
     * jar
     */
    private File tmpJarUnzipDir = null;

    /**
     * ?Jar
     *
     * @param app ?
     * @param tmpDir ?Jar
     * @param jarOutputFile ?Jar
     * @throws IOException ?
     */
    public void merge(Application app, String tmpDir, String jarOutputFile) throws IOException {
        // uuidjar
        createTmpDir(tmpDir);
        // ?jarjar
        copyFilesToTmp(app);
        // jartmpjar
        new JarExpander(tmpJarUnzipDir, tmpOutputDir).expand();
        // tmp?jar?jar???????
        new JarFilesMerger(tmpJarUnzipDir, tmpOutputDir).mergeJarFiles();
        // tmp
        FileUtils.deleteDirectory(tmpJarUnzipDir);
        // ?jar
        new JarPacker(jarOutputFile, tmpOutputDir).pack();
        // jar
        FileUtils.deleteDirectory(tmpOutputDir);

    }

    private void copyFilesToTmp(Application app) throws IOException {
        String[] files = app.getUserFiles();

        if (files != null) {
            for (String file : files) {
                FileUtils.copyFileToDirectory(new File(file), tmpOutputDir);
            }
        }

        if (Strings.isNullOrEmpty(System.getProperty("cql.dependency.jar"))) {
            //for unit test, not throw exception here.
            LOG.error("Failed to found cql.dependency.jar path in System properties.");
        } else {
            String path = System.getProperty("cql.dependency.jar");
            FileUtils.copyFileToDirectory(new File(URLDecoder.decode(path, "UTF-8")), tmpOutputDir);
        }
    }

    private void createTmpDir(String tmpDir) throws IOException {
        UUID uuid = UUID.randomUUID();
        String dirName = uuid.toString().replace("-", "");
        File baseDir = new File(tmpDir);
        tmpOutputDir = new File(baseDir, dirName);
        tmpJarUnzipDir = new File(tmpOutputDir, JAR_UNZIP_DIR_NAME);
        if (!tmpJarUnzipDir.mkdirs()) {
            LOG.error("failed to create tmp dir");
            throw new IOException("failed to create tmp dir");
        }
    }

}