/*
 * 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 io.aeron.driver.MediaDriver.loadPropertiesFiles;

/**
 * Archiving media driver which has dedicated threads for high throughput.
 */
public class ArchivingMediaDriver
{
    /**
     * Start an {@link ArchiveConductor} as a stand-alone process, with a {@link MediaDriver}.
     *
     * @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);

        launch();
    }

    /**
     * Launch an {@link Archive} with embedded {@link MediaDriver} and await a shutdown signal.
     *
     */
    public static void launch()
    {
        final MediaDriver.Context driverCtx = new MediaDriver.Context()
            .useConcurrentCounterManager(Configuration.THREADING_MODE_DEFAULT != ThreadingMode.INVOKER);
        final MediaDriver mediaDriver = MediaDriver.launch(driverCtx);

        final Archive.Context archiveCtx = new Archive.Context()
            .mediaDriverAgentInvoker(mediaDriver.sharedAgentInvoker());

        archiveCtx
            .countersManager(driverCtx.countersManager())
            .errorHandler(driverCtx.errorHandler());

        final Archive archive = Archive.launch(archiveCtx);

        new ShutdownSignalBarrier().await();
        System.out.println("Shutdown Archive...");

        archive.close();
        mediaDriver.close();
    }
}

@@ -1406,23 +1406,16 @@
 signal.%0A
-     *%0A
      */%0A
@@ -1470,79 +1470,16 @@
 nal 
-MediaDriver.Context driverCtx = new MediaDriver.Context()%0A            .
+boolean 
 useC
@@ -1501,17 +1501,19 @@
 rManager
-(
+ = 
 Configur
@@ -1565,18 +1565,18 @@
 .INVOKER
-)
 ;
+%0A
 %0A       
@@ -1576,22 +1576,21 @@
         
-final 
+try (
 MediaDri
@@ -1593,22 +1593,17 @@
 aDriver 
-mediaD
+d
 river = 
@@ -1625,63 +1625,193 @@
 nch(
-driverCtx);%0A%0A        final Archive.Context archiveCtx =
+%0A                new MediaDriver.Context()%0A                    .useConcurrentCounterManager(useConcurrentCounterManager));%0A             Archive ignore = Archive.launch(%0A                
  new
@@ -1841,16 +1841,24 @@
         
+      
+  
 .mediaDr
@@ -1874,22 +1874,17 @@
 Invoker(
-mediaD
+d
 river.sh
@@ -1898,26 +1898,24 @@
 ntInvoker())
-;%0A
 %0A        arc
@@ -1915,19 +1915,8 @@
     
-archiveCtx%0A
     
@@ -1946,19 +1946,26 @@
 r(driver
-Ctx
+.context()
 .counter
@@ -1988,16 +1988,24 @@
         
+    
+    
 .errorHa
@@ -2016,19 +2016,26 @@
 r(driver
-Ctx
+.context()
 .errorHa
@@ -2042,18 +2042,18 @@
 ndler())
-;%0A
+))
 %0A       
@@ -2057,61 +2057,14 @@
     
-final Archive archive = Archive.launch(archiveCtx);%0A%0A
+%7B%0A    
     
@@ -2104,24 +2104,25 @@
 wait();%0A
+%0A
         
 System.o
@@ -2113,16 +2113,20 @@
         
+    
 System.o
@@ -2160,25 +2160,24 @@
 e...%22);%0A
-%0A
         
 archive.
@@ -2172,53 +2172,9 @@
     
-archive.close();%0A        mediaDriver.close();
+%7D
 %0A   

Tidy up ArchivingMediaDriver.