# Loading an Application Module Instance in Code

The following example illustrates code snippets for setting up an environment, loading an application module, and creating a platform-independent application module. The example assumes that connection parameters, including user name and password would probably be provided by a user.

An Application Module's lifetime depends on the deployment platform. When deployed to the Visibroker platform or Oracle8i CORBA server, the lifetime is until the client calls `remove` on the Application Module. When running locally, an Application Module lasts as long as the local variable that represents it.

This topic provides code examples for:

- Setting up the environment

- Setting platform-dependent environment variables for deploying the Application Module. You use different environment variables depending on whether you are:

  - deploying locally

  - deploying on Oracle8i as a CORBA server object

  - deploying on Oracle8i as an EJB

  - deploying on the Visibroker platform as a CORBA server object

- Loading an Application Module

## Setting up the Environment

Your set up code, in addition to importing the appropriate files for your application, must include a hash table to hold the values of environment variables.

      static Hashtable env = new Hashtable(10);

Your code must also include a definition of the Application Module home:

      static ApplicationModuleHome mHome ;

You must include code to connect to the database. For information on how to connect to a database, see the JDBC Developer's Guide and Reference. To access this book you must be registered with the Oracle Technical Network (OTN). To use this free resource, go to OTN's registration page.

### Setting Environment Variables for Deploying Locally

The following code snippet illustrates how to set the environment variables to run an Application Module deployed locally.

    // Component running locally
         {
         public static void setLocalEnv()  {
         env.put(JboContext.INITIAL_CONTEXT_FACTORY, JboContext.JBO_CONTEXT_FACTORY);
         env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_LOCAL);
         }

        }

### Setting Environment Variables for Deploying on Oracle8i as a CORBA Server Object

The following code snippet illustrates how to set the environment for loading an Application Module deployed on Oracle8i as a CORBA object.

         // Component deployed to Oracle8i CORBA Server.
         // Set up the 8i environment

          public static void set8iEnv()  {

          env.put(JboContext.INITIAL_CONTEXT_FACTORY,
                  JboContext.JBO_CONTEXT_FACTORY);
          env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_ORACLE8I);
          env.put(JboContext.SECURITY_PRINCIPAL, "scott");
          env.put(JboContext.SECURITY_CREDENTIALS, "tiger");
          env.put(JboContext.HOST_NAME, "localhost");
          env.put(JboContext.CONNECTION_PORT, "2481");
          env.put(JboContext.ORACLE_SID, "ORA815");
          env.put(JboContext.APPLICATION_PATH, "test");

        }

### Setting Environment Variables for Deploying on Oracle8i as an EJB

The following code snippet illustrates how to set the environment for loading an Application Module deployed on Oracle8i as an EJB.

         // Component deployed to Oracle8i as EJB.
         // Set up the 8i environment

          public static void set8iEnv()  {

          env.put(JboContext.INITIAL_CONTEXT_FACTORY,
                  JboContext.JBO_CONTEXT_FACTORY);
          env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_EJB);
          env.put(JboContext.SECURITY_PRINCIPAL, "scott");
          env.put(JboContext.SECURITY_CREDENTIALS, "tiger");
          env.put(JboContext.HOST_NAME, "localhost");
          env.put(JboContext.CONNECTION_PORT, "2481");
          env.put(JboContext.ORACLE_SID, "ORA815");
          env.put(JboContext.APPLICATION_PATH, "test");

        }

### Setting Environment Variables for Deploying on the Visibroker Platform as a CORBA Server Object

The following code snippet illustrates how to set the environment for loading an Application Module on the Visibroker platform. You provide different values for the connection parameter, depending on whether you are connecting in naming service (remote), use binding, or colocate mode.

#### Setting the Environment for the Naming Service Mode

      // Component deployed to Visibroker connecting in Naming Service mode

                public static void setRemoteVbEnv()  {

                env.put(JboContext.INITIAL_CONTEXT_FACTORY,
                         JboContext.JBO_CONTEXT_FACTORY);
                env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_VB);
                env.put(JboContext.CONNECTION_MODE,
                         new Integer(ConnectionModeConstants.REMOTE));
                env.put(JboContext.APPLICATION_PATH, "RemoteAlias");

      }

where the `RemoteAlias` is the named service which is aliased by using the `startNamingService.bat` file.

#### Setting the Environment for the Use Binding Mode

    // Component deployed to Visibroker connecting in Use Binding mode
                 public static void setBindVbEnv()  {

                env.put(JboContext.INITIAL_CONTEXT_FACTORY,
                        JboContext.JBO_CONTEXT_FACTORY);
                env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_VB);
                env.put(JboContext.CONNECTION_MODE,
                        new Integer(ConnectionModeConstants.USE_BIND));
               env.put(JboContext.HOST_NAME, hostName);

      }

#### Setting the Environment for the Colocate Mode

    // Component deployed to Visibroker connecting  Colocate mode
              public static void setColocatedVbEnv()  {

              env.put(JboContext.INITIAL_CONTEXT_FACTORY,
                         JboContext.JBO_CONTEXT_FACTORY);
              env.put(JboContext.DEPLOY_PLATFORM, JboContext.PLATFORM_VB);
              env.put(JboContext.CONNECTION_MODE,
                       new Integer(ConnectionModeConstants.COLOCATE));

      }

## Loading the Application Module

The following code snippet illustrates creating an initial context for the environment and creating the Application Module `d2e.D2eModule` in the database. This Application Module was created at design time.

     // Load the Application Module
       ApplicationModule appMod = null;

       try
          {
                Context ic = new InitialContext(env);
                String theAMDefName = "d2e.D2eModule";
                ApplicationModuleHome home = (ApplicationModuleHome)ic.lookup(theAMDefName);
                ApplicationModule appMod = home.create();
                appMod.getTransaction().connect("jdbc:oracle:thin:scott/tiger@pc3:1521:ORCL");
                return appMod;
          }
          catch(Exception e) {
        e.printStackTrace();
       }