View Javadoc
1   /*
2    * Copyright 2008-2016 Brian Thomas Matthews
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.btmatthews.maven.plugins.ldap.apache;
18  
19  import com.btmatthews.maven.plugins.ldap.AbstractLDAPServer;
20  import com.btmatthews.utils.monitor.Logger;
21  import org.apache.directory.server.core.DefaultDirectoryService;
22  import org.apache.directory.server.core.DirectoryService;
23  import org.apache.directory.server.core.authn.AuthenticationInterceptor;
24  import org.apache.directory.server.core.entry.ServerEntry;
25  import org.apache.directory.server.core.exception.ExceptionInterceptor;
26  import org.apache.directory.server.core.interceptor.Interceptor;
27  import org.apache.directory.server.core.normalization.NormalizationInterceptor;
28  import org.apache.directory.server.core.operational.OperationalAttributeInterceptor;
29  import org.apache.directory.server.core.partition.Partition;
30  import org.apache.directory.server.core.partition.impl.btree.jdbm.JdbmPartition;
31  import org.apache.directory.server.core.referral.ReferralInterceptor;
32  import org.apache.directory.server.core.subtree.SubentryInterceptor;
33  import org.apache.directory.server.ldap.LdapServer;
34  import org.apache.directory.server.protocol.shared.store.LdifFileLoader;
35  import org.apache.directory.server.protocol.shared.transport.TcpTransport;
36  import org.apache.directory.shared.ldap.exception.LdapNameNotFoundException;
37  import org.apache.directory.shared.ldap.name.LdapDN;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  /**
43   * Implements an embedded ApacheDS LDAP apache.
44   *
45   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
46   * @since 1.1.0
47   */
48  public final class ApacheDSServer extends AbstractLDAPServer {
49  
50      /**
51       * The LDAP directory service.
52       */
53      private DirectoryService service;
54      /**
55       * The server that listens for LDAP requests and forwards them to the directory service.
56       */
57      private LdapServer server;
58  
59      /**
60       * Configure and start the embedded ApacheDS server creating the root DN and loading the LDIF seed data.
61       *
62       * @param logger Used to log informational and error messages.
63       */
64      @Override
65      public void start(final Logger logger) {
66          try {
67              logger.logInfo("Starting ApacheDS server");
68              service = new DefaultDirectoryService();
69  
70              final List<Interceptor> list = new ArrayList<Interceptor>();
71  
72              list.add(new NormalizationInterceptor());
73              list.add(new AuthenticationInterceptor());
74              list.add(new ReferralInterceptor());
75              // list.add( new AciAuthorizationInterceptor() );
76              // list.add( new DefaultAuthorizationInterceptor() );
77              list.add(new ExceptionInterceptor());
78              // list.add( new ChangeLogInterceptor() );
79              list.add(new OperationalAttributeInterceptor());
80              // list.add( new SchemaInterceptor() );
81              list.add(new SubentryInterceptor());
82              // list.add( new CollectiveAttributeInterceptor() );
83              // list.add( new EventInterceptor() );
84              // list.add( new TriggerInterceptor() );
85              // list.add( new JournalInterceptor() );
86              service.setInterceptors(list);
87              service.setWorkingDirectory(getWorkingDirectory());
88  
89              final JdbmPartition partition = new JdbmPartition();
90              partition.setId("rootPartition");
91              partition.setSuffix(getRoot());
92              service.addPartition(partition);
93              service.setExitVmOnShutdown(false);
94              service.setShutdownHookEnabled(false);
95              service.getChangeLog().setEnabled(false);
96              service.setDenormalizeOpAttrsEnabled(true);
97  
98              server = new LdapServer();
99              server.setDirectoryService(service);
100             server.setTransports(new TcpTransport(getServerPort()));
101 
102             service.startup();
103             server.start();
104 
105             createRoot(partition);
106 
107             loadLdifFile();
108 
109             logger.logInfo("Started ApacheDS server");
110 
111         } catch (final Exception e) {
112             logger.logError("Error starting ApacheDS server e");
113         }
114     }
115 
116     /**
117      * Shutdown the the embedded ApacheDS server.
118      *
119      * @param logger Used to log informational and error messages.
120      */
121     @Override
122     public void stop(final Logger logger) {
123         try {
124             logger.logInfo("Stopping ApacheDS server");
125             server.stop();
126             service.shutdown();
127             logger.logInfo("Stopped ApacheDS server");
128         } catch (final Exception e) {
129             logger.logError("Error stopping ApacheDS server", e);
130         }
131     }
132 
133     @Override
134     public boolean isStarted(final Logger logger) {
135         return server != null && server.isStarted();
136     }
137 
138     @Override
139     public boolean isStopped(final Logger logger) {
140         return server == null || !server.isStarted();
141     }
142 
143     /**
144      * Create the root DN.
145      *
146      * @param partition The partition in which to create the root DN.
147      * @throws Exception If there was an error creating the root DN.
148      */
149 
150     private void createRoot(final Partition partition) throws Exception {
151         try {
152             service.getAdminSession().lookup(partition.getSuffixDn());
153         } catch (final LdapNameNotFoundException e) {
154             final LdapDN dn = new LdapDN(getRoot());
155             final String dc = getRoot().substring(3, getRoot().indexOf(','));
156             final ServerEntry entry = service.newEntry(dn);
157             entry.add("objectClass", "top", "domain", "extensibleObject");
158             entry.add("dc", dc);
159             service.getAdminSession().add(entry);
160         }
161     }
162 
163     /**
164      * Load the LDIF file used to seed the directory.
165      *
166      * @throws Exception If there was an error.
167      */
168     private void loadLdifFile() throws Exception {
169         new LdifFileLoader(service.getAdminSession(), getLdifFile(), null)
170                 .execute();
171     }
172 }