Coverage details for umich.cac.util.Passwd

LineHitsSource
1 package umich.cac.util;
2  
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.util.Vector;
9 import java.util.logging.Logger;
10  
11 /**
12  * A class that reads a UNIX style password file so we can obtain the UNIX uid
13  * for a given user for PBS accounting
14  *
15  * @author rmach@umich.edu
16  * @Version $Header: /cvsroot/pbsaccounting/pbsaccounting/src/umich/cac/util/Passwd.java,v 1.2 2003/10/21 19:08:00 rodmach Exp $
17  */
18 public class Passwd {
19  
20     /** If UID not found, the default value is NOT_FOUND */
213    public long NOT_FOUND = -1;
22  
23     /** The default system password file */
243    private String passwdFile = "/etc/passwd";
25     /** The Login ID field position */
263    private int LOGIN_ID = 0;
27     /** The Passwd field posistion */
283    private int PASSWORD = 1;
29     /** The UID field position */
303    private int UID = 2;
31     /** The GID field position */
323    private int GID = 3;
33     /** The GECOS field position */
343    private int GECOS = 4;
35     /** The HOME DIR field position */
363    private int HOME_DIR = 5;
37     /** The shell field position */
383    private int SHELL = 6;
39  
40     /** Used for logging */
416    private Logger logger = Logger.getLogger(Passwd.class.getName());
42  
43     /**
44      * A vector of String[] of the tokens in a unix passwd entries
45      * [0] -> [LOGIN ID] [ PASSWORD] [UID] [GID] [GECOS] [HOME_DIR] [SHELL]
46      * [1] -> [LOGIN ID] [ PASSWORD] [UID] [GID] [GECOS] [HOME_DIR] [SHELL] etc. etc.
47      */
483    private Vector passwdVector = new Vector();
49  
50     /**
51      * Constructor for unit testing
52      *
53      * @param passwdFile
54      * an optional password file to use for lookups
55      * @throws IOException
56      * if problem reading password file
57      * @throws FileNotFoundException
58      * if doesn't exist
59      */
60     public Passwd(String passwdFile)
613        throws FileNotFoundException, IOException {
623        this.passwdFile = passwdFile;
63  
643        if (!isExist(passwdFile)) {
650            throw new FileNotFoundException("File was not found " + passwdFile);
66         }
67  
683        readFile();
693    }
70  
71     /**
72      * Constructor. By default reads "/etc/passwd" file
73      *
74      * @throws IOException
75      * if problem reading password file
76      * @throws FileNotFoundException
77      * if doesn't exist
78      *
79      */
800    public Passwd() throws FileNotFoundException, IOException {
81  
820        if (!isExist(passwdFile)) {
830            throw new FileNotFoundException("File was not found " + passwdFile);
84         }
85  
860        readFile();
87  
880    }
89  
90     /**
91      * Checks if passwd file exists
92      *
93      * @param passwd
94      * file to check for
95      * @return true if exists, false if not
96      */
97  
98     public boolean isExist(String filename) {
993        File passFile = new File(filename);
100  
1013        if (passFile.exists()) {
1023            return true;
103         } else {
1040            return false;
105         }
106  
107     }
108  
109     /**
110      * Reads the passwd file and creates the passwdVector. If the passwd file
111      * can not be read, then passwordVector is set to null
112      *
113      */
114     private void readFile() throws IOException {
115  
116         try {
1173            BufferedReader in = new BufferedReader(new FileReader(passwdFile));
118             String str;
119  
1209            while ((str = in.readLine()) != null) {
1216                String[] entries = str.split(":");
1226                passwdVector.add(entries);
123             }
1243            in.close();
1250        } catch (IOException e) {
1260            logger.throwing("readFile", "readFile", e);
1270            throw e;
1283        }
129  
1303    }
131  
132     /**
133      * Lookup the UID for a user given the loginId
134      *
135      * @param loginId
136      * The login ID (entry 0 in the passwd file)
137      * @return The UID for the corresponding loginId, or NOT_FOUND if not found
138      */
139     public long lookupUid(String loginId) {
140  
1416        for (int i = 0; i < passwdVector.size(); i++) {
1426            String[] entry = (String[]) passwdVector.get(i);
1436            if (entry[LOGIN_ID].equals(loginId)) {
1443                Long uid = new Long(entry[UID]);
1453                return uid.longValue();
146             }
147         }
148  
1490        return NOT_FOUND;
150     }
151  
152 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.