FileDocCategorySizeDatePackage
CocoaJavaBridge.javaAPI DocAzureus 3.0.3.412202Thu Feb 09 19:42:44 GMT 2006org.gudy.azureus2.platform.macosx.access.cocoa

CocoaJavaBridge

public final class CocoaJavaBridge extends org.gudy.azureus2.platform.macosx.NativeInvocationBridge

Performs PlatformManager tasks using Cocoa-Java (FoundationKit only)

For now, operations are performed using NSAppleScript, rather than using NSWorkspace. This is still significantly faster than calling the cmd-line osascript.

version
2.1 Apr 2, 2005

Fields Summary
protected static final String
CLASS_PATH
The path the Cocoa-Java class files are located at
private static final String
PF_SCRIPT_SRC
private final com.apple.cocoa.foundation.NSAppleScript
PF_SCRIPT
private final boolean
USE_PF
private static final String
REVEAL_SCRIPT_FORMAT
private static final String
DEL_SCRIPT_FORMAT
private int
mainPool
Main NSAutoreleasePool
protected org.gudy.azureus2.core3.util.AEMonitor
classMon
private org.gudy.azureus2.core3.util.AEMonitor
scriptMon
protected boolean
isDisposed
protected RunnableDispatcher
scriptDispatcher
Constructors Summary
public CocoaJavaBridge()


     
    
        try
        {
            classMon.enter();
            mainPool = NSAutoreleasePool.push();

            PF_SCRIPT = new NSAppleScript(PF_SCRIPT_SRC);
            USE_PF = PF_SCRIPT.compile(new NSMutableDictionary());

            scriptDispatcher = new RunnableDispatcher();
        }
        finally
        {
            classMon.exit();
        }
    
Methods Summary
protected voiddispose()
{@inheritDoc}

        try
        {
            classMon.enter();
            if(!isDisposed)
            {
                Debug.outNoStack("Disposing Native PlatformManager...");
                NSAutoreleasePool.pop(mainPool);
                isDisposed = true;
                Debug.outNoStack("Done");
            }
        }
        finally
        {
            classMon.exit();
        }
    
protected final com.apple.cocoa.foundation.NSAppleEventDescriptorexecuteScript(java.lang.String scriptFormat, java.lang.Object[] params)

Executes a new instance of NSAppleScript

The method is wrapped in an autorelease pool and an AEMonitor. If there are no format parameters, MessageFormat is not used to parse the format string, and the format string will be treated as the source itself.

see
MessageFormat#format(String, Object...)
see
NSAppleScript#execute(com.apple.cocoa.foundation.NSMutableDictionary)

        try
        {
            scriptMon.enter();

            int pool = NSAutoreleasePool.push();
            long start = System.currentTimeMillis();

            String src;
            if(params == null || params.length == 0)
            {
                src = scriptFormat;
            }
            else
            {
                src = MessageFormat.format(scriptFormat, params);
            }

            Debug.outNoStack("Executing: \n" + src);

            NSAppleScript scp = new NSAppleScript(src);
            NSAppleEventDescriptor result =  scp.execute(new NSMutableDictionary());

            Debug.outNoStack(MessageFormat.format("Elapsed time: {0}ms\n", new Object[]{new Long(System.currentTimeMillis() - start)}));
            NSAutoreleasePool.pop(pool);
            return result;
        }
        finally
        {
            scriptMon.exit();
        }
    
protected final com.apple.cocoa.foundation.NSAppleEventDescriptorexecuteScriptWithAsync(java.lang.String scriptFormat, java.lang.Object[] params)

Asynchronously executes a new instance of NSAppleScript

This method always returns a "true" event descriptor. Callbacks are currently unsupported , so in the event of an error, the logger is autuomatically notified.

The thread's runSupport method is wrapped in an autorelease pool. If there are no format parameters, MessageFormat is not used to parse the format string, and the format string will be treated as the source itself.

see
org.gudy.azureus2.core3.util.AEThread#runSupport()
see
MessageFormat#format(String, Object...)
see
NSAppleScript#execute(com.apple.cocoa.foundation.NSMutableDictionary)
return
NSAppleEventDescriptor.descriptorWithBoolean(true)

        final AERunnable worker = new AERunnable()
        {
            public void runSupport()
            {
                int pool = NSAutoreleasePool.push();
                long start = System.currentTimeMillis();

                String src;
                if(params == null || params.length == 0)
                {
                    src = scriptFormat;
                }
                else
                {
                    src = MessageFormat.format(scriptFormat, params);
                }

                Debug.outNoStack("Executing: \n" + src);

                NSMutableDictionary errorInfo = new NSMutableDictionary();
                if(new NSAppleScript(src).execute(errorInfo) == null)
                {
                    Debug.out(String.valueOf(errorInfo.objectForKey(NSAppleScript.AppleScriptErrorMessage)));
                    //logWarning(String.valueOf(errorInfo.objectForKey(NSAppleScript.AppleScriptErrorBriefMessage)));
                }

                Debug.outNoStack(MessageFormat.format("Elapsed time: {0}ms\n", new Object[]{new Long(System.currentTimeMillis() - start)}));
                NSAutoreleasePool.pop(pool);
            }
        };

        AEThread t = new AEThread("ScriptObject", true)
        {
            public void runSupport()
            {
                scriptDispatcher.exec(worker);
            }
        };
        t.setPriority(Thread.NORM_PRIORITY - 1);
        t.start();

        return NSAppleEventDescriptor.descriptorWithBoolean(true);
    
protected final com.apple.cocoa.foundation.NSAppleEventDescriptorexecuteScriptWithNewThread(java.lang.String scriptFormat, java.lang.Object[] params)

Executes a new instance of NSAppleScript in a forked AEThread

This method always returns a "true" event descriptor. Callbacks are currently unsupported , so in the event of an error, the logger is autuomatically notified.

The thread's runSupport method is wrapped in an autorelease pool. If there are no format parameters, MessageFormat is not used to parse the format string, and the format string will be treated as the source itself.

see
org.gudy.azureus2.core3.util.AEThread#runSupport()
see
MessageFormat#format(String, Object...)
see
NSAppleScript#execute(com.apple.cocoa.foundation.NSMutableDictionary)
return
NSAppleEventDescriptor.descriptorWithBoolean(true)

        Thread worker = new AEThread("ScriptObject", true)
        {
            public void runSupport()
            {
                int pool = NSAutoreleasePool.push();
                long start = System.currentTimeMillis();

                String src;
                if(params == null || params.length == 0)
                {
                    src = scriptFormat;
                }
                else
                {
                    src = MessageFormat.format(scriptFormat, params);
                }

                Debug.outNoStack("Executing: \n" + src);

                NSMutableDictionary errorInfo = new NSMutableDictionary();
                if(new NSAppleScript(src).execute(errorInfo) == null)
                {
                    Debug.out(String.valueOf(errorInfo.objectForKey(NSAppleScript.AppleScriptErrorMessage)));
                    //logWarning(String.valueOf(errorInfo.objectForKey(NSAppleScript.AppleScriptErrorBriefMessage)));
                }

                Debug.outNoStack(MessageFormat.format("Elapsed time: {0}ms\n", new Object[]{new Long(System.currentTimeMillis() - start)}));
                NSAutoreleasePool.pop(pool);
            }
        };

        worker.setPriority(Thread.NORM_PRIORITY - 1);
        worker.start();

        return NSAppleEventDescriptor.descriptorWithBoolean(true);
    
protected voidfinalize()
{@inheritDoc}

        dispose();
        super.finalize();
    
protected booleanisEnabled()
{@inheritDoc}

        // simple check with classpath
        return System.getProperty("java.class.path").toLowerCase().indexOf(CLASS_PATH) != -1;
    
private voidlogWarning(java.lang.String message)
Logs a warning message to Logger. The class monitor is used.

param
message A warning message

        try
        {
            classMon.enter();
            Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING, message));
        }
        finally
        {
            classMon.exit();
        }
    
protected booleanperformRecoverableFileDelete(java.io.File path)
{@inheritDoc}

        if(!path.exists())
            return false;

        NSAppleEventDescriptor result =  executeScriptWithAsync(DEL_SCRIPT_FORMAT, new Object[]{path.getAbsolutePath()});
        return (result != null);
    
protected booleanshowInFinder(java.io.File path)
{@inheritDoc}

        if(!path.exists())
            return false;

        int pool = NSAutoreleasePool.push();

        String fb = "Finder";
        if(USE_PF)
        {
            NSAppleEventDescriptor result = PF_SCRIPT.execute(new NSMutableDictionary());
            if(result != null && result.booleanValue())
                fb = "Path Finder";
        }

        NSAppleEventDescriptor result =  executeScriptWithAsync(REVEAL_SCRIPT_FORMAT, new Object[]{fb, path.getAbsolutePath()});

        NSAutoreleasePool.pop(pool);
        return (result != null);