You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/04/29 18:33:07 UTC

svn commit: r769820 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/ main/native/include/ main/native/os/unix/ test/org/apache/commons/runtime/

Author: mturk
Date: Wed Apr 29 16:33:06 2009
New Revision: 769820

URL: http://svn.apache.org/viewvc?rev=769820&view=rev
Log:
Add groupMembers method

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Group.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Group.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Group.java?rev=769820&r1=769819&r2=769820&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Group.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Group.java Wed Apr 29 16:33:06 2009
@@ -52,7 +52,8 @@
         throws IOException, SecurityException;
     private static native Group[] enum1()
         throws IOException, SecurityException, UnsupportedOperationException;
-
+    private static native User[]  enum2(Descriptor gid)
+        throws IOException, SecurityException;
 
     /**
      * Create the {@code Group} object from the {@code name}.
@@ -109,6 +110,25 @@
     }
 
     /**
+     * Get the {@link UserIterator} of all users defined that are member
+     * of {@code this} group.
+     *
+     * @return UserIterator containing all user members.
+     * @throws IOException in case of I/O error.
+     * @throws SecurityException if the current user is not allowed to
+     *         access the system group database.
+     */
+    public UserIterator getMembers()
+        throws IOException, SecurityException
+    {
+        UserIterator iter;
+        synchronized(lock) {
+            iter = new UserIteratorImpl(enum2(Id));
+        }
+        return iter;
+    }
+
+    /**
      * String that specifies the name of the group account.
      */
     public  String       getName()

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java?rev=769820&r1=769819&r2=769820&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/User.java Wed Apr 29 16:33:06 2009
@@ -91,9 +91,10 @@
     }
 
     /**
-     * Get the {@link UserIterator} of all users defined on the system.
+     * Get the {@link UserIterator} of all users that are currently
+     * logged on to the system.
      *
-     * @return UserIterator containing all users.
+     * @return UserIterator containing all logged users.
      * @throws IOException in case of I/O error.
      * @throws SecurityException if the current user is not allowed to
      *         access the system user database.

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h?rev=769820&r1=769819&r2=769820&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h Wed Apr 29 16:33:06 2009
@@ -328,6 +328,9 @@
 #define GET_IFIELD_I(I, O)  \
     _f##I##n.i ? (*_E)->GetIntField(_E, (O), _f##I##n.i) : 0
 
+#define GET_IFIELD_O(I, O)  \
+    _f##I##n.i ? (*_E)->GetObjectField(_E, (O), _f##I##n.i) : NULL
+
 #define SET_IFIELD_J(I, O, V)  \
     if (_f##I##n.i) {                                                       \
         (*_E)->SetLongField(_E, (O), _f##I##n.i, (jlong)(V));               \

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c?rev=769820&r1=769819&r2=769820&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/group.c Wed Apr 29 16:33:06 2009
@@ -164,7 +164,55 @@
     UNREFERENCED_O;
 
     /* Global groups are not supported on unixes */
-    ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENOTIMPL, 0);    
+    ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENOTIMPL, 0);
     return NULL;
 }
 
+ACR_JNI_EXPORT_DECLARE(jobjectArray, Group, enum2)(ACR_JNISTDARGS,
+                                                   jobject gdd)
+{
+    jobjectArray usrs = NULL;
+    jsize i, n;
+    int    rc;
+    gid_t  gid;
+    struct group grb;
+    struct group *gr;
+    char   buffer[4096];
+
+    UNREFERENCED_O;
+
+    gid = (gid_t)ACR_DescriptorGetInt(_E, gdd);
+    if (gid == -1) {
+        return NULL;
+    }
+
+    rc = getgrgid_r(gid, &grb, buffer, sizeof(buffer), &gr);
+    if (rc) {
+        if (ACR_STATUS_IS_ENOENT(ACR_FROM_OS_ERROR(rc)))
+            return NULL;
+        else if  (ACR_STATUS_IS_EACCES(ACR_FROM_OS_ERROR(rc)))
+            ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ESECURITY, 0);
+        else
+            ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EIO,
+                                ACR_FROM_OS_ERROR(rc));
+    }
+    if (!gr || !gr->gr_mem)
+        return NULL;
+    for (n = 0; gr->gr_mem[n]; n++)
+        ;
+    if (n)
+        usrs = ACR_NewUserArray(_E, n);
+    if (!usrs) {
+        return NULL;
+    }
+    for (i = 0; i < n; i++) {
+        jobject uid = ACR_UserObjectCreate(_E, gr->gr_mem[i]);
+        if ((*_E)->ExceptionCheck(_E) || uid == NULL) {
+            /* Object creation failed */
+            break;
+        }
+        (*_E)->SetObjectArrayElement(_E, usrs, i, uid);
+        (*_E)->DeleteLocalRef(_E, uid);
+    }
+    return usrs;
+}

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java?rev=769820&r1=769819&r2=769820&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestGroup.java Wed Apr 29 16:33:06 2009
@@ -48,7 +48,7 @@
             g = Group.get("Users");
         else
             g = Group.get("bin");
-        assertNotNull("Group" + g);
+        assertNotNull("Group", g);
         assertTrue("Local",  g.IsLocal());
         System.out.println("Group      " + g.getName());
         System.out.println("Comment    " + g.getComment());
@@ -71,8 +71,8 @@
     {
         GroupIterator groups = Group.getLocalGroups();
 
-        assertNotNull("Group" + groups);
-        assertTrue("Size", groups.hasNext());
+        assertNotNull("Iterator", groups);
+        assertTrue("Groups", groups.hasNext());
         int i = 0;
         for (Group g : groups) {
             i++;
@@ -87,8 +87,8 @@
         try {
             GroupIterator groups = Group.getGlobalGroups();
 
-            assertNotNull("Group" + groups);
-            assertTrue("Size", groups.hasNext());
+            assertNotNull("Iterator", groups);
+            assertTrue("Groups", groups.hasNext());
         } catch (UnsupportedOperationException ex) {
             if (OS.TYPE.contains(OsType.WINDOWS)) {
                 fail("Unexpected exception");
@@ -97,4 +97,45 @@
         }
     }
 
+    public void testGroupMembers()
+        throws Exception
+    {
+        Group g;
+
+        if (OS.TYPE.contains(OsType.WINDOWS))
+            g = Group.get("Users");
+        else
+            g = Group.get("sys");
+        assertNotNull("Group", g);
+        assertTrue("Local",  g.IsLocal());
+
+        UserIterator members = g.getMembers();
+
+        assertNotNull("Iterator", members);
+        assertTrue("Members", members.hasNext());
+        int i = 0;
+        for (User u : members) {
+            i++;
+        }
+        System.out.println();
+        System.out.println("G Members  " + i);
+    }
+
+    public void testGroupNoMembers()
+        throws Exception
+    {
+        Group g;
+
+        if (!OS.TYPE.contains(OsType.WINDOWS)) {
+            g = Group.get("nobody");
+            assertNotNull("Group", g);
+            assertTrue("Local",  g.IsLocal());
+
+            UserIterator members = g.getMembers();
+
+            assertNotNull("Iterator", members);
+            assertFalse("Members", members.hasNext());
+        }
+    }
+
 }

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java?rev=769820&r1=769819&r2=769820&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestUser.java Wed Apr 29 16:33:06 2009
@@ -75,8 +75,8 @@
     {
         UserIterator users = User.getUsers();
 
-        assertNotNull("Users" + users);
-        assertTrue("Size", users.hasNext());
+        assertNotNull("Iterator", users);
+        assertTrue("Users", users.hasNext());
         int i = 0;
         while (users.hasNext()) {
             User u = users.next();
@@ -91,8 +91,8 @@
     {
         UserIterator users = User.getLoggedUsers();
 
-        assertNotNull("Users" + users);
-        assertTrue("Size", users.hasNext());
+        assertNotNull("Iterator", users);
+        assertTrue("Users", users.hasNext());
         int i = 0;
         while (users.hasNext()) {
             User u = users.next();