1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.directory.server.core.factory;
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.List;
25
26 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
27 import org.apache.directory.api.ldap.model.schema.LdapComparator;
28 import org.apache.directory.api.ldap.model.schema.SchemaManager;
29 import org.apache.directory.api.ldap.model.schema.comparators.NormalizingComparator;
30 import org.apache.directory.api.ldap.model.schema.registries.ComparatorRegistry;
31 import org.apache.directory.api.ldap.model.schema.registries.SchemaLoader;
32 import org.apache.directory.api.ldap.schema.extractor.SchemaLdifExtractor;
33 import org.apache.directory.api.ldap.schema.extractor.impl.DefaultSchemaLdifExtractor;
34 import org.apache.directory.api.ldap.schema.loader.LdifSchemaLoader;
35 import org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager;
36 import org.apache.directory.api.util.FileUtils;
37 import org.apache.directory.api.util.exception.Exceptions;
38 import org.apache.directory.server.constants.ServerDNConstants;
39 import org.apache.directory.server.core.DefaultDirectoryService;
40 import org.apache.directory.server.core.api.DirectoryService;
41 import org.apache.directory.server.core.api.InstanceLayout;
42 import org.apache.directory.server.core.api.partition.Partition;
43 import org.apache.directory.server.core.api.schema.SchemaPartition;
44 import org.apache.directory.server.core.partition.ldif.LdifPartition;
45 import org.apache.directory.server.i18n.I18n;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49
50
51
52
53
54
55 public class DefaultDirectoryServiceFactory implements DirectoryServiceFactory
56 {
57
58 private static final Logger LOG = LoggerFactory.getLogger( DefaultDirectoryServiceFactory.class );
59
60
61 private DirectoryService directoryService;
62
63
64 private PartitionFactory partitionFactory;
65
66
67 public DefaultDirectoryServiceFactory()
68 {
69 try
70 {
71
72
73
74 directoryService = new DefaultDirectoryService();
75
76
77
78 directoryService.setShutdownHookEnabled( false );
79 }
80 catch ( Exception e )
81 {
82 throw new RuntimeException( e );
83 }
84
85 try
86 {
87 String typeName = System.getProperty( "apacheds.partition.factory" );
88
89 if ( typeName != null )
90 {
91 Class<? extends PartitionFactory> type = ( Class<? extends PartitionFactory> ) Class.forName( typeName );
92 partitionFactory = type.newInstance();
93 }
94 else
95 {
96 partitionFactory = new JdbmPartitionFactory();
97 }
98 }
99 catch ( Exception e )
100 {
101 LOG.error( "Error instantiating custom partiton factory", e );
102 throw new RuntimeException( e );
103 }
104 }
105
106
107 public DefaultDirectoryServiceFactory( DirectoryService directoryService, PartitionFactory partitionFactory )
108 {
109 this.directoryService = directoryService;
110 this.partitionFactory = partitionFactory;
111 }
112
113
114
115
116
117 public void init( String name ) throws Exception
118 {
119 if ( ( directoryService != null ) && directoryService.isStarted() )
120 {
121 return;
122 }
123
124 build( name );
125 }
126
127
128
129
130
131 private void buildInstanceDirectory( String name ) throws IOException
132 {
133 String instanceDirectory = System.getProperty( "workingDirectory" );
134
135 if ( instanceDirectory == null )
136 {
137 instanceDirectory = System.getProperty( "java.io.tmpdir" ) + "/server-work-" + name;
138 }
139
140 InstanceLayoutnstanceLayout.html#InstanceLayout">InstanceLayout instanceLayout = new InstanceLayout( instanceDirectory );
141
142 if ( instanceLayout.getInstanceDirectory().exists() )
143 {
144 try
145 {
146 FileUtils.deleteDirectory( instanceLayout.getInstanceDirectory() );
147 }
148 catch ( IOException e )
149 {
150 LOG.error( "couldn't delete the instance directory before initializing the DirectoryService", e );
151 }
152 }
153
154 directoryService.setInstanceLayout( instanceLayout );
155 }
156
157
158
159
160
161 private void initSchema() throws Exception
162 {
163 File workingDirectory = directoryService.getInstanceLayout().getPartitionsDirectory();
164
165
166 File schemaRepository = new File( workingDirectory, "schema" );
167 SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor( workingDirectory );
168
169 try
170 {
171 extractor.extractOrCopy();
172 }
173 catch ( IOException ioe )
174 {
175
176 }
177
178 SchemaLoader loader = new LdifSchemaLoader( schemaRepository );
179 SchemaManager schemaManager = new DefaultSchemaManager( loader );
180
181
182
183
184 schemaManager.loadAllEnabled();
185
186
187 ComparatorRegistry comparatorRegistry = schemaManager.getComparatorRegistry();
188
189 for ( LdapComparator<?> comparator : comparatorRegistry )
190 {
191 if ( comparator instanceof NormalizingComparator )
192 {
193 ( ( NormalizingComparator ) comparator ).setOnServer();
194 }
195 }
196
197 directoryService.setSchemaManager( schemaManager );
198
199
200 LdifPartitiontition/ldif/LdifPartition.html#LdifPartition">LdifPartition ldifPartition = new LdifPartition( schemaManager, directoryService.getDnFactory() );
201 ldifPartition.setPartitionPath( new File( workingDirectory, "schema" ).toURI() );
202 SchemaPartitionema/SchemaPartition.html#SchemaPartition">SchemaPartition schemaPartition = new SchemaPartition( schemaManager );
203 schemaPartition.setWrappedPartition( ldifPartition );
204 directoryService.setSchemaPartition( schemaPartition );
205
206 List<Throwable> errors = schemaManager.getErrors();
207
208 if ( !errors.isEmpty() )
209 {
210 throw new Exception( I18n.err( I18n.ERR_317, Exceptions.printErrors( errors ) ) );
211 }
212 }
213
214
215
216
217
218
219
220 private void initSystemPartition() throws Exception
221 {
222
223
224
225
226
227 Partition systemPartition = partitionFactory.createPartition( directoryService.getSchemaManager(),
228 directoryService.getDnFactory(),
229 "system", ServerDNConstants.SYSTEM_DN, 500,
230 new File( directoryService.getInstanceLayout().getPartitionsDirectory(), "system" ) );
231 systemPartition.setSchemaManager( directoryService.getSchemaManager() );
232
233 partitionFactory.addIndex( systemPartition, SchemaConstants.OBJECT_CLASS_AT, 100 );
234
235 directoryService.setSystemPartition( systemPartition );
236 }
237
238
239
240
241
242
243
244 private void build( String name ) throws Exception
245 {
246 directoryService.setInstanceId( name );
247 buildInstanceDirectory( name );
248
249
250 initSchema();
251 initSystemPartition();
252
253 directoryService.startup();
254 }
255
256
257
258
259
260 public DirectoryService getDirectoryService() throws Exception
261 {
262 return directoryService;
263 }
264
265
266
267
268
269 public PartitionFactory getPartitionFactory() throws Exception
270 {
271 return partitionFactory;
272 }
273 }