IBM TIM API 使用手册

来源:互联网 发布:node schedulejob 编辑:程序博客网 时间:2024/06/03 12:34


一、连接TIM的配置

1,  修改JDK(IBM Java60)目录下的文件【java.security】

2,  现在从Tim安装目录下下载was登录配置文件【jaas_login_was.conf】

3,  参考下载路径:[/opt/IBM/itim/extensions/5.1/examples/apps/bin/jaas_login_was.conf]

示例本地存放路径:[D:\jaas_login_was.conf]

    4,修改JDK(IBM Java50)目录下的文件【java.security】

示例JDK路径:[E:\JavaJDK\IBM\Java50\jre\lib\security\java.security]

编辑此文件修改“login.config.url.1”的值,如下:

# Default login configuration file

#

#login.config.url.1=file:${user.home}/.java.login.config

login.config.url.1=file:///D:/jaas_login_was.conf




二、开发环境

1,  开发工具: eclipse

2,  JDK:  ibm jdk 6.0

3,  所需JAR包 activation.jar,api_ejb.jar,com.ibm.ws.admin.client_6.1.0.jar,com.ibm.ws.ejbportable_6.1.0.jar,itim_api.jar,itim_common.jar,itim_server.jar,itim_server_api.jar,javamail-crypto-bouncycastle-smime.jar,jlog.jar

三、开发前提

1,  确认was,tim 及所需的ldap,db2的实例和Adapter-Dispatcher都已成功启动。

2,  首先在tim web下进行创建人,创建账号等操作,确认成功。

四、使用代码

1,连接代码

public class ItimUtil {

    private static final String LOGIN_CONTEXT ="ITIM";

    public static final String DEFAULT_ORG_ID ="erglobalid=00000000000000000000";

 

    private StringcontextFactory;

    private StringplatformUrl//连接地址

    private StringitimUser;      //tim用户名

    private StringitimPwd //tim 密码

    private StringejbUser;   // was 用户名

    private StringejbPwd;    // was 密码

 

    //创建 platformContext

    public PlatformContext getPlatformContext()throws RemoteException,

           ApplicationException {

 

       Hashtable<String, String> env = new Hashtable<String, String>();

       env.put(InitialPlatformContext.CONTEXT_FACTORY,contextFactory);

       env.put(PlatformContext.PLATFORM_URL,platformUrl);

       env.put(PlatformContext.PLATFORM_PRINCIPAL,ejbUser);

       env.put(PlatformContext.PLATFORM_CREDENTIALS,ejbPwd);

       env.put("enrole.appServer.usertransaction.jndiname","jta/usertransaction");    //was群集时要用此参数

       return new InitialPlatformContext(env);

    }

    //获得 subject

    public Subject getSubject(PlatformContext platform)throws LoginException {

       PlatformCallbackHandler handler = new PlatformCallbackHandler(itimUser,

              itimPwd);

       handler.setPlatformContext(platform);

 

       LoginContext lc = new LoginContext(LOGIN_CONTEXT, handler);

       lc.login();

       return lc.getSubject();

    }

}



























2,创建person代码

//创建person

public String createPerson()throws Exception {

PlatformContext platform =null;

String requestId=null;

try {

    Util utils = new Utils();

    platform = utils.getPlatformContext();

    Subject subject = utils.getSubject(platform);

    String defaultOrg = utils.DEFAULT_ORG_ID + "," + baseDn;

    log.info("defaultOrg=" + defaultOrg);

    OrganizationalContainerMO containerMO = new OrganizationalContainerMO(

    platform, subject, new DistinguishedName(defaultOrg));

    AttributeValues attrs = new AttributeValues();

    AttributeValue attrcn =new AttributeValue("cn","test");

    attrs.put(attrcn);

AttributeValue attrsn =new AttributeValue("sn","test");

    attrs.put(attrsn);

AttributeValue attruid =new AttributeValue("uid","test");

    attrs.put(attruid);

    Person person = new Person("Person");

    person.setAttributes(attrs);

    PersonManager manager = new PersonManager(platform, subject);

    Request request = manager.createPerson(containerMO, person, null);

    requestId = request.getID()+"";

    log.info("Request submitted. Process Id: " + request.getID());

}

}

 





















3,创建account 代码

//创建account

public void createAccount(){

       Utils utils=new Utils();

       PlatformContext platformContext;

       try {

           platformContext = utils.getPlatformContext();

           Subject subject = utils.getSubject(platformContext);

           AccountManager am=new AccountManager (platformContext,subject);

           // person tim ldap下的DN,可以用ldap browser查看,如果没有查看条件,可先根据person的名字查找person,然后获得DN

           String personDN="erglobalid=5006686972586335480,ou=0,ou=people,

erglobalid=00000000000000000000,ou=CSLC,dc=com";

                  DistinguishedName personDn=new DistinguishedName(personDN);

           PersonMO personMO=new PersonMO(platformContext,subject,personDn);

           // service tim ldap下的DN,可以用ldap browser查看,如果没有查看条件,可先根据service的名字查找service,然后获得DN

           String serviceDN="erglobalid=5573272193232429401,ou=services,

erglobalid=00000000000000000000,ou=CSLC,dc=com";

                  DistinguishedName serviceDn=new DistinguishedName(serviceDN);

           ServiceMO servieMo=new ServiceMO(platformContext,subject,serviceDn);

          

           AttributeValues avs =new AttributeValues();

//以下是三个必填属性

           AttributeValue a1=new AttributeValue("eruid","snldhtim");

           AttributeValue a3=new AttributeValue("cn","snldhtim");

           AttributeValue a4=new AttributeValue("sn","snldhtim");

           avs.put(a1);

           avs.put(a3);

           avs.put(a4);

Account account=new Account("LdapAccount");

           account.setAttributes(avs);

           Request request=am.createAccount(personMO, servieMo, account,null);

           System.out.println(request.getID());

       } catch (Exception e) {

           e.printStackTrace();

       }

      

    }






























4,修改person代码

//修改person

public void updatePerson() {

 

       Utils utils = null;

 

       PlatformContext platform = null;

 

       try {

           utils = new Utils();

      

           platform = utils.getPlatformContext();

           Subject subject = utils.getSubject(platform);

          

           String personFilter = "(uid=chenan)";

           SearchMO searchMO = new SearchMO(platform, subject);

           searchMO.setCategory(ObjectProfileCategory.PERSON);

           String TENANT_DN = "ou=people,erglobalid=00000000000000000000,ou=CSLC,dc=com";

           searchMO.setContext(new CompoundDN(new DistinguishedName(TENANT_DN)));

           searchMO.setProfileName("Person");

           searchMO.setFilter(personFilter);

           SearchResultsMO searchResultsMO = null;

           Collection people = null;

           try {

              searchResultsMO = searchMO.execute();

              people = searchResultsMO.getResults();

 

              int num = people.size();

              utils.print("Search by the filter found " + num +" people.\n");

              if (num == 0) {

                  utils.print("No matches to person filter, aborting.");

                  returnfalse;

              }

           } finally {

              // close SearchResultsMO

              if (searchResultsMO !=null) {

                  try {

                     searchResultsMO.close();

                  } catch (Exception e) {

                     e.printStackTrace();

                  }

              }

           }

 

           Person person = (Person) people.iterator().next();

 

           utils.print("Getting the person named, " + person.getName()

                  + ", to be changed." +"\n");

           AttributeValues avs = new AttributeValues();

 

           AttributeValue a3 = new AttributeValue("cn","cccc");

           AttributeValue a4 = new AttributeValue("sn","dddd");

 

           avs.put(a3);

           avs.put(a4);

           // create a PersonMO with the owner's DistinguishedName

           PersonMO personMO = new PersonMO(platform, subject,

                  person.getDistinguishedName());

 

           utils.print("Submitting request to change Person\n");

//注意:此处要new一个新的person,使用查找出来的person会报没权限异常,上面查person的目的是为了获得其DN,如果已知DN可以省略查找步骤。

           Person pe = new Person();

           pe.setAttributes(avs);

           pe.setDistinguishedName(person.getDistinguishedName());

           pe.setProfileName("Person");

           Request request = personMO.update(pe, null);

 

           utils.print("Request submitted. Process Id: " + request.getID());

 

       } catch (RemoteException e) {

           e.printStackTrace();

       } catch (LoginException e) {

           e.printStackTrace();

       } catch (SchemaViolationException e) {

           e.printStackTrace();

       } catch (AuthorizationException e) {

           e.printStackTrace();

       } catch (ApplicationException e) {

           e.printStackTrace();

       } finally {

           if (platform !=null) {

              platform.close();

              platform = null;

           }

       }

    }



































































5, 修改account代码

//修改account

public void updateAccount () {

 

       Utils utils = null;

   

       PlatformContext platform = null;

 

       try {

          

           utils = new Utils();

           platform = utils.getPlatformContext();

           Subject subject = utils.getSubject(platform);

 

           utils.print("Searching for Account \n");

           // Use the Search API to locate the Account todeprovision

           String accountProfile = "NT40Account";

           String accountFilter = "eruid=snldhtim";

 

           SearchMO searchMO = new SearchMO(platform, subject);

           searchMO.setCategory(ObjectProfileCategory.ACCOUNT);

           String tenantId="CSLC";

           String ldapServerRoot="dc=com";

           String dn = "ou=" + tenantId +"," + ldapServerRoot;

          

           searchMO.setContext(new CompoundDN(new DistinguishedName(dn)));

           searchMO.setProfileName(accountProfile);

           searchMO.setFilter(accountFilter);

           SearchResultsMO searchResultsMO = null;

           Collection accounts = null;

           try {

              searchResultsMO = searchMO.execute();

              accounts = searchResultsMO.getResults();

              if (accounts.size() == 0) {

                  utils.print("No matching account found.");

                  returnfalse;

              }

           } finally {

              // close SearchResultsMO

              if(searchResultsMO !=null) {

                  try {

                     searchResultsMO.close();

                  } catch(Exception e) {

                     e.printStackTrace();

                  }

              }

           }

           Account account = (Account) accounts.iterator().next();

           utils.print("Creating AccountMO \n");

           // create an AccountMO

           AccountMO accountMO = new AccountMO(platform, subject, account

                  .getDistinguishedName());

           AttributeValues avs = new AttributeValues();

           AttributeValue a1=new AttributeValue("eruid","snldhtim");

           AttributeValue a3=new AttributeValue("cn","cccc");

           AttributeValue a4=new AttributeValue("sn","dddd");

           avs.put(a1);

           avs.put(a3);

           avs.put(a4);

           account.setAttributes(avs);

 

           utils.print("Submitting request to change Account\n");

 

           Request request = accountMO.update(account, null);

 

           utils.print("Request submitted. Process Id: " + request.getID());

 

       } catch (RemoteException e) {

           e.printStackTrace();

       } catch (LoginException e) {

           e.printStackTrace();

       } catch (SchemaViolationException e) {

           e.printStackTrace();

       } catch (AuthorizationException e) {

           e.printStackTrace();

       } catch (ApplicationException e) {

           e.printStackTrace();

       } finally {

           if(platform !=null) {

              platform.close();

              platform = null;

           }

       }

           }

 

1 0
原创粉丝点击