KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > groups


1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
27  * must not be used to endorse or promote products derived from this
28  * software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
33  * name, without prior written permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon software originally written
55  * by Daniel F. Savarese. We appreciate his contributions.
56  */

57
58 import java.io.*;
59 import java.util.*;
60
61 import org.apache.oro.text.*;
62 import org.apache.oro.text.regex.*;
63
64 /**
65  * This is a sample program mimicking the Unix groups command. It assumes
66  * the /etc/group file exists.
67  *
68  @author <a HREF="dfs@savarese.org">Daniel F. Savarese</a>
69  @version $Id: groups.java,v 1.1.1.1 2000/07/23 23:08:48 jon Exp $
70  */

71 public final class groups {
72
73   public static final void main(String JavaDoc[] args) {
74     int user;
75     MatchActionProcessor processor = new MatchActionProcessor();
76     final Hashtable groups = new Hashtable();
77     Vector users = new Vector();
78     Enumeration usersElements;
79     MatchAction action = new MatchAction() {
80       public void processMatch(MatchActionInfo info) {
81     // Add group name to hashtable entry
82
((Vector)groups.get(info.match.toString())).addElement(
83                        info.fields.elementAt(0));
84       }
85     };
86
87     if(args.length == 0) {
88       // No arguments assumes calling user
89
args = new String JavaDoc[1];
90       args[0] = System.getProperty("user.name");
91     }
92
93     try {
94       processor.setFieldSeparator(":");
95       for(user = 0; user < args.length; user++) {
96     // Screen out duplicates
97
if(!groups.containsKey(args[user])) {
98       groups.put(args[user], new Vector());
99       // We assume usernames contain no special characters
100
processor.addAction(args[user], action);
101       // Add username to Vector to preserve argument order when printing
102
users.addElement(args[user]);
103     }
104       }
105     } catch(MalformedPatternException e) {
106       e.printStackTrace();
107       System.exit(1);
108     }
109
110     try {
111       processor.processMatches(new FileInputStream("/etc/group"),
112                    System.out);
113     } catch(IOException e) {
114       e.printStackTrace();
115       System.exit(1);
116     }
117
118     usersElements = users.elements();
119
120     while(usersElements.hasMoreElements()) {
121       String JavaDoc username;
122       Enumeration values;
123
124       username = (String JavaDoc)usersElements.nextElement();
125       values = ((Vector)groups.get(username)).elements();
126
127       System.out.print(username + " :");
128       while(values.hasMoreElements()) {
129     System.out.print(" " + values.nextElement());
130       }
131       System.out.println();
132     }
133
134     System.out.flush();
135   }
136
137 }
138
Free Books   Free Magazines  
Popular Tags