1
1
/*
2
- * Copyright 2005-2013 the original author or authors.
2
+ * Copyright 2005-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .util .ArrayList ;
20
20
import java .util .Hashtable ;
21
+ import java .util .Iterator ;
21
22
import java .util .LinkedList ;
22
23
import java .util .List ;
23
24
import java .util .SortedSet ;
@@ -238,26 +239,16 @@ public String[] getNamesOfModifiedAttributes() {
238
239
239
240
List <String > tmpList = new ArrayList <String >();
240
241
241
- NamingEnumeration <? extends Attribute > attributesEnumeration ;
242
242
if (isUpdateMode ()) {
243
- attributesEnumeration = this .updatedAttrs .getAll ();
243
+ for (NameAwareAttribute attribute : this .updatedAttrs ) {
244
+ tmpList .add (attribute .getID ());
245
+ }
244
246
}
245
247
else {
246
- attributesEnumeration = this .originalAttrs .getAll ();
247
- }
248
-
249
- try {
250
- while (attributesEnumeration .hasMore ()) {
251
- Attribute oneAttribute = attributesEnumeration .next ();
252
- tmpList .add (oneAttribute .getID ());
248
+ for (NameAwareAttribute attribute : this .originalAttrs ) {
249
+ tmpList .add (attribute .getID ());
253
250
}
254
251
}
255
- catch (NamingException ex ) {
256
- throw LdapUtils .convertLdapException (ex );
257
- }
258
- finally {
259
- closeNamingEnumeration (attributesEnumeration );
260
- }
261
252
262
253
return tmpList .toArray (new String [tmpList .size ()]);
263
254
}
@@ -283,22 +274,8 @@ public ModificationItem[] getModificationItems() {
283
274
}
284
275
285
276
List <ModificationItem > tmpList = new LinkedList <ModificationItem >();
286
- NamingEnumeration <? extends Attribute > attributesEnumeration = null ;
287
- try {
288
- attributesEnumeration = this .updatedAttrs .getAll ();
289
-
290
- // find attributes that have been changed, removed or added
291
- while (attributesEnumeration .hasMore ()) {
292
- NameAwareAttribute oneAttr = (NameAwareAttribute ) attributesEnumeration .next ();
293
-
294
- collectModifications (oneAttr , tmpList );
295
- }
296
- }
297
- catch (NamingException ex ) {
298
- throw LdapUtils .convertLdapException (ex );
299
- }
300
- finally {
301
- closeNamingEnumeration (attributesEnumeration );
277
+ for (NameAwareAttribute attribute : this .updatedAttrs ) {
278
+ collectModifications (attribute , tmpList );
302
279
}
303
280
304
281
if (log .isDebugEnabled ()) {
@@ -318,10 +295,8 @@ public ModificationItem[] getModificationItems() {
318
295
* (removals and additions) will be collected individually.
319
296
* @param changedAttr the value of the changed attribute.
320
297
* @param modificationList the list in which to add the modifications.
321
- * @throws NamingException if thrown by called Attribute methods.
322
298
*/
323
- private void collectModifications (NameAwareAttribute changedAttr , List <ModificationItem > modificationList )
324
- throws NamingException {
299
+ private void collectModifications (NameAwareAttribute changedAttr , List <ModificationItem > modificationList ) {
325
300
NameAwareAttribute currentAttribute = this .originalAttrs .get (changedAttr .getID ());
326
301
if (currentAttribute != null && changedAttr .hasValuesAsNames ()) {
327
302
try {
@@ -372,17 +347,15 @@ else if (changedAttr.size() > 0) {
372
347
}
373
348
}
374
349
375
- private void collectModifications (Attribute originalAttr , Attribute changedAttr ,
376
- List <ModificationItem > modificationList ) throws NamingException {
350
+ private void collectModifications (NameAwareAttribute originalAttr , NameAwareAttribute changedAttr ,
351
+ List <ModificationItem > modificationList ) {
377
352
378
353
Attribute originalClone = (Attribute ) originalAttr .clone ();
379
354
Attribute addedValuesAttribute = new NameAwareAttribute (originalAttr .getID ());
380
355
381
- NamingEnumeration <?> allValues = changedAttr .getAll ();
382
- while (allValues .hasMoreElements ()) {
383
- Object attributeValue = allValues .nextElement ();
384
- if (!originalClone .remove (attributeValue )) {
385
- addedValuesAttribute .add (attributeValue );
356
+ for (Object value : changedAttr ) {
357
+ if (!originalClone .remove (value )) {
358
+ addedValuesAttribute .add (value );
386
359
}
387
360
}
388
361
@@ -696,30 +669,15 @@ public void setAttributeValues(String name, Object[] values, boolean orderMatter
696
669
*/
697
670
@ Override
698
671
public void update () {
699
- NamingEnumeration <? extends Attribute > attributesEnumeration = null ;
700
-
701
- try {
702
- attributesEnumeration = this .updatedAttrs .getAll ();
703
-
704
- // find what to update
705
- while (attributesEnumeration .hasMore ()) {
706
- Attribute a = attributesEnumeration .next ();
707
-
708
- // if it does not exist it should be added
709
- if (isEmptyAttribute (a )) {
710
- this .originalAttrs .remove (a .getID ());
711
- }
712
- else {
713
- // Otherwise it should be set.
714
- this .originalAttrs .put (a );
715
- }
672
+ for (NameAwareAttribute attribute : this .updatedAttrs ) {
673
+ // if it does not exist it should be added
674
+ if (isEmptyAttribute (attribute )) {
675
+ this .originalAttrs .remove (attribute .getID ());
676
+ }
677
+ else {
678
+ // Otherwise it should be set.
679
+ this .originalAttrs .put (attribute );
716
680
}
717
- }
718
- catch (NamingException ex ) {
719
- throw LdapUtils .convertLdapException (ex );
720
- }
721
- finally {
722
- closeNamingEnumeration (attributesEnumeration );
723
681
}
724
682
725
683
// Reset the attributes to be updated
@@ -1359,8 +1317,9 @@ public String toString() {
1359
1317
builder .append (" {" );
1360
1318
1361
1319
try {
1362
- for (NamingEnumeration <NameAwareAttribute > i = this .originalAttrs .getAll (); i .hasMore ();) {
1363
- Attribute attribute = i .next ();
1320
+ Iterator <NameAwareAttribute > attributes = this .originalAttrs .iterator ();
1321
+ while (attributes .hasNext ()) {
1322
+ NameAwareAttribute attribute = attributes .next ();
1364
1323
if (attribute .size () == 1 ) {
1365
1324
builder .append (attribute .getID ());
1366
1325
builder .append ('=' );
@@ -1374,7 +1333,7 @@ public String toString() {
1374
1333
}
1375
1334
}
1376
1335
1377
- if (i . hasMore ()) {
1336
+ if (attributes . hasNext ()) {
1378
1337
builder .append (", " );
1379
1338
}
1380
1339
}
0 commit comments