KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Netrc.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: Netrc.java,v 1.2 2003/06/29 00:19:34 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 java.io.InputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Vector JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 public final class Netrc
30 {
31     private static Vector JavaDoc logins;
32     private static long lastModified;
33
34     public static Login getLogin(String JavaDoc host)
35     {
36         if (host == null)
37             return null;
38         parseNetrc();
39         if (logins == null)
40             return null;
41         final int limit = logins.size();
42         for (int i = 0; i < limit; i++) {
43             Login login = (Login) logins.get(i);
44             if (host.equals(login.host))
45                 return login;
46         }
47         return null;
48     }
49
50     public static String JavaDoc getPassword(String JavaDoc host, String JavaDoc user)
51     {
52         if (host == null)
53             return null;
54         parseNetrc();
55         if (logins == null)
56             return null;
57         final int limit = logins.size();
58         for (int i = 0; i < limit; i++) {
59             Login login = (Login) logins.get(i);
60             if (host.equals(login.host)) {
61                 if (user == null || user.equals(login.user))
62                     return login.password;
63             }
64         }
65         return null;
66     }
67
68     // Returns Vector of Login objects.
69
private static void parseNetrc()
70     {
71         File file = File.getInstance(Directories.getUserHomeDirectory(), ".netrc");
72         if (!file.isFile() || !file.canRead()) {
73             logins = null; // Nuke old cache, if any.
74
return;
75         }
76         // File exists and is readable.
77
if (logins != null) {
78             // We have a cache.
79
if (file.lastModified() == lastModified)
80                 return; // File hasn't changed.
81
// Cache is stale.
82
logins = null;
83         }
84         try {
85             lastModified = file.lastModified();
86             int length = (int) file.length();
87             byte bytes[] = new byte[length];
88             InputStream JavaDoc in = file.getInputStream();
89             if (in.read(bytes) != length)
90                 return;
91             in.close();
92             String JavaDoc s = new String JavaDoc(bytes);
93             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s);
94             String JavaDoc host = null;
95             String JavaDoc user = null;
96             String JavaDoc password = null;
97             logins = new Vector JavaDoc();
98             while (st.hasMoreTokens()) {
99                 String JavaDoc token = st.nextToken();
100                 if (token.equals("machine")) {
101                     // Add current entry to vector.
102
if (host != null && user != null && password != null)
103                         logins.add(new Login(host, user, password));
104                     // Start new entry.
105
host = st.nextToken();
106                     user = null;
107                     password = null;
108                 } else if (token.equals("login")) {
109                     user = st.nextToken();
110                 } else if (token.equals("password"))
111                     password = st.nextToken();
112             }
113             // Add final entry to vector.
114
if (host != null && user != null && password != null)
115                 logins.add(new Login(host, user, password));
116         }
117         catch (IOException JavaDoc e) {
118             Log.error(e);
119         }
120         if (logins.size() == 0)
121             logins = null;
122     }
123 }
124
Popular Tags