KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > startup > PasswdUserDatabase


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.catalina.startup;
20
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Enumeration JavaDoc;
27
28
29 /**
30  * Concrete implementation of the <strong>UserDatabase</code> interface
31  * that processes the <code>/etc/passwd</code> file on a Unix system.
32  *
33  * @author Craig R. McClanahan
34  * @version $Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
35  */

36
37 public final class PasswdUserDatabase
38     implements UserDatabase {
39
40
41     // --------------------------------------------------------- Constructors
42

43
44     /**
45      * Initialize a new instance of this user database component.
46      */

47     public PasswdUserDatabase() {
48
49         super();
50
51     }
52
53
54     // --------------------------------------------------- Instance Variables
55

56
57     /**
58      * The pathname of the Unix password file.
59      */

60     private static final String JavaDoc PASSWORD_FILE = "/etc/passwd";
61
62
63     /**
64      * The set of home directories for all defined users, keyed by username.
65      */

66     private Hashtable JavaDoc homes = new Hashtable JavaDoc();
67
68
69     /**
70      * The UserConfig listener with which we are associated.
71      */

72     private UserConfig userConfig = null;
73
74
75     // ----------------------------------------------------------- Properties
76

77
78     /**
79      * Return the UserConfig listener with which we are associated.
80      */

81     public UserConfig getUserConfig() {
82
83         return (this.userConfig);
84
85     }
86
87
88     /**
89      * Set the UserConfig listener with which we are associated.
90      *
91      * @param userConfig The new UserConfig listener
92      */

93     public void setUserConfig(UserConfig userConfig) {
94
95         this.userConfig = userConfig;
96         init();
97
98     }
99
100
101     // ------------------------------------------------------- Public Methods
102

103
104     /**
105      * Return an absolute pathname to the home directory for the specified user.
106      *
107      * @param user User for which a home directory should be retrieved
108      */

109     public String JavaDoc getHome(String JavaDoc user) {
110
111         return ((String JavaDoc) homes.get(user));
112
113     }
114
115
116     /**
117      * Return an enumeration of the usernames defined on this server.
118      */

119     public Enumeration JavaDoc getUsers() {
120
121         return (homes.keys());
122
123     }
124
125
126     // ------------------------------------------------------ Private Methods
127

128
129     /**
130      * Initialize our set of users and home directories.
131      */

132     private void init() {
133
134         BufferedReader JavaDoc reader = null;
135         try {
136
137             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(PASSWORD_FILE));
138
139             while (true) {
140
141                 // Accumulate the next line
142
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
143                 while (true) {
144                     int ch = reader.read();
145                     if ((ch < 0) || (ch == '\n'))
146                         break;
147                     buffer.append((char) ch);
148                 }
149                 String JavaDoc line = buffer.toString();
150                 if (line.length() < 1)
151                     break;
152
153                 // Parse the line into constituent elements
154
int n = 0;
155                 String JavaDoc tokens[] = new String JavaDoc[7];
156                 for (int i = 0; i < tokens.length; i++)
157                     tokens[i] = null;
158                 while (n < tokens.length) {
159                     String JavaDoc token = null;
160                     int colon = line.indexOf(':');
161                     if (colon >= 0) {
162                         token = line.substring(0, colon);
163                         line = line.substring(colon + 1);
164                     } else {
165                         token = line;
166                         line = "";
167                     }
168                     tokens[n++] = token;
169                 }
170
171                 // Add this user and corresponding directory
172
if ((tokens[0] != null) && (tokens[5] != null))
173                     homes.put(tokens[0], tokens[5]);
174
175             }
176
177             reader.close();
178             reader = null;
179
180         } catch (Exception JavaDoc e) {
181             if (reader != null) {
182                 try {
183                     reader.close();
184                 } catch (IOException JavaDoc f) {
185                     ;
186                 }
187                 reader = null;
188             }
189         }
190
191     }
192
193
194 }
195
Popular Tags