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