001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.auth.module;
019:
020: /**
021: * A helper class which queries an information about the current user.
022: */
023: public class UnixSystem {
024:
025: // Shows whether the hyauth library was loaded already
026: private static boolean loadLibDone;
027:
028: /**
029: * User's main group's ID
030: */
031: protected long gid;
032:
033: /**
034: * User's main group's name
035: */
036: protected String groupname;
037:
038: /**
039: * List of group IDs the user belongs to
040: */
041: protected long[] groups;
042:
043: /**
044: * List of group names the user belongs to
045: */
046: protected String[] groupsNames;
047:
048: /**
049: * User ID
050: */
051: protected long uid;
052:
053: /**
054: * User name
055: */
056: protected String username;
057:
058: /**
059: * Sole constructor.
060: * @throws UnsatisfiedLinkError if hyauth library could not be loaded
061: */
062: public UnixSystem() {
063: if (!loadLibDone) {
064: System.loadLibrary("hyauth"); //$NON-NLS-1$
065: loadLibDone = true;
066: }
067: load();
068: }
069:
070: /**
071: * returns user's main group's id
072: */
073: public long getGid() {
074: return gid;
075: }
076:
077: /**
078: * returns user's main group's name
079: */
080: public String getGroupName() {
081: return groupname;
082: }
083:
084: /**
085: * returns list of ids of groups the user belongs to
086: */
087: public long[] getGroups() {
088: if (groups == null) {
089: return new long[0];
090: }
091: long[] tmp = new long[groups.length];
092: System.arraycopy(groups, 0, tmp, 0, tmp.length);
093: return tmp;
094: }
095:
096: /**
097: * returns list of names of groups the user belongs to
098: */
099: public String[] getGroupNames() {
100: if (groupsNames == null) {
101: return new String[0];
102: }
103: String[] tmp = new String[groupsNames.length];
104: System.arraycopy(groupsNames, 0, tmp, 0, tmp.length);
105: return tmp;
106: }
107:
108: /**
109: * returns user's id
110: */
111: public long getUid() {
112: return uid;
113: }
114:
115: /**
116: * returns user name
117: */
118: public String getUsername() {
119: return username;
120: }
121:
122: /**
123: * The function which actually does all the work with loading info
124: */
125: public native void load();
126:
127: /**
128: * Returns string representation of this object
129: */
130: @Override
131: public String toString() {
132: StringBuilder buf = new StringBuilder();
133: buf.append("UnixSystem: \n"); //$NON-NLS-1$
134: buf.append("uid:gid=").append(uid).append(":").append(gid); //$NON-NLS-1$ //$NON-NLS-2$
135: buf.append("="); //$NON-NLS-1$
136: buf.append(username).append(":").append(groupname); //$NON-NLS-1$
137: buf.append("\n"); //$NON-NLS-1$
138:
139: buf.append("total groups: "); //$NON-NLS-1$
140: buf.append(groups == null ? 0 : groups.length);
141:
142: if (groups != null) {
143: buf.append("\n"); //$NON-NLS-1$
144: for (int i = 0; i < groups.length; i++) {
145: buf
146: .append(i)
147: .append(") ").append(groupsNames[i]).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$
148: }
149: }
150: return buf.toString();
151: }
152: }
|