Project

General

Profile

Download (4.14 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 homeDir = ConfigFileUtil.getCdmHomeDir();
56
                    if (homeDir.getPath().equals("null")){
57
                        //no application context available
58
                        homeDir = ConfigFileUtil.getCdmHomeDirFallback();
59
                    }
60
                    file = new File(homeDir, "writableResources" );
61

    
62
                    file.mkdirs();
63
                    copyResources(file);
64
                    fileResourceDir = file;
65
                }
66
            }
67
            logger.info("Resource directory: " + (fileResourceDir == null?"null":fileResourceDir.getAbsolutePath()));
68
        }
69
        return fileResourceDir;
70
    }
71

    
72
    static private void copyResources(File directory){
73
        copyResource(directory, CdmPersistentXMLSource.CDMSOURCE_FILE_NAME);
74
    }
75

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

    
102

    
103
}
    (1-1/1)