/*******************************************************************************
 * Copyright (c) 2011, 2016 Eurotech and/or its affiliates
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Eurotech
 *******************************************************************************/
package org.eclipse.kura.emulator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.osgi.service.component.ComponentContext;


public class Emulator {
    private static final String SNAPSHOT_0_NAME = "snapshot_0.xml";

    private ComponentContext m_componentContext;

    protected void activate(ComponentContext componentContext) 
    {
        m_componentContext = componentContext;

        try {
            //Properties props = System.getProperties();
            String mode = System.getProperty("org.eclipse.kura.mode");
            if(mode.equals("emulator")) {
                System.out.println("Framework is running in emulation mode");
            } else {
                System.out.println("Framework is not running in emulation mode");
            }

            String snapshotFolderPath= System.getProperty("kura.snapshots");
            File snapshotFolder = new File(snapshotFolderPath);
            if (!snapshotFolder.exists() || snapshotFolder.list().length == 0) {
                snapshotFolder.mkdirs();
                copySnapshot(snapshotFolderPath);
            }
        } catch(Exception e) {
            System.out.println("Framework is not running in emulation mode or initialization failed!");
        }
    }

    protected void deactivate(ComponentContext componentContext) {
        m_componentContext = null;
    }

    private void copySnapshot(String snapshotFolderPath) throws IOException{
        InputStream fileInput= null;
        OutputStream fileOutput= null;
        try {
            URL internalSnapshotURL= m_componentContext.getBundleContext().getBundle().getResource(SNAPSHOT_0_NAME); 
            fileInput= internalSnapshotURL.openStream();
            fileOutput= new FileOutputStream(snapshotFolderPath + File.separator + SNAPSHOT_0_NAME);
            if (fileInput != null) {
                IOUtils.copy(fileInput, fileOutput);
            }
        } finally {
            if (fileOutput != null) {
                fileOutput.close();
            }
            if (fileInput != null) {
                fileInput.close();
            }
        }
    }
}

@@ -1211,30 +1211,30 @@
  if(
-mode.equals(%22emulator%22
+%22emulator%22.equals(mode
 )) %7B
@@ -1438,24 +1438,30 @@
             
+final 
 String snaps
@@ -1473,16 +1473,17 @@
 lderPath
+ 
 = System
@@ -1526,16 +1526,208 @@
         
+if (snapshotFolderPath == null %7C%7C snapshotFolderPath.isEmpty()) %7B%0A            %09throw new IllegalStateException (%22System property 'kura.snapshots' is not set%22);%0A            %7D%0A            final 
 File sna
@@ -2087,17 +2087,36 @@
  failed!
-%22
+: %22 + e.getMessage()
 );%0A     

Fix two NullPointerExceptions and improve the error output