# How to Use the SAS Java Metadata Interface

- Package Description
- Example 1: Server Connection
- Example 2: Get Repositories
- Example 3: Get Metadata Types
- Example 4: Create Objects
- Example 5: Read Objects
- Example 6: Delete Objects
- Example 7: Get all objects of a particular type
- Example 8: Get an existing object
- Sample Program

## Example 1: Server Connection

This is an example of how to connect to a SAS Metadata Server.

       /**
        * This is the object factory used to create objects.
        */
       private MdFactory _factory = null;

       /**
        * Default constructor
        */
       public MdTesterExamples()
       {
          //intialize the factory
          initializeFactory();
       }

       private void initializeFactory()
       {
          try
          {
             // Construct a new 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.
             _factory = new MdFactoryImpl(false);

             // Intialize debug logging if necessary
             boolean debug = false;
             if (debug)
             {
                _factory.setDebug(true);
                _factory.setLoggingEnabled(true);

                // Set 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);
             }
             else
             {
                _factory.setDebug(false);
                _factory.setLoggingEnabled(false);
             }

             // To be notified when changes have been persisted to the metadata server
             // within this factory (this includes adding objects, updating objects, and
             // deleting objects), we can add a listener to the factory.
             //
             // See MdFactory.addMdFactoryListener()
          }
          catch (Exception e)
          {
             e.printStackTrace();
          }
       }

       /**
        * This example makes a connection to the metadata server and checks
        * exceptions if there is an error connecting.  The server name, port,
        * user, and password variables must be substituted with actual values.
        * @return true if the connection was successful
        */
       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)
                {
                   // is 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;
       }