前提
1.要求有一个能够连接到SAS元数据的管理用户
2.要求存在如下JAR包
(
sas.security.sspi.jar
sas.oma.joma.rmt.jar
sas.oma.omi.jar
sas.oma.joma.jar
sas.svc.connection.jar
sas.core.jar
)
相关类/API参考文档
主要使用到SAS package com.sas.metadata.remote ,该包为远端或者中间层的JAVA应用提供了访问SAS元数据服务接口的能力
具体的说明参考<SAS? 9.2 Integration Technologies Java Client Developer’s Guide>文档
关于类中的每个方法和属性说明请参考下面链接地址:
http://support.sas.com/rnd/gendoc/bi92/api/metadata/index.html
样例类runToOMI
样例类主要方法
连接到元数据服务器
方法定义: public boolean connectToServer(String serverName,String serverPort,String serverUser,String serverPass)
方法说明:通过调用该方法,传入相应的服务器名,端口号,用户名,用户口令连接到元数据服务器,如果连接成功,则返回为true,否则返回为false
获得元数据存储库列表
方法定义: public List getRepositories()
方法说明:获得当前连接下元数据存储的列表,所有对元数据的操作必须位于某一特定的元数据存储上
获得验证域
方法定义: public AuthenticationDomain getDomain(CMetadata repository,String domainName)
方式说明:获得指定元数据存储库上指定验证域名的验证域对象
获得是否存在指定的用户
方法定义: public boolean ExistPerson(CMetadata repository,String username)
方法说明:判断指定元数据存储库上是否存在指定的用户名,如果存在,则返回为true,否则返回为false
删除指定用户
方法定义: public void DeletePerson(CMetadata repository,String username)
方法说明:删除指定元数据存储上的指定用户
修改用户描述
方法定义: public void modifyPerson(CMetadata repository,String username,String userdesc)
方法说明:修改指定存储库上指定用户的用户描述
建立用户
方法定义: public void createPerson(CMetadata repository,AuthenticationDomain domain,String username,String userdesc)
方法说明:在指定存储库上,建立指定验证域下的指定用户,并设置用户描述信息
(注:该方法在建立用户时会自动建立相应的登录ID,并把该登录ID用户名附加上”@验证域”信息)
样例类调用示例
runToOMI tester = new runToOMI();
//设置服务器连接信息
String serverName = "你的主机";
String serverPort = "8561";
String serverUser = "sasadm@saspw";
String serverPass = "sasdddddss";
// 连接到服务器
boolean connected = tester.connectToServer(serverName,serverPort,serverUser,serverPass);
if(connected)
{
System.out.println("Connected...");
}
else
{
System.out.println("Error Connecting...");
return;
}
// 获得元数据存储库
List repositories = tester.getRepositories();
CMetadata repos = (CMetadata) repositories.get(0);
//获得域名为TestAuth的验证域对象
AuthenticationDomain domain=tester.getDomain(repos,"TestAuth");
// 在域名为TestAuth的验证域下建立testuser1用户,用户描述为测试用户1
tester.createPerson(repos,domain,"testuser1","测试用户1");
样例代码
请参考下面代码
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.sas.metadata.remote.AssociationList;
import com.sas.metadata.remote.CMetadata;
import com.sas.metadata.remote.Column;
import com.sas.metadata.remote.Person;
import com.sas.metadata.remote.Identity;
import com.sas.metadata.remote.AuthenticationDomain;
import com.sas.metadata.remote.Login;
import com.sas.metadata.remote.MdException;
import com.sas.metadata.remote.MdFactory;
import com.sas.metadata.remote.MdFactoryImpl;
import com.sas.metadata.remote.MdOMIUtil;
import com.sas.metadata.remote.MdOMRConnection;
import com.sas.metadata.remote.MdObjectStore;
import com.sas.metadata.remote.MetadataObjects;
import com.sas.metadata.remote.PhysicalTable;
import com.sas.metadata.remote.TextStore;
/**
* This is a test class that contains examples for the SAS Java Metadata Interface.
*/
public class runToOMI
{
/**
* The following statements instantiate the object factory.
*/
private MdFactory _factory = null;
/**
* Default constructor
*/
public runToOMI()
{
// 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,String serverPort,String serverUser,String serverPass)
{
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;
}
/**
* This example displays the status of the SAS Metadata Server.
*/
public void displayServerInformation()
{
try
{
MdOMRConnection connection = _factory.getConnection();
// Check the status of the metadata server
System.out.println("/nGetting server status...");
int status = connection.getServerStatus();
switch (status)
{
case MdOMRConnection.SERVER_STATUS_OK:
System.out.println("Server is running");
break;
case MdOMRConnection.SERVER_STATUS_PAUSED:
System.out.println("Server is paused");
break;
case MdOMRConnection.SERVER_STATUS_ERROR:
System.out.println("Server is not running");
break;
}
// Check the version of the SAS Metadata Server
int version = connection.getPlatformVersion();
System.out.println("Server platform version: " + version);
}
catch (MdException e)
{
e.printStackTrace();
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
/**
* This example retrieves a list of the repositories that are registered
* on the SAS Metadata Server.
* @return the list of available repositories (list of CMetadata objects)
*/
public List getRepositories()
{
try
{
// Repositories are listed with the getRepositories method.
System.out.println("/nThe repositories contained on this server are: ");
MdOMIUtil omiUtil = _factory.getOMIUtil();
List reposList = omiUtil.getRepositories();
Iterator iter = reposList.iterator();
while (iter.hasNext())
{
// Print the Name= and Id= of each repository.
CMetadata repository = (CMetadata) iter.next();
System.out.println("Repository: " +
repository.getName()
+ " (" + repository.getFQID() +")");
}
return reposList;
}
catch (MdException e)
{
e.printStackTrace();
}
catch (RemoteException e)
{
e.printStackTrace();
}
return new ArrayList();
}
/**
* This example reads the newly created objects from the server.
* @param repository identifies the repository from which to read our objects.
*/
public AuthenticationDomain getDomain(CMetadata repository,String domainName)
{
if(repository != null)
{
try
{
System.out.println("/nReading objects from the server...");
// First we create an MdObjectStore as a container for the
// objects that we will create/read/persist to the server as one collection.
MdObjectStore store = _factory.createObjectStore();
// The following statements define GetMetadataObjectsSubset options
// strings. These XML strings are used in conjunction with SAS Open
// Metadata Interface flags. The <XMLSELECT> element specifies
// filter criteria. The <TEMPLATES> element specifies the metadata
// properties to be returned for each object from the server.
String xmlSelect = String template =
"<Templates>" +
"<AuthenticationDomain Id=/"/" Name=/"/" >" +
"</AuthenticationDomain>" +
"</Templates>";
// Add the xmlSelect and template strings together
String sOptions = xmlSelect + template;
// The following statements go to the server with a fully qualified
// repository ID and specify the type of object we are searching for
// (MdObjectFactory.PHYSICALTABLE) using the OMI_XMLSELECT, OMI_TEMPLATE,
// and OMI_GET_METADATA flags. OMI_GET_METADATA tells the server to get
// all of the attributes specified in the template for each object that
// is returned.
//
// The table, column, and note will be read from the server and created
// within the specified object store.
int flags = MdOMIUtil.OMI_XMLSELECT | MdOMIUtil.OMI_TEMPLATE |
MdOMIUtil.OMI_GET_METADATA;
List domainList = _factory.getOMIUtil().getMetadataObjectsSubset(store,
repository.getFQID(),
MetadataObjects.AUTHENTICATIONDOMAIN,
flags,
sOptions);
Iterator iter = domainList.iterator();
System.out.println("domainList");
while (iter.hasNext())
{
// Print the Id= and Name= of the table returned from the server
AuthenticationDomain domain = (AuthenticationDomain) iter.next();
System.out.println("Found Domain: " + domain.getName() + " (" +
domain.getId() + ")");
return domain;
}
// When finished, clean up the objects in the store if they
// are no longer being used.
store.dispose();
}
catch (MdException e)
{
e.printStackTrace();
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
return null;
}
public boolean ExistPerson(CMetadata repository,String username){
if(repository != null)
{
try
{
System.out.println("/nDeleting the objects from the server...");
MdObjectStore store = _factory.createObjectStore();
// Create a list of the objects that need to be deleted
// from the server.
List deleteList = new ArrayList();
// Query for the table again
String xmlSelect =
"<Person>" +
"</Person>" +
"</Templates>";
// Add the xmlSelect and template strings together
String sOptions = xmlSelect + template;
int flags = MdOMIUtil.OMI_XMLSELECT | MdOMIUtil.OMI_TEMPLATE |
MdOMIUtil.OMI_GET_METADATA;
List tableList = _factory.getOMIUtil().getMetadataObjectsSubset(store,
repository.getFQID(),
MetadataObjects.PERSON,
flags,
sOptions);
// Add the found objects to the delete list
Iterator iter = tableList.iterator();
store.dispose();
return iter.hasNext();
}
catch (MdException e)
{
e.printStackTrace();
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
return false;
}
public void DeletePerson(CMetadata repository,String username){
if(repository != null)
{
try
{
System.out.println("/nDeleting the objects from the server...");
MdObjectStore store = _factory.createObjectStore();
// Create a list of the objects that need to be deleted
// from the server.
List deleteList = new ArrayList();
// Query for the table again
String xmlSelect = String template = "<Templates>" +
"<Person>" +
"</Person>" +
"</Templates>";
// Add the xmlSelect and template strings together
String sOptions = xmlSelect + template;
int flags = MdOMIUtil.OMI_XMLSELECT | MdOMIUtil.OMI_TEMPLATE |
MdOMIUtil.OMI_GET_METADATA;
List tableList = _factory.getOMIUtil().getMetadataObjectsSubset(store,
repository.getFQID(),
MetadataObjects.PERSON,
flags,
sOptions);
// Add the found objects to the delete list
Iterator iter = tableList.iterator();
while (iter.hasNext())
{
Person person = (Person) iter.next();
deleteList.add(person);
}
// Delete everything that is in the delete list
if (deleteList.size() > 0)
{
System.out.println("Deleting " + deleteList.size() + " objects");
_factory.deleteMetadataObjects(deleteList);
}
// When finished, clean up the objects in the store if they
// are no longer being used.
store.dispose();
}
catch (MdException e)
{
e.printStackTrace();
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
}
public void modifyPerson(CMetadata repository,String username,String userdesc){
if(repository != null)
{
try
{
System.out.println("/nModifying the objects from the server...");
MdObjectStore store = _factory.createObjectStore();
// Query for the table again
String xmlSelect =
"<Person>" +
"</Person>" +
"</Templates>";
// Add the xmlSelect and template strings together
String sOptions = xmlSelect + template;
int flags = MdOMIUtil.OMI_XMLSELECT | MdOMIUtil.OMI_TEMPLATE |
MdOMIUtil.OMI_GET_METADATA;
List tableList = _factory.getOMIUtil().getMetadataObjectsSubset(store,
repository.getFQID(),
MetadataObjects.PERSON,
flags,
sOptions);
// Add the found objects to the delete list
Iterator iter = tableList.iterator();
while (iter.hasNext())
{
Person person = (Person) iter.next();
person.setDisplayName(userdesc);
person.updateMetadataAll();
System.out.println("/nModifying objects on the server... username="+username);
}
// When finished, clean up the objects in the store if they
// are no longer being used.
store.dispose();
}
catch (MdException e)
{
e.printStackTrace();
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
}
public void createPerson(CMetadata repository,AuthenticationDomain domain,String username,String userdesc){
if (repository != null)
{
try
{
System.out.println("/nCreating objects on the server...");
// We have a repository object.
// We use the reposFQID method to get its fully qualified ID.
String reposFQID = repository.getFQID();
// We need the short repository ID to create an object.
String shortReposID = reposFQID.substring(reposFQID.indexOf(".") + 1,
reposFQID.length());
// Now we create an object store to hold all our objects.
// This will be used to maintain a list of objects to persist
// to the server.
MdObjectStore store = _factory.createObjectStore();
Person person = (Person) _factory.createComplexMetadataObject
(store,
null,
username,
MetadataObjects.PERSON,
shortReposID);
Login login = (Login) _factory.createComplexMetadataObject
(store,
null,
username+"@"+domain.getName(),
MetadataObjects.LOGIN,
shortReposID);
login.setAssociatedIdentity((Identity )person);
login.setUserID(username+"@"+domain.getName());
login.setDomain(domain);
person.setDisplayName(userdesc);
person.updateMetadataAll();
// When finished, clean up the objects in the store if they
// are no longer being used.
store.dispose();
}
catch (MdException e)
{
e.printStackTrace();
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
}
/**
* The main method for the class
*/
public static void main(String[] args)
{
runToOMI tester = new runToOMI();
// Server's connection information
String serverName = "你的主机";
String serverPort = "8561";
String serverUser = "
String serverPass = "sasadds";
// Connect to the SAS Metadata Server
boolean connected = tester.connectToServer(serverName,serverPort,serverUser,serverPass);
if(connected)
{
System.out.println("Connected...");
}
else
{
System.out.println("Error Connecting...");
return;
}
// Now that we are connected, check the status of the server
tester.displayServerInformation();
// Get the list of repositories on the server
List repositories = tester.getRepositories();
CMetadata repos = (CMetadata) repositories.get(0);
//tester.createPerson(repos);
AuthenticationDomain domain=tester.getDomain(repos,"TestAuth");
tester.createPerson(repos,domain,"testzlb","zlbtest");
tester.DeletePerson(repos,"testzlb");
System.exit(1);
}
}
(转载请注明出处,谢谢)