com.c4om.autoconf.ulysses.configanalyzer.guilauncher.ChromeAppEditorGUILauncher.java Source code

Java tutorial

Introduction

Here is the source code for com.c4om.autoconf.ulysses.configanalyzer.guilauncher.ChromeAppEditorGUILauncher.java

Source

/*
Copyright 2014 Universidad Politcnica de Madrid - Center for Open Middleware (http://www.centeropenmiddleware.com)
    
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 com.c4om.autoconf.ulysses.configanalyzer.guilauncher;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import java.util.regex.Pattern;

import com.c4om.autoconf.ulysses.configanalyzer.core.GUILauncher;
import com.c4om.autoconf.ulysses.configanalyzer.core.datastructures.ConfigurationAnalysisContext;
import com.c4om.autoconf.ulysses.configanalyzer.core.exceptions.GUILaunchException;
import com.c4om.autoconf.ulysses.interfaces.Target;
import com.google.common.collect.ImmutableList;

import org.apache.commons.io.Charsets;
import org.apache.commons.io.FileUtils;

/**
 * This {@link GUILauncher} launches the editor as a Google Chrome web app
 * @author Pablo Alonso Rodrguez (Center for Open Middleware - UPM)
 *
 */
public class ChromeAppEditorGUILauncher extends TemporaryChangesetFilesSavingGUILauncher implements GUILauncher {

    /**
     * Key for the property that points  to the editor location.
     */
    private static final String PROPERTIES_KEY_EDITOR_CHROME_APP_LOCATION = "guieditor.editorChromeAppLocation";

    /**
     * Key for the property that points  to the editor location.
     */
    private static final String PROPERTIES_KEY_CHROME_LOCATION = "guieditor.chromeLocation";

    /**
     * File name for the properties file that controls the editor launch.
     */
    public static final String LAUNCH_PROPERTIES_FILE_NAME = "launch.properties";

    //   /**
    //    * The changeset file to apply at editor (this value will be used to build the launch.properties).
    //    */
    //   private static final String CHANGESET_TO_APPLY = "/testApp-applicationRequirements.xml";

    //   /**
    //    * Key of the property that specifies which changeset file must be loaded at editor.
    //    */
    //   private static final String KEY_LOAD_RECOMMENDATIONS_FILE = "load_recommendations_file";
    //   
    /**
     * Key of the property that specifies which catalog file to use.
     */
    private static final String KEY_CATALOG = "catalog";

    /**
     * Value for property {@link ChromeAppEditorGUILauncher#KEY_CATALOG} to get the generated catalog file.
     */
    public static final String CATALOG_FILE_PATH = "/catalog.xml";

    /**
     * Constructor. It reads some GUI launch configuration properties.
     * @throws GUILaunchException if those properties could not be read.
     */
    public ChromeAppEditorGUILauncher() throws GUILaunchException {

    }

    /**
     * @see com.c4om.autoconf.ulysses.interfaces.configanalyzer.core.GUILauncher#launchGUI(com.c4om.autoconf.ulysses.interfaces.configanalyzer.core.datastructures.ConfigurationAnalysisContext, com.c4om.autoconf.ulysses.interfaces.Target)
     */
    @SuppressWarnings("resource")
    @Override
    public void launchGUI(ConfigurationAnalysisContext context, Target target) throws GUILaunchException {
        try {
            List<File> temporaryFolders = this.getTemporaryStoredChangesetsAndConfigs(context, target);

            for (File temporaryFolder : temporaryFolders) {

                //The temporary folder where one subfolder per analyzer execution will be stored (and removed).
                File temporaryFolderRoot = temporaryFolder.getParentFile().getParentFile();

                System.out.println("Writing launch properties.");

                //Now, we build the properties file
                Properties launchProperties = new Properties();
                String relativeTemporaryFolder = temporaryFolder.getAbsolutePath()
                        .replaceAll("^" + Pattern.quote(temporaryFolderRoot.getAbsolutePath()), "")
                        .replaceAll("^" + Pattern.quote(File.separator), "")
                        .replaceAll(Pattern.quote(File.separator) + "$", "")
                        .replaceAll(Pattern.quote(File.separator) + "+", "/");
                //            launchProperties.put(KEY_LOAD_RECOMMENDATIONS_FILE, relativeTemporaryFolder+CHANGESET_TO_APPLY);
                launchProperties.put(KEY_CATALOG, relativeTemporaryFolder + CATALOG_FILE_PATH);
                Writer writer = new OutputStreamWriter(
                        new FileOutputStream(new File(temporaryFolderRoot, LAUNCH_PROPERTIES_FILE_NAME)),
                        Charsets.UTF_8);
                launchProperties.store(writer, "");
                writer.close();
                System.out.println("Launch properties written!!!!");

                System.out.println("Launching XML editor Chrome app");

                Properties configurationAnalyzerProperties = context.getConfigurationAnalyzerSettings();
                String chromeLocation = configurationAnalyzerProperties.getProperty(PROPERTIES_KEY_CHROME_LOCATION);
                String editorChromeAppLocation = configurationAnalyzerProperties
                        .getProperty(PROPERTIES_KEY_EDITOR_CHROME_APP_LOCATION);

                ProcessBuilder chromeEditorPB = new ProcessBuilder(
                        ImmutableList.of(chromeLocation, "--load-and-launch-app=" + editorChromeAppLocation));
                chromeEditorPB.directory(new File(editorChromeAppLocation));
                chromeEditorPB.start();
                System.out.println("Editor started!!!");
                System.out.println("Now, make all your changes and press ENTER when finished...");
                new Scanner(System.in).nextLine();
                FileUtils.forceDeleteOnExit(temporaryFolder.getParentFile());

            }
        } catch (IOException e) {
            throw new GUILaunchException(e);
        }
    }

}