/*
 * Copyright 2016 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 org.agrona.concurrent;

import javax.sound.midi.MidiDevice;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.spi.MidiDeviceProvider;

/**
 * Control the supporting of high resolution timers for scheduled events by enabling the {@link MidiDevice}.
 */
public class HighResolutionTimer
{
    private static final MidiDevice MIDI_DEVICE;

    static
    {
        final MidiDeviceProvider provider = new com.sun.media.sound.MidiOutDeviceProvider();
        final MidiDevice.Info[] info = provider.getDeviceInfo();

        MIDI_DEVICE = info.length > 0 ? provider.getDevice(info[0]) : null;
    }

    private static volatile boolean isEnabled = false;

    /**
     * Has the high resolution timer been enabled?
     *
     * @return true if the we believe it is enabled otherwise false.
     */
    public static boolean isEnabled()
    {
        return isEnabled;
    }

    /**
     * Attempt to enable the {@link MidiDevice} which requires the high resolution timer.
     */
    public static synchronized void enable()
    {
        if (null != MIDI_DEVICE && !MIDI_DEVICE.isOpen())
        {
            try
            {
                MIDI_DEVICE.open();
            }
            catch (final MidiUnavailableException ignore)
            {
                isEnabled = false;
                return;
            }

            isEnabled = true;
        }
    }

    /**
     * Attempt to disable the high resolution timer by closing the {@link MidiDevice}.
     */
    public static synchronized void disable()
    {
        if (MIDI_DEVICE != null && MIDI_DEVICE.isOpen())
        {
            MIDI_DEVICE.close();
        }

        isEnabled = false;
    }
}
@@ -753,16 +753,49 @@
 rovider;
+%0Aimport java.lang.reflect.Method;
 %0A%0A/**%0A *
@@ -1007,38 +1007,32 @@
 c%0A    %7B%0A        
-final 
 MidiDeviceProvid
@@ -1029,217 +1029,636 @@
 vice
-Provider provider = new com.sun.media.sound.MidiOutDeviceProvider();%0A        final MidiDevice.Info%5B%5D info = provider.getDeviceInfo();%0A%0A        MIDI_DEVICE = info.length %3E 0 ? provider.getDevice(info%5B0%5D) : null
+ midiDevice = null;%0A        try%0A        %7B%0A            final Class%3C?%3E mediaProviderClass = Class.forName(%22com.sun.media.sound.MidiOutDeviceProvider%22);%0A            final Method getDeviceInfoMethod = mediaProviderClass.getMethod(%22getDeviceInfo%22);%0A            final MidiDeviceProvider provider = (MidiDeviceProvider)mediaProviderClass.newInstance();%0A            final MidiDevice.Info%5B%5D info = (MidiDevice.Info%5B%5D)getDeviceInfoMethod.invoke(provider);%0A%0A            midiDevice = info.length %3E 0 ? provider.getDevice(info%5B0%5D) : null;%0A        %7D%0A        catch (final Exception ignore)%0A        %7B%0A        %7D%0A%0A        MIDI_DEVICE = midiDevice
 ;%0A  

Remove explicit dependency on com.sun.media package by loading the provider via reflection so it is not an explicit dependency unless the HighResolutionTimer is used. Issue #87.