/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 * <p>
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * <p>
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package org.apache.aries.osgi.functional.internal;

import org.apache.aries.osgi.functional.OSGi;
import org.apache.aries.osgi.functional.OSGiResult;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author Carlos Sierra Andrés
 */
public class DistributeOSGi extends OSGiImpl<Void> {

    public DistributeOSGi(OSGi<?>... programs) {
        super(bundleContext -> {
            Pipe<Tuple<Void>, Tuple<Void>> added = Pipe.create();

            Consumer<Tuple<Void>> addedSource = added.getSource();

            List<OSGiResult<?>> results = new ArrayList<>();

            Pipe<Tuple<Void>, Tuple<Void>> removed = Pipe.create();

            Consumer<Tuple<Void>> removedSource = removed.getSource();

            return new OSGiResultImpl<>(
                added, removed,
                () -> {
                    results.addAll(
                        Arrays.stream(programs).
                            map(o -> o.run(bundleContext)).
                            collect(Collectors.toList()));

                    addedSource.accept(Tuple.create(null));
                },
                () -> {
                    removedSource.accept(Tuple.create(null));

                    results.forEach(OSGiResult::close);
                }
            );
        });
    }
}

@@ -1,426 +1,632 @@
 /*
-*
 %0A * 
-Copyright (c) 2000-present Liferay, Inc. All rights reserved.%0A * %3Cp%3E%0A * This library is free software; you can redistribute it and/or modify it under
+Licensed to the Apache Software Foundation (ASF) under one or more%0A * contributor license agreements.  See the NOTICE file distributed with%0A * this work for additional information regarding copyright ownership.
 %0A * 
-the terms of the GNU Lesser General Public License as published by the Free%0A * Software Foundation; either version 2.1 of the License, or (at your option)%0A * any later version.%0A * %3Cp%3E%0A * This library is distributed in the hope that it will be useful, but
+The ASF licenses this file to You under the Apache License, Version 2.0%0A * (the %22License%22); you may not use this file except in compliance with%0A * the License.  You 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, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A *
  WITHOUT
 %0A * 
@@ -625,23 +625,16 @@
 HOUT
-%0A * ANY
  WARRANT
 Y; w
@@ -633,165 +633,169 @@
 RANT
-Y; without even the implied warranty of MERCHANTABILITY or FITNESS%0A * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more%0A * details
+IES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License
 .%0A *

Fix license headers