View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 11 package com.sun.xml.registry.client.admin; 12 13 import com.sun.xml.registry.ebxml.util.ProviderProperties; 14 import com.sun.xml.registry.client.util.RegistryObjectUtil; 15 import com.sun.xml.registry.ebxml.util.SecurityUtil; 16 import com.sun.xml.registry.ebxml.util.KeystoreUtil; 17 import com.sun.xml.registry.ebxml.util.ProviderProperties; 18 19 import java.util.ArrayList; 20 import java.util.HashSet; 21 22 import javax.xml.registry.BulkResponse; 23 import javax.xml.registry.BusinessLifeCycleManager; 24 import javax.xml.registry.BusinessQueryManager; 25 import javax.xml.registry.Connection; 26 import javax.xml.registry.ConnectionFactory; 27 import javax.xml.registry.DeclarativeQueryManager; 28 import javax.xml.registry.JAXRException; 29 import javax.xml.registry.RegistryService; 30 import javax.xml.registry.infomodel.InternationalString; 31 import javax.xml.registry.infomodel.RegistryPackage; 32 import javax.xml.registry.infomodel.TelephoneNumber; 33 import javax.xml.registry.infomodel.User; 34 35 36 /* 37 * Program to administer users for ebxmlrr server. Note: this program is 38 * specific to a ebXML JAXR provider and to the ebxmlrr implementation of a 39 * registry as it depends upon implementation specific aspects of user 40 * registration. See http://ebxmlrr.sourceforge.net for more info. 41 * 42 * $Header: /cvsroot/ebxmlrr/jaxr/src/com/sun/xml/registry/client/admin/UserAdmin.java,v 1.8 2003/07/18 02:16:16 farrukh_najmi Exp $ 43 * 44 * @author Edwin Goei 45 * 46 */ 47 public class UserAdmin { 48 49 /*** DOCUMENT ME! */ 50 private static final SecurityUtil su = SecurityUtil.getInstance(); 51 52 /*** DOCUMENT ME! */ 53 BusinessQueryManager bqm; 54 55 /*** DOCUMENT ME! */ 56 BusinessLifeCycleManager lcm; 57 58 /*** DOCUMENT ME! */ 59 DeclarativeQueryManager dqm; 60 61 /*** 62 * DOCUMENT ME! 63 * 64 * @param serverUrl DOCUMENT ME! 65 * @param alias DOCUMENT ME! 66 * 67 * @throws JAXRException DOCUMENT ME! 68 */ 69 public void init(String serverUrl, String alias) 70 throws JAXRException { 71 72 ConnectionFactory connFactory = ConnectionFactory.newInstance(); 73 74 ProviderProperties.getInstance().put("javax.xml.registry.queryManagerURL", 75 serverUrl); 76 77 Connection connection = connFactory.createConnection(); 78 79 HashSet creds = new HashSet(); 80 creds.add(su.aliasToX500PrivateCredential(alias)); 81 connection.setCredentials(creds); 82 83 RegistryService service = connection.getRegistryService(); 84 bqm = service.getBusinessQueryManager(); 85 lcm = service.getBusinessLifeCycleManager(); 86 dqm = service.getDeclarativeQueryManager(); 87 } 88 89 /*** 90 * DOCUMENT ME! 91 * 92 * @return null if User is not registered, else User object 93 * corresponding to Connection credentials 94 * 95 * @throws JAXRException DOCUMENT ME! 96 */ 97 public User getUser() throws JAXRException { 98 99 RegistryPackage pkg1 = 100 lcm.createRegistryPackage("Dummy Package 1"); 101 ArrayList al = new ArrayList(); 102 al.add(pkg1); 103 104 BulkResponse br = lcm.saveObjects(al); 105 106 if (br.getExceptions() != null) { 107 108 // Assume credentials are unknown to server so therefore User 109 // is not registered 110 return null; 111 } 112 113 User user = RegistryObjectUtil.getOwner(pkg1); 114 115 // Now clean up by removing dummy objects 116 al.clear(); 117 al.add(pkg1.getKey()); 118 br = lcm.deleteObjects(al); 119 120 if (br.getStatus() != BulkResponse.STATUS_SUCCESS) { 121 122 // Not able to clean up 123 System.err.println("Warning: unable clean up dummy objects created on server"); 124 } 125 126 if (user == null) { 127 128 // Unable to get AuditTrail for some reason 129 throw new JAXRException("Unable to obtain User object corresponding to credentials"); 130 } 131 132 return user; 133 } 134 135 /*** 136 * DOCUMENT ME! 137 * 138 * @param user DOCUMENT ME! 139 * @param personName DOCUMENT ME! 140 * 141 * @return DOCUMENT ME! 142 * 143 * @throws JAXRException DOCUMENT ME! 144 */ 145 private User populateUser(User user, String personName) 146 throws JAXRException { 147 148 // Set the name of the User object itself (so it shows up in the 149 // current browser) 150 InternationalString isName = 151 lcm.createInternationalString(personName); 152 user.setName(isName); 153 154 user.setPersonName(lcm.createPersonName(personName)); 155 156 ArrayList al = new ArrayList(); 157 al.add(lcm.createEmailAddress("jaxr-discussion@yahoogroups.com")); 158 user.setEmailAddresses(al); 159 160 al.clear(); 161 al.add(lcm.createPostalAddress("1234", "Test Blvd", 162 "Santa Clara", "CA", "USA", 163 "95054", null)); 164 user.setPostalAddresses(al); 165 166 TelephoneNumber phone = lcm.createTelephoneNumber(); 167 phone.setAreaCode("408"); 168 phone.setNumber("555-1212"); 169 al.clear(); 170 al.add(phone); 171 user.setTelephoneNumbers(al); 172 173 return user; 174 } 175 176 /*** 177 * Register a new user 178 * 179 * @param personName DOCUMENT ME! 180 * 181 * @throws JAXRException DOCUMENT ME! 182 */ 183 public void registerUser(String personName) throws JAXRException { 184 185 if (getUser() != null) { 186 System.out.println("Error: User is already registered," 187 + " use -update command instead"); 188 189 return; 190 } 191 192 User newUser = lcm.createUser(); 193 populateUser(newUser, personName); 194 195 ArrayList al = new ArrayList(); 196 al.add(newUser); 197 198 BulkResponse br = lcm.saveObjects(al); 199 RegistryObjectUtil.checkBulkResponse(br); 200 } 201 202 /*** 203 * Update an existing User 204 * 205 * @param personName DOCUMENT ME! 206 * 207 * @throws JAXRException DOCUMENT ME! 208 */ 209 public void updateUser(String personName) throws JAXRException { 210 211 User user = getUser(); 212 213 if (user == null) { 214 System.out.println("Error: User is not registered"); 215 216 return; 217 } 218 219 populateUser(user, personName); 220 221 ArrayList al = new ArrayList(); 222 al.add(user); 223 224 BulkResponse br = lcm.saveObjects(al); 225 RegistryObjectUtil.checkBulkResponse(br); 226 } 227 228 /*** 229 */ 230 public void removeUser() throws JAXRException { 231 232 User user = getUser(); 233 234 if (user == null) { 235 System.out.println("Error: User is not registered"); 236 237 return; 238 } 239 240 ArrayList al = new ArrayList(); 241 al.add(user.getKey()); 242 243 BulkResponse br = lcm.deleteObjects(al); 244 RegistryObjectUtil.checkBulkResponse(br); 245 } 246 247 /*** 248 * DOCUMENT ME! 249 */ 250 private static void usage() { 251 System.err.println("Usage: UserAdmin -<command> [parameters]"); 252 System.err.println(" -getUser Query the server for a User"); 253 System.err.println(" -register personName=<> Register a new User"); 254 System.err.println(" -update personName=<> Update an existing User"); 255 System.err.println(" -remove Remove an existing User"); 256 System.err.println(" alias=<keystore alias> used to link to credentials"); 257 System.err.println(" personName=<person name>"); 258 System.err.println(" url=<server url>"); 259 System.exit(-1); 260 } 261 262 /*** 263 * DOCUMENT ME! 264 * 265 * @param args DOCUMENT ME! 266 * 267 * @throws Exception DOCUMENT ME! 268 */ 269 public static void main(String[] args) throws Exception { 270 271 String alias = 272 ProviderProperties.getInstance().getProperty("UserAdmin.default.keystore.alias"); 273 String serverUrl = 274 ProviderProperties.getInstance().getProperty("UserAdmin.server.url"); 275 String personName = null; 276 String command = null; 277 278 for (int i = 0; i < args.length; i++) { 279 280 if (args[i].equalsIgnoreCase("-help")) { 281 usage(); 282 } else if (args[i].equalsIgnoreCase("-usage")) { 283 usage(); 284 } else if (args[i].equalsIgnoreCase("-getUser")) { 285 command = "getUser"; 286 } else if (args[i].equalsIgnoreCase("-register")) { 287 command = "register"; 288 } else if (args[i].equalsIgnoreCase("-update")) { 289 command = "update"; 290 } else if (args[i].equalsIgnoreCase("-remove")) { 291 command = "remove"; 292 } else if (args[i].startsWith("alias=")) { 293 alias = args[i].substring(6); 294 } else if (args[i].startsWith("personName=")) { 295 personName = args[i].substring(11); 296 } else if (args[i].startsWith("url=")) { 297 serverUrl = args[i].substring(4); 298 } else { 299 usage(); 300 } 301 } 302 303 if (alias == null) { 304 throw new Exception("KeyStore alias is required"); 305 } else { 306 307 if (!su.getKeyStore().containsAlias(alias)) { 308 System.err.println("Unknown alias in KeyStore: alias=" 309 + alias + ", KeyStore file=" 310 + KeystoreUtil.getKeystoreFile()); 311 usage(); 312 } 313 } 314 315 if (command == null) { 316 command = "getUser"; 317 } 318 319 UserAdmin userAdmin = new UserAdmin(); 320 userAdmin.init(serverUrl, alias); 321 322 System.out.println("Executing command: -" + command); 323 324 if ("getUser".equals(command)) { 325 System.out.println("Getting User linked to credentials with alias=" 326 + alias + "..."); 327 328 User user = userAdmin.getUser(); 329 System.out.println("userId=" 330 + RegistryObjectUtil.toId(user)); 331 } else if ("register".equals(command)) { 332 333 if (personName == null) { 334 usage(); 335 } 336 337 System.out.println("Registering new User linked to credentials with alias=" 338 + alias + " using personName=" 339 + personName + "..."); 340 userAdmin.registerUser(personName); 341 } else if ("update".equals(command)) { 342 343 if (personName == null) { 344 usage(); 345 } 346 347 System.out.println("Updating User linked to credentials with alias=" 348 + alias + " to personName=" + personName 349 + "..."); 350 userAdmin.updateUser(personName); 351 } else if ("remove".equals(command)) { 352 System.out.println("Removing User linked to credentials with alias=" 353 + alias + "..."); 354 355 // ??eeg finish this 356 userAdmin.removeUser(); 357 } 358 } 359 }

This page was automatically generated by Maven