1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.ldap.server.configuration;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Set;
27
28 import javax.naming.directory.Attributes;
29
30 /***
31 * A utility class that provides common functionality while validating configuration.
32 *
33 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34 * @version $Rev: 264732 $, $Date: 2005-08-30 04:04:51 -0400 (Tue, 30 Aug 2005) $
35 */
36 class ConfigurationUtil
37 {
38 /***
39 * Checks all elements of the specified set is of the specified type,
40 * and returns cloned set.
41 *
42 * @throws ConfigurationException if the specified set has an element of wrong type
43 */
44 static Set getTypeSafeSet( Set set, Class type )
45 {
46 Set newSet = new HashSet();
47 getTypeSafeCollection( set, type, newSet );
48 return newSet;
49 }
50
51 /***
52 * Checks all elements of the specified list is of the specified type,
53 * and returns cloned list.
54 *
55 * @throws ConfigurationException if the specified set has an element of wrong type
56 */
57 static List getTypeSafeList( List list, Class type )
58 {
59 List newList = new ArrayList();
60 getTypeSafeCollection( list, type, newList );
61 return newList;
62 }
63
64 private static void getTypeSafeCollection( Collection collection, Class type, Collection newCollection )
65 {
66 Iterator i = collection.iterator();
67 while( i.hasNext() )
68 {
69 Object e = i.next();
70 if( !type.isAssignableFrom( e.getClass() ) )
71 {
72 throw new ConfigurationException(
73 "Invalid element type: " + e.getClass() +
74 " (expected " + type );
75 }
76 newCollection.add( e );
77 }
78 }
79
80 /***
81 * Returns the clone of the specified set.
82 */
83 static Set getClonedSet( Set set )
84 {
85 Set newSet = new HashSet();
86 newSet.addAll( set );
87 return newSet;
88 }
89
90 /***
91 * Returns the clone of the specified list.
92 */
93 static List getClonedList( List list )
94 {
95 List newList = new ArrayList();
96 newList.addAll( list );
97 return newList;
98 }
99
100 /***
101 * Returns the deep clone of the specified {@link Attributes} list.
102 */
103 static List getClonedAttributesList( List list )
104 {
105 List newList = new ArrayList();
106 Iterator i = list.iterator();
107 while( i.hasNext() )
108 {
109 newList.add( ( ( Attributes ) i.next() ).clone() );
110 }
111 return newList;
112 }
113
114 /***
115 * Throws a {@link ConfigurationException} if the specified port number
116 * is out of range.
117 */
118 static void validatePortNumber( int port )
119 {
120 if( port < 0 || port > 65535 )
121 {
122 throw new ConfigurationException( "Invalid port number: " + port );
123 }
124 }
125
126 private ConfigurationUtil()
127 {
128 }
129 }