Previous Page | Next Page

Using the SAS Java Metadata Interface

Instantiating an Object Factory and Connecting to the SAS Metadata Server

This section provides an example of the SAS Java Metadata Interface calls necessary to instantiate an object factory and to connect to the SAS Metadata Server.

When using the remote version of the interface, you create an object factory by instantiating the MdFactory interface. This interface contains all of the methods to create Java metadata objects and to invoke Java event-handling and messaging mechanisms.

You create a connection to the SAS Metadata Server using the makeOMRConnection method from the MdOMRConnection interface. The makeOMRConnection method connects to the SAS Metadata Server and provides access to com.sas.metadata.remote methods for reading, writing, and updating metadata objects that represent application elements. The com.sas.metadata.remote interface is a Java implementation of the SAS Open Metadata Interface IOMI server interface.


Example of Connecting to the SAS Metadata Server with the makeOMRConnection Method

An object factory is instantiated once per use. Each user has his own object factory instance. The following code instantiates an object factory and creates a connection to the SAS Metadata Server using the makeOMRConnection method:

   /**
    * The following statements instantiate the object factory.
    */
   private MdFactory _factory = null;

   /**
    * Default constructor
    */
   public MdTesterExamples()
   {
      // Calls the factory's constructor
      initializeFactory();
   }

   private void initializeFactory()
   {
      try
      {
         // Initializes the factory.  The Boolean parameter is used to   
         // determine if the application is running in a remote or local  
         // environment. If the data does not need to be accessible across   
         // remote JVMs, then "false" can be used, as shown here.
         _factory = new MdFactoryImpl(false);
         
         // Defines debug logging, but does not turn it on.
         boolean debug = false;
         if (debug)
         {
            _factory.setDebug(false);
            _factory.setLoggingEnabled(false);
            
            // Sets the output streams for logging.  The logging output can be  
            // directed to any OutputStream, including a file.
            _factory.getUtil().setOutputStream(System.out);
            _factory.getUtil().setLogStream(System.out);
         }
         
         // To be notified of changes that have been persisted to the SAS Metadata 
         // Server within this factory (this includes adding objects, updating   
         // objects, and deleting objects), we can add a listener to the factory 
         // here. See MdFactory.addMdFactoryListener().
         // A listener is not needed for this example.
      }
      catch (Exception e)
      {
         e.printStackTrace();   
      }
   }
   
   /**
    * The following statements define variables for SAS Metadata Server
    * connection properties, instantiate a connection factory, issue
    * the makeOMRConnection method, and check exceptions for error conditions. 
    * 
    */
   public boolean connectToServer()
   {
      String serverName = "MACHINE_NAME";
      String serverPort = "8561";
      String serverUser = "USERNAME";
      String serverPass = "PASSWORD";
      
      
      try
      {
         MdOMRConnection connection = _factory.getConnection();
         
         // This statement makes the connection to the server.
         connection.makeOMRConnection(
                  serverName, 
                  serverPort, 
                  serverUser,
                  serverPass
                  );
     
         // The following statements define error handling and error 
         // reporting messages.
      }
      catch (MdException e)
      {
         Throwable t = e.getCause();
         if (t != null)
         {
            String ErrorType = e.getSASMessageSeverity();
            String ErrorMsg = e.getSASMessage();
            if (ErrorType == null)
            {
               // If there is no SAS server message, write a Java/CORBA message.
            }
            else
            {
               // If there is a message from the server:
               System.out.println(ErrorType + ": " + ErrorMsg);
            }
            if (t instanceof org.omg.CORBA.COMM_FAILURE)
            {
               // If there is an invalid port number or host name:
               System.out.println(e.getLocalizedMessage());
            }
            else if (t instanceof org.omg.CORBA.NO_PERMISSION)
            {
               // If there is an invalid user ID or password:
               System.out.println(e.getLocalizedMessage());
            }
         }
         else
         {
            // If we cannot find a nested exception, get message and print.
            System.out.println(e.getLocalizedMessage());
         }
         // If there is an error, print the entire stack trace.
         e.printStackTrace();
         return false;
      }
      catch (RemoteException e)
      {
         // Unknown exception.
         e.printStackTrace();
         return false;
      }
      // If no errors occur, then a connection is made.
      return true;
   }

From this example, we have the following:

We can now get information about repositories defined on the SAS Metadata Server and create metadata object instances.

Previous Page | Next Page | Top of Page