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
|
package eu.etaxonomy.cdm.api.application;
|
10
|
|
11
|
import java.io.File;
|
12
|
import java.io.FileOutputStream;
|
13
|
import java.io.IOException;
|
14
|
import java.io.InputStream;
|
15
|
import java.net.URL;
|
16
|
|
17
|
import org.apache.logging.log4j.LogManager;
|
18
|
import org.apache.logging.log4j.Logger;
|
19
|
import org.springframework.beans.BeansException;
|
20
|
import org.springframework.context.ApplicationContext;
|
21
|
import org.springframework.context.ApplicationContextAware;
|
22
|
|
23
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
24
|
import eu.etaxonomy.cdm.common.FileCopy;
|
25
|
import eu.etaxonomy.cdm.config.CdmPersistentXMLSource;
|
26
|
import eu.etaxonomy.cdm.config.ConfigFileUtil;
|
27
|
|
28
|
public class CdmApplicationUtils implements ApplicationContextAware {
|
29
|
private static final Logger logger = LogManager.getLogger(CdmApplicationUtils.class);
|
30
|
|
31
|
private static ApplicationContext applicationContext;
|
32
|
|
33
|
//directory of the resources (configfiles etc.)
|
34
|
static File fileResourceDir;
|
35
|
static final String MUST_EXIST_FILE = CdmPersistentXMLSource.CDMSOURCE_PATH + CdmPersistentXMLSource.CDMSOURCE_FILE_NAME;
|
36
|
|
37
|
// static final String MUST_EXIST_FILE = "persistence.xml";
|
38
|
// static final String MUST_EXIST_FILE = "applicationContext.xml";
|
39
|
|
40
|
/**
|
41
|
* Returns the directory path to the writable resources (cdm.datasources.xml and H2 databases).
|
42
|
* (Resources must be copied to this directory, this is automatically done for the cdm.datasources.xml)
|
43
|
* @return
|
44
|
* @throws IOException if resource directory is not writable
|
45
|
*/
|
46
|
public static File getWritableResourceDir() throws IOException{
|
47
|
//compute only once
|
48
|
if (fileResourceDir == null){
|
49
|
//workaround to find out in which environment the library is executed
|
50
|
URL url = CdmUtils.class.getResource(MUST_EXIST_FILE);
|
51
|
if (url != null){
|
52
|
String fileName = url.getPath();
|
53
|
if (fileName.contains("%20")) {
|
54
|
fileName = fileName.replaceAll("%20", " ");
|
55
|
}
|
56
|
File file = new File(fileName);
|
57
|
if (file.exists()){
|
58
|
fileResourceDir = file.getParentFile();
|
59
|
}else{
|
60
|
File homeDir = null;
|
61
|
if(applicationContext != null){
|
62
|
try {
|
63
|
ConfigFileUtil configFileUtil = applicationContext.getBean(ConfigFileUtil.class);
|
64
|
homeDir = configFileUtil.perUserCdmFolder();
|
65
|
} catch(Exception e) {
|
66
|
logger.info("Can not use ConfigFileUtil to determine perUserCdmFolder, due to: " + e.getClass().toString() + " - " + e.getMessage());
|
67
|
}
|
68
|
}
|
69
|
if (homeDir == null){
|
70
|
//no application context available
|
71
|
homeDir = ConfigFileUtil.getCdmHomeDirFallback();
|
72
|
}
|
73
|
file = new File(homeDir, "writableResources" );
|
74
|
|
75
|
file.mkdirs();
|
76
|
copyResources(file);
|
77
|
fileResourceDir = file;
|
78
|
}
|
79
|
}
|
80
|
logger.info("Resource directory: " + (fileResourceDir == null?"null":fileResourceDir.getAbsolutePath()));
|
81
|
}
|
82
|
return fileResourceDir;
|
83
|
}
|
84
|
|
85
|
static private void copyResources(File directory){
|
86
|
copyResource(directory, CdmPersistentXMLSource.CDMSOURCE_FILE_NAME);
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Copies a file from the classpath resource (e.g. jar-File) to the resources directory in the file system (get
|
91
|
* @param directory
|
92
|
* @param resourceFileName
|
93
|
*/
|
94
|
static private boolean copyResource(File directory, String resourceFileName){
|
95
|
try {
|
96
|
InputStream isDataSource = CdmUtils.class.getResourceAsStream(CdmPersistentXMLSource.CDMSOURCE_PATH + resourceFileName);
|
97
|
if (isDataSource != null){
|
98
|
File fileToCopy = new File(directory + File.separator + resourceFileName);
|
99
|
if (fileToCopy.createNewFile()){
|
100
|
FileOutputStream outStream = new FileOutputStream(fileToCopy);
|
101
|
FileCopy.copy(isDataSource, outStream);
|
102
|
//XmlHelp.saveToXml(XmlHelp.getBeansRoot(isDataSource).getDocument(), outStream, Format.getPrettyFormat());
|
103
|
}
|
104
|
return true;
|
105
|
}else{
|
106
|
logger.error("Input datasource file " + resourceFileName + " + could not be found");
|
107
|
}
|
108
|
} catch (IOException e) {
|
109
|
logger.error("File " + resourceFileName + " + could not be created");
|
110
|
throw new RuntimeException(e);
|
111
|
}
|
112
|
return false;
|
113
|
}
|
114
|
|
115
|
@Override
|
116
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
117
|
CdmApplicationUtils.applicationContext = applicationContext;
|
118
|
}
|
119
|
}
|