# 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 MdObjectFactory _factory = null;

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

       private void initializeFactory()
       {
          // Construct a new factory.
          _factory = MdObjectFactory.getInstance();

          // 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.
             Util.setOutputStream(System.out);
             Util.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 MdObjectFactory.addMdFactoryListener()
       }

       /**
        * 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
          {
             MetadataWorkspace workspace = MetadataWorkspace.getWorkspace();

             // this statement makes the connection to the server
             workspace.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;
       }