Revision cf29bfc1
Added by Andreas Müller almost 2 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/api/application/CdmApplicationUtils.java | ||
---|---|---|
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 |
} |
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/config/CdmPersistentSourceUtils.java | ||
---|---|---|
22 | 22 |
import org.jdom.Document; |
23 | 23 |
import org.jdom.Element; |
24 | 24 |
|
25 |
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils; |
|
26 | 25 |
import eu.etaxonomy.cdm.common.XmlHelp; |
26 |
import eu.etaxonomy.cdm.persistence.utils.CdmPersistenceUtils; |
|
27 | 27 |
|
28 | 28 |
/** |
29 | 29 |
* Utility class which manages the persistent source settings |
... | ... | |
40 | 40 |
*/ |
41 | 41 |
public static String getResourceDirectory(){ |
42 | 42 |
try { |
43 |
File f = CdmApplicationUtils.getWritableResourceDir();
|
|
43 |
File f = CdmPersistenceUtils.getWritableResourceDir();
|
|
44 | 44 |
return f.getPath(); |
45 | 45 |
} catch (IOException e) { |
46 | 46 |
logger.error(e); |
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/config/CdmPersistentXMLSource.java | ||
---|---|---|
38 | 38 |
|
39 | 39 |
/** |
40 | 40 |
* Directory path of the template CDM sources config file - this file is used |
41 |
* in the case of the very first call to persist sources see {@link eu.etaxonomy.cdm.api.application.CdmApplicationUtils#getWritableResourceDir()}
|
|
41 |
* in the case of the very first call to persist sources see {@link eu.etaxonomy.cdm.persistence.utils.CdmPersistenceUtils#getWritableResourceDir()}
|
|
42 | 42 |
*/ |
43 | 43 |
public final static String CDMSOURCE_PATH = "/eu/etaxonomy/cdm/"; |
44 | 44 |
|
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/LocalH2.java | ||
---|---|---|
25 | 25 |
import org.h2.tools.Server; |
26 | 26 |
import org.springframework.jdbc.CannotGetJdbcConnectionException; |
27 | 27 |
|
28 |
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
|
|
28 |
import eu.etaxonomy.cdm.persistence.utils.CdmPersistenceUtils;
|
|
29 | 29 |
|
30 | 30 |
/** |
31 | 31 |
* @author a.mueller |
... | ... | |
166 | 166 |
private static final String getDefaultPath(){ |
167 | 167 |
File path; |
168 | 168 |
try { |
169 |
path = CdmApplicationUtils.getWritableResourceDir();
|
|
169 |
path = CdmPersistenceUtils.getWritableResourceDir();
|
|
170 | 170 |
} catch (IOException e) { |
171 | 171 |
logger.error(e); |
172 | 172 |
throw new RuntimeException(e); |
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/database/types/H2DatabaseType.java | ||
---|---|---|
18 | 18 |
import org.hibernate.dialect.Dialect; |
19 | 19 |
import org.hibernate.dialect.H2CorrectedDialect; |
20 | 20 |
|
21 |
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils; |
|
22 | 21 |
import eu.etaxonomy.cdm.database.H2Mode; |
23 | 22 |
import eu.etaxonomy.cdm.database.ICdmDataSource; |
24 | 23 |
import eu.etaxonomy.cdm.database.LocalH2; |
24 |
import eu.etaxonomy.cdm.persistence.utils.CdmPersistenceUtils; |
|
25 | 25 |
|
26 | 26 |
/** |
27 | 27 |
* @author a.mueller |
... | ... | |
147 | 147 |
|
148 | 148 |
private static final String getDefaultPath(){ |
149 | 149 |
try{ |
150 |
File path = CdmApplicationUtils.getWritableResourceDir();
|
|
150 |
File path = CdmPersistenceUtils.getWritableResourceDir();
|
|
151 | 151 |
String subPath = File.separator + "h2" + File.separator + "LocalH2"; |
152 | 152 |
return path + subPath; |
153 | 153 |
}catch(IOException e){ |
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/utils/CdmPersistenceUtils.java | ||
---|---|---|
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.persistence.utils; |
|
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 CdmPersistenceUtils implements ApplicationContextAware { |
|
29 |
|
|
30 |
private static final Logger logger = LogManager.getLogger(CdmPersistenceUtils.class); |
|
31 |
|
|
32 |
private static ApplicationContext applicationContext; |
|
33 |
|
|
34 |
//directory of the resources (configfiles etc.) |
|
35 |
static File fileResourceDir; |
|
36 |
static final String MUST_EXIST_FILE = CdmPersistentXMLSource.CDMSOURCE_PATH + CdmPersistentXMLSource.CDMSOURCE_FILE_NAME; |
|
37 |
|
|
38 |
// static final String MUST_EXIST_FILE = "persistence.xml"; |
|
39 |
// static final String MUST_EXIST_FILE = "applicationContext.xml"; |
|
40 |
|
|
41 |
/** |
|
42 |
* Returns the directory path to the writable resources (cdm.datasources.xml and H2 databases). |
|
43 |
* (Resources must be copied to this directory, this is automatically done for the cdm.datasources.xml) |
|
44 |
* @return |
|
45 |
* @throws IOException if resource directory is not writable |
|
46 |
*/ |
|
47 |
public static File getWritableResourceDir() throws IOException{ |
|
48 |
//compute only once |
|
49 |
if (fileResourceDir == null){ |
|
50 |
//workaround to find out in which environment the library is executed |
|
51 |
URL url = CdmUtils.class.getResource(MUST_EXIST_FILE); |
|
52 |
if (url != null){ |
|
53 |
String fileName = url.getPath(); |
|
54 |
if (fileName.contains("%20")) { |
|
55 |
fileName = fileName.replaceAll("%20", " "); |
|
56 |
} |
|
57 |
File file = new File(fileName); |
|
58 |
if (file.exists()){ |
|
59 |
fileResourceDir = file.getParentFile(); |
|
60 |
}else{ |
|
61 |
File homeDir = null; |
|
62 |
if(applicationContext != null){ |
|
63 |
try { |
|
64 |
ConfigFileUtil configFileUtil = applicationContext.getBean(ConfigFileUtil.class); |
|
65 |
homeDir = configFileUtil.perUserCdmFolder(); |
|
66 |
} catch(Exception e) { |
|
67 |
logger.info("Can not use ConfigFileUtil to determine perUserCdmFolder, due to: " + e.getClass().toString() + " - " + e.getMessage()); |
|
68 |
} |
|
69 |
} |
|
70 |
if (homeDir == null){ |
|
71 |
//no application context available |
|
72 |
homeDir = ConfigFileUtil.getCdmHomeDirFallback(); |
|
73 |
} |
|
74 |
file = new File(homeDir, "writableResources" ); |
|
75 |
|
|
76 |
file.mkdirs(); |
|
77 |
copyResources(file); |
|
78 |
fileResourceDir = file; |
|
79 |
} |
|
80 |
} |
|
81 |
logger.info("Resource directory: " + (fileResourceDir == null?"null":fileResourceDir.getAbsolutePath())); |
|
82 |
} |
|
83 |
return fileResourceDir; |
|
84 |
} |
|
85 |
|
|
86 |
static private void copyResources(File directory){ |
|
87 |
copyResource(directory, CdmPersistentXMLSource.CDMSOURCE_FILE_NAME); |
|
88 |
} |
|
89 |
|
|
90 |
/** |
|
91 |
* Copies a file from the classpath resource (e.g. jar-File) to the resources directory in the file system (get |
|
92 |
* @param directory |
|
93 |
* @param resourceFileName |
|
94 |
*/ |
|
95 |
static private boolean copyResource(File directory, String resourceFileName){ |
|
96 |
try { |
|
97 |
InputStream isDataSource = CdmUtils.class.getResourceAsStream(CdmPersistentXMLSource.CDMSOURCE_PATH + resourceFileName); |
|
98 |
if (isDataSource != null){ |
|
99 |
File fileToCopy = new File(directory + File.separator + resourceFileName); |
|
100 |
if (fileToCopy.createNewFile()){ |
|
101 |
FileOutputStream outStream = new FileOutputStream(fileToCopy); |
|
102 |
FileCopy.copy(isDataSource, outStream); |
|
103 |
//XmlHelp.saveToXml(XmlHelp.getBeansRoot(isDataSource).getDocument(), outStream, Format.getPrettyFormat()); |
|
104 |
} |
|
105 |
return true; |
|
106 |
}else{ |
|
107 |
logger.error("Input datasource file " + resourceFileName + " + could not be found"); |
|
108 |
} |
|
109 |
} catch (IOException e) { |
|
110 |
logger.error("File " + resourceFileName + " + could not be created"); |
|
111 |
throw new RuntimeException(e); |
|
112 |
} |
|
113 |
return false; |
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
|
118 |
CdmPersistenceUtils.applicationContext = applicationContext; |
|
119 |
} |
|
120 |
} |
cdmlib-persistence/src/test/java/eu/etaxonomy/cdm/test/function/TestCdmApplicationUtils.java | ||
---|---|---|
12 | 12 |
|
13 | 13 |
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger; |
14 | 14 |
|
15 |
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils;
|
|
15 |
import eu.etaxonomy.cdm.persistence.utils.CdmPersistenceUtils;
|
|
16 | 16 |
|
17 | 17 |
/** |
18 | 18 |
* @author a.mueller |
... | ... | |
23 | 23 |
private static final Logger logger = LogManager.getLogger(TestCdmApplicationUtils.class); |
24 | 24 |
|
25 | 25 |
private boolean testWritableResourceDirectory() throws IOException{ |
26 |
CdmApplicationUtils.getWritableResourceDir();
|
|
26 |
CdmPersistenceUtils.getWritableResourceDir();
|
|
27 | 27 |
return true; |
28 | 28 |
} |
29 | 29 |
|
cdmlib-services/src/test/java/eu/etaxonomy/cdm/test/function/TestScriptService.java | ||
---|---|---|
23 | 23 |
import org.springframework.transaction.TransactionStatus; |
24 | 24 |
|
25 | 25 |
import eu.etaxonomy.cdm.api.application.CdmApplicationController; |
26 |
import eu.etaxonomy.cdm.api.application.CdmApplicationUtils; |
|
27 | 26 |
import eu.etaxonomy.cdm.api.service.ITaxonNodeService; |
28 | 27 |
import eu.etaxonomy.cdm.api.service.ITaxonService; |
29 | 28 |
import eu.etaxonomy.cdm.api.service.UpdateResult; |
... | ... | |
61 | 60 |
import eu.etaxonomy.cdm.model.term.DefinedTermBase; |
62 | 61 |
import eu.etaxonomy.cdm.model.term.TermTree; |
63 | 62 |
import eu.etaxonomy.cdm.model.term.init.TermNotFoundException; |
63 |
import eu.etaxonomy.cdm.persistence.utils.CdmPersistenceUtils; |
|
64 | 64 |
|
65 | 65 |
public class TestScriptService { |
66 | 66 |
|
... | ... | |
384 | 384 |
} |
385 | 385 |
|
386 | 386 |
private boolean testWritableResourceDirectory() throws IOException{ |
387 |
CdmApplicationUtils.getWritableResourceDir();
|
|
387 |
CdmPersistenceUtils.getWritableResourceDir();
|
|
388 | 388 |
return true; |
389 | 389 |
} |
390 | 390 |
|
Also available in: Unified diff
move CdmApplicationUtils to real persistence package to avoid multiple scanning with service layer