/*
 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
 */

package com.palantir.remoting2.http2;

import com.ea.agentloader.AgentLoader;
import java.lang.instrument.Instrumentation;
import java.util.concurrent.atomic.AtomicBoolean;
import org.mortbay.jetty.alpn.agent.Premain;

/** A very simple wrapper around the jetty-alpn-agent for dynamic loading. */
public final class Http2Agent {
    private Http2Agent() {}

    private static final AtomicBoolean installed = new AtomicBoolean();

    /**
     * Installs the jetty-alpn-agent dynamically.
     * <p>
     * This method protects itself from multiple invocations so may be called in multiple places, but will only
     * ever invoke the installation once.
     */
    public static void install() {
        if (installed.compareAndSet(false, true)) {
            AgentLoader.loadAgentClass(Http2Agent.class.getName(), "");
        }
    }

    /** Agent entry-point. */
    public static void agentmain(String args, Instrumentation inst) throws Exception {
        Premain.premain(args, inst);
    }
}

@@ -195,58 +195,8 @@
 on;%0A
-import java.util.concurrent.atomic.AtomicBoolean;%0A
 impo
@@ -236,16 +236,16 @@
 remain;%0A
+
 %0A/** A v
@@ -399,59 +399,40 @@
 tic 
-final AtomicBoolean installed = new AtomicBoolean()
+boolean hasBeenInstalled = false
 ;%0A%0A 
@@ -675,24 +675,37 @@
 blic static 
+synchronized 
 void install
@@ -725,52 +725,59 @@
 if (
-installed.compareAndSet(false, true)) %7B%0A
+hasBeenInstalled) %7B%0A            return;%0A    
     
+%7D%0A%0A
     
@@ -848,17 +848,40 @@
         
-%7D
+hasBeenInstalled = true;
 %0A    %7D%0A%0A

Synchronize instead of broken atomic boolean (#379)