1 23 package com.sun.enterprise.management.offline; 24 25 import java.util.Enumeration ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 29 import java.io.IOException ; 30 31 import com.sun.appserv.management.util.misc.CollectionUtil; 32 33 import com.sun.enterprise.admin.config.MBeanConfigException; 34 35 import com.sun.enterprise.security.auth.realm.file.FileRealm; 36 import com.sun.enterprise.security.auth.realm.BadRealmException; 37 import com.sun.enterprise.security.auth.realm.NoSuchUserException; 38 39 40 49 final class AuthRealmMBeanX 50 { 51 private final String mFile; 52 53 public 54 AuthRealmMBeanX( final String file ) 55 { 56 mFile = file; 57 } 58 59 private FileRealm 60 getRealmKeyFile() 61 throws MBeanConfigException 62 { 63 try 64 { 65 return new FileRealm( mFile ); 66 } 67 catch(Exception e) 68 { 69 throw new MBeanConfigException( e.getMessage() ); 70 } 71 } 72 73 private static String [] 74 toStringArray(final Enumeration e) 75 { 76 final List <String > list = new ArrayList <String >(); 77 78 while( e.hasMoreElements() ) 79 { 80 list.add( "" + e.nextElement() ); 81 } 82 return CollectionUtil.toStringArray( list ); 83 } 84 85 public String [] 86 getUserNames() 87 throws MBeanConfigException 88 { 89 final FileRealm realm = getRealmKeyFile(); 90 try 91 { 92 return toStringArray(realm.getUserNames()); 93 } 94 catch(BadRealmException bre) 95 { 96 throw new MBeanConfigException(bre.getMessage()); 97 } 98 } 99 100 103 public String [] getGroupNames() throws MBeanConfigException 104 { 105 final FileRealm realm = getRealmKeyFile(); 106 try 107 { 108 return toStringArray(realm.getGroupNames()); 109 } 110 catch(BadRealmException bre) 111 { 112 throw new MBeanConfigException(bre.getMessage()); 113 } 114 } 115 116 119 public String [] getUserGroupNames(String userName) throws MBeanConfigException 120 { 121 if( userName == null ) 122 { 123 throw new IllegalArgumentException ( "" + null ); 124 } 125 126 final FileRealm realm = getRealmKeyFile(); 127 try 128 { 129 return toStringArray(realm.getGroupNames(userName)); 130 } 131 catch(NoSuchUserException nse) 132 { 133 throw new MBeanConfigException(nse.getMessage()); 134 } 135 } 136 137 140 public void 141 addUser( 142 final String userName, 143 final String password, 144 final String [] groupList) 145 throws MBeanConfigException 146 { 147 final FileRealm realm = getRealmKeyFile(); 148 try 149 { 150 realm.addUser(userName, password, groupList); 151 saveInstanceRealmKeyFile(realm); 152 } 153 catch(Exception e) 154 { 155 throw new MBeanConfigException( e.getMessage() ); 156 } 157 } 158 159 162 public void removeUser( final String userName) 163 throws MBeanConfigException 164 { 165 final FileRealm realm = getRealmKeyFile(); 166 try 167 { 168 realm.removeUser(userName); 169 saveInstanceRealmKeyFile(realm); 170 } 171 catch(NoSuchUserException nse) 172 { 173 throw new MBeanConfigException(nse.getMessage()); 174 } 175 } 176 177 181 public void updateUser( 182 final String userName, 183 final String password, 184 final String [] groupList) 185 throws MBeanConfigException 186 { 187 final FileRealm realm = getRealmKeyFile(); 188 try 189 { 190 realm.updateUser(userName, userName, password, groupList); 191 saveInstanceRealmKeyFile( realm ); 192 } 193 catch( Exception e ) 194 { 195 throw new MBeanConfigException( e .getMessage() ); 196 } 197 } 198 199 private void 201 saveInstanceRealmKeyFile(final FileRealm realm) 202 throws MBeanConfigException 203 { 204 try 205 { 206 realm.writeKeyFile( mFile ); 207 } 208 catch(IOException e) 209 { 210 throw new MBeanConfigException( e.getMessage() ); 211 } 212 } 213 214 215 } 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | Popular Tags |