KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > SshFile


1 /*
2  * SshFile.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: SshFile.java,v 1.8 2002/12/07 19:27:03 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import gnu.regexp.RE;
25 import gnu.regexp.REException;
26 import gnu.regexp.REMatch;
27
28 public final class SshFile extends File
29 {
30     public static final int DEFAULT_PORT = 22;
31
32     private SshFile()
33     {
34         isRemote = true;
35         protocol = PROTOCOL_SSH;
36         port = DEFAULT_PORT;
37     }
38
39     public SshFile(String JavaDoc hostName, String JavaDoc path, String JavaDoc userName,
40         String JavaDoc password, int port)
41     {
42         isRemote = true;
43         protocol = PROTOCOL_SSH;
44         this.hostName = hostName;
45         this.canonicalPath = path;
46         this.userName = userName;
47         this.password = password;
48         this.port = port;
49     }
50
51     public static SshFile getSshFile(String JavaDoc name)
52     {
53         SshFile file = new SshFile();
54         if (file.initRemote(name, PREFIX_SSH))
55             return file;
56         return null;
57     }
58
59     public static SshFile getSshFile(SshFile directory, String JavaDoc name)
60     {
61         SshFile file = new SshFile();
62
63         file.hostName = directory.hostName;
64         file.userName = directory.userName;
65         file.password = directory.password;
66         file.port = directory.port;
67
68         if (Utilities.isFilenameAbsolute(name))
69             file.canonicalPath = canonicalize(name, "/");
70         else
71             file.canonicalPath = canonicalize(appendNameToPath(directory.canonicalPath(), name, '/'), "/");
72
73         return file;
74     }
75
76     public final File getRoot()
77     {
78         SshFile file = new SshFile();
79         file.hostName = this.hostName;
80         file.userName = this.userName;
81         file.password = this.password;
82         file.port = this.port;
83         file.canonicalPath = "/";
84         file.type = TYPE_DIRECTORY;
85         return file;
86     }
87
88     public String JavaDoc netPath()
89     {
90         FastStringBuffer sb = new FastStringBuffer(256);
91         sb.append(PREFIX_SSH);
92         if (userName != null) {
93             sb.append(userName);
94             sb.append('@');
95         }
96         sb.append(hostName);
97         if (port != DEFAULT_PORT) {
98             sb.append(':');
99             sb.append(port);
100         }
101         if (canonicalPath != null)
102             sb.append(canonicalPath);
103         return sb.toString();
104     }
105
106     public File getParentFile()
107     {
108         if (canonicalPath() == null || canonicalPath.equals("/"))
109             return null; // No parent.
110
int index = canonicalPath.lastIndexOf('/');
111         if (index < 0)
112             return null; // No parent.
113
if (index == 0) // "/usr"
114
return new SshFile(hostName, "/", userName, password, port);
115         return new SshFile(hostName, canonicalPath.substring(0, index),
116             userName, password, port);
117     }
118
119     public boolean isDirectory()
120     {
121         if (type == TYPE_LINK) {
122             if (DirectoryCache.getDirectoryCache().getListing(this) != null)
123                 return true;
124             SshSession session = SshSession.getSession(this);
125             if (session != null) {
126                 Debug.assertTrue(session.isLocked());
127                 boolean result = session.isDirectory(canonicalPath());
128                 session.unlock();
129                 return result;
130             } else
131                 return false;
132         }
133         if (type == TYPE_UNKNOWN) {
134             if (DirectoryCache.getDirectoryCache().getListing(this) != null)
135                 return true;
136             SshFile parent = (SshFile) getParentFile();
137             if (parent != null) {
138                 String JavaDoc listing = parent.getDirectoryListing();
139                 if (listing != null) {
140                     try {
141                         String JavaDoc pattern =
142                             "\\n[d\\-][^\\n]+ " + getName() + "\\n";
143                         RE re = new RE(pattern);
144                         REMatch match = re.getMatch(listing);
145                         if (match != null) {
146                             String JavaDoc s = match.toString();
147                             if (s.length() > 1) {
148                                 char c = s.charAt(1);
149                                 if (c == 'd')
150                                     return true;
151                                 if (c == '-')
152                                     return false;
153                                 // Otherwise fall through...
154
}
155                         }
156                     }
157                     catch (REException e) {
158                         Log.error(e);
159                     }
160                 }
161             }
162             SshSession session = SshSession.getSession(this);
163             if (session != null) {
164                 Debug.assertTrue(session.isLocked());
165                 if (session.isDirectory(canonicalPath()))
166                     type = TYPE_DIRECTORY;
167                 session.unlock();
168             }
169         }
170         return type == TYPE_DIRECTORY;
171     }
172
173     public boolean isLink()
174     {
175         return type == TYPE_LINK;
176     }
177
178     public boolean exists()
179     {
180         SshSession session = SshSession.getSession(this);
181         if (session == null)
182             return false;
183         Debug.assertTrue(session.isLocked());
184         boolean result = session.exists(canonicalPath());
185         session.unlock();
186         return result;
187     }
188
189     public String JavaDoc getDirectoryListing()
190     {
191         return getDirectoryListing(false);
192     }
193
194     public String JavaDoc getDirectoryListing(boolean forceRefresh)
195     {
196         if (!forceRefresh) {
197             String JavaDoc listing =
198                 DirectoryCache.getDirectoryCache().getListing(this);
199             if (listing != null)
200                 return listing;
201         }
202         SshSession session = SshSession.getSession(this);
203         if (session == null)
204             return null;
205         if (!session.isLocked()) {
206             Debug.bug();
207             return null;
208         }
209         String JavaDoc listing = session.retrieveDirectoryListing(this);
210         session.unlock();
211         DirectoryCache.getDirectoryCache().put(this, listing);
212         return listing;
213     }
214
215     public boolean equals(Object JavaDoc obj)
216     {
217         if (!(obj instanceof SshFile))
218             return false;
219         SshFile f = (SshFile) obj;
220         // Protocol.
221
if (f.protocol != protocol)
222             return false;
223         // Port.
224
if (f.port != port)
225             return false;
226         // Host name.
227
if (f.hostName == null) {
228             if (hostName != null)
229                 return false;
230         } else if (!f.hostName.equals(hostName))
231             return false;
232         // Handle pathological corner case where both canonical paths are null.
233
if (f.canonicalPath() == canonicalPath())
234             return true;
235         // At this point the canonical paths are cached for both files.
236
// If either one is null, they can't be equal.
237
if (f.canonicalPath == null || canonicalPath == null)
238             return false;
239         return f.canonicalPath.equals(canonicalPath);
240     }
241
242     public final String JavaDoc getSeparator()
243     {
244         return "/";
245     }
246
247     public final char getSeparatorChar()
248     {
249         return '/';
250     }
251 }
252
Popular Tags