001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.server.tools; 021 022 023 import java.io.File; 024 import java.io.IOException; 025 import java.net.InetSocketAddress; 026 import java.net.SocketAddress; 027 import java.net.UnknownHostException; 028 import java.nio.ByteBuffer; 029 import java.nio.channels.SocketChannel; 030 031 import org.apache.commons.cli.CommandLine; 032 import org.apache.commons.cli.Option; 033 import org.apache.commons.cli.Options; 034 import org.apache.directory.daemon.AvailablePortFinder; 035 import org.apache.directory.server.i18n.I18n; 036 import org.apache.directory.shared.asn1.ber.Asn1Decoder; 037 import org.apache.directory.shared.asn1.ber.IAsn1Container; 038 import org.apache.directory.shared.asn1.ber.tlv.TLVStateEnum; 039 import org.apache.directory.shared.asn1.codec.DecoderException; 040 import org.apache.directory.shared.asn1.codec.EncoderException; 041 import org.apache.directory.shared.ldap.codec.LdapMessageCodec; 042 import org.apache.directory.shared.ldap.codec.LdapMessageContainer; 043 import org.apache.directory.shared.ldap.codec.LdapResponseCodec; 044 import org.apache.directory.shared.ldap.codec.LdapResultCodec; 045 import org.apache.directory.shared.ldap.codec.add.AddRequestCodec; 046 import org.apache.directory.shared.ldap.codec.bind.BindRequestCodec; 047 import org.apache.directory.shared.ldap.codec.bind.BindResponseCodec; 048 import org.apache.directory.shared.ldap.codec.bind.LdapAuthentication; 049 import org.apache.directory.shared.ldap.codec.bind.SimpleAuthentication; 050 import org.apache.directory.shared.ldap.codec.del.DelRequestCodec; 051 import org.apache.directory.shared.ldap.codec.extended.ExtendedResponseCodec; 052 import org.apache.directory.shared.ldap.codec.modify.ModifyRequestCodec; 053 import org.apache.directory.shared.ldap.codec.modifyDn.ModifyDNRequestCodec; 054 import org.apache.directory.shared.ldap.codec.unbind.UnBindRequestCodec; 055 import org.apache.directory.shared.ldap.entry.Entry; 056 import org.apache.directory.shared.ldap.entry.EntryAttribute; 057 import org.apache.directory.shared.ldap.entry.Modification; 058 import org.apache.directory.shared.ldap.entry.Value; 059 import org.apache.directory.shared.ldap.exception.LdapException; 060 import org.apache.directory.shared.ldap.exception.LdapInvalidDnException; 061 import org.apache.directory.shared.ldap.ldif.ChangeType; 062 import org.apache.directory.shared.ldap.ldif.LdifEntry; 063 import org.apache.directory.shared.ldap.ldif.LdifReader; 064 import org.apache.directory.shared.ldap.message.ResultCodeEnum; 065 import org.apache.directory.shared.ldap.name.DN; 066 import org.apache.directory.shared.ldap.name.RDN; 067 import org.apache.directory.shared.ldap.util.StringTools; 068 069 070 /** 071 * A command to import data into a server. The data to be imported must be 072 * stored in a Ldif File, and they could be added entries or modified entries. 073 * 074 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 075 * @version $Rev: 406112 $ 076 */ 077 public class ImportCommand extends ToolCommand 078 { 079 public static final String PORT_RANGE = "(" + AvailablePortFinder.MIN_PORT_NUMBER + ", " 080 + AvailablePortFinder.MAX_PORT_NUMBER + ")"; 081 082 private int port = 10389; 083 084 private String host = "localhost"; 085 086 private String password = "secret"; 087 088 private String user = "uid=admin,ou=system"; 089 090 private String auth = "simple"; 091 092 private File ldifFile; 093 094 private String logs; 095 096 private boolean ignoreErrors = false; 097 098 private static final int IMPORT_ERROR = -1; 099 private static final int IMPORT_SUCCESS = 0; 100 101 /** 102 * Socket used to connect to the server 103 */ 104 private SocketChannel channel; 105 106 private SocketAddress serverAddress; 107 108 private IAsn1Container ldapMessageContainer = new LdapMessageContainer(); 109 110 private Asn1Decoder ldapDecoder = new Asn1Decoder(); 111 112 113 /** 114 * The constructor save the command's name into it's super class 115 * 116 */ 117 protected ImportCommand() 118 { 119 super( "import" ); 120 } 121 122 123 /** 124 * Connect to the LDAP server through a socket and establish the Input and 125 * Output Streams. All the required information for the connection should be 126 * in the options from the command line, or the default values. 127 * 128 * @throws UnknownHostException 129 * The hostname or the Address of server could not be found 130 * @throws IOException 131 * There was a error opening or establishing the socket 132 */ 133 private void connect() throws UnknownHostException, IOException 134 { 135 serverAddress = new InetSocketAddress( host, port ); 136 channel = SocketChannel.open( serverAddress ); 137 channel.configureBlocking( true ); 138 } 139 140 141 private void sendMessage( ByteBuffer bb ) throws IOException 142 { 143 channel.write( bb ); 144 bb.clear(); 145 } 146 147 148 private LdapMessageCodec readResponse( ByteBuffer bb ) throws IOException, DecoderException 149 { 150 151 LdapMessageCodec messageResp = null; 152 153 while ( true ) 154 { 155 int nbRead = channel.read( bb ); 156 157 if ( nbRead == -1 ) 158 { 159 break; 160 } 161 else 162 { 163 bb.flip(); 164 165 // Decode the PDU 166 ldapDecoder.decode( bb, ldapMessageContainer ); 167 168 if ( ldapMessageContainer.getState() == TLVStateEnum.PDU_DECODED ) 169 { 170 messageResp = ( ( LdapMessageContainer ) ldapMessageContainer ).getLdapMessage(); 171 172 if ( messageResp instanceof BindResponseCodec ) 173 { 174 BindResponseCodec resp = ( ( LdapMessageContainer ) ldapMessageContainer ).getBindResponse(); 175 176 if ( resp.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS ) 177 { 178 System.out.println( "Error : " + resp.getLdapResult().getErrorMessage() ); 179 } 180 } 181 else if ( messageResp instanceof ExtendedResponseCodec ) 182 { 183 ExtendedResponseCodec resp = ( ( LdapMessageContainer ) ldapMessageContainer ).getExtendedResponse(); 184 185 if ( resp.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS ) 186 { 187 System.out.println( "Error : " + resp.getLdapResult().getErrorMessage() ); 188 } 189 } 190 191 ( ( LdapMessageContainer ) ldapMessageContainer ).clean(); 192 break; 193 } 194 else 195 { 196 bb.flip(); 197 } 198 } 199 } 200 201 return messageResp; 202 203 } 204 205 206 /** 207 * Send the entry to the encoder, then wait for a 208 * reponse from the LDAP server on the results of the operation. 209 * 210 * @param ldifEntry 211 * The entry to add 212 * @param msgId 213 * message id number 214 */ 215 private int addEntry( LdifEntry ldifEntry, int messageId ) throws IOException, DecoderException, LdapException, 216 EncoderException 217 { 218 AddRequestCodec addRequest = new AddRequestCodec(); 219 220 String dn = ldifEntry.getDn().getName(); 221 222 if ( isDebugEnabled() ) 223 { 224 System.out.println( "Adding entry " + dn ); 225 } 226 227 Entry entry = ldifEntry.getEntry(); 228 229 addRequest.setEntryDn( new DN( dn ) ); 230 231 // Copy the attributes 232 for ( EntryAttribute attribute:entry ) 233 { 234 addRequest.addAttributeType( attribute.getId() ); 235 236 for ( Value<?> value: attribute ) 237 { 238 addRequest.addAttributeValue( value ); 239 } 240 } 241 242 addRequest.setMessageId( messageId ); 243 244 // Encode and send the addRequest message 245 ByteBuffer bb = addRequest.encode(); 246 bb.flip(); 247 248 sendMessage( bb ); 249 250 bb.clear(); 251 252 // Get the response 253 LdapMessageCodec response = readResponse( bb ); 254 255 LdapResultCodec result = ((LdapResponseCodec)response).getLdapResult(); 256 257 if ( result.getResultCode() == ResultCodeEnum.SUCCESS ) 258 { 259 if ( isDebugEnabled() ) 260 { 261 System.out.println( "Add of Entry " + entry.getDn() + " was successful" ); 262 } 263 264 return IMPORT_SUCCESS; 265 } 266 else 267 { 268 System.err.println( I18n.err( I18n.ERR_203, entry.getDn(), result.getErrorMessage() ) ); 269 270 return IMPORT_ERROR; 271 } 272 } 273 274 275 /** 276 * Send the entry to the encoder, then wait for a 277 * reponse from the LDAP server on the results of the operation. 278 * 279 * @param entry 280 * The entry to delete 281 * @param msgId 282 * message id number 283 */ 284 private int deleteEntry( LdifEntry entry, int messageId ) throws IOException, DecoderException, 285 LdapInvalidDnException, EncoderException 286 { 287 DelRequestCodec delRequest = new DelRequestCodec(); 288 289 String dn = entry.getDn().getName(); 290 291 if ( isDebugEnabled() ) 292 { 293 System.out.println( "Deleting entry " + dn ); 294 } 295 296 delRequest.setEntry( new DN( dn ) ); 297 298 delRequest.setMessageId( messageId ); 299 300 // Encode and send the delete request 301 ByteBuffer bb = delRequest.encode(); 302 bb.flip(); 303 304 sendMessage( bb ); 305 306 bb.clear(); 307 308 // Get the response 309 LdapMessageCodec response = readResponse( bb ); 310 311 LdapResultCodec result = ((LdapResponseCodec)response).getLdapResult(); 312 313 if ( result.getResultCode() == ResultCodeEnum.SUCCESS ) 314 { 315 if ( isDebugEnabled() ) 316 { 317 System.out.println( "Delete of Entry " + entry.getDn() + " was successful" ); 318 } 319 320 return IMPORT_SUCCESS; 321 } 322 else 323 { 324 System.err.println( I18n.err( I18n.ERR_204, entry.getDn(), result.getErrorMessage() ) ); 325 return IMPORT_ERROR; 326 } 327 } 328 329 330 /** 331 * Send the entry to the encoder, then wait for a 332 * reponse from the LDAP server on the results of the operation. 333 * 334 * @param entry 335 * The entry to modify 336 * @param msgId 337 * message id number 338 */ 339 private int changeModRDNEntry( LdifEntry entry, int messageId ) throws IOException, DecoderException, 340 LdapInvalidDnException, EncoderException 341 { 342 ModifyDNRequestCodec modifyDNRequest = new ModifyDNRequestCodec(); 343 344 String dn = entry.getDn().getName(); 345 346 if ( isDebugEnabled() ) 347 { 348 System.out.println( "Modify DN of entry " + dn ); 349 } 350 351 modifyDNRequest.setEntry( new DN( dn ) ); 352 modifyDNRequest.setDeleteOldRDN( entry.isDeleteOldRdn() ); 353 modifyDNRequest.setNewRDN( new RDN( entry.getNewRdn() ) ); 354 355 if ( StringTools.isEmpty( entry.getNewSuperior() ) == false ) 356 { 357 modifyDNRequest.setNewSuperior( new DN( entry.getNewSuperior() ) ); 358 } 359 360 modifyDNRequest.setMessageId( messageId ); 361 362 // Encode and send the delete request 363 ByteBuffer bb = modifyDNRequest.encode(); 364 bb.flip(); 365 366 sendMessage( bb ); 367 368 bb.clear(); 369 370 // Get the response 371 LdapMessageCodec response = readResponse( bb ); 372 373 LdapResultCodec result = ((LdapResponseCodec)response).getLdapResult(); 374 375 if ( result.getResultCode() == ResultCodeEnum.SUCCESS ) 376 { 377 if ( isDebugEnabled() ) 378 { 379 System.out.println( "ModifyDn of Entry " + entry.getDn() + " was successful" ); 380 } 381 382 return IMPORT_SUCCESS; 383 } 384 else 385 { 386 System.err.println( I18n.err( I18n.ERR_205, entry.getDn(), result.getErrorMessage() ) ); 387 return IMPORT_ERROR; 388 } 389 } 390 391 392 /** 393 * Send the entry to the encoder, then wait for a 394 * reponse from the LDAP server on the results of the operation. 395 * 396 * @param entry The entry to modify 397 * @param msgId message id number 398 */ 399 private int changeModifyEntry( LdifEntry entry, int messageId ) throws IOException, DecoderException, 400 LdapInvalidDnException, EncoderException 401 { 402 ModifyRequestCodec modifyRequest = new ModifyRequestCodec(); 403 404 String dn = entry.getDn().getName(); 405 406 if ( isDebugEnabled() ) 407 { 408 System.out.println( "Modify of entry " + dn ); 409 } 410 411 modifyRequest.setObject( new DN( dn ) ); 412 modifyRequest.initModifications(); 413 414 for ( Modification modification: entry.getModificationItems() ) 415 { 416 modifyRequest.setCurrentOperation( modification.getOperation() ); 417 modifyRequest.addAttributeTypeAndValues( modification.getAttribute().getId() ); 418 419 for ( Value<?> value:modification.getAttribute() ) 420 { 421 modifyRequest.addAttributeValue( value ); 422 } 423 } 424 425 modifyRequest.setMessageId( messageId ); 426 427 // Encode and send the delete request 428 ByteBuffer bb = modifyRequest.encode(); 429 bb.flip(); 430 431 sendMessage( bb ); 432 433 bb.clear(); 434 435 // Get the response 436 LdapMessageCodec response = readResponse( bb ); 437 438 LdapResultCodec result = ((LdapResponseCodec)response).getLdapResult(); 439 440 if ( result.getResultCode() == ResultCodeEnum.SUCCESS ) 441 { 442 if ( isDebugEnabled() ) 443 { 444 System.out.println( "Modify of Entry " + entry.getDn() + " was successful" ); 445 } 446 447 return IMPORT_SUCCESS; 448 } 449 else 450 { 451 System.err.println( I18n.err( I18n.ERR_206, entry.getDn(), result.getErrorMessage() ) ); 452 return IMPORT_ERROR; 453 } 454 } 455 456 457 /** 458 * Send the change operation to the encoder, then wait for a 459 * reponse from the LDAP server on the results of the operation. 460 * 461 * @param entry 462 * The entry to add 463 * @param msgId 464 * message id number 465 */ 466 private int changeEntry( LdifEntry entry, int messageId ) throws IOException, DecoderException, 467 LdapException, EncoderException 468 { 469 switch ( entry.getChangeType().getChangeType() ) 470 { 471 case ChangeType.ADD_ORDINAL: 472 // No difference with the injection of new entries 473 return addEntry( entry, messageId ); 474 475 case ChangeType.DELETE_ORDINAL: 476 return deleteEntry( entry, messageId ); 477 478 case ChangeType.MODIFY_ORDINAL: 479 return changeModifyEntry( entry, messageId ); 480 481 case ChangeType.MODDN_ORDINAL: 482 case ChangeType.MODRDN_ORDINAL: 483 return changeModRDNEntry( entry, messageId ); 484 485 default: 486 return IMPORT_ERROR; 487 } 488 } 489 490 491 /** 492 * Bind to the ldap server 493 * 494 * @param messageId The message Id 495 */ 496 private void bind( int messageId ) throws LdapInvalidDnException, EncoderException, DecoderException, IOException 497 { 498 BindRequestCodec bindRequest = new BindRequestCodec(); 499 LdapAuthentication authentication = null; 500 501 if ( "simple".equals( auth ) ) 502 { 503 authentication = new SimpleAuthentication(); 504 ( ( SimpleAuthentication ) authentication ).setSimple( StringTools.getBytesUtf8( password ) ); 505 } 506 507 bindRequest.setAuthentication( authentication ); 508 bindRequest.setName( new DN( user ) ); 509 bindRequest.setVersion( 3 ); 510 511 bindRequest.setMessageId( messageId ); 512 513 // Encode and send the bind request 514 ByteBuffer bb = bindRequest.encode(); 515 bb.flip(); 516 517 connect(); 518 sendMessage( bb ); 519 520 bb.clear(); 521 522 // Get the bind response 523 LdapMessageCodec response = readResponse( bb ); 524 525 LdapResultCodec result = ((LdapResponseCodec)response).getLdapResult(); 526 527 if ( result.getResultCode() == ResultCodeEnum.SUCCESS ) 528 { 529 if ( isDebugEnabled() ) 530 { 531 System.out.println( "Binding of user " + user + " was successful" ); 532 } 533 } 534 else 535 { 536 System.err.println( I18n.err( I18n.ERR_207, user, result.getErrorMessage() ) ); 537 System.exit( 1 ); 538 } 539 } 540 541 542 /** 543 * Unbind from the server 544 * 545 * @param messageId 546 * The message Id 547 * @throws EncoderException 548 * @throws DecoderException 549 * @throws IOException 550 */ 551 private void unbind( int messageId ) throws EncoderException, DecoderException, IOException 552 { 553 UnBindRequestCodec unbindRequest = new UnBindRequestCodec(); 554 555 unbindRequest.setMessageId( messageId ); 556 ByteBuffer bb = unbindRequest.encode(); 557 bb.flip(); 558 559 sendMessage( bb ); 560 561 if ( isDebugEnabled() ) 562 { 563 System.out.println( "Unbinding of user " + user + " was successful" ); 564 } 565 } 566 567 568 /** 569 * Execute the command 570 * 571 * @param cmd 572 * The command to be executed 573 */ 574 public void execute( CommandLine cmd ) throws Exception 575 { 576 processOptions( cmd ); 577 578 if ( isDebugEnabled() ) 579 { 580 System.out.println( "Parameters for Ldif import request:" ); 581 System.out.println( "port = " + port ); 582 System.out.println( "host = " + host ); 583 System.out.println( "user = " + user ); 584 System.out.println( "auth type = " + auth ); 585 System.out.println( "file = " + ldifFile ); 586 System.out.println( "logs = " + logs ); 587 } 588 589 int messageId = 0; 590 591 // Login to the server 592 bind( messageId++ ); 593 594 if ( isDebugEnabled() ) 595 { 596 System.out.println( "Connection to the server established.\n" + "Importing data ... " ); 597 } 598 599 LdifReader ldifReader = new LdifReader( ldifFile ); 600 601 if ( ldifReader.containsEntries() ) 602 { 603 // Parse the file and inject every entry 604 long t0 = System.currentTimeMillis(); 605 int nbAdd = 0; 606 607 for ( LdifEntry entry:ldifReader ) 608 { 609 // Check if we have had some error, has next() does not throw any exception 610 if ( ldifReader.hasError() ) 611 { 612 System.err 613 .println( "Found an error while persing an entry : " + ldifReader.getError().getLocalizedMessage() ); 614 615 if ( ignoreErrors == false ) 616 { 617 unbind( messageId ); 618 619 System.err.println( I18n.err( I18n.ERR_208 ) ); 620 System.exit( 1 ); 621 } 622 } 623 624 if ( ( addEntry( entry, messageId++ ) == IMPORT_ERROR ) && ( ignoreErrors == false ) ) 625 { 626 unbind( messageId ); 627 628 System.err.println( I18n.err( I18n.ERR_208 ) ); 629 System.exit( 1 ); 630 } 631 632 nbAdd++; 633 634 if ( nbAdd % 10 == 0 ) 635 { 636 System.out.print( '.' ); 637 } 638 639 if ( nbAdd % 500 == 0 ) 640 { 641 System.out.println( nbAdd ); 642 } 643 } 644 645 long t1 = System.currentTimeMillis(); 646 647 System.out.println( "Done!" ); 648 System.out.println( nbAdd + " entries added in " + ( ( t1 - t0 ) / 1000 ) + " seconds" ); 649 } 650 else 651 { 652 // Parse the file and inject every modification 653 long t0 = System.currentTimeMillis(); 654 int nbMod = 0; 655 656 for ( LdifEntry entry:ldifReader ) 657 { 658 // Check if we have had some error, has next() does not throw any exception 659 if ( ldifReader.hasError() ) 660 { 661 System.err 662 .println( "Found an error while persing an entry : " + ldifReader.getError().getLocalizedMessage() ); 663 664 if ( ignoreErrors == false ) 665 { 666 unbind( messageId ); 667 668 System.err.println( I18n.err( I18n.ERR_208 ) ); 669 System.exit( 1 ); 670 } 671 } 672 673 if ( ( changeEntry( entry, messageId++ ) == IMPORT_ERROR ) && ( ignoreErrors == false ) ) 674 { 675 unbind( messageId ); 676 677 System.err.println( I18n.err( I18n.ERR_208 ) ); 678 System.exit( 1 ); 679 } 680 681 nbMod++; 682 683 if ( nbMod % 10 == 0 ) 684 { 685 System.out.print( '.' ); 686 } 687 688 if ( nbMod % 500 == 0 ) 689 { 690 System.out.println( nbMod ); 691 } 692 } 693 694 long t1 = System.currentTimeMillis(); 695 696 System.out.println( "Done!" ); 697 System.out.println( nbMod + " entries changed in " + ( ( t1 - t0 ) / 1000 ) + " seconds" ); 698 } 699 700 ldifReader.close(); 701 702 // Logout to the server 703 unbind( messageId++ ); 704 705 } 706 707 708 /** 709 * Read the command line and get the options : 'h' : host 'p' : port 'u' : 710 * user 'w' : password 'a' : authentication type 'i' : ignore errors 'f' : 711 * ldif file to import 712 * 713 * @param cmd 714 * The command line 715 */ 716 private void processOptions( CommandLine cmd ) 717 { 718 if ( isDebugEnabled() ) 719 { 720 System.out.println( "Processing options for launching diagnostic UI ..." ); 721 } 722 723 // ------------------------------------------------------------------- 724 // figure out the host value 725 // ------------------------------------------------------------------- 726 727 if ( cmd.hasOption( 'h' ) ) 728 { 729 host = cmd.getOptionValue( 'h' ); 730 731 if ( isDebugEnabled() ) 732 { 733 System.out.println( "ignore-errors overriden by -i option: true" ); 734 } 735 } 736 else if ( isDebugEnabled() ) 737 { 738 System.out.println( "ignore-errors set to default: false" ); 739 } 740 741 // ------------------------------------------------------------------- 742 // figure out and error check the port value 743 // ------------------------------------------------------------------- 744 745 if ( cmd.hasOption( 'p' ) ) // - user provided port w/ -p takes 746 // precedence 747 { 748 String val = cmd.getOptionValue( 'p' ); 749 750 try 751 { 752 port = Integer.parseInt( val ); 753 } 754 catch ( NumberFormatException e ) 755 { 756 System.err.println( I18n.err( I18n.ERR_193, val ) ); 757 System.exit( 1 ); 758 } 759 760 if ( port > AvailablePortFinder.MAX_PORT_NUMBER ) 761 { 762 System.err.println( I18n.err( I18n.ERR_194, val, AvailablePortFinder.MAX_PORT_NUMBER ) ); 763 System.exit( 1 ); 764 } 765 else if ( port < AvailablePortFinder.MIN_PORT_NUMBER ) 766 { 767 System.err.println( I18n.err( I18n.ERR_195, val, AvailablePortFinder.MIN_PORT_NUMBER ) ); 768 System.exit( 1 ); 769 } 770 771 if ( isDebugEnabled() ) 772 { 773 System.out.println( "port overriden by -p option: " + port ); 774 } 775 } 776 else if ( getApacheDS() != null ) 777 { 778 port = getApacheDS().getLdapServer().getPort(); 779 780 if ( isDebugEnabled() ) 781 { 782 System.out.println( "port overriden by server.xml configuration: " + port ); 783 } 784 } 785 else if ( isDebugEnabled() ) 786 { 787 System.out.println( "port set to default: " + port ); 788 } 789 790 // ------------------------------------------------------------------- 791 // figure out the user value 792 // ------------------------------------------------------------------- 793 794 if ( cmd.hasOption( 'u' ) ) 795 { 796 user = cmd.getOptionValue( 'u' ); 797 798 if ( isDebugEnabled() ) 799 { 800 System.out.println( "user overriden by -u option: " + user ); 801 } 802 } 803 else if ( isDebugEnabled() ) 804 { 805 System.out.println( "user set to default: " + user ); 806 } 807 808 // ------------------------------------------------------------------- 809 // figure out the password value 810 // ------------------------------------------------------------------- 811 812 if ( cmd.hasOption( 'w' ) ) 813 { 814 password = cmd.getOptionValue( 'w' ); 815 816 if ( isDebugEnabled() ) 817 { 818 System.out.println( "password overriden by -w option: " + password ); 819 } 820 } 821 else if ( isDebugEnabled() ) 822 { 823 System.out.println( "password set to default: " + password ); 824 } 825 826 // ------------------------------------------------------------------- 827 // figure out the authentication type 828 // ------------------------------------------------------------------- 829 830 if ( cmd.hasOption( 'a' ) ) 831 { 832 auth = cmd.getOptionValue( 'a' ); 833 834 if ( isDebugEnabled() ) 835 { 836 System.out.println( "authentication type overriden by -a option: " + auth ); 837 } 838 } 839 else if ( isDebugEnabled() ) 840 { 841 System.out.println( "authentication type set to default: " + auth ); 842 } 843 844 // ------------------------------------------------------------------- 845 // figure out the 'ignore-errors' flag 846 // ------------------------------------------------------------------- 847 848 if ( cmd.hasOption( 'e' ) ) 849 { 850 ignoreErrors = true; 851 852 if ( isDebugEnabled() ) 853 { 854 System.out.println( "authentication type overriden by -a option: " + auth ); 855 } 856 } 857 else if ( isDebugEnabled() ) 858 { 859 System.out.println( "authentication type set to default: " + auth ); 860 } 861 862 // ------------------------------------------------------------------- 863 // figure out the ldif file to import 864 // ------------------------------------------------------------------- 865 866 if ( cmd.hasOption( 'f' ) ) 867 { 868 String ldifFileName = cmd.getOptionValue( 'f' ); 869 870 ldifFile = new File( ldifFileName ); 871 872 if ( ldifFile.exists() == false ) 873 { 874 System.err.println( I18n.err( I18n.ERR_209, ldifFileName ) ); 875 System.exit( 1 ); 876 } 877 878 if ( ldifFile.canRead() == false ) 879 { 880 System.err.println( I18n.err( I18n.ERR_210, ldifFileName ) ); 881 System.exit( 1 ); 882 } 883 884 if ( isDebugEnabled() ) 885 { 886 try 887 { 888 System.out.println( "ldif file to import: " + ldifFile.getCanonicalPath() ); 889 } 890 catch ( IOException ioe ) 891 { 892 System.out.println( "ldif file to import: " + ldifFileName ); 893 } 894 } 895 } 896 else 897 { 898 System.err.println( I18n.err( I18n.ERR_211 ) ); 899 System.exit( 1 ); 900 } 901 } 902 903 904 public Options getOptions() 905 { 906 Options opts = new Options(); 907 Option op = new Option( "h", "host", true, "server host: defaults to localhost" ); 908 op.setRequired( false ); 909 opts.addOption( op ); 910 op = new Option( "p", "port", true, "server port: defaults to 10389 or server.xml specified port" ); 911 op.setRequired( false ); 912 opts.addOption( op ); 913 op = new Option( "u", "user", true, "the user: default to uid=admin, ou=system" ); 914 op.setRequired( false ); 915 opts.addOption( op ); 916 op = new Option( "w", "password", true, "the apacheds administrator's password: defaults to secret" ); 917 op.setRequired( false ); 918 opts.addOption( op ); 919 op = new Option( "a", "auth", true, "the authentication mode: defaults to 'simple'" ); 920 op.setRequired( false ); 921 opts.addOption( op ); 922 op = new Option( "f", "file", true, "the ldif file to import" ); 923 op.setRequired( true ); 924 opts.addOption( op ); 925 op = new Option( "e", "ignore", false, "continue to process the file even if errors are encountered " ); 926 op.setRequired( false ); 927 opts.addOption( op ); 928 929 return opts; 930 } 931 }