Skip to main content

Listing Members of a Group

Listing the members of a Group.


Java_Admin_Principal_Group_Get_List.java

The following code sample illustrates how to list all the members of a Group.


/*****************************************************************************************
*
* Java_Admin_Principal_Group_Get_List.java
*
******************************************************************************************/

import java.util.TreeSet;
import java.util.Iterator;
import com.eruces.teadmin.*;
import com.eruces.teagent.*;
public class Java_Admin_Principal_Group_Get_List {

private static final String m_strServerName = "sampleserver"; // the key server you want to connect to
private static final int m_intPort = 8888; // the secure (SSL) connection port of the key server
private static final String m_strAdminUser = "sadmin"; // the administrator name for the key server
private static final String m_strAdminPass = "password1A"; // the administrator password for the key server

private static final String m_strUserName = "ls"; // the user whose groups you want to list
private static final String m_strUserType = "Native"; // the type of the user whose groups you want to list

private static TEAgentConnection m_conn;
public static void connect() throws TEAgentException // standard way of connecting
{
m_conn = TEAgentConnectionFactory.getInstance(TEAgentConnectionFactory.SECURE_USER_PASSWORD);
m_conn.open(m_strServerName, m_intPort, m_strAdminUser, m_strAdminPass);
System.out.println("Connected to " + m_strServerName + ":" + m_intPort + " as " + m_strAdminUser + ".");
}
public static void disconnect() throws TEAgentException // standard way of disconnecting
{
m_conn.close();
System.out.println("Disconnected from " + m_strServerName + ":" + m_intPort + " as " + m_strAdminUser + ".");
}

public static void getGroupList() throws TEAgentException, AdminException, Exception
{
Administration admin = new Administration();
admin.attachConnection(m_conn);
// Get the native user whose name is specified in the member constants above.
PrincipalInfo principal = null;

if (m_strUserType.compareTo("Native") == 0) {

principal = new UserAndPasswordInfo();

((UserAndPasswordInfo)principal).setUserName(m_strUserName);
principal = admin.getPrincipal(principal);

System.out.print("Principal: ");
System.out.println(((UserAndPasswordInfo)principal).getUserName());
}
else if (m_strUserType.compareTo("Windows") == 0) {

principal = new WindowsPrincipalInfo();

((WindowsPrincipalInfo)principal).setUserName(m_strUserName);
principal = admin.getPrincipal(principal);

System.out.print("Principal: ");
System.out.println(((WindowsPrincipalInfo)principal).getUserName());
}
else if (m_strUserType.compareTo("LDAP") == 0) {

principal = new LdapPrincipalInfo();

((LdapPrincipalInfo)principal).setName(m_strUserName);
principal = admin.getPrincipal(principal);

System.out.print("Principal: ");
System.out.println(((LdapPrincipalInfo)principal).getName());
}
else if (m_strUserType.compareTo("Unix") == 0) {

principal = new UnixPrincipalInfo();

((UnixPrincipalInfo)principal).setName(m_strUserName);
principal = admin.getPrincipal(principal);

System.out.print("Principal: ");
System.out.println(((UnixPrincipalInfo)principal).getName());
}

// Get a list of groups that the principal is a member of.
PList groupIDs = principal.getGroupIDs();
TreeSet<?> groupSet = groupIDs.getAllElements();
Iterator<?> iter = groupSet.iterator();
while (iter.hasNext()) { // Iterate through all the groups in the list.
PAny panyID = (PAny)iter.next();

// First construct an empty 'GroupInfo' object. Then use 'setPrincipalID()' to fill out the ID
// number field of that object. Now pass that incomplete 'GroupInfo' object to the 'getPrincipal()' function,
// which searches the key service for the group matching the ID that was provided. Once found,
// the remaining fields of the 'GroupInfo' are filled and the object returned. Note that a generic
// 'PrincipalInfo' object is returned, which then is cast into the child 'GroupInfo' class.
GroupInfo group = new GroupInfo();
group.setPrincipalID(panyID.getLong());
group = (GroupInfo)admin.getPrincipal(group);

String groupName = group.getPrincipalName(); // Get the name of the group from the 'GroupInfo' object.

System.out.println(groupName);
}
admin.detachConnection();
}
public static void main(String[] args)
{
try {
connect(); // Establish a connection to the key server.
System.out.println("");
getGroupList();
System.out.println("");
disconnect(); // Disconnect from the key server.
}
catch (Exception ex) {
System.out.println("Exception encountered: ");
ex.printStackTrace();
}
}
}
//Output
//Connected to sampleserver:8888 as sadmin.
//
//Principal: ls
//ALPHA
//BRAVO
//CHARLIE
//DELTA
//ECHO
//
//Disconnected from sampleserver:8888 as sadmin.