Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Private Types | Private Member Functions | Private Attributes
MRJSession Class Reference

#include <MRJSession.h>

Collaboration diagram for MRJSession:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 MRJSession ()
virtual ~MRJSession ()
JMSessionRef getSessionRef ()
JNIEnvgetCurrentEnv ()
JNIEnvgetMainEnv ()
JavaVMgetJavaVM ()
Boolean onMainThread ()
Boolean addToClassPath (const FSSpec &fileSpec)
Boolean addToClassPath (const char *dirPath)
Boolean addURLToClassPath (const char *fileURL)
char * getProperty (const char *propertyName)
void setStatus (OSStatus status)
OSStatus getStatus ()
void idle (UInt32 milliseconds=kDefaultJMTime)
void sendMessage (NativeMessage *message, Boolean async=false)
void lock ()
 Used to prevent reentering the VM.
void unlock ()
 MRJSession ()
virtual ~MRJSession ()
OSStatus open (const char *consolePath)
OSStatus close ()
JNIEnvgetCurrentEnv ()
JNIEnvgetMainEnv ()
JavaVMgetJavaVM ()
Boolean onMainThread ()
Boolean addToClassPath (const FSSpec &fileSpec)
Boolean addToClassPath (const char *dirPath)
Boolean addURLToClassPath (const char *fileURL)
char * getProperty (const char *propertyName)
void setStatus (OSStatus status)
OSStatus getStatus ()
void idle (UInt32 milliseconds=0x00000400)
void sendMessage (NativeMessage *message, Boolean async=false)
void lock ()
 Used to prevent reentering the VM.
void unlock ()

Private Types

typedef vector< FSRefMRJClassPath

Private Member Functions

void postMessage (NativeMessage *message)
 Put a message on the queue.
void dispatchMessage ()
void postMessage (NativeMessage *message)
void dispatchMessage ()
string getClassPath ()
string getPluginHome ()

Private Attributes

JMSessionRef mSession
OSStatus mStatus
JNIEnvmMainEnv
NativeMessagemFirst
NativeMessagemLast
MonitormMessageMonitor
UInt32 mLockCount
MRJClassPath mClassPath
JavaVMmJavaVM
jclass mSession

Detailed Description

Definition at line 72 of file MRJSession.h.


Member Typedef Documentation

Definition at line 120 of file MRJSession.h.


Constructor & Destructor Documentation

Definition at line 303 of file MRJSession.cpp.

       :      mSession(NULL), mStatus(noErr), mMainEnv(NULL), mFirst(NULL), mLast(NULL), mMessageMonitor(NULL), mLockCount(0)
{
       // Make sure JManager exists.
       if (&::JMGetVersion != NULL && ::JMGetVersion() >= kJMVersion) {
              static JMSessionCallbacks callbacks = {
                     kJMVersion,                               /* should be set to kJMVersion */
                     &java_stdout,                      /* JM will route "stdout" to this function. */
                     &java_stderr,                      /* JM will route "stderr" to this function. */
                     &java_stdin,                       /* read from console - can be nil for default behavior (no console IO) */
                     &java_exit,                               /* handle System.exit(int) requests */
                     &java_authenticate,         /* present basic authentication dialog */
                     &java_lowmem                       /* Low Memory notification Proc */
              };

              JMTextRef nameRef = NULL;
              JMTextRef valueRef = NULL;
              OSStatus status = noErr;

              mStatus = ::JMOpenSession(&mSession, eJManager2Defaults, eCheckRemoteCode,
                                                                             &callbacks, kTextEncodingMacRoman, NULL);

              // capture the main environment, so it can be distinguished from true Java threads.
              if (mStatus == noErr) {
                     mMainEnv = ::JMGetCurrentEnv(mSession);
              
                     // create a monitor for the message queue to unblock Java threads.
                     mMessageMonitor = new MRJMonitor(this);

#ifdef MRJPLUGIN_4X
            // hook up MRJ networking layer, to permit SSL, etc.
            ::OpenMRJNetworking(this);
#endif
              }
       } else {
              mStatus = kJMVersionError;
       }
}

Here is the call graph for this function:

MRJSession::~MRJSession ( ) [virtual]

Definition at line 342 of file MRJSession.cpp.

{
#ifdef MRJPLUGIN_4X
    // is this perhaps too late?
    ::CloseMRJNetworking(this);
#endif

       if (mMessageMonitor != NULL) {
              mMessageMonitor->notifyAll();
              delete mMessageMonitor;
       }

       if (mSession != NULL) {
              mStatus = ::JMCloseSession(mSession);
              mSession = NULL;
       }
}

Here is the call graph for this function:

virtual MRJSession::~MRJSession ( ) [virtual]

Member Function Documentation

Definition at line 396 of file MRJSession.cpp.

{
       return (::JMAddToClassPath(mSession, &fileSpec) == noErr);
}

Here is the caller graph for this function:

Definition at line 401 of file MRJSession.cpp.

{
       // Need to convert the path into an FSSpec, and add it MRJ's class path.
       Str255 path;
       FSSpec pathSpec;
       OSStatus status = ::FSMakeFSSpec(0, 0, c2p(dirPath, path), &pathSpec);
       if (status == noErr)
              status = ::JMAddToClassPath(mSession, &pathSpec);
       return (status == noErr);
}

Here is the call graph for this function:

Definition at line 412 of file MRJSession.cpp.

{
       OSStatus status = noErr;
       // Need to convert the URL into an FSSpec, and add it MRJ's class path.
       JMTextRef urlRef = NULL;
       status = ::JMNewTextRef(mSession, &urlRef, kTextEncodingMacRoman, fileURL, strlen(fileURL));
       if (status == noErr) {
              FSSpec pathSpec;
              status = ::JMURLToFSS(mSession, urlRef, &pathSpec);
              if (status == noErr)
                     status = ::JMAddToClassPath(mSession, &pathSpec);
              ::JMDisposeTextRef(urlRef);
       }
       
       return (status == noErr);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 285 of file MRJSession.cpp.

{
    theSession = NULL;

    if (mJavaVM) {
       if (mMessageMonitor != NULL) {
              mMessageMonitor->notifyAll();
              delete mMessageMonitor;
              mMessageMonitor = NULL;
       }
       
       if (mSession) {
           jclass session = mSession;
           JNIEnv* env = mMainEnv;
            jmethodID closeMethod = env->GetStaticMethodID(session, "close", "()V");
            if (closeMethod)
                env->CallStaticVoidMethod(session, closeMethod);
            else
                env->ExceptionClear();
           env->DeleteGlobalRef(mSession);
           mSession = NULL;
        }

#if !TARGET_RT_MAC_MACHO // XXX right now, this hangs Chimera...
        mJavaVM->DestroyJavaVM();
#endif
        mJavaVM = NULL;
    }
    return noErr;
}

Here is the call graph for this function:

Definition at line 516 of file MRJSession.cpp.

{
       if (mFirst != NULL) {
              NativeMessage* message = mFirst;
              mFirst = message->getNext();
              if (mFirst == NULL) mLast = NULL;
              
              message->setNext(NULL);
              message->execute();
              mMessageMonitor->notify();
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 505 of file MRJSession.cpp.

{
    // to work around problem in Mac OS X 10.2 (Jaguar) (bugzilla #164712),
    // put our classes in the boot class path.
    string classPath("-Xbootclasspath/p:");
    
    // keep appending paths make from FSSpecs.
    MRJClassPath::const_iterator i = mClassPath.begin();
    if (i != mClassPath.end()) {
        char path[1024];
        if (ref2path(*i, path, sizeof(path)) == noErr)
            classPath += path;
        ++i;
        while (i != mClassPath.end()) {
            if (ref2path(*i, path, sizeof(path)) == noErr) {
                classPath += ":";
                classPath += path;
            }    
            ++i;
        }
    }

    return classPath;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 365 of file MRJSession.cpp.

{
       return ::JMGetCurrentEnv(mSession);
}

Here is the caller graph for this function:

Definition at line 375 of file MRJSession.cpp.

{
       JNIEnv* env = ::JMGetCurrentEnv(mSession);
       JavaVM* vm = NULL;
       env->GetJavaVM(&vm);
       return vm;
}

Here is the caller graph for this function:

Definition at line 370 of file MRJSession.cpp.

{
       return mMainEnv;
}

Here is the caller graph for this function:

Definition at line 530 of file MRJSession.cpp.

{
    string pluginHome("-Dnetscape.oji.plugin.home=");

    char path[1024];
    if (spec2path(thePluginSpec, path, sizeof(path)) == noErr) {
        char* lastSlash = strrchr(path, '/');
        if (lastSlash) {
            *lastSlash = '\0';
            pluginHome += path;
        }
    }

    return pluginHome;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char * MRJSession::getProperty ( const char *  propertyName)

Definition at line 429 of file MRJSession.cpp.

{
       char* result = NULL;
       OSStatus status = noErr;
       JMTextRef nameRef = NULL, valueRef = NULL;
       status = ::JMNewTextRef(mSession, &nameRef, kTextEncodingMacRoman, propertyName, strlen(propertyName));
       if (status == noErr) {
              status = ::JMGetSessionProperty(mSession, nameRef, &valueRef);
              ::JMDisposeTextRef(nameRef);
              if (status == noErr && valueRef != NULL) {
                     UInt32 valueLength = 0;
                     status = ::JMGetTextLengthInBytes(valueRef, kTextEncodingMacRoman, &valueLength);
                     if (status == noErr) {
                            result = new char[valueLength + 1];
                            if (result != NULL) {
                                   UInt32 actualLength;
                                   status = ::JMGetTextBytes(valueRef, kTextEncodingMacRoman, result, valueLength, &actualLength);
                                   result[valueLength] = '\0';
                            }
                            ::JMDisposeTextRef(valueRef);
                     }
              }
       }
       return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* MRJSession::getProperty ( const char *  propertyName)
JMSessionRef MRJSession::getSessionRef ( )

Definition at line 360 of file MRJSession.cpp.

{
       return mSession;
}

Here is the caller graph for this function:

Definition at line 460 of file MRJSession.cpp.

{
       return mStatus;
}

Here is the caller graph for this function:

void MRJSession::idle ( UInt32  milliseconds = kDefaultJMTime)

Definition at line 465 of file MRJSession.cpp.

{
       // Each call to idle processes a single message.
       dispatchMessage();

       // Guard against entering the VM multiple times.
       if (mLockCount == 0) {
              lock();
              mStatus = ::JMIdle(mSession, milliseconds);
              unlock();
       }
#if 0
        else {
              // sleep the current thread.
              JNIEnv* env = getCurrentEnv();
              jclass threadClass = env->FindClass("java/lang/Thread");
              if (threadClass != NULL) {
                     jmethodID sleepMethod = env->GetStaticMethodID(threadClass, "sleep", "(J)V");
                     env->CallStaticVoidMethod(threadClass, sleepMethod, jlong(milliseconds));
                     env->DeleteLocalRef(threadClass);
              }
       }
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void MRJSession::idle ( UInt32  milliseconds = 0x00000400)

Used to prevent reentering the VM.

Definition at line 529 of file MRJSession.cpp.

{
       ++mLockCount;
}

Here is the caller graph for this function:

Used to prevent reentering the VM.

Definition at line 383 of file MRJSession.cpp.

{
       JNIEnv* env = ::JMGetCurrentEnv(mSession);
       return (env == mMainEnv);
}

Here is the caller graph for this function:

OSStatus MRJSession::open ( const char *  consolePath)

Definition at line 222 of file MRJSession.cpp.

{
    // Use vanilla JNI invocation API to fire up a fresh JVM.
    string classPath = getClassPath();
    string pluginHome = getPluginHome();
    JavaVMOption theOptions[] = {
       { (char*) classPath.c_str() },
       { (char*) pluginHome.c_str() },
#if REDIRECT_VFPRINTF
       { "vfprintf", NewMachOFunctionPointer(&java_vfprintf) }
#endif
    };

    JavaVMInitArgs theInitArgs = {
       JNI_VERSION_1_2,
       sizeof(theOptions) / sizeof(JavaVMOption),
       theOptions,
       JNI_TRUE
    };

    mStatus = ::JNI_CreateJavaVM(&mJavaVM, (void**) &mMainEnv, &theInitArgs);
    
    if (mStatus == noErr) {
              // create a monitor for the message queue to unblock Java threads.
              mMessageMonitor = new MRJMonitor(this);
    }

    JNIEnv* env = mMainEnv;
    jclass session = env->FindClass("netscape/oji/MRJSession");
    if (session) {
        mSession = (jclass) env->NewGlobalRef(session);
        jmethodID openMethod = env->GetStaticMethodID(session, "open", "(Ljava/lang/String;)V");
        if (openMethod) {
            jstring path = env->NewStringUTF(consolePath);
            if (path) {
                env->CallStaticVoidMethod(session, openMethod, path);
                if (env->ExceptionCheck())
                    env->ExceptionClear();
                env->DeleteLocalRef(path);
            }
        } else {
            env->ExceptionClear();
        }
        env->DeleteLocalRef(session);
    } else {
        env->ExceptionClear();
    }

    if (mStatus == noErr)
        theSession = this;

#if REDIRECT_VFPRINTF
    // XXX test the vfprintf function.
    jclass notThere = env->FindClass("class/not/Found");
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        env->ExceptionClear();
    }
#endif

    return mStatus;
}

Here is the call graph for this function:

void MRJSession::postMessage ( NativeMessage message) [private]

Put a message on the queue.

Definition at line 505 of file MRJSession.cpp.

{
       if (mFirst == NULL) {
              mFirst = mLast = message;
       } else {
              mLast->setNext(message);
              mLast = message;
       }
       message->setNext(NULL);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void MRJSession::postMessage ( NativeMessage message) [private]
void MRJSession::sendMessage ( NativeMessage message,
Boolean  async = false 
)

Definition at line 490 of file MRJSession.cpp.

{
       // can't block the main env, otherwise messages will never be processed!
       if (onMainThread()) {
              message->execute();
       } else {
              postMessage(message);
              if (!async)
                     mMessageMonitor->wait();
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void MRJSession::sendMessage ( NativeMessage message,
Boolean  async = false 
)

Definition at line 455 of file MRJSession.cpp.

{
       mStatus = status;
}

Definition at line 534 of file MRJSession.cpp.

{
       --mLockCount;
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 121 of file MRJSession.h.

Definition at line 115 of file MRJSession.h.

Definition at line 124 of file MRJSession.h.

Definition at line 116 of file MRJSession.h.

Definition at line 119 of file MRJSession.h.

Definition at line 112 of file MRJSession.h.

Definition at line 117 of file MRJSession.h.

JMSessionRef MRJSession::mSession [private]

Definition at line 109 of file MRJSession.h.

Definition at line 125 of file MRJSession.h.

Definition at line 110 of file MRJSession.h.


The documentation for this class was generated from the following files: