/plugins/FTP/tags/release-0-7-2/ftp/FtpPlugin.java

# · Java · 168 lines · 118 code · 19 blank · 31 comment · 20 complexity · 0a408dc9e92fdd7c9d19e91b87c8be99 MD5 · raw file

  1. /*
  2. * FtpPlugin.java - Main class of FTP plugin
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2003 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package ftp;
  23. //{{{ Imports
  24. import java.awt.Component;
  25. import java.io.*;
  26. import java.util.Hashtable;
  27. import java.util.Vector;
  28. import org.gjt.sp.jedit.*;
  29. import org.gjt.sp.jedit.browser.VFSBrowser;
  30. import org.gjt.sp.jedit.io.*;
  31. import org.gjt.sp.jedit.msg.*;
  32. import org.gjt.sp.util.Log;
  33. // SSHTools uses log4j
  34. import org.apache.log4j.*;
  35. //}}}
  36. public class FtpPlugin extends EditPlugin
  37. {
  38. //{{{ stop() method
  39. public void stop()
  40. {
  41. DirectoryCache.clearAllCachedDirectories();
  42. } //}}}
  43. //{{{ showOpenFTPDialog() method
  44. public static void showOpenFTPDialog(View view, boolean secure)
  45. {
  46. if(secure && !OperatingSystem.hasJava14())
  47. {
  48. GUIUtilities.error(view,"vfs.sftp.no-java14",null);
  49. return;
  50. }
  51. String path = ((FtpVFS)VFSManager.getVFSForProtocol(
  52. secure ? "sftp" : "ftp"))
  53. .showBrowseDialog(new Object[1],view);
  54. if(path != null)
  55. {
  56. String[] files = GUIUtilities.showVFSFileDialog(
  57. view,path,VFSBrowser.OPEN_DIALOG,true);
  58. if(files == null)
  59. return;
  60. Buffer buffer = null;
  61. for(int i = 0; i < files.length; i++)
  62. {
  63. Buffer _buffer = jEdit.openFile(null,files[i]);
  64. if(_buffer != null)
  65. buffer = _buffer;
  66. }
  67. if(buffer != null)
  68. view.setBuffer(buffer);
  69. }
  70. } //}}}
  71. //{{{ showSaveFTPDialog() method
  72. public static void showSaveFTPDialog(View view, boolean secure)
  73. {
  74. if(secure && !OperatingSystem.hasJava14())
  75. {
  76. GUIUtilities.error(view,"vfs.sftp.no-java14",null);
  77. return;
  78. }
  79. String path = ((FtpVFS)VFSManager.getVFSForProtocol(
  80. secure ? "sftp" : "ftp"))
  81. .showBrowseDialog(new Object[1],view);
  82. if(path != null)
  83. {
  84. String[] files = GUIUtilities.showVFSFileDialog(
  85. view,path,VFSBrowser.SAVE_DIALOG,false);
  86. if(files == null)
  87. return;
  88. view.getBuffer().save(view,files[0],true);
  89. }
  90. } //}}}
  91. //{{{ initSshtoolsHome() method
  92. public static void initSshtoolsHome()
  93. {
  94. String path = MiscUtilities.constructPath(
  95. jEdit.getSettingsDirectory(),"sshtools");
  96. System.getProperties().put("sshtools.home",path);
  97. String[] files = new String[] {
  98. "authorization.xml",
  99. "automation.xml",
  100. "sshtools.xml"
  101. };
  102. File dir = new File(path,"conf");
  103. dir.mkdirs();
  104. try
  105. {
  106. for(int i = 0; i < files.length; i++)
  107. {
  108. File file = new File(dir,files[i]);
  109. if(!file.exists())
  110. {
  111. copy(FtpPlugin.class.getResourceAsStream(
  112. "/conf/" + files[i]),
  113. new FileOutputStream(
  114. file));
  115. }
  116. }
  117. RollingFileAppender log = new RollingFileAppender(
  118. new PatternLayout(),
  119. MiscUtilities.constructPath(path,"ssh.log"),
  120. true);
  121. log.setMaxFileSize("100KB");
  122. BasicConfigurator.configure(log);
  123. }
  124. catch(IOException io)
  125. {
  126. Log.log(Log.ERROR,FtpPlugin.class,io);
  127. }
  128. } //}}}
  129. //{{{ Private members
  130. //{{{ copy() method
  131. private static void copy(InputStream in, OutputStream out) throws IOException
  132. {
  133. try
  134. {
  135. byte[] buf = new byte[4096];
  136. int count;
  137. while((count = in.read(buf,0,buf.length)) != -1)
  138. {
  139. out.write(buf,0,count);
  140. }
  141. }
  142. finally
  143. {
  144. in.close();
  145. out.close();
  146. }
  147. } //}}}
  148. //}}}
  149. }