Project

General

Profile

Download (3.89 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.api.application;
11

    
12
import java.io.File;
13
import java.io.FileOutputStream;
14
import java.io.IOException;
15
import java.io.InputStream;
16
import java.net.URL;
17

    
18
import org.apache.log4j.Logger;
19

    
20
import eu.etaxonomy.cdm.common.CdmUtils;
21
import eu.etaxonomy.cdm.common.FileCopy;
22
import eu.etaxonomy.cdm.config.CdmPersistentXMLSource;
23
import eu.etaxonomy.cdm.config.ConfigFileUtil;
24

    
25
public class CdmApplicationUtils {
26
    private static final Logger logger = Logger.getLogger(CdmApplicationUtils.class);
27

    
28
    //directory of the resources (configfiles etc.)
29
    static File fileResourceDir;
30
    static final String MUST_EXIST_FILE = CdmPersistentXMLSource.CDMSOURCE_PATH + CdmPersistentXMLSource.CDMSOURCE_FILE_NAME;
31

    
32
//	static final String MUST_EXIST_FILE = "persistence.xml";
33
//	static final String MUST_EXIST_FILE = "applicationContext.xml";
34

    
35
    /**
36
     * Returns the directory path to the writable resources (cdm.datasources.xml and H2 databases).
37
     * (Resources must be copied to this directory, this is automatically done for the cdm.datasources.xml)
38
     * @return
39
     * @throws IOException if resource directory is not writable
40
     */
41
    public static File getWritableResourceDir() throws IOException{
42
        //compute only once
43
        if (fileResourceDir == null){
44
            //workaround to find out in which environment the library is executed
45
            URL url = CdmUtils.class.getResource(MUST_EXIST_FILE);
46
            if (url != null){
47
                String fileName = url.getPath();
48
                if (fileName.contains("%20")) {
49
                    fileName = fileName.replaceAll("%20", " ");
50
                }
51
                File file = new File(fileName);
52
                if (file.exists()){
53
                    fileResourceDir = file.getParentFile();
54
                }else{
55
                    file = new File(ConfigFileUtil.getCdmHomeDir(), "writableResources" );
56

    
57
                    file.mkdirs();
58
                    copyResources(file);
59
                    fileResourceDir = file;
60
                }
61
            }
62
            logger.info("Resource directory: " + (fileResourceDir == null?"null":fileResourceDir.getAbsolutePath()));
63
        }
64
        return fileResourceDir;
65
    }
66

    
67
    static private void copyResources(File directory){
68
        copyResource(directory, CdmPersistentXMLSource.CDMSOURCE_FILE_NAME);
69
    }
70

    
71
    /**
72
     * Copies a file from the classpath resource (e.g. jar-File) to the resources directory in the file system (get
73
     * @param directory
74
     * @param resourceFileName
75
     */
76
    static private boolean copyResource(File directory, String resourceFileName){
77
        try {
78
            InputStream isDataSource = CdmUtils.class.getResourceAsStream(CdmPersistentXMLSource.CDMSOURCE_PATH + resourceFileName);
79
            if (isDataSource != null){
80
                File fileToCopy = new File(directory + File.separator + resourceFileName);
81
                if (fileToCopy.createNewFile()){
82
                    FileOutputStream outStream = new FileOutputStream(fileToCopy);
83
                    FileCopy.copy(isDataSource, outStream);
84
                    //XmlHelp.saveToXml(XmlHelp.getBeansRoot(isDataSource).getDocument(), outStream, Format.getPrettyFormat());
85
                }
86
                return true;
87
            }else{
88
                logger.error("Input datasource file "  + resourceFileName + " + could not be found");
89
            }
90
        } catch (IOException e) {
91
            logger.error("File "  + resourceFileName + " + could not be created");
92
            throw new RuntimeException(e);
93
        }
94
        return false;
95
    }
96

    
97

    
98
}
    (1-1/1)