001 /*
002 * Copyright 2011-2013 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2011-2013 UnboundID Corp.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021 package com.unboundid.util;
022
023
024
025 import java.util.Arrays;
026 import java.util.Collections;
027 import java.util.Iterator;
028 import java.util.List;
029
030
031
032 /**
033 * This class provides a data structure which holds information about a SASL
034 * mechanism supported for use with the {@link SASLUtils} class.
035 */
036 @NotMutable()
037 @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
038 public final class SASLMechanismInfo
039 {
040 // Indicates whether this SASL mechanism allows a password to be provided.
041 private final boolean acceptsPassword;
042
043 // Indicates whether this SASL mechanism requires a password to be provided.
044 private final boolean requiresPassword;
045
046 // The list of options available for use with this mechanism.
047 private final List<SASLOption> options;
048
049 // A description for this SASL mechanism.
050 private final String description;
051
052 // The name for this SASL mechanism.
053 private final String name;
054
055
056
057 /**
058 * Creates a new SASL mechanism info object with the provided information.
059 *
060 * @param name The name for the SASL mechanism.
061 * @param description A description for the SASL mechanism.
062 * @param acceptsPassword Indicates whether the SASL mechanism allows a
063 * password to be provided.
064 * @param requiresPassword Indicates whether the SASL mechanism requires a
065 * password to be provided.
066 * @param options The set of options that are associated with the
067 * SASL mechanism.
068 */
069 public SASLMechanismInfo(final String name, final String description,
070 final boolean acceptsPassword,
071 final boolean requiresPassword,
072 final SASLOption... options)
073 {
074 this.name = name;
075 this.description = description;
076 this.acceptsPassword = acceptsPassword;
077 this.requiresPassword = requiresPassword;
078
079 if ((options == null) || (options.length == 0))
080 {
081 this.options = Collections.emptyList();
082 }
083 else
084 {
085 this.options = Collections.unmodifiableList(Arrays.asList(options));
086 }
087 }
088
089
090
091 /**
092 * Retrieves the name of the SASL mechanism.
093 *
094 * @return The name of the SASL mechanism.
095 */
096 public String getName()
097 {
098 return name;
099 }
100
101
102
103 /**
104 * Retrieves a description for the SASL mechanism.
105 *
106 * @return A description for the SASL mechanism.
107 */
108 public String getDescription()
109 {
110 return description;
111 }
112
113
114
115 /**
116 * Indicates whether the SASL mechanism accepts a password for authentication
117 * processing.
118 *
119 * @return {@code true} if the SASL mechanism accepts a password for
120 * authentication processing, or {@code false} if not.
121 */
122 public boolean acceptsPassword()
123 {
124 return acceptsPassword;
125 }
126
127
128
129 /**
130 * Indicates whether the SASL mechanism requires a password for authentication
131 * processing.
132 *
133 * @return {@code true} if the SASL mechanism requires a password for
134 * authentication processing, or {@code false} if not.
135 */
136 public boolean requiresPassword()
137 {
138 return requiresPassword;
139 }
140
141
142
143 /**
144 * Retrieves a list of the options that may be used with the SASL mechanism.
145 *
146 * @return A list of the options that may be used with the SASL mechanism, or
147 * an empty list if there are no supported SASL options for the
148 * associated mechanism.
149 */
150 public List<SASLOption> getOptions()
151 {
152 return options;
153 }
154
155
156
157 /**
158 * Retrieves a string representation of this SASL mechanism info object.
159 *
160 * @return A string representation of this SASL mechanism info object.
161 */
162 @Override()
163 public String toString()
164 {
165 final StringBuilder buffer = new StringBuilder();
166 toString(buffer);
167 return buffer.toString();
168 }
169
170
171
172 /**
173 * Appends a string representation of this SASL mechanism info object to the
174 * provided buffer.
175 *
176 * @param buffer The buffer to which the information should be appended.
177 */
178 public void toString(final StringBuilder buffer)
179 {
180 buffer.append("SASLMechanismInfo(name='");
181 buffer.append(name);
182 buffer.append("', description='");
183 buffer.append(description);
184 buffer.append("', acceptsPassword=");
185 buffer.append(acceptsPassword);
186 buffer.append(", requiresPassword=");
187 buffer.append(requiresPassword);
188 buffer.append(", options={");
189
190 final Iterator<SASLOption> iterator = options.iterator();
191 while (iterator.hasNext())
192 {
193 iterator.next().toString(buffer);
194 if (iterator.hasNext())
195 {
196 buffer.append(", ");
197 }
198 }
199
200 buffer.append("})");
201 }
202 }