de.hu_berlin.german.korpling.saltnpepper.debugger.PepperConversionWorker.java Source code

Java tutorial

Introduction

Here is the source code for de.hu_berlin.german.korpling.saltnpepper.debugger.PepperConversionWorker.java

Source

/*
 * Copyright 2013 Thomas Krause <krauseto@hu-berlin.de>.
 *
 * 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.
 */
package de.hu_berlin.german.korpling.saltnpepper.debugger;

import com.google.common.io.Files;
import com.google.common.io.PatternFilenameFilter;
import static de.hu_berlin.german.korpling.saltnpepper.pepper.pepperStarter.PepperProperties.KW_ENV_PEPPER_HOME;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.SwingWorker;
import org.eclipse.core.runtime.adaptor.EclipseStarter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Thomas Krause <krauseto@hu-berlin.de>
 */
public class PepperConversionWorker extends SwingWorker<Integer, String> {
    private static final Logger log = LoggerFactory.getLogger(PepperConversionWorker.class);

    private JButton btStart;
    private File[] selectedModules;
    private File pepperRoot;
    private File pepperParam;
    private Properties pepperProperties;

    public PepperConversionWorker(File[] selectedModules, File pepperParam, File pepperRoot, JButton btStart) {
        this.selectedModules = selectedModules;
        this.pepperParam = pepperParam;
        this.pepperRoot = pepperRoot;
        this.btStart = btStart;
        this.pepperProperties = new Properties();
    }

    @Override
    protected Integer doInBackground() {
        try {
            EclipseStarter.shutdown();

            File pluginDir = new File(pepperRoot, "plugins");

            copyFiles(pluginDir);

            // Pepper expects the PEPPER_HOME variable to be set
            // set some properties
            pepperProperties.load(new FileReader(new File(pepperRoot, "conf/userDefined.properties")));
            for (Object key : Collections.synchronizedSet(pepperProperties.keySet())) {
                pepperProperties.put(key, pepperProperties.get(key).toString().replace(KW_ENV_PEPPER_HOME,
                        pepperRoot.getAbsolutePath()));
            }

            // actually execute the pepper starter
            PepperStarter starter = new PepperStarter();
            starter.setPepperHomeDir(pepperRoot);
            starter.setProperties(pepperProperties);

            publish("starting Pepper\n");
            starter.start(new URI(pepperParam.getAbsolutePath()));

        } catch (Exception ex) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            ex.printStackTrace(pw);
            publish("Exception occured: " + ex.getMessage() + "\n" + sw.toString() + "\n");

        }

        return 0;
    }

    private void copyFiles(File pluginDir) throws IOException {
        Pattern versionPattern = Pattern.compile("(-|_)?([\\.0-9]|-SNAPSHOT)+\\.jar$");
        Pattern packagePattern = Pattern.compile("^de\\.hu_berlin.german\\.korpling\\.saltnpepper\\.");
        // copy all the needed jars into the plugin folder
        for (File f : selectedModules) {
            // delete existing other versions of this file with a heuristic
            // remove the version part of the current name
            String pureName = versionPattern.matcher(f.getName()).replaceFirst("");
            // also remove a trailing package name which is used by the core pepper plugins
            pureName = packagePattern.matcher(pureName).replaceFirst("");

            for (File existing : pluginDir.listFiles(new PatternFilenameFilter("^" + pureName + ".*\\.jar$"))) {
                if (existing.delete()) {
                    log.info("Deleted existing version {}", existing.getName());
                }
            }

            Files.copy(f, new File(pluginDir, f.getName()));
            publish("copied " + f.getName() + "\n");
        }
    }

    @Override
    protected void process(List<String> chunks) {
        for (String s : chunks) {
            log.info(s);
        }

    }

    @Override
    protected void done() {
        if (btStart != null) {
            btStart.setEnabled(true);
        }
    }

}