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    package org.apache.directory.server.schema.bootstrap.partition;
021    
022    import java.io.File;
023    import java.io.FileOutputStream;
024    import java.io.IOException;
025    import java.io.InputStream;
026    import java.util.Iterator;
027    
028    /**
029     * Extracts dbfiles for the schema partition onto a destination directory.
030     *
031     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032     * @version $Rev: 664295 $
033     */
034    public class SchemaPartitionExtractor
035    {
036        private DbFileListing listing;
037        private File outputDirectory;
038    
039    
040        public SchemaPartitionExtractor( File outputDirectory ) throws IOException
041        {
042            this.outputDirectory = outputDirectory;
043            this.listing = new DbFileListing();
044        }
045    
046    
047        public void extract() throws IOException
048        {
049            if ( ! outputDirectory.exists() )
050            {
051                outputDirectory.mkdirs();
052            }
053    
054            File schemaDirectory = new File( outputDirectory, "schema" );
055            if ( ! schemaDirectory.exists() )
056            {
057                schemaDirectory.mkdirs();
058            }
059    
060            Iterator<String> ii = listing.iterator();
061            
062            while ( ii.hasNext() )
063            {
064                extract( ii.next() );
065            }
066        }
067    
068    
069        public DbFileListing getDbFileListing()
070        {
071            return listing;
072        }
073    
074    
075        private void extract( String resource ) throws IOException
076        {
077            byte[] buf = new byte[512];
078            InputStream in = DbFileListing.getUniqueResourceAsStream( resource, "database file in bootstrap partition" );
079    
080            try
081            {
082                FileOutputStream out = new FileOutputStream( new File( outputDirectory, resource ) );
083                try
084                {
085                    while ( in.available() > 0 )
086                    {
087                        int readCount = in.read( buf );
088                        out.write( buf, 0, readCount );
089                    }
090                    out.flush();
091                } finally
092                {
093                    out.close();
094                }
095            }
096            finally
097            {
098                in.close();
099            }
100        }
101    }