/plugins/SVNPlugin/tags/0.10.0/src/ise/plugin/svn/command/Lock.java

# · Java · 121 lines · 71 code · 14 blank · 36 comment · 14 complexity · 29638c5c841c1da4080965880b3c5f8e MD5 · raw file

  1. /*
  2. Copyright (c) 2007, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the author nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package ise.plugin.svn.command;
  26. import java.io.*;
  27. import java.util.*;
  28. import org.tmatesoft.svn.core.wc.ISVNOptions;
  29. import org.tmatesoft.svn.core.wc.SVNClientManager;
  30. import org.tmatesoft.svn.cli.command.SVNCommandEventProcessor;
  31. import org.tmatesoft.svn.core.SVNException;
  32. import org.tmatesoft.svn.core.SVNURL;
  33. import org.tmatesoft.svn.core.wc.SVNWCClient;
  34. import org.tmatesoft.svn.core.wc.SVNWCUtil;
  35. import ise.plugin.svn.data.*;
  36. /**
  37. * Lock and unlock commands.
  38. */
  39. public class Lock {
  40. public LockResults lock( CommitData data ) throws CommandInitializationException, SVNException {
  41. return doLock( data, true );
  42. }
  43. public LockResults unlock( CommitData data ) throws CommandInitializationException, SVNException {
  44. return doLock( data, false );
  45. }
  46. private LockResults doLock( CommitData data, boolean lock ) throws CommandInitializationException, SVNException {
  47. SVNKit.setupLibrary();
  48. // validate data values
  49. if ( data.getPaths() == null ) {
  50. return null; // nothing to do
  51. }
  52. if ( data.getOut() == null ) {
  53. throw new CommandInitializationException( "Invalid output stream." );
  54. }
  55. if ( data.getErr() == null ) {
  56. data.setErr( data.getOut() );
  57. }
  58. // use default svn config options
  59. ISVNOptions options = SVNWCUtil.createDefaultOptions( true );
  60. // use the svnkit client manager
  61. SVNClientManager clientManager = SVNClientManager.newInstance( options, data.getUsername(), data.getPassword() );
  62. // get a working copy client
  63. SVNWCClient client = clientManager.getWCClient();
  64. // set an event handler so that messages go to the output streams for display
  65. client.setEventHandler( new SVNCommandEventProcessor( data.getOut(), data.getErr(), false ) );
  66. // actually do the lock
  67. PrintStream out = data.getOut();
  68. LockResults results = new LockResults();
  69. try {
  70. if ( data.pathsAreURLs() ) {
  71. SVNURL[] urls = new SVNURL[ data.getPaths().size() ];
  72. for ( int i = 0; i < data.getPaths().size(); i++ ) {
  73. urls[ i ] = SVNURL.parseURIDecoded( data.getPaths().get( i ) );
  74. }
  75. if ( lock ) {
  76. client.doLock( urls, data.getForce(), data.getCommitMessage() );
  77. }
  78. else {
  79. client.doUnlock( urls, data.getForce() );
  80. }
  81. }
  82. else {
  83. File[] files = new File[ data.getPaths().size() ];
  84. for ( int i = 0; i < data.getPaths().size(); i++ ) {
  85. files[ i ] = new File( data.getPaths().get( i ) );
  86. }
  87. if ( lock ) {
  88. client.doLock( files, data.getForce(), data.getCommitMessage() );
  89. }
  90. else {
  91. client.doUnlock( files, data.getForce() );
  92. }
  93. }
  94. results.addPaths( data.getPaths() );
  95. }
  96. catch ( Exception e ) {
  97. out.println( e.getMessage() );
  98. results.addErrorPath( "?", e.getMessage() );
  99. }
  100. out.flush();
  101. out.close();
  102. return results;
  103. }
  104. }