001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020
021package org.apache.directory.server.config;
022
023
024import java.io.BufferedReader;
025import java.io.File;
026import java.io.FileNotFoundException;
027import java.io.FileReader;
028import java.io.FileWriter;
029import java.io.IOException;
030import java.io.InputStream;
031import java.io.OutputStream;
032import java.net.URL;
033import java.nio.file.Files;
034import java.util.Map;
035import java.util.Map.Entry;
036import java.util.Stack;
037import java.util.regex.Pattern;
038
039import org.apache.directory.api.ldap.schema.extractor.impl.DefaultSchemaLdifExtractor;
040import org.apache.directory.api.ldap.schema.extractor.impl.ResourceMap;
041import org.apache.directory.api.util.Strings;
042import org.apache.directory.server.i18n.I18n;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046
047/**
048 * A class to copy the default config to the work directory of a DirectoryService instance.
049 * 
050 * NOTE: much of this class code is duplicated from DefaultSchemaLdifExtractor class
051 *       We should create a AbstractLdifExtractor class and move the reusable code there
052 * 
053 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
054 */
055public final class LdifConfigExtractor
056{
057
058    public static final String LDIF_CONFIG_FILE = "config.ldif";
059
060    private static final String CONFIG_SUBDIR = "config";
061
062    private static final Logger LOG = LoggerFactory.getLogger( LdifConfigExtractor.class );
063
064    // java.util.regex.Pattern is immutable so only one instance is needed for all uses.
065    private static final Pattern EXTRACT_PATTERN = Pattern.compile( ".*config"
066        + "[/\\Q\\\\E]" + "ou=config.*\\.ldif" );
067
068
069    private LdifConfigExtractor()
070    {
071    }
072
073
074    /**
075     * Extracts the LDIF files from a Jar file or copies exploded LDIF resources.
076     *
077     * @param outputDirectory The directory where to extract the configuration
078     * @param overwrite over write extracted structure if true, false otherwise
079     * @throws IOException if schema already extracted and on IO errors
080     */
081    public static void extract( File outputDirectory, boolean overwrite ) throws IOException
082    {
083        if ( !outputDirectory.exists() )
084        {
085            LOG.debug( "creating non existing output directory {}", outputDirectory.getAbsolutePath() );
086            if ( !outputDirectory.mkdir() )
087            {
088                throw new IOException( I18n.err( I18n.ERR_112_COULD_NOT_CREATE_DIRECTORY, outputDirectory ) );
089            }
090        }
091
092        File configDirectory = new File( outputDirectory, CONFIG_SUBDIR );
093
094        if ( !configDirectory.exists() )
095        {
096            LOG.debug( "creating non existing config directory {}", configDirectory.getAbsolutePath() );
097            if ( !configDirectory.mkdir() )
098            {
099                throw new IOException( I18n.err( I18n.ERR_112_COULD_NOT_CREATE_DIRECTORY, configDirectory ) );
100            }
101        }
102        else if ( !overwrite )
103        {
104            throw new IOException( I18n.err( I18n.ERR_508, configDirectory.getAbsolutePath() ) );
105        }
106
107        LOG.debug( "extracting the configuration to the directory at {}", configDirectory.getAbsolutePath() );
108
109        Map<String, Boolean> list = ResourceMap.getResources( EXTRACT_PATTERN );
110
111        for ( Entry<String, Boolean> entry : list.entrySet() )
112        {
113            if ( entry.getValue() )
114            {
115                extractFromJar( outputDirectory, entry.getKey() );
116            }
117            else
118            {
119                File resource = new File( entry.getKey() );
120                copyFile( resource, getDestinationFile( outputDirectory, resource ) );
121            }
122        }
123    }
124
125
126    /**
127     * Copies a file line by line from the source file argument to the
128     * destination file argument.
129     *
130     * @param source the source file to copy
131     * @param destination the destination to copy the source to
132     * @throws IOException if there are IO errors or the source does not exist
133     */
134    private static void copyFile( File source, File destination ) throws IOException
135    {
136        LOG.debug( "copyFile(): source = {}, destination = {}", source, destination );
137
138        if ( !destination.getParentFile().exists() )
139        {
140            if ( !destination.getParentFile().mkdirs() )
141            {
142                throw new IOException( I18n.err( I18n.ERR_112_COULD_NOT_CREATE_DIRECTORY, destination.getParentFile() ) );
143            }
144        }
145
146        if ( !source.getParentFile().exists() )
147        {
148            throw new FileNotFoundException( I18n.err( I18n.ERR_509, source.getAbsolutePath() ) );
149        }
150
151        FileWriter out = new FileWriter( destination );
152        BufferedReader in = new BufferedReader( new FileReader( source ) );
153        String line;
154        while ( null != ( line = in.readLine() ) )
155        {
156            out.write( line + "\n" );
157        }
158
159        in.close();
160        out.flush();
161        out.close();
162    }
163
164
165    /**
166     * Extracts the LDIF schema resource from a Jar.
167     *
168     * @param resource the LDIF schema resource
169     * @throws IOException if there are IO errors
170     */
171    private static void extractFromJar( File outputDirectory, String resource ) throws IOException
172    {
173        byte[] buf = new byte[512];
174
175        try ( InputStream in = DefaultSchemaLdifExtractor.getUniqueResourceAsStream( resource,
176            "LDIF file in config repository" ) ) 
177        {
178            File destination = new File( outputDirectory, resource );
179
180            /*
181             * Do not overwrite an LDIF file if it has already been extracted.
182             */
183            if ( destination.exists() )
184            {
185                return;
186            }
187
188            if ( !destination.getParentFile().exists() )
189            {
190                if ( !destination.getParentFile().mkdirs() )
191                {
192                    throw new IOException( I18n.err( I18n.ERR_112_COULD_NOT_CREATE_DIRECTORY,
193                        destination.getParentFile() ) );
194                }
195            }
196
197            try ( OutputStream out = Files.newOutputStream( destination.toPath() ) )
198            {
199                while ( in.available() > 0 )
200                {
201                    int readCount = in.read( buf );
202                    out.write( buf, 0, readCount );
203                }
204                out.flush();
205            }
206        }
207    }
208
209
210    /**
211     * Calculates the destination file.
212     *
213     * @param resource the source file
214     * @return the destination file's parent directory
215     */
216    private static File getDestinationFile( File outputDirectory, File resource )
217    {
218        File parent = resource.getParentFile();
219        Stack<String> fileComponentStack = new Stack<String>();
220        fileComponentStack.push( resource.getName() );
221
222        while ( parent != null )
223        {
224            if ( parent.getName().equals( "config" ) )
225            {
226                // All LDIF files besides the config.ldif are under the
227                // config/config base path. So we need to add one more
228                // schema component to all LDIF files minus this config.ldif
229                fileComponentStack.push( "config" );
230
231                return assembleDestinationFile( outputDirectory, fileComponentStack );
232            }
233
234            fileComponentStack.push( parent.getName() );
235
236            if ( parent.equals( parent.getParentFile() ) || parent.getParentFile() == null )
237            {
238                throw new IllegalStateException( I18n.err( I18n.ERR_510 ) );
239            }
240
241            parent = parent.getParentFile();
242        }
243
244        throw new IllegalStateException( I18n.err( I18n.ERR_511 ) );
245    }
246
247
248    /**
249     * Assembles the destination file by appending file components previously
250     * pushed on the fileComponentStack argument.
251     *
252     * @param fileComponentStack stack containing pushed file components
253     * @return the assembled destination file
254     */
255    private static File assembleDestinationFile( File outputDirectory, Stack<String> fileComponentStack )
256    {
257        File destinationFile = outputDirectory.getAbsoluteFile();
258
259        while ( !fileComponentStack.isEmpty() )
260        {
261            destinationFile = new File( destinationFile, fileComponentStack.pop() );
262        }
263
264        return destinationFile;
265    }
266
267
268    /**
269     * extracts or overwrites the configuration LDIF file and returns the absolute path of this file
270     *
271     * @param configDir the directory where the config file should be extracted to
272     * @param file The file containing the configuration
273     * @param overwrite flag to indicate to overwrite the config file if already present in the given config directory
274     * @return complete path of the config file on disk
275     */
276    public static String extractSingleFileConfig( File configDir, String file, boolean overwrite )
277    {
278        if ( file == null )
279        {
280            file = LDIF_CONFIG_FILE;
281        }
282
283        File configFile = new File( configDir, file );
284
285        if ( !configDir.exists() )
286        {
287            LOG.debug( "creating non existing config directory {}", configDir.getAbsolutePath() );
288            if ( !configDir.mkdir() )
289            {
290                throw new RuntimeException(
291                    new IOException( I18n.err( I18n.ERR_112_COULD_NOT_CREATE_DIRECTORY, configDir ) ) );
292            }
293        }
294        else
295        {
296            if ( configFile.exists() && !overwrite )
297            {
298                LOG.warn( "config file already exists, returning, cause overwrite flag was set to false" );
299                return configFile.getAbsolutePath();
300            }
301        }
302
303        try
304        {
305
306            URL configUrl = LdifConfigExtractor.class.getClassLoader().getResource( file );
307
308            LOG.debug( "URL of the config ldif file {}", configUrl );
309
310            byte[] buf = new byte[1024 * 1024];
311
312            try ( InputStream in = configUrl.openStream();
313                FileWriter fw = new FileWriter( configFile ) )
314            {
315                while ( true )
316                {
317                    int read = in.read( buf );
318
319                    if ( read <= 0 )
320                    {
321                        break;
322                    }
323
324                    String s = Strings.utf8ToString( buf, 0, read );
325                    fw.write( s );
326                }
327            }
328
329            LOG.info( "successfully extracted the config file {}", configFile.getAbsoluteFile() );
330
331            return configFile.getAbsolutePath();
332        }
333        catch ( Exception e )
334        {
335            throw new RuntimeException( e );
336        }
337    }
338}