<<Back to http://devdoc.net
Mine with nofee-ng to get DevFee back!
View Javadoc
1   /**
2    *    Copyright 2009-2015 the original author or authors.
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  package org.apache.ibatis.io;
17  
18  import java.io.Closeable;
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileNotFoundException;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.nio.channels.FileChannel;
25  import java.util.Properties;
26  
27  /**
28   * @author Clinton Begin
29   */
30  public class ExternalResources {
31  
32    private ExternalResources() {
33      // do nothing
34    }
35  
36    public static void copyExternalResource(File sourceFile, File destFile) throws IOException {
37      if (!destFile.exists()) {
38        destFile.createNewFile();
39      }
40  
41      FileChannel source = null;
42      FileChannel destination = null;
43      try {
44        source = new FileInputStream(sourceFile).getChannel();
45        destination = new FileOutputStream(destFile).getChannel();
46        destination.transferFrom(source, 0, source.size());
47      } finally {
48        closeQuietly(source);
49        closeQuietly(destination);
50      }
51  
52    }
53  
54    private static void closeQuietly(Closeable closeable) {
55      if (closeable != null) {
56        try {
57          closeable.close();
58        } catch (IOException e) {
59          // do nothing, close quietly
60        }
61      }
62    }
63  
64    public static String getConfiguredTemplate(String templatePath, String templateProperty) throws FileNotFoundException {
65      String templateName = "";
66      Properties migrationProperties = new Properties();
67  
68      try {
69        migrationProperties.load(new FileInputStream(templatePath));
70        templateName = migrationProperties.getProperty(templateProperty);
71      } catch (FileNotFoundException e) {
72        throw e;
73      } catch (Exception e) {
74        e.printStackTrace();
75      }
76  
77      return templateName;
78    }
79  
80  }