001 /* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at 010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE 011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE. 012 * See the License for the specific language governing permissions 013 * and limitations under the License. 014 * 015 * When distributing Covered Code, include this CDDL HEADER in each 016 * file and include the License file at 017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable, 018 * add the following below this CDDL HEADER, with the fields enclosed 019 * by brackets "[]" replaced with your own identifying information: 020 * Portions Copyright [yyyy] [name of copyright owner] 021 * 022 * CDDL HEADER END 023 * 024 * 025 * Copyright 2006-2008 Sun Microsystems, Inc. 026 */ 027 package org.opends.server.admin.client.cli; 028 029 import static org.opends.messages.AdminMessages.*; 030 import static org.opends.messages.ToolMessages.*; 031 import static org.opends.server.tools.ToolConstants.*; 032 033 import java.io.OutputStream; 034 import java.util.ArrayList; 035 import java.util.HashSet; 036 import java.util.List; 037 import java.util.Set; 038 039 import javax.naming.NamingException; 040 import javax.naming.ldap.InitialLdapContext; 041 042 import org.opends.admin.ads.ADSContext; 043 import org.opends.admin.ads.ADSContextException; 044 import org.opends.admin.ads.ADSContextHelper; 045 import org.opends.server.util.args.ArgumentException; 046 import org.opends.server.util.args.BooleanArgument; 047 import org.opends.server.util.args.StringArgument; 048 import org.opends.server.util.args.SubCommand; 049 050 import static org.opends.server.admin.client.cli.DsFrameworkCliReturnCode.*; 051 052 /** 053 * This class is handling server group CLI. 054 */ 055 public class DsFrameworkCliAds implements DsFrameworkCliSubCommandGroup 056 { 057 /** 058 * The subcommand Parser. 059 */ 060 DsFrameworkCliParser argParser ; 061 062 /** 063 * The enumeration containing the different subCommand names. 064 */ 065 private enum SubCommandNameEnum 066 { 067 /** 068 * The create-ads subcommand. 069 */ 070 CREATE_ADS("create-ads"), 071 072 /** 073 * The delete-ads subcommand. 074 */ 075 DELETE_ADS("delete-ads"); 076 077 // String representation of the value. 078 private final String name; 079 080 // Private constructor. 081 private SubCommandNameEnum(String name) 082 { 083 this.name = name; 084 } 085 086 /** 087 * {@inheritDoc} 088 */ 089 public String toString() 090 { 091 return name; 092 } 093 094 // A lookup table for resolving a unit from its name. 095 private static final List<String> nameToSubCmdName ; 096 static 097 { 098 nameToSubCmdName = new ArrayList<String>(); 099 100 for (SubCommandNameEnum subCmd : SubCommandNameEnum.values()) 101 { 102 nameToSubCmdName.add(subCmd.toString()); 103 } 104 } 105 public static boolean isSubCommand(String name) 106 { 107 return nameToSubCmdName.contains(name); 108 } 109 } 110 111 /** 112 * The 'create-ads' subcommand. 113 */ 114 public SubCommand createAdsSubCmd; 115 116 /** 117 * The 'backend-name' argument of the 'create-ads' subcommand. 118 */ 119 private StringArgument createAdsBackendNameArg; 120 121 /** 122 * The 'delete-ads' subcommand. 123 */ 124 private SubCommand deleteAdsSubCmd; 125 126 /** 127 * The 'backend-name' argument of the 'delete-ads' subcommand. 128 */ 129 private StringArgument deleteAdsBackendNameArg; 130 131 /** 132 * The subcommand list. 133 */ 134 private HashSet<SubCommand> subCommands = new HashSet<SubCommand>(); 135 136 /** 137 * Indicates whether this subCommand should be hidden in the usage 138 * information. 139 */ 140 private boolean isHidden; 141 142 /** 143 * The subcommand group name. 144 */ 145 private String groupName; 146 147 /** 148 * {@inheritDoc} 149 */ 150 public Set<SubCommand> getSubCommands() 151 { 152 return subCommands; 153 } 154 155 /** 156 * {@inheritDoc} 157 */ 158 public boolean isHidden() 159 { 160 return isHidden ; 161 } 162 163 /** 164 * {@inheritDoc} 165 */ 166 public String getGroupName() 167 { 168 return groupName ; 169 } 170 171 /** 172 * {@inheritDoc} 173 */ 174 public void initializeCliGroup(DsFrameworkCliParser argParser, 175 BooleanArgument verboseArg) 176 throws ArgumentException 177 { 178 179 isHidden = true; 180 groupName = "ads"; 181 this.argParser = argParser; 182 183 // Create-ads subcommand 184 createAdsSubCmd = new SubCommand(argParser, SubCommandNameEnum.CREATE_ADS 185 .toString(), INFO_ADMIN_SUBCMD_CREATE_ADS_DESCRIPTION.get()); 186 createAdsSubCmd.setHidden(true); 187 subCommands.add(createAdsSubCmd); 188 189 createAdsBackendNameArg = new StringArgument("backendName", 190 OPTION_SHORT_BACKENDNAME, OPTION_LONG_BACKENDNAME, false, true, 191 INFO_BACKENDNAME_PLACEHOLDER.get(), 192 INFO_ADMIN_ARG_BACKENDNAME_DESCRIPTION.get()); 193 createAdsSubCmd.addArgument(createAdsBackendNameArg); 194 195 // delete-ads 196 deleteAdsSubCmd = new SubCommand(argParser,SubCommandNameEnum.DELETE_ADS 197 .toString(), INFO_ADMIN_SUBCMD_DELETE_ADS_DESCRIPTION.get()); 198 deleteAdsSubCmd.setHidden(true); 199 subCommands.add(deleteAdsSubCmd); 200 201 deleteAdsBackendNameArg = new StringArgument("backendName", 202 OPTION_SHORT_BACKENDNAME, OPTION_LONG_BACKENDNAME, true, true, 203 INFO_BACKENDNAME_PLACEHOLDER.get(), 204 INFO_ADMIN_ARG_BACKENDNAME_DESCRIPTION.get()); 205 deleteAdsSubCmd.addArgument(deleteAdsBackendNameArg); 206 } 207 208 /** 209 * {@inheritDoc} 210 */ 211 public boolean isSubCommand(SubCommand subCmd) 212 { 213 return SubCommandNameEnum.isSubCommand(subCmd.getName()); 214 } 215 216 217 /** 218 * {@inheritDoc} 219 */ 220 public DsFrameworkCliReturnCode performSubCommand(SubCommand subCmd, 221 OutputStream outStream, OutputStream errStream) 222 throws ADSContextException, ArgumentException 223 { 224 ADSContext adsCtx = null ; 225 InitialLdapContext ctx = null ; 226 227 DsFrameworkCliReturnCode returnCode = ERROR_UNEXPECTED; 228 229 try 230 { 231 // 232 // create-ads subcommand 233 if (subCmd.getName().equals(createAdsSubCmd.getName())) 234 { 235 String backendName = null; 236 if (createAdsBackendNameArg.isPresent()) 237 { 238 backendName = createAdsBackendNameArg.getValue(); 239 } 240 ctx = argParser.getContext(outStream, errStream); 241 if (ctx == null) 242 { 243 return CANNOT_CONNECT_TO_ADS; 244 } 245 adsCtx = new ADSContext(ctx); 246 adsCtx.createAdminData(backendName); 247 returnCode = SUCCESSFUL; 248 } 249 else if (subCmd.getName().equals(deleteAdsSubCmd.getName())) 250 { 251 String backendName = deleteAdsBackendNameArg.getValue(); 252 ADSContextHelper helper = new ADSContextHelper(); 253 ctx = argParser.getContext(outStream, errStream); 254 if (ctx == null) 255 { 256 return CANNOT_CONNECT_TO_ADS; 257 } 258 adsCtx = new ADSContext(ctx); 259 helper 260 .removeAdministrationSuffix(adsCtx.getDirContext(), backendName); 261 returnCode = SUCCESSFUL; 262 } 263 else 264 { 265 // Should never occurs: If we are here, it means that the code to 266 // handle to subcommand is not yet written. 267 returnCode = ERROR_UNEXPECTED; 268 } 269 } 270 catch (ADSContextException e) 271 { 272 if (ctx != null) 273 { 274 try 275 { 276 ctx.close(); 277 } 278 catch (NamingException x) 279 { 280 } 281 } 282 throw e; 283 } 284 285 // Close the connection, if needed 286 if (ctx != null) 287 { 288 try 289 { 290 ctx.close(); 291 } 292 catch (NamingException x) 293 { 294 } 295 } 296 297 // return part 298 return returnCode; 299 } 300 }