/*
 * Copyright 2014-2017 Real Logic Ltd.
 *
 * 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 io.aeron.archive;

import io.aeron.driver.Configuration;
import io.aeron.driver.MediaDriver;
import io.aeron.driver.ThreadingMode;
import org.agrona.concurrent.ShutdownSignalBarrier;

import static org.agrona.SystemUtil.loadPropertiesFiles;

/**
 * Archiving {@link MediaDriver}.
 */
public final class ArchivingMediaDriver implements AutoCloseable
{
    private final MediaDriver driver;
    private final Archive archive;

    private ArchivingMediaDriver(final MediaDriver driver, final Archive archive)
    {
        this.driver = driver;
        this.archive = archive;
    }

    /**
     * Get the launched {@link Archive}.
     *
     * @return the launched {@link Archive}.
     */
    public Archive archive()
    {
        return archive;
    }

    /**
     * Get the launched {@link MediaDriver}.
     *
     * @return the launched {@link MediaDriver}.
     */
    public MediaDriver mediaDriver()
    {
        return driver;
    }

    public void close()
    {
        archive.close();
        driver.close();
    }

    /**
     * Launch an {@link Archive} with an embedded {@link MediaDriver} and await a shutdown signal.
     *
     * @param args command line argument which is a list for properties files as URLs or filenames.
     */
    public static void main(final String[] args)
    {
        loadPropertiesFiles(args);

        try (ArchivingMediaDriver ignore = launch())
        {
            new ShutdownSignalBarrier().await();

            System.out.println("Shutdown Archive...");
        }
    }

    /**
     * Launch a new {@link ArchivingMediaDriver} with default contexts.
     *
     * @return a new {@link ArchivingMediaDriver} with default contexts.
     */
    public static ArchivingMediaDriver launch()
    {
        return launch(new MediaDriver.Context(), new Archive.Context());
    }

    /**
     * Launch a new {@link ArchivingMediaDriver} with provided contexts.
     *
     * @param driverCtx  for configuring the {@link MediaDriver}.
     * @param archiveCtx for configuring the {@link Archive}.
     * @return a new {@link ArchivingMediaDriver} with the provided contexts.
     */
    public static ArchivingMediaDriver launch(final MediaDriver.Context driverCtx, final Archive.Context archiveCtx)
    {
        final boolean useConcurrentCounterManager =
            driverCtx.threadingMode() != ThreadingMode.INVOKER ||
                (driverCtx.threadingMode() == null && Configuration.THREADING_MODE_DEFAULT != ThreadingMode.INVOKER);

        final MediaDriver driver = MediaDriver.launch(driverCtx
            .spiesSimulateConnection(true)
            .useConcurrentCountersManager(useConcurrentCounterManager));

        final Archive archive = Archive.launch(archiveCtx
            .mediaDriverAgentInvoker(driver.sharedAgentInvoker())
            .countersManager(driver.context().countersManager())
            .errorHandler(driver.context().errorHandler()));

        return new ArchivingMediaDriver(driver, archive);
    }
}

@@ -860,34 +860,97 @@
 chiving 
-%7B@link MediaDriver
+media driver which is an aggregate of a %7B@link MediaDriver%7D and an %7B@link Archive
 %7D.%0A */%0Ap
@@ -1250,459 +1250,8 @@
  %7D%0A%0A
-    /**%0A     * Get the launched %7B@link Archive%7D.%0A     *%0A     * @return the launched %7B@link Archive%7D.%0A     */%0A    public Archive archive()%0A    %7B%0A        return archive;%0A    %7D%0A%0A    /**%0A     * Get the launched %7B@link MediaDriver%7D.%0A     *%0A     * @return the launched %7B@link MediaDriver%7D.%0A     */%0A    public MediaDriver mediaDriver()%0A    %7B%0A        return driver;%0A    %7D%0A%0A    public void close()%0A    %7B%0A        archive.close();%0A        driver.close();%0A    %7D%0A%0A
     
@@ -1809,33 +1809,92 @@
 with default
- c
+s for %7B@link MediaDriver.Context%7D and%0A     * %7B@link Archive.C
 ontext
-s
+%7D
 .%0A     *%0A   
@@ -3195,24 +3195,24 @@
 ndler()));%0A%0A
-
         retu
@@ -3262,11 +3262,462 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Get the launched %7B@link Archive%7D.%0A     *%0A     * @return the launched %7B@link Archive%7D.%0A     */%0A    public Archive archive()%0A    %7B%0A        return archive;%0A    %7D%0A%0A    /**%0A     * Get the launched %7B@link MediaDriver%7D.%0A     *%0A     * @return the launched %7B@link MediaDriver%7D.%0A     */%0A    public MediaDriver mediaDriver()%0A    %7B%0A        return driver;%0A    %7D%0A%0A    public void close()%0A    %7B%0A        archive.close();%0A        driver.close();%0A    %7D
 %0A%7D%0A

Improve ArchivingMediaDriver javadoc.