Home » geronimo-2.2-source-release » org.apache.geronimo.security.credentialstore » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
   10    *  http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    * Unless required by applicable law or agreed to in writing,
   13    * software distributed under the License is distributed on an
   14    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    * KIND, either express or implied.  See the License for the
   16    * specific language governing permissions and limitations
   17    * under the License.
   18    */
   19   
   20   
   21   package org.apache.geronimo.security.credentialstore;
   22   
   23   import java.util.Map;
   24   import java.util.HashMap;
   25   import java.lang.reflect.Constructor;
   26   import java.lang.reflect.InvocationTargetException;
   27   import java.security.Principal;
   28   
   29   import javax.security.auth.Subject;
   30   import javax.security.auth.login.LoginException;
   31   
   32   import org.apache.geronimo.common.DeploymentException;
   33   import org.apache.geronimo.gbean.GBeanInfo;
   34   import org.apache.geronimo.gbean.GBeanInfoBuilder;
   35   
   36   /**
   37    * Hopefully this will only be used for tests where you need to set up a simple credential store
   38    * but don't want to set up a login configuration
   39    *
   40    * @version $Rev: 720842 $ $Date: 2008-11-26 05:18:49 -0800 (Wed, 26 Nov 2008) $
   41    */
   42   public class DirectConfigurationCredentialStoreImpl implements CredentialStore {
   43   
   44       private final Map<String, Map<String, Subject>> subjectStore = new HashMap<String, Map<String, Subject>>();
   45   
   46       public DirectConfigurationCredentialStoreImpl(Map<String, Map<String, Map<String, String>>> subjectInfo, ClassLoader cl) throws DeploymentException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
   47           if (cl == null) {
   48               cl = getClass().getClassLoader();
   49           }
   50           for (Map.Entry<String, Map<String, Map<String, String>>> realmEntry: subjectInfo.entrySet()) {
   51               Map<String, Subject> realm = new HashMap<String, Subject>();
   52               for (Map.Entry<String, Map<String, String>> subjectEntry: realmEntry.getValue().entrySet()) {
   53                   String id = subjectEntry.getKey();
   54                   Map<String, String> principals = subjectEntry.getValue();
   55                   Subject subject = new Subject();
   56                   for (Map.Entry<String, String> principalInfo: principals.entrySet()) {
   57                       String className = principalInfo.getKey();
   58                       String principalName = principalInfo.getValue();
   59                       Class<? extends Principal> clazz = (Class<? extends Principal>) cl.loadClass(className);
   60                       Constructor<? extends Principal> c = clazz.getConstructor(new Class[] {String.class});
   61                       Principal p = c.newInstance(new Object[] {principalName});
   62                       subject.getPrincipals().add(p);
   63                   }
   64                   realm.put(id, subject);
   65               }
   66               subjectStore.put(realmEntry.getKey(), realm);
   67           }
   68       }
   69   
   70       public Subject getSubject(String realm, String id) throws LoginException {
   71           Map<String, Subject> realmMap = subjectStore.get(realm);
   72           if (realmMap == null) {
   73               throw new LoginException("Unknown realm : " + realm);
   74           }
   75           Subject subject = realmMap.get(id);
   76           if (subject == null) {
   77               throw new LoginException("Unknown id: " + id + " in realm: " + realm);
   78           }
   79           return subject;
   80       }
   81   
   82       public static final GBeanInfo GBEAN_INFO;
   83   
   84       static {
   85           GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(DirectConfigurationCredentialStoreImpl.class);
   86   
   87           infoBuilder.addAttribute("credentialStore", Map.class, true);
   88           infoBuilder.addAttribute("classLoader", ClassLoader.class, false);
   89   
   90           infoBuilder.setConstructor(new String[]{"credentialStore", "classLoader"});
   91   
   92           GBEAN_INFO = infoBuilder.getBeanInfo();
   93       }
   94   
   95       public static GBeanInfo getGBeanInfo() {
   96           return GBEAN_INFO;
   97       }
   98   
   99   }

Home » geronimo-2.2-source-release » org.apache.geronimo.security.credentialstore » [javadoc | source]