KickJava   Java API By Example, From Geeks To Geeks.

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


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@reddot.de
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 package com.izforge.izpack.util.os.unix;
22
23 import com.izforge.izpack.util.StringTool;
24
25 import java.io.File JavaDoc;
26
27 import java.util.ArrayList JavaDoc;
28
29 /**
30  * Unix Users Collection Class and related static Helper Methods
31  *
32  * @author marc.eppelmann@reddot.de
33  */

34 public class UnixUsers extends ArrayList JavaDoc
35 {
36
37     // ~ Static fields/initializers *********************************************************
38

39     /** serialVersionUID = -4804842346742194981L; */
40     private static final long serialVersionUID = -4804842346742194981L;
41
42     // ~ Constructors ***********************************************************************
43

44     /**
45      * Creates a new UnixUsers object.
46      */

47     public UnixUsers()
48     {
49         fromUsersArrayList(getEtcPasswdUsersAsArrayList());
50         fromUsersArrayList(getYpPasswdUsersAsArrayList());
51     }
52
53     // ~ Methods ****************************************************************************
54
/**
55      * Gets all known users with valid shells
56      *
57      * @return an UnixUsers arraylist of these users
58      */

59     public ArrayList JavaDoc getUsersWithValidShells()
60     {
61         ArrayList JavaDoc result = new ArrayList JavaDoc();
62
63         for (int idx = 0; idx < size(); idx++)
64         {
65             UnixUser user = (UnixUser) get(idx);
66
67             if ((user.getShell() != null) && user.getShell().trim().endsWith("sh"))
68             {
69                 result.add(user);
70             }
71         }
72
73         return result;
74     }
75
76     /**
77      * Gets all known users with valid shells and really existing (not dummy) Homefolders.
78      *
79      * @return an UnixUsers Arraylist of these users
80      */

81     public ArrayList JavaDoc getUsersWithValidShellsAndExistingHomes()
82     {
83         ArrayList JavaDoc result = new ArrayList JavaDoc();
84
85         ArrayList JavaDoc usersWithValidShells = getUsersWithValidShells();
86
87         for (int idx = 0; idx < usersWithValidShells.size(); idx++)
88         {
89             UnixUser user = (UnixUser) usersWithValidShells.get(idx);
90
91             if ((user.getHome() != null) && new File JavaDoc(user.getHome().trim()).exists())
92             {
93                 result.add(user);
94             }
95         }
96
97         return result;
98     }
99
100     /**
101      * Gets all known users with valid shells and really existing (not dummy) Home And!
102      * freedesktop.org/RFC-based "Desktop" folders.
103      *
104      * @return an UnixUsers Arraylist of these users
105      */

106     public ArrayList JavaDoc _getUsersWithValidShellsExistingHomesAndDesktops()
107     {
108         ArrayList JavaDoc result = new ArrayList JavaDoc();
109
110         ArrayList JavaDoc usersWithValidShellsAndExistingHomes = getUsersWithValidShellsAndExistingHomes();
111
112         for (int idx = 0; idx < usersWithValidShellsAndExistingHomes.size(); idx++)
113         {
114             UnixUser user = (UnixUser) usersWithValidShellsAndExistingHomes.get(idx);
115
116             if ((user.getHome() != null)
117                     && new File JavaDoc(user.getHome().trim() + File.separator + "Desktop").exists())
118             {
119                 result.add(user);
120             }
121         }
122
123         return result;
124     }
125
126     /**
127      * An StringArray of the existing Desktop folders of all valid users.
128      *
129      * @return the Stringlist of ValidUsersDesktopFolders
130      */

131     public ArrayList JavaDoc getValidUsersDesktopFolders()
132     {
133         ArrayList JavaDoc result = new ArrayList JavaDoc();
134
135         ArrayList JavaDoc validUserDesktops = getUsersWithValidShellsExistingHomesAndDesktops();
136
137         for (int idx = 0; idx < validUserDesktops.size(); idx++)
138         {
139             UnixUser user = (UnixUser) validUserDesktops.get(idx);
140             new File JavaDoc(user.getHome().trim() + File.separator + "Desktop");
141
142             if (user.getHome() != null)
143             {
144                 File JavaDoc DesktopFolder = new File JavaDoc(user.getHome().trim() + File.separator + "Desktop");
145
146                 if (DesktopFolder.exists() && DesktopFolder.isDirectory())
147                 {
148                     result.add(DesktopFolder.toString());
149                 }
150             }
151         }
152
153         return result;
154     }
155
156     /**
157      * Gets all known users with valid shells and really existing (not dummy) Home And!
158      * freedesktop.org/RFC-based "Desktop" folders.
159      *
160      * @return an UnixUsers Arraylist of these users
161      */

162     public static ArrayList JavaDoc getUsersWithValidShellsExistingHomesAndDesktops()
163     {
164         UnixUsers users = new UnixUsers();
165
166         return users._getUsersWithValidShellsExistingHomesAndDesktops();
167     }
168
169     /**
170      * Builds the internal Array from the given UsersArrayList
171      *
172      * @param anUsersArrayList an Users ArrayList reded from /etc/passwd
173      */

174     private void fromUsersArrayList(ArrayList JavaDoc anUsersArrayList)
175     {
176         for (int idx = 0; idx < anUsersArrayList.size(); idx++)
177         {
178             add(new UnixUser().fromEtcPasswdLine((String JavaDoc) anUsersArrayList.get(idx)));
179         }
180     }
181
182     /**
183      * Gets all Users from /etc/passwd as StringList
184      *
185      * @return the UserNames extracted from the getEtcPasswdArray()
186      */

187     public static ArrayList JavaDoc getEtcPasswdUsersAsArrayList()
188     {
189         ArrayList JavaDoc result = new ArrayList JavaDoc();
190         ArrayList JavaDoc etcPasswdArray = UnixHelper.getEtcPasswdArray();
191
192         for (int idx = 0; idx < etcPasswdArray.size(); idx++)
193         {
194             String JavaDoc line = (String JavaDoc) etcPasswdArray.get(idx);
195             result.add(line);
196         }
197
198         return result;
199     }
200
201     /**
202      * Gets all Users from /etc/passwd as StringList
203      *
204      * @return the UserNames extracted from the getEtcPasswdArray()
205      */

206     public static ArrayList JavaDoc getYpPasswdUsersAsArrayList()
207     {
208         return UnixHelper.getYpPasswdArray();
209     }
210
211     /**
212      * Returns all Users as ColonSeparated String
213      *
214      * @return "asterisk:at:avahi:beagleindex:bin:daemon:dhcpd:ftp:games:gdm:haldaemon:icecream:irc:ldap:lp:mail:mailman:man:...."
215      */

216     public static String JavaDoc getUsersColonString()
217     {
218         ArrayList JavaDoc usersArrayList = getEtcPasswdUsersAsArrayList();
219
220         String JavaDoc retValue = "";
221
222         for (int user = 0; user < usersArrayList.size(); user++)
223         {
224             String JavaDoc userline = (String JavaDoc) usersArrayList.get(user);
225             retValue += (userline.substring(0, userline.indexOf(":")) + ":");
226         }
227
228         if (retValue.endsWith(":"))
229         {
230             retValue = retValue.substring(0, retValue.length() - 1);
231         }
232
233         return retValue;
234     }
235
236     /**
237      * Test main Method
238      *
239      * @param args from Commandline
240      */

241     public static void main(String JavaDoc[] args)
242     {
243         System.out.println("UnixUsers:");
244
245         UnixUsers users = new UnixUsers();
246
247         // users.fromUsersArrayList();
248
for (int idx = 0; idx < users.size(); idx++)
249         {
250             System.out.println(((UnixUser) users.get(idx)).getName());
251         }
252
253         System.out.println(StringTool
254                 .stringArrayListToString(getUsersWithValidShellsExistingHomesAndDesktops()));
255
256         // getUsersWithValidShellsAndExistingHomes();
257
}
258 }
259
Popular Tags