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: */package org.apache.geronimo.console.util;
017:
018: import java.util.Hashtable;
019:
020: import org.apache.geronimo.kernel.Kernel;
021: import org.apache.geronimo.kernel.KernelRegistry;
022:
023: public class SERealmUserHelper extends RealmHelper {
024:
025: private static final String GET_USERS_FUNCTION = "getUsers";
026:
027: private static final String ADD_USER_FUNCTION = "addUserPrincipal";
028:
029: private static final String USER_EXISTS_FUNCTION = "userExists";
030:
031: private static final String UPDATE_USER_FUNCTION = "updateUserPrincipal";
032:
033: private static final String DELETE_USER_FUNCTION = "removeUserPrincipal";
034:
035: private static final String GET_PASSWORD_FUNCTION = "getPassword";
036:
037: private static final String[] STRING = { "java.lang.String" };
038:
039: private static final String[] HASHTABLE = { "java.util.Hashtable" };
040:
041: private static final Kernel kernel = KernelRegistry
042: .getSingleKernel();
043:
044: public static String[] getUsers() throws Exception {
045: return (String[]) invoke(
046: ObjectNameConstants.SE_REALM_MBEAN_NAME,
047: GET_USERS_FUNCTION);
048: }
049:
050: private static void refresh() {
051: try {
052:
053: kernel.stopGBean(ObjectNameConstants.SE_REALM_MBEAN_NAME);
054: kernel.startGBean(ObjectNameConstants.SE_REALM_MBEAN_NAME);
055: // kernel.stopGBean(ObjectNameConstants.SE_REALM_IMMUTABLE_MBEAN_NAME);
056: // kernel.startGBean(ObjectNameConstants.SE_REALM_IMMUTABLE_MBEAN_NAME);
057:
058: } catch (Exception e) {
059: }
060: }
061:
062: public static String getPassword(String username) throws Exception {
063: Object ret;
064: String[] arg = { username };
065: ret = invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME,
066: GET_PASSWORD_FUNCTION, arg, STRING);
067: return (ret != null) ? ret.toString() : "";
068: }
069:
070: public static boolean userExists(String username) throws Exception {
071: Boolean ret;
072: String[] arg = { username };
073: ret = (Boolean) invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME,
074: USER_EXISTS_FUNCTION, arg, STRING);
075: return ret.booleanValue();
076: }
077:
078: public static void addUser(String username, String password)
079: throws Exception {
080: Hashtable props = new Hashtable();
081: props.put("UserName", username);
082: props.put("Password", password);
083: Object[] args = { props };
084: invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME,
085: ADD_USER_FUNCTION, args, HASHTABLE);
086: refresh();
087: }
088:
089: public static void updateUser(String username, String password)
090: throws Exception {
091: Hashtable props = new Hashtable();
092: props.put("UserName", username);
093: props.put("Password", password);
094: Object[] args = { props };
095: invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME,
096: UPDATE_USER_FUNCTION, args, HASHTABLE);
097: refresh();
098: }
099:
100: public static void deleteUser(String username) throws Exception {
101: String[] args = { username };
102: invoke(ObjectNameConstants.SE_REALM_MBEAN_NAME,
103: DELETE_USER_FUNCTION, args, STRING);
104: refresh();
105: }
106:
107: }
|