/*
 * $Url: $
 * $Id$
 *
 * Copyright 1997-2005 Day Management AG
 * Barfuesserplatz 6, 4001 Basel, Switzerland
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * Day Management AG, ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Day.
 */
package org.apache.sling.launchpad.base.impl;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import org.apache.felix.framework.Felix;
import org.apache.felix.framework.Logger;
import org.apache.sling.launchpad.base.shared.Loader;
import org.apache.sling.launchpad.base.shared.Notifiable;
import org.osgi.framework.BundleException;

public class SlingFelix extends Felix {

    private final Notifiable notifiable;
    
    private Notifier notifierThread;
    
    public SlingFelix(Notifiable notifiable, Logger arg0, Map<?, ?> arg1, List<?> arg2) {
        super(arg0, arg1, arg2);
        this.notifiable = notifiable;
    }

    public SlingFelix(Notifiable notifiable, Map<?, ?> arg1, List<?> arg2) {
        super(arg1, arg2);
        this.notifiable = notifiable;
    }

    @Override
    public void update() throws BundleException {
        update(null);
    }
    
    @Override
    public void update(InputStream is) throws BundleException {
        // get the update file
        startNotifier(true, is);
        
        // just stop the framework now
        super.stop();
    }
    
    @Override
    public void stop() throws BundleException {
        startNotifier(false, null);
        super.stop();
    }

    public void stop(int status) throws BundleException {
        startNotifier(false, null);
        super.stop(/*TODO status*/);
    }
    
    private synchronized void startNotifier(boolean restart, InputStream ins) {
        if (notifierThread == null) {
            notifierThread = new Notifier(restart, ins);
            notifierThread.setDaemon(false);
            notifierThread.start();
        }
    }
    
    private class Notifier extends Thread {

        private final boolean restart;
        
        private final File updateFile;
        
        private Notifier(boolean restart, InputStream ins) {
            super("Sling Notifier");
            this.restart = restart;
            
            if (ins != null) {
                File tmpFile;
                try {
                    tmpFile = File.createTempFile("slingupdate", ".jar");
                    Loader.spool(ins, tmpFile);
                } catch (IOException ioe) {
                    // TOOD: log
                    tmpFile = null;
                }
                updateFile = tmpFile;
            } else {
                updateFile = null;
            }
        }
        
        @Override
        public void run() {
            stopAndWait();
            
            if (restart) {
                notifiable.updated(updateFile);
            } else {
                notifiable.stopped();
            }
        }
    }
}

@@ -3,424 +3,804 @@
 %0A * 
-$Url: $%0A * $Id$%0A *%0A * Copyright 1997-2005 Day Management AG%0A * Barfuesserplatz 6, 4001 Basel, Switzerland%0A * All Rights Reserved.%0A *%0A * This software is the confidential and proprietary information of%0A * Day Management AG, (%22Confidential Information%22).
+Licensed to the Apache Software Foundation (ASF) under one%0A * or more contributor license agreements.  See the NOTICE file%0A * distributed with this work for additional information%0A * regarding copyright ownership.  The ASF licenses this file%0A * to you under the Apache License, Version 2.0 (the%0A * %22License%22); you may not use this file except in compliance%0A * with the License. 
  You 
-shall not%0A * disclose such Confidential Information and shall use it only in%0A * accordance with the terms of
+may obtain a copy of the License at%0A *%0A *   http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing,%0A * software distributed under the License is distributed on an%0A * %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY%0A * KIND, either express or implied.  See
  the 
-l
+L
 icense 
-agreement you entered into%0A * with Day
+for the%0A * specific language governing permissions and limitations%0A * under the License
 .%0A *

Fix license header