/*
 * Copyright 2005-2014 Red Hat, Inc.
 * Red Hat licenses this file to you 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.fabric8.testApp;

public class TestApp {

    public static void main(String args[]) {
        try {
            System.err.println("RUNNING ...");
            final int COUNT = 2;
            for (int i = 0; i < COUNT; i++) {
                TestLoad testLoad = new TestLoad("TestLoad:" + i);
                testLoad.doStart();
            }
            TestLoad testLoad = new TestLoad("CreateDeath", 1000);
            testLoad.doStart();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

@@ -646,30 +646,846 @@
 p;%0A%0A
-public class TestApp %7B
+import javax.management.InstanceNotFoundException;%0Aimport javax.management.MBeanException;%0Aimport javax.management.MBeanServer;%0Aimport javax.management.MalformedObjectNameException;%0Aimport javax.management.ObjectName;%0Aimport javax.management.ReflectionException;%0Aimport java.lang.management.ManagementFactory;%0Aimport java.util.concurrent.atomic.AtomicBoolean;%0A%0Apublic class TestApp %7B%0A    public static ObjectName AGENT_MBEAN_NAME;%0A    protected static AtomicBoolean enabledAgent = new AtomicBoolean(false);%0A%0A    static %7B%0A        try %7B%0A            AGENT_MBEAN_NAME = new ObjectName(%22io.fabric8.apmagent%22, %22type%22, %22apmAgent%22);%0A        %7D catch (MalformedObjectNameException e) %7B%0A            System.out.println(%22Failed to create object name: %22 + e);%0A            e.printStackTrace();%0A            throw new RuntimeException(e);%0A        %7D%0A    %7D%0A
 %0A%0A  
@@ -1587,16 +1587,109 @@
 G ...%22);
+%0A%0A            // lets see if the agent has started up yet:%0A            checkEnabledMetrics();
 %0A       
@@ -2056,11 +2056,828 @@
  %7D%0A    %7D
+%0A%0A    protected static void checkEnabledMetrics() %7B%0A        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();%0A        if (mBeanServer != null) %7B%0A            if (mBeanServer.isRegistered(AGENT_MBEAN_NAME)) %7B%0A                if (enabledAgent.compareAndSet(false, true)) %7B%0A                    System.out.println(%22==== HAS AGENT!%22);%0A                    try %7B%0A                        mBeanServer.invoke(AGENT_MBEAN_NAME, %22startMetrics%22, new Object%5B0%5D, new String%5B0%5D);%0A                        System.out.println(%22Enabled agent %22 + AGENT_MBEAN_NAME);%0A                    %7D catch (Exception e) %7B%0A                        System.out.println(%22Failed to invoke the mbean: %22 + AGENT_MBEAN_NAME);%0A                        e.printStackTrace();%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

enable the metrics by default in the test app