public static boolean addUser(String username, String psw, String desc) {
try {
/** As of connection.getAttribute you can use connection.invoke to invoke an action
It Takes ObjectName, String OperationName, Object[] Parameters, and String[] Parameters
Definition
**/
connection.invoke(defaultAuthenticator, "createUser",
new Object[] { username, psw, desc },
new String[] { "java.lang.String",
"java.lang.String",
"java.lang.String" });
return true;
} catch (Exception e) {
return false;
//throw new RuntimeException(e);
}
}
public static boolean removeUser(String username) {
try {
if (!username.equalsIgnoreCase("weblogic")) {
connection.invoke(defaultAuthenticator, "removeUser",
new Object[] { username },
new String[] { "java.lang.String" });
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static boolean resetUserPassword(String username,
String newPassword) {
try {
if (!username.equalsIgnoreCase("weblogic")) {
connection.invoke(defaultAuthenticator, "resetUserPassword",
new Object[] { username, newPassword },
new String[] { "java.lang.String",
"java.lang.String" });
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/** As of connection.getAttribute you can use connection.invoke to invoke an action
It Takes ObjectName, String OperationName, Object[] Parameters, and String[] Parameters
Definition, It returns an Object we cast it to Boolean, you can know all about function from
MBeans Reference
**/
public static boolean isUserExists(String currentUser) throws RuntimeException {
try {
boolean userExists =
((Boolean)connection.invoke(defaultAuthenticator, "userExists",
new Object[] { currentUser },
new String[] { "java.lang.String" })).booleanValue();
return userExists;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static boolean isGroupExists(String currentGroup) throws RuntimeException {
try {
boolean gourpExists =
((Boolean)connection.invoke(defaultAuthenticator,
"groupExists",
new Object[] { currentGroup },
new String[] { "java.lang.String" })).booleanValue();
return gourpExists;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
/** This one is tricky, You first obtain a String cursor of the Iterator of Users, then you check if
It have current, while true we invoke another function called "getCurrentName" which returns the name
of the user, then I call advance function for the cursor to move forward, and invoke haveCurrent again
and assign it to the same boolean I entered the while with (In order to get out of it!)
**/
public static List<String> getListOfUsers() throws RuntimeException {
try {
List<String> allUsers = new ArrayList<String>();
String cursor =
(String)connection.invoke(defaultAuthenticator, "listUsers",
new Object[] { "*",
Integer.valueOf(9999) },
new String[] { "java.lang.String",
"java.lang.Integer" });
boolean haveCurrent =
((Boolean)connection.invoke(defaultAuthenticator,
"haveCurrent",
new Object[] { cursor },
new String[] { "java.lang.String" })).booleanValue();
while (haveCurrent) {
String currentName =
(String)connection.invoke(defaultAuthenticator,
"getCurrentName",
new Object[] { cursor },
new String[] { "java.lang.String" });
allUsers.add(currentName);
connection.invoke(defaultAuthenticator, "advance",
new Object[] { cursor },
new String[] { "java.lang.String" });
haveCurrent =
((Boolean)connection.invoke(defaultAuthenticator, "haveCurrent",
new Object[] { cursor },
new String[] { "java.lang.String" })).booleanValue();
}
return allUsers;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static List<String> getUserGroups(String username) throws RuntimeException {
try {
List<String> allUserGroups = new ArrayList<String>();
String cursor =
(String)connection.invoke(defaultAuthenticator, "listMemberGroups",
new Object[] { username },
new String[] { "java.lang.String"});
boolean haveCurrent =
((Boolean)connection.invoke(defaultAuthenticator,
"haveCurrent",
new Object[] { cursor },
new String[] { "java.lang.String" })).booleanValue();
while (haveCurrent) {
String currentName =
(String)connection.invoke(defaultAuthenticator,
"getCurrentName",
new Object[] { cursor },
new String[] { "java.lang.String" });
allUserGroups.add(currentName);
connection.invoke(defaultAuthenticator, "advance",
new Object[] { cursor },
new String[] { "java.lang.String" });
haveCurrent =
((Boolean)connection.invoke(defaultAuthenticator, "haveCurrent",
new Object[] { cursor },
new String[] { "java.lang.String" })).booleanValue();
}
return allUserGroups;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static List<String> getGroupMembers(String groupName) throws RuntimeException {
try {
List<String> allGroupMembers = new ArrayList<String>();
String cursor =
(String)connection.invoke(defaultAuthenticator, "listGroupMembers",
new Object[] { groupName, "*", new java.lang.Integer(0) },
new String [] { "java.lang.String", "java.lang.String", "java.lang.Integer" });
boolean haveCurrent =
((Boolean)connection.invoke(defaultAuthenticator,
"haveCurrent",
new Object[] { cursor },
new String[] { "java.lang.String" })).booleanValue();
while (haveCurrent) {
String currentName =
(String)connection.invoke(defaultAuthenticator,
"getCurrentName",
new Object[] { cursor },
new String[] { "java.lang.String" });
allGroupMembers.add(currentName);
connection.invoke(defaultAuthenticator, "advance",
new Object[] { cursor },
new String[] { "java.lang.String" });
haveCurrent =
((Boolean)connection.invoke(defaultAuthenticator, "haveCurrent",
new Object[] { cursor },
new String[] { "java.lang.String" })).booleanValue();
}
return allGroupMembers;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static List<String> getListOfGroups() throws RuntimeException {
try {
List<String> allUsers = new ArrayList<String>();
String cursor =
(String)connection.invoke(defaultAuthenticator, "listGroups",
new Object[] { "*",
Integer.valueOf(9999) },
new String[] { "java.lang.String",
"java.lang.Integer" });
boolean haveCurrent =
((Boolean)connection.invoke(defaultAuthenticator,
"haveCurrent",
new Object[] { cursor },
new String[] { "java.lang.String" })).booleanValue();
while (haveCurrent) {
String currentName =
(String)connection.invoke(defaultAuthenticator,
"getCurrentName",
new Object[] { cursor },
new String[] { "java.lang.String" });
allUsers.add(currentName);
connection.invoke(defaultAuthenticator, "advance",
new Object[] { cursor },
new String[] { "java.lang.String" });
haveCurrent =
((Boolean)connection.invoke(defaultAuthenticator, "haveCurrent",
new Object[] { cursor },
new String[] { "java.lang.String" })).booleanValue();
}
return allUsers;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}