KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > izforge > izpack > util > os > unix > UnixUser


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2006 Marc Eppelmann
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.izforge.izpack.util.os.unix;
23
24 import java.util.StringTokenizer JavaDoc;
25
26
27 /**
28  * This represents a Unix User. If initialized via fromEtcPasswdLine(), the users
29  * Name, home, uid, gid, and shell can be asked.
30  *
31  * @author marc.eppelmann@reddot.de
32  */

33 public class UnixUser
34 {
35   //~ Instance fields ********************************************************************
36

37   /** internal itsName */
38   private String JavaDoc itsName;
39
40   /** internal itsPasswdDigest */
41   private String JavaDoc itsPasswdDigest;
42
43   /** internal itsId */
44   private String JavaDoc itsId;
45
46   /** internal itsGid */
47   private String JavaDoc itsGid;
48
49   /** internal itsDescription */
50   private String JavaDoc itsDescription;
51
52   /** internal itsHome */
53   private String JavaDoc itsHome;
54
55   /** internal itsShell */
56   private String JavaDoc itsShell;
57
58   //~ Methods ****************************************************************************
59

60   /**
61    * Gets the Users Login Name
62    *
63    * @return the users login Name
64    */

65   public String JavaDoc getName( )
66   {
67     return itsName;
68   }
69
70   /**
71    * Gets the users passwd Digest or X if hidden in /etc/shadow
72    *
73    * @return the passwdDigest or x
74    */

75   public String JavaDoc getPasswdDigest( )
76   {
77     return itsPasswdDigest;
78   }
79
80   /**
81    * Gets the Users UID
82    *
83    * @return The Uid
84    */

85   public String JavaDoc getId( )
86   {
87     return itsId;
88   }
89
90   /**
91    * Gtes the Users Group ID
92    *
93    * @return the gid
94    */

95   public String JavaDoc getGid( )
96   {
97     return itsGid;
98   }
99
100   /**
101    * Gets the Description aka Full Name
102    *
103    * @return the users descriptio or full name
104    */

105   public String JavaDoc getDescription( )
106   {
107     return itsDescription;
108   }
109
110   /**
111    * Gets the Users Home Directory
112    *
113    * @return the users home dir
114    */

115   public String JavaDoc getHome( )
116   {
117     return itsHome;
118   }
119
120   /**
121    * Gets the users default Login-Shell
122    *
123    * @return The login shell or /bin/false for system users
124    */

125   public String JavaDoc getShell( )
126   {
127     return itsShell;
128   }
129
130   /**
131    * Parses a Line from /etc/passwd and stores each :token: in their field of the user.
132    * Sample Line from /etc/passwd "eppelmann.local:x:900:100:Marc Eppelmann:/mnt/local/home/eppelmann.local:/bin/bash"
133    * @param anEtcPasswdLine A Passwd Line of the User.
134    *
135    * @return The filled User
136    */

137   public UnixUser fromEtcPasswdLine( String JavaDoc anEtcPasswdLine )
138   {
139     if( anEtcPasswdLine == null )
140     {
141       return null;
142     }
143
144     StringTokenizer JavaDoc usersToken = new StringTokenizer JavaDoc( anEtcPasswdLine, ":" );
145
146     UnixUser u = new UnixUser( );
147
148     if( usersToken.hasMoreTokens( ) )
149     {
150       u.itsName = usersToken.nextToken( );
151     }
152
153     if( usersToken.hasMoreTokens( ) )
154     {
155       u.itsPasswdDigest = usersToken.nextToken( );
156     }
157
158     if( usersToken.hasMoreTokens( ) )
159     {
160       u.itsId = usersToken.nextToken( );
161     }
162
163     if( usersToken.hasMoreTokens( ) )
164     {
165       u.itsGid = usersToken.nextToken( );
166     }
167
168     if( usersToken.hasMoreTokens( ) )
169     {
170       u.itsDescription = usersToken.nextToken( );
171     }
172
173     if( usersToken.hasMoreTokens( ) )
174     {
175       u.itsHome = usersToken.nextToken( );
176     }
177
178     if( usersToken.hasMoreTokens( ) )
179     {
180       u.itsShell = usersToken.nextToken( );
181     }
182
183     return u;
184   }
185
186   /**
187    * Dumps the USer fields
188    *
189    * @return The User representation as String
190    */

191   public String JavaDoc toString( )
192   {
193     StringBuffer JavaDoc result = new StringBuffer JavaDoc( );
194
195     result.append( "User: " );
196     result.append( itsName );
197
198     result.append( " X: " );
199     result.append( itsPasswdDigest );
200
201     result.append( " Id: " );
202     result.append( itsId );
203
204     result.append( " Gid: " );
205     result.append( itsGid );
206
207     result.append( " Desc.: " );
208     result.append( itsDescription );
209
210     result.append( " Home: " );
211     result.append( itsHome );
212
213     result.append( " Shell: " );
214     result.append( itsShell );
215
216     return result.toString( );
217   }
218
219   /**
220    * Static Test Main
221    *
222    * @param args
223    */

224   public static void main( String JavaDoc[] args )
225   {
226     System.out.println( new UnixUser( ).fromEtcPasswdLine( "" ) );
227     System.out.println( new UnixUser( ).fromEtcPasswdLine( "eppelmann.local:x:500:100:Marc L Eppelmann:/mnt/local/home/eppelmann.local:/bin/bash" ) );
228   }
229 }
230
Popular Tags