001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2006 Marc Eppelmann@reddot.de
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021: package com.izforge.izpack.util.os.unix;
022:
023: import com.izforge.izpack.util.StringTool;
024:
025: import java.io.File;
026:
027: import java.util.ArrayList;
028:
029: /**
030: * Unix Users Collection Class and related static Helper Methods
031: *
032: * @author marc.eppelmann@reddot.de
033: */
034: public class UnixUsers extends ArrayList {
035:
036: // ~ Static fields/initializers *********************************************************
037:
038: /** serialVersionUID = -4804842346742194981L; */
039: private static final long serialVersionUID = -4804842346742194981L;
040:
041: // ~ Constructors ***********************************************************************
042:
043: /**
044: * Creates a new UnixUsers object.
045: */
046: public UnixUsers() {
047: fromUsersArrayList(getEtcPasswdUsersAsArrayList());
048: fromUsersArrayList(getYpPasswdUsersAsArrayList());
049: }
050:
051: // ~ Methods ****************************************************************************
052: /**
053: * Gets all known users with valid shells
054: *
055: * @return an UnixUsers arraylist of these users
056: */
057: public ArrayList<UnixUser> getUsersWithValidShells() {
058: ArrayList<UnixUser> result = new ArrayList<UnixUser>();
059:
060: for (int idx = 0; idx < size(); idx++) {
061: UnixUser user = (UnixUser) get(idx);
062:
063: if ((user.getShell() != null)
064: && user.getShell().trim().endsWith("sh")) {
065: result.add(user);
066: }
067: }
068:
069: return result;
070: }
071:
072: /**
073: * Gets all known users with valid shells and really existing (not dummy) Homefolders.
074: *
075: * @return an UnixUsers Arraylist of these users
076: */
077: public ArrayList<UnixUser> getUsersWithValidShellsAndExistingHomes() {
078: ArrayList<UnixUser> result = new ArrayList<UnixUser>();
079:
080: ArrayList<UnixUser> usersWithValidShells = getUsersWithValidShells();
081:
082: for (int idx = 0; idx < usersWithValidShells.size(); idx++) {
083: UnixUser user = usersWithValidShells.get(idx);
084:
085: if ((user.getHome() != null)
086: && new File(user.getHome().trim()).exists()) {
087: result.add(user);
088: }
089: }
090:
091: return result;
092: }
093:
094: /**
095: * Gets all known users with valid shells and really existing (not dummy) Home And!
096: * freedesktop.org/RFC-based "Desktop" folders.
097: *
098: * @return an UnixUsers Arraylist of these users
099: */
100: public ArrayList _getUsersWithValidShellsExistingHomesAndDesktops() {
101: ArrayList result = new ArrayList();
102:
103: ArrayList<UnixUser> usersWithValidShellsAndExistingHomes = getUsersWithValidShellsAndExistingHomes();
104:
105: for (int idx = 0; idx < usersWithValidShellsAndExistingHomes
106: .size(); idx++) {
107: UnixUser user = usersWithValidShellsAndExistingHomes
108: .get(idx);
109:
110: if ((user.getHome() != null)
111: && new File(user.getHome().trim() + File.separator
112: + "Desktop").exists()) {
113: result.add(user);
114: }
115: }
116:
117: return result;
118: }
119:
120: /**
121: * An StringArray of the existing Desktop folders of all valid users.
122: *
123: * @return the Stringlist of ValidUsersDesktopFolders
124: */
125: public ArrayList<String> getValidUsersDesktopFolders() {
126: ArrayList<String> result = new ArrayList<String>();
127:
128: ArrayList validUserDesktops = getUsersWithValidShellsExistingHomesAndDesktops();
129:
130: for (int idx = 0; idx < validUserDesktops.size(); idx++) {
131: UnixUser user = (UnixUser) validUserDesktops.get(idx);
132: new File(user.getHome().trim() + File.separator + "Desktop");
133:
134: if (user.getHome() != null) {
135: File DesktopFolder = new File(user.getHome().trim()
136: + File.separator + "Desktop");
137:
138: if (DesktopFolder.exists()
139: && DesktopFolder.isDirectory()) {
140: result.add(DesktopFolder.toString());
141: }
142: }
143: }
144:
145: return result;
146: }
147:
148: /**
149: * Gets all known users with valid shells and really existing (not dummy) Home And!
150: * freedesktop.org/RFC-based "Desktop" folders.
151: *
152: * @return an UnixUsers Arraylist of these users
153: */
154: public static ArrayList getUsersWithValidShellsExistingHomesAndDesktops() {
155: UnixUsers users = new UnixUsers();
156:
157: return users._getUsersWithValidShellsExistingHomesAndDesktops();
158: }
159:
160: /**
161: * Builds the internal Array from the given UsersArrayList
162: *
163: * @param anUsersArrayList an Users ArrayList reded from /etc/passwd
164: */
165: private void fromUsersArrayList(ArrayList<String> anUsersArrayList) {
166: for (int idx = 0; idx < anUsersArrayList.size(); idx++) {
167: add(new UnixUser().fromEtcPasswdLine(anUsersArrayList
168: .get(idx)));
169: }
170: }
171:
172: /**
173: * Gets all Users from /etc/passwd as StringList
174: *
175: * @return the UserNames extracted from the getEtcPasswdArray()
176: */
177: public static ArrayList<String> getEtcPasswdUsersAsArrayList() {
178: ArrayList<String> result = new ArrayList<String>();
179: ArrayList<String> etcPasswdArray = UnixHelper
180: .getEtcPasswdArray();
181:
182: for (int idx = 0; idx < etcPasswdArray.size(); idx++) {
183: String line = etcPasswdArray.get(idx);
184: result.add(line);
185: }
186:
187: return result;
188: }
189:
190: /**
191: * Gets all Users from /etc/passwd as StringList
192: *
193: * @return the UserNames extracted from the getEtcPasswdArray()
194: */
195: public static ArrayList<String> getYpPasswdUsersAsArrayList() {
196: return UnixHelper.getYpPasswdArray();
197: }
198:
199: /**
200: * Returns all Users as ColonSeparated String
201: *
202: * @return "asterisk:at:avahi:beagleindex:bin:daemon:dhcpd:ftp:games:gdm:haldaemon:icecream:irc:ldap:lp:mail:mailman:man:...."
203: */
204: public static String getUsersColonString() {
205: ArrayList<String> usersArrayList = getEtcPasswdUsersAsArrayList();
206:
207: String retValue = "";
208:
209: for (int user = 0; user < usersArrayList.size(); user++) {
210: String userline = usersArrayList.get(user);
211: retValue += (userline.substring(0, userline.indexOf(":")) + ":");
212: }
213:
214: if (retValue.endsWith(":")) {
215: retValue = retValue.substring(0, retValue.length() - 1);
216: }
217:
218: return retValue;
219: }
220:
221: /**
222: * Test main Method
223: *
224: * @param args from Commandline
225: */
226: public static void main(String[] args) {
227: System.out.println("UnixUsers:");
228:
229: UnixUsers users = new UnixUsers();
230:
231: // users.fromUsersArrayList();
232: for (int idx = 0; idx < users.size(); idx++) {
233: System.out.println(((UnixUser) users.get(idx)).getName());
234: }
235:
236: System.out
237: .println(StringTool
238: .stringArrayListToString(getUsersWithValidShellsExistingHomesAndDesktops()));
239:
240: // getUsersWithValidShellsAndExistingHomes();
241: }
242: }
|