View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.schema.bootstrap.partition;
21  
22  import java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Iterator;
27  
28  /**
29   * Extracts dbfiles for the schema partition onto a destination directory.
30   *
31   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32   * @version $Rev: 664295 $
33   */
34  public class SchemaPartitionExtractor
35  {
36      private DbFileListing listing;
37      private File outputDirectory;
38  
39  
40      public SchemaPartitionExtractor( File outputDirectory ) throws IOException
41      {
42          this.outputDirectory = outputDirectory;
43          this.listing = new DbFileListing();
44      }
45  
46  
47      public void extract() throws IOException
48      {
49          if ( ! outputDirectory.exists() )
50          {
51              outputDirectory.mkdirs();
52          }
53  
54          File schemaDirectory = new File( outputDirectory, "schema" );
55          if ( ! schemaDirectory.exists() )
56          {
57              schemaDirectory.mkdirs();
58          }
59  
60          Iterator<String> ii = listing.iterator();
61          
62          while ( ii.hasNext() )
63          {
64              extract( ii.next() );
65          }
66      }
67  
68  
69      public DbFileListing getDbFileListing()
70      {
71          return listing;
72      }
73  
74  
75      private void extract( String resource ) throws IOException
76      {
77          byte[] buf = new byte[512];
78          InputStream in = DbFileListing.getUniqueResourceAsStream( resource, "database file in bootstrap partition" );
79  
80          try
81          {
82              FileOutputStream out = new FileOutputStream( new File( outputDirectory, resource ) );
83              try
84              {
85                  while ( in.available() > 0 )
86                  {
87                      int readCount = in.read( buf );
88                      out.write( buf, 0, readCount );
89                  }
90                  out.flush();
91              } finally
92              {
93                  out.close();
94              }
95          }
96          finally
97          {
98              in.close();
99          }
100     }
101 }