1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.schema.bootstrap.partition;
21
22
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.net.URL;
28 import java.util.Enumeration;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Set;
34
35
36
37
38
39
40
41
42 public class DbFileListing
43 {
44 Map<String, DbFileType> name2type = new HashMap<String, DbFileType>();
45 private static final String BASE_PATH = DbFileListing.class.getName()
46 .substring( 0, DbFileListing.class.getName().lastIndexOf( "." ) + 1 ).replace( '.', '/' );
47
48
49 public DbFileListing() throws IOException
50 {
51 init();
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 private void init() throws IOException
78 {
79
80 boolean userIndexMode = false;
81 String line = null;
82 BufferedReader in = new BufferedReader(
83 new InputStreamReader(
84 getUniqueResourceAsStream(
85 "DBFILES",
86 "bootstrap partition database file list. " +
87 "Be sure there is exactly one bootstrap partition jar in your classpath." ) ) );
88 try
89 {
90 while ( ( line = in.readLine() ) != null )
91 {
92 if ( line.indexOf( "master.db" ) != -1 )
93 {
94 name2type.put( line.trim(), DbFileType.MASTER_FILE );
95 continue;
96 }
97
98 if ( line.indexOf( "USER INDICES" ) != -1 )
99 {
100 userIndexMode = true;
101 continue;
102 }
103
104 if ( userIndexMode )
105 {
106 name2type.put( line.trim(), DbFileType.USER_INDEX );
107 }
108 else
109 {
110 name2type.put( line.trim(), DbFileType.SYSTEM_INDEX );
111 }
112 }
113 }
114 finally
115 {
116 in.close();
117 }
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131 public static InputStream getUniqueResourceAsStream( String resourceName, String resourceDescription ) throws IOException
132 {
133 resourceName = BASE_PATH + resourceName;
134 URL result = getUniqueResource( resourceName, resourceDescription );
135 return result.openStream();
136 }
137
138 static URL getUniqueResource( String resourceName, String resourceDescription )
139 throws IOException
140 {
141 Enumeration<URL> resources = DbFileListing.class.getClassLoader().getResources( resourceName );
142 if ( !resources.hasMoreElements() )
143 {
144 throw new UniqueResourceException( resourceName, resourceDescription );
145 }
146 URL result = resources.nextElement();
147 if ( resources.hasMoreElements() )
148 {
149 throw new UniqueResourceException( resourceName, result, resources, resourceDescription);
150 }
151 return result;
152 }
153
154
155 public DbFileType getType( String dbfile )
156 {
157 return name2type.get( dbfile );
158 }
159
160
161 public Iterator<String> iterator()
162 {
163 return name2type.keySet().iterator();
164 }
165
166
167 public String getIndexAttributeName( String dbfile )
168 {
169 if ( dbfile.length() < 10 )
170 {
171 throw new IllegalArgumentException( "db file must have a relative jar path name of over 10 characters" );
172 }
173
174
175 String dbfileName = dbfile.substring( 7 );
176 return dbfileName.substring( 0, dbfileName.length() - 3 );
177 }
178
179
180
181
182
183
184
185 public Set<String> getIndexedAttributes()
186 {
187 Set<String> attributes = new HashSet<String>();
188 Iterator<String> ii = iterator();
189 while( ii.hasNext() )
190 {
191 String name = ii.next();
192 if ( name2type.get( name ) == DbFileType.USER_INDEX )
193 {
194 attributes.add( getIndexAttributeName( name ) );
195 }
196 }
197 return attributes;
198 }
199 }