1 /*
   2  * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.security.auth.login;
  27 
  28 import java.util.Map;
  29 import java.util.Collections;
  30 
  31 /**
  32  * This class represents a single {@code LoginModule} entry
  33  * configured for the application specified in the
  34  * {@code getAppConfigurationEntry(String appName)}
  35  * method in the {@code Configuration} class.  Each respective
  36  * {@code AppConfigurationEntry} contains a {@code LoginModule} name,
  37  * a control flag (specifying whether this {@code LoginModule} is
  38  * REQUIRED, REQUISITE, SUFFICIENT, or OPTIONAL), and LoginModule-specific
  39  * options.  Please refer to the {@code Configuration} class for
  40  * more information on the different control flags and their semantics.
  41  *
  42  * @since 1.4
  43  * @see javax.security.auth.login.Configuration
  44  */
  45 public class AppConfigurationEntry {
  46 
  47     private String loginModuleName;
  48     private LoginModuleControlFlag controlFlag;
  49     private Map<String,?> options;
  50 
  51     /**
  52      * Default constructor for this class.
  53      *
  54      * <p> This entry represents a single {@code LoginModule}
  55      * entry configured for the application specified in the
  56      * {@code getAppConfigurationEntry(String appName)}
  57      * method from the {@code Configuration} class.
  58      *
  59      * @param loginModuleName String representing the class name of the
  60      *                  {@code LoginModule} configured for the
  61      *                  specified application.
  62      *
  63      * @param controlFlag either REQUIRED, REQUISITE, SUFFICIENT,
  64      *                  or OPTIONAL.
  65      *
  66      * @param options the options configured for this {@code LoginModule}.
  67      *
  68      * @exception IllegalArgumentException if {@code loginModuleName}
  69      *                  is null, if {@code LoginModuleName}
  70      *                  has a length of 0, if {@code controlFlag}
  71      *                  is not either REQUIRED, REQUISITE, SUFFICIENT
  72      *                  or OPTIONAL, or if {@code options} is null.
  73      */
  74     public AppConfigurationEntry(String loginModuleName,
  75                                 LoginModuleControlFlag controlFlag,
  76                                 Map<String,?> options)
  77     {
  78         if (loginModuleName == null || loginModuleName.length() == 0 ||
  79             (controlFlag != LoginModuleControlFlag.REQUIRED &&
  80                 controlFlag != LoginModuleControlFlag.REQUISITE &&
  81                 controlFlag != LoginModuleControlFlag.SUFFICIENT &&
  82                 controlFlag != LoginModuleControlFlag.OPTIONAL) ||
  83             options == null)
  84             throw new IllegalArgumentException();
  85 
  86         this.loginModuleName = loginModuleName;
  87         this.controlFlag = controlFlag;
  88         this.options = Collections.unmodifiableMap(options);
  89     }
  90 
  91     /**
  92      * Get the class name of the configured {@code LoginModule}.
  93      *
  94      * @return the class name of the configured {@code LoginModule} as
  95      *          a String.
  96      */
  97     public String getLoginModuleName() {
  98         return loginModuleName;
  99     }
 100 
 101     /**
 102      * Return the controlFlag
 103      * (either REQUIRED, REQUISITE, SUFFICIENT, or OPTIONAL)
 104      * for this {@code LoginModule}.
 105      *
 106      * @return the controlFlag
 107      *          (either REQUIRED, REQUISITE, SUFFICIENT, or OPTIONAL)
 108      *          for this {@code LoginModule}.
 109      */
 110     public LoginModuleControlFlag getControlFlag() {
 111         return controlFlag;
 112     }
 113 
 114     /**
 115      * Get the options configured for this {@code LoginModule}.
 116      *
 117      * @return the options configured for this {@code LoginModule}
 118      *          as an unmodifiable {@code Map}.
 119      */
 120     public Map<String,?> getOptions() {
 121         return options;
 122     }
 123 
 124     /**
 125      * This class represents whether or not a {@code LoginModule}
 126      * is REQUIRED, REQUISITE, SUFFICIENT or OPTIONAL.
 127      */
 128     public static class LoginModuleControlFlag {
 129 
 130         private String controlFlag;
 131 
 132         /**
 133          * Required {@code LoginModule}.
 134          */
 135         public static final LoginModuleControlFlag REQUIRED =
 136                                 new LoginModuleControlFlag("required");
 137 
 138         /**
 139          * Requisite {@code LoginModule}.
 140          */
 141         public static final LoginModuleControlFlag REQUISITE =
 142                                 new LoginModuleControlFlag("requisite");
 143 
 144         /**
 145          * Sufficient {@code LoginModule}.
 146          */
 147         public static final LoginModuleControlFlag SUFFICIENT =
 148                                 new LoginModuleControlFlag("sufficient");
 149 
 150         /**
 151          * Optional {@code LoginModule}.
 152          */
 153         public static final LoginModuleControlFlag OPTIONAL =
 154                                 new LoginModuleControlFlag("optional");
 155 
 156         private LoginModuleControlFlag(String controlFlag) {
 157             this.controlFlag = controlFlag;
 158         }
 159 
 160         /**
 161          * Return a String representation of this controlFlag.
 162          *
 163          * <p> The String has the format, "LoginModuleControlFlag: <i>flag</i>",
 164          * where <i>flag</i> is either <i>required</i>, <i>requisite</i>,
 165          * <i>sufficient</i>, or <i>optional</i>.
 166          *
 167          * @return a String representation of this controlFlag.
 168          */
 169         public String toString() {
 170             return (sun.security.util.ResourcesMgr.getString
 171                 ("LoginModuleControlFlag.") + controlFlag);
 172         }
 173     }
 174 }