[Enterprise Extensions only]

Examples: ASP helper functions

The following Active Server Pages (ASP) code extracts can be used to help create various types of ActiveX to EJB bridge objects and Enterprise JaveBeans. This code is taken from the ASP samples included with the ActiveX to EJB Client provided with WebSphere Application Server. For information about the samples, see the related samples information.

For equivalent Visual Basic example code, see Examples: Visual Basic helper functions.

Initializing the ActiveX to EJB bridge and JVM

The following code extract initializes the ActiveX to EJB bridge and the JVM:

'--------------------------------------------------------------
' Initialize the ActiveX to EJB bridge and the JVM
'--------------------------------------------------------------
Private Function XJBInit(strWAS_HOME, strJAVA_HOME, strNAMING_FACTORY)
    
    Dim oXJB
    
    ' Create an XJB.JClassFactory object.  This object
    ' is responsible for finding classes and instantiating
    ' objects.
    Set oXJB = Server.CreateObject("XJB.JClassFactory")
    
    ' If you intend to debug using a JPDA debugger, you will need tools.jar
    ' strClassPath = strClassPath & ";" & strJAVA_HOME & "\lib\tools.jar"
    
    ' The ActiveX to EJB bridge properties are in the form of an array of strings.
    ' Each property that you would normally pass on a command-line
    ' is passed as an individual string in the array.
    ' Here, we are adding the classpath (put together above), extension directories
    ' which are required for J2EE access and the java.naming.factory.initial property
    ' which is always the same.
    Dim astrJavaProps(2)
    astrJavaProps(0) = "-Djava.ext.dirs=" & strWAS_HOME & "\classes;" & _
                        strWAS_HOME & "\lib\ext;" & strWAS_HOME & "\lib;" & _
                        strJAVA_HOME & "\jre\lib\ext;" & strWAS_HOME & "\properties;"
    astrJavaProps(1) = "-Dws.ext.dirs=" & strWAS_HOME & "\classes;" & _
                        strWAS_HOME & "\lib\ext;" & strWAS_HOME & "\lib;" & _
                        strJAVA_HOME & "\jre\lib\ext;" & strWAS_HOME & "\properties;"
    astrJavaProps(2) = "-Djava.naming.factory.initial=" & strNAMING_FACTORY
    
    
    ' If you want to add more to the class path, you can do it here
    ' astrJavaProps(3) = "-Djava.class.path=" & strClassPath
    
    ' If you wish to debug using a JPDA debugger (you can't use JDB because there is
    ' no console to get the agent password), use parameters similar to the following
    '    astrJavaProps(3) = "-Djava.compiler=NONE"
    '    astrJavaProps(4) = "-Xnoagent"
    '    astrJavaProps(5) = "-Xdebug"
    '    astrJavaProps(6) = "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000"
    
    ' Initialize the JVM using our properties that we just setup.
    ' The JVM runs in Visual Basic's process space (the IDE if running
    ' in the Visual Basic interactive debugger)
    ' The JVM remains in this process space until the process ends.
    ' Running XJBInit a second time has no effect.  It continues
    ' to use the classpath and properties of the JVM that were initially used.
    oXJB.XJBInit astrJavaProps
    
    Set XJBInit = oXJB

End Function

Creating a J2EE client container

The following code extract creates a J2EE client container:

'--------------------------------------------------------------
' XJBGetJ2EEClientContainer - Get the J2EE Client Container Object
' Input:
'   oXJB             - Initialized ActiveX to EJB bridge object
'   strEARFile       - Location where EJB stub jars and J2EE App Client Jar is stored
'   strBootstrapHost - Name of the server where the EJB app is running
'   strBootstrapPort - Port where the EJB server is running
'   strAppClientJar  - Name of the jar file in the EAR file that contains the Java
'                      Application Client.  We need this only when we have multiple
'                      client jars in a single ear.  Even though we don't run the
'                      Java client, the J2EE Client Container will use the manifest
'                      inside to reference the EJB JAR file(s) that we will need
'                      to reference.
'   strEARTempDir    - Directory to extract the EAR file to.
'--------------------------------------------------------------
Function XJBGetJ2EEClientContainer(oXJB, strEARFile, strBootstrapHost, _
                                   strBootstrapPort, strAppClientJar, _
                                   strEARTempDir)
    Dim oProps
    Dim oCC
    Dim astrAppParams(0)
    
    ' Initialize the J2EE Client Container

    ' Before initializing the Client Container, set the 
    ' System Property that tells it what directory to 
    ' extract the EAR file to.  If we don't do this, then
    ' a new directory is created each time we launch the
    ' Client Container and is not cleaned-up.
    if Trim(strEARTempDir) <> "" Then
        XJBPutProperty oXJB, oXJB.FindClass("java.lang.System").getProperties, _
                       "com.ibm.websphere.client.applicationclient.archivedir", _
                       strEARTempDir
    End If
    
    ' - Here we are creating a launchClient object which is supplied
    '   in appclient.jar supplied with WebSphere (Client or Server install).
    ' - Notice that we are eliminating the need to store the Class Proxy object
    '   returned by FindClass.  We are using as a temporary variable and immediately
    '   creating a new instance (Object Proxy) and throwing the Class Proxy away.
    Set oCC = oXJB.NewInstance(oXJB.FindClass("com.ibm.websphere.client.applicationclient.launchClient"))
    
    ' Create a java.util.Properties object to use in our launchClient() J2EE API
    Set oProps = oXJB.NewInstance(oXJB.FindClass("java.util.Properties"))
    
    ' Add properties to the Properties object.
    ' - initonly=true is required to avoid running the Java Client that is stored
    '   in the EAR file.
    ' - BootstrapHost = "xxxx" : The name of the WebSphere server where your
    '                            Enterprise JsvaBeans are stored.
    ' - BootstrapPort = nnnn   : The port of the listener on the WebSphere server.
    ' - jar = "xxxx"           : The name of the jar within the ear file that
    '                            contains the Java Client (if you have more than one
    '                            Java Client Jar in the same EAR file.
    '                           (the path is relative to the root of the EAR file)
    XJBPutProperty oXJB, oProps, "initonly", "true"
    If Trim(strBootstrapHost) <> "" Then
        XJBPutProperty oXJB, oProps, "BootstrapHost", strBootstrapHost
    End If
    If IsNumeric(strBootstrapPort) Then
        XJBPutProperty oXJB, oProps, "BootstrapPort", strBootstrapPort
    End If
    If Trim(strAppClientJar) <> "" Then
        XJBPutProperty oXJB, oProps, "jar", strAppClientJar
    End If
    
    ' If we want to turn tracing on, we can do something like this:
    'XJBPutProperty oXJB, oProps, "trace", "com.ibm.ws.client.applicationclient.ApplicationClientMetaData=debug=enabled"
    'XJBPutProperty oXJB, oProps, "tracefile", "c:\xjb.log"
    
    ' Add an empty string to the Application Parameters
	    astrAppParams(0) = ""
    
    ' Launch the client container.
    ' In the java world, it would start the Java Application within the
    ' container.  Here, however, we ARE the application, so we avoid
    ' starting it by setting the property initonly=true (above).
    ' The Client Container overrides the Java Namespace so that we can load
    ' the appropriate EJB class files from within our supplied ear file.
    oCC.launch strEARFile, oProps, astrAppParams

    Set XJBGetJ2EEClientContainer = oCC
    
    Set oCC = Nothing
    Set oProps = Nothing
End Function

Getting an Enterprise JavaBean home object

The following code extract gets an Enterprise JavaBean's home object:

'--------------------------------------------------------------
' XJBGetEJBHome - Get the Home EJB Object
' Input:
'   oXJB             - Initialized ActiveX to EJB bridge Object
'   oInitialContext  - Java InitialContext object
'   strJNDIName      - JNDI Name of the EJB you are going to
'                      get the Home of.
'   strHomeClassName - Class Name of the Home object
'--------------------------------------------------------------
Private Function XJBGetEJBHome(oXJB, oInitialContext, strJNDIName, strHomeClassName)
    
    ' Create an XJB.JMethodArgs object.
    ' The prototype for javax.rmi.PortableRemoteObject.narrow() is:
    '   javax.rmi.PortableRemoteObject.narrow(Object, Class)
    ' In this case we want to use our Java Object and the Java Class that this
    ' Java Object needs to be "narrowed" to.
    ' Because the ActiveX to EJB bridge is unable to coerce data types from String to
    ' Object (or any other type of implicit coersion), we must manually tell the
    ' ActiveX to EJB bridge how to do this (map a String to an Object).
    ' The JMethodsArgs object is a container for Method Arguments.  Here we
    ' are adding two Objects, which are both Strings (Key and Value).
    Dim oArgs 
    Set oArgs = oXJB.GetArgsContainer
    
    ' Lookup an object's home
    ' - This is typical J2EE Java code.  We use the JNDI Name that was
    '   passed-in to lookup our EJB
    Dim oJavaObj
    Set oJavaObj = oInitialContext.lookup(strJNDIName)
    
    ' Narrow it
    ' - This is more EJB/J2EE code.  Here we use or JMethodArgs object
    '   to coerce our EJB object to a specific class.   In this case
    '   our EJB Home class.
    ' - Notice that we are calling the static narrow() method on a Class Proxy
    '   object that was created from FindClass().
    Dim clsPortRemObj 
    Dim oHome 
    oArgs.AddObject "java.lang.Object", oJavaObj
    oArgs.AddObject "java.lang.Class", oXJB.FindClass(strHomeClassName)
    Set clsPortRemObj = oXJB.FindClass("javax.rmi.PortableRemoteObject")
    Set oHome = clsPortRemObj.narrow(oArgs)
    
    ' Return the actual bean
    Set XJBGetEJBHome = oHome

    Set oArgs = Nothing
    Set oJavaObj = Nothing
    Set clsPortRemObj = Nothing
    Set oHome = Nothing

End Function

Adding properties to java.util.Properties

The following code extract adds a property to the java.util.Properties object that is used in the launchClient() J2EE API:

'--------------------------------------------------------------
' XJBPutProperty - Add a string key/value to a Java
'                  Properties object
' Input:
'   oXJB             - Initialized ActiveX to EJB bridge object
'   oProperties      - A Instantiated java.util.Properties object
'   strKey           - String representing the Key of the Key=Value
'   strValue         - String representing the Value of the Key=Value
'--------------------------------------------------------------
Private Sub XJBPutProperty(oXJB, oProperties, strKey, strValue)
   
    ' Create an XJB.JMethodArgs object.
    ' The prototype for java.util.Properties.put() is:
    '   java.util.Properties.put(Object, Object)
    ' In this case we want to setup two Strings as properties.  A Key=Value pair.
    ' Because the ActiveX to EJB bridge cannot coerce data types from String to
    ' Object (or any other type of implicit coersion), we must manually tell
	' the ActiveX to EJB bridge how to do this (map a String to an Object).
    ' The JMethodsArgs object is a container for Method Arguments.  Here we
    ' are adding two Objects, which are both Strings (Key and Value).
    Dim oArgs
    Set oArgs = oXJB.GetArgsContainer
    
    ' Add the key and value to the JMethodArgs object and coerce them to Object types.
    oArgs.AddObject "java.lang.Object", strKey
    oArgs.AddObject "java.lang.Object", strValue
    
    ' Put the key/value into our Properties object using the put() method
    oProperties.put oArgs
    
    Set oArgs = Nothing
    
End Sub