*/
public DsFrameworkCliReturnCode performSubCommand(SubCommand subCmd,
OutputStream outStream, OutputStream errStream)
throws ADSContextException, ArgumentException
{
ADSContext adsCtx = null ;
InitialLdapContext ctx = null ;
DsFrameworkCliReturnCode returnCode = ERROR_UNEXPECTED;
try
{
// -----------------------
// create-group subcommand
// -----------------------
if (subCmd.getName().equals(createGroupSubCmd.getName()))
{
String groupId = createGroupGroupNameArg.getValue();
HashMap<ServerGroupProperty, Object> serverGroupProperties =
new HashMap<ServerGroupProperty, Object>();
// get the GROUP_NAME
serverGroupProperties.put(ServerGroupProperty.UID, groupId);
// get the Description
if (createGroupDescriptionArg.isPresent())
{
serverGroupProperties.put(ServerGroupProperty.DESCRIPTION,
createGroupDescriptionArg.getValue());
}
// Create the group
ctx = argParser.getContext(outStream, errStream);
if (ctx == null)
{
return CANNOT_CONNECT_TO_ADS;
}
adsCtx = new ADSContext(ctx) ;
adsCtx.createServerGroup(serverGroupProperties);
returnCode = SUCCESSFUL;
}
// -----------------------
// delete-group subcommand
// -----------------------
else if (subCmd.getName().equals(deleteGroupSubCmd.getName()))
{
returnCode = SUCCESSFUL;
String groupId = deleteGroupGroupNameArg.getValue();
if (groupId.equals(ADSContext.ALL_SERVERGROUP_NAME))
{
return ACCESS_PERMISSION ;
}
HashMap<ServerGroupProperty, Object> serverGroupProperties =
new HashMap<ServerGroupProperty, Object>();
// Get ADS context
ctx = argParser.getContext(outStream, errStream);
if (ctx == null)
{
return CANNOT_CONNECT_TO_ADS;
}
adsCtx = new ADSContext(ctx) ;
// update server Property "GROUPS"
Set<String> serverList = adsCtx.getServerGroupMemberList(groupId);
for (String serverId : serverList)
{
// serverId contains "cn=" string, just remove it.
removeServerFromGroup(adsCtx, groupId,serverId.substring(3));
}
// Delete the group
serverGroupProperties.put(ServerGroupProperty.UID, groupId);
adsCtx.deleteServerGroup(serverGroupProperties);
}
// -----------------------
// list-groups subcommand
// -----------------------
else if (subCmd.getName().equals(listGroupSubCmd.getName()))
{
ctx = argParser.getContext(outStream, errStream);
if (ctx == null)
{
return CANNOT_CONNECT_TO_ADS;
}
adsCtx = new ADSContext(ctx) ;
Set<Map<ServerGroupProperty, Object>> result = adsCtx
.readServerGroupRegistry();
StringBuilder buffer = new StringBuilder();
// if not verbose mode, print group name (1 per line)
if (! verboseArg.isPresent())
{
for (Map<ServerGroupProperty, Object> groupProps : result)
{
// Get the group name
buffer.append(groupProps.get(ServerGroupProperty.UID));
buffer.append(EOL);
}
}
else
{
// Look for the max group identifier length
int uidLength = 0 ;
for (ServerGroupProperty sgp : ServerGroupProperty.values())
{
int cur = attributeDisplayName.get(sgp).toString().length();
if (cur > uidLength)
{
uidLength = cur;
}
}
uidLength++;
for (Map<ServerGroupProperty, Object> groupProps : result)
{
// Get the group name
buffer.append(attributeDisplayName.get(ServerGroupProperty.UID));
// add space
int curLen = attributeDisplayName.get(ServerGroupProperty.UID)
.length();
for (int i = curLen; i < uidLength; i++)
{
buffer.append(" ");
}
buffer.append(": ");
buffer.append(groupProps.get(ServerGroupProperty.UID));
buffer.append(EOL);
// Write other props
for (ServerGroupProperty propName : ServerGroupProperty.values())
{
if (propName.compareTo(ServerGroupProperty.UID) == 0)
{
// We have already displayed the group Id
continue;
}
buffer.append(attributeDisplayName.get(propName));
// add space
curLen = attributeDisplayName.get(propName).length();
for (int i = curLen; i < uidLength; i++)
{
buffer.append(" ");
}
buffer.append(": ");
if (propName.compareTo(ServerGroupProperty.MEMBERS) == 0)
{
Set atts = (Set) groupProps.get(propName);
if (atts != null)
{
boolean indent = false;
for (Object att : atts)
{
if (indent)
{
buffer.append(EOL);
for (int i = 0; i < uidLength + 2; i++)
{
buffer.append(" ");
}
}
else
{
indent = true;
}
buffer.append(att.toString().substring(3));
}
}
}
else
{
if (groupProps.get(propName) != null)
{
buffer.append(groupProps.get(propName));
}
}
buffer.append(EOL);
}
buffer.append(EOL);
}
}
try
{
outStream.write(buffer.toString().getBytes());
}
catch (IOException e)
{
}
returnCode = SUCCESSFUL;
}
// -----------------------
// modify-group subcommand
// -----------------------
else if (subCmd.getName().equals(modifyGroupSubCmd.getName()))
{
String groupId = modifyGroupGroupNameArg.getValue();
HashMap<ServerGroupProperty, Object> serverGroupProperties =
new HashMap<ServerGroupProperty, Object>();
HashSet<ServerGroupProperty> serverGroupPropertiesToRemove =
new HashSet<ServerGroupProperty>();
Boolean updateRequired = false;
Boolean removeRequired = false;
// get the GROUP_ID
if (modifyGroupGroupIdArg.isPresent())
{
// rename the entry !
serverGroupProperties.put(ServerGroupProperty.UID,
modifyGroupGroupIdArg.getValue());
updateRequired = true;
}
else
{
serverGroupProperties.put(ServerGroupProperty.UID, groupId) ;
}
// get the Description
if (modifyGroupDescriptionArg.isPresent())
{
String newDesc = modifyGroupDescriptionArg.getValue();
if (newDesc.length() == 0)
{
serverGroupPropertiesToRemove.add(ServerGroupProperty.DESCRIPTION);
removeRequired = true;
}
else
{
serverGroupProperties.put(ServerGroupProperty.DESCRIPTION,
modifyGroupDescriptionArg.getValue());
updateRequired = true;
}
}
// Update the server group
if ( ! (updateRequired || removeRequired ) )
{
returnCode = SUCCESSFUL_NOP;
}
// We need to perform an update
ctx = argParser.getContext(outStream, errStream);
if (ctx == null)
{
return CANNOT_CONNECT_TO_ADS;
}
adsCtx = new ADSContext(ctx) ;
if (updateRequired)
{
adsCtx.updateServerGroup(groupId, serverGroupProperties);
}
if (removeRequired)
{
adsCtx.removeServerGroupProp(groupId,
serverGroupPropertiesToRemove);
}
returnCode = SUCCESSFUL;
}
// -----------------------
// add-to-group subcommand
// -----------------------
else if (subCmd.getName().equals(addToGroupSubCmd.getName()))
{
String groupId = addToGroupGroupNameArg.getValue();
ctx = argParser.getContext(outStream, errStream);
if (ctx == null)
{
return CANNOT_CONNECT_TO_ADS;
}
adsCtx = new ADSContext(ctx) ;
// Check if the server is registered inside to ADS
Set<Map<ServerProperty, Object>> serverList = adsCtx
.readServerRegistry();
boolean found = false ;
Map<ServerProperty, Object> foundServerProperties = null ;
for (Map<ServerProperty, Object> serverProperties : serverList)
{
String serverId = ADSContext
.getServerIdFromServerProperties(serverProperties);
if (addToGoupMemberNameArg.getValue().equals(serverId))
{
found = true;
foundServerProperties = serverProperties ;
break;
}
}
if ( !found )
{
throw new ADSContextException (ErrorType.NOT_YET_REGISTERED) ;
}
// Add the server inside the group
returnCode = addServerTogroup(adsCtx, groupId, foundServerProperties);
}
// -----------------------
// remove-from-group subcommand
// -----------------------
else if (subCmd.getName().equals(removeFromGroupSubCmd.getName()))
{
ctx = argParser.getContext(outStream, errStream);
if (ctx == null)
{
return CANNOT_CONNECT_TO_ADS;
}
adsCtx = new ADSContext(ctx) ;
returnCode = removeServerFromGroup(adsCtx,
removeFromGroupGroupNameArg.getValue(),
removeFromGoupMemberNameArg.getValue());
}
// -----------------------
// list-members subcommand
// -----------------------
else if (subCmd.getName().equals(listMembersSubCmd.getName()))
{
String groupId = listMembersGroupNameArg.getValue();
ctx = argParser.getContext(outStream, errStream);
if (ctx == null)
{
return CANNOT_CONNECT_TO_ADS;
}
adsCtx = new ADSContext(ctx) ;
// get the current member list
Set<String> memberList = adsCtx.getServerGroupMemberList(groupId);
if (memberList == null)
{
returnCode = SUCCESSFUL;
}
StringBuilder buffer = new StringBuilder();
for (String member : memberList)
{
// We shouldn't print out the "cn="
buffer.append(member.substring(3));
buffer.append(EOL);
}
try
{
outStream.write(buffer.toString().getBytes());
}
catch (IOException e)
{
}
returnCode = SUCCESSFUL;
}
// -----------------------
// list-membership subcommand
// -----------------------
else if (subCmd.getName().equals(listMembershipSubCmd.getName()))
{
ctx = argParser.getContext(outStream, errStream);
if (ctx == null)
{
return CANNOT_CONNECT_TO_ADS;
}
adsCtx = new ADSContext(ctx) ;
Set<Map<ServerGroupProperty, Object>> result = adsCtx
.readServerGroupRegistry();
String MemberId = listMembershipMemberNameArg.getValue();
StringBuilder buffer = new StringBuilder();
for (Map<ServerGroupProperty, Object> groupProps : result)