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 SAS Java Metadata 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.
An object factory is instantiated once per user. The following code instantiates an object factory, and creates a connection to the SAS Metadata Server using the makeOMRConnection method:
   /**
    * The object factory instance.
    */
   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, here are the results:
  • An object factory in which to create SAS Metadata Model metadata objects.
  • Log and output location definitions that can be turned on and off for debugging. SAS Java Metadata Interface logging methods should not be used for client-side logging.
  • An available connection to the SAS Metadata Server.
You can now get information about repositories defined on the SAS Metadata Server, and you can create metadata object instances.