Project

General

Profile

« Previous | Next » 

Revision 44b37b30

Added by Andreas Kohlbecker about 5 years ago

ref #8189 moving folder funstins from CdmUtils to ConfigFileUtil

View differences:

cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/CdmUtils.java
10 10
package eu.etaxonomy.cdm.common;
11 11

  
12 12
import java.io.BufferedReader;
13
import java.io.File;
14 13
import java.io.IOException;
15 14
import java.io.InputStream;
16 15
import java.io.InputStreamReader;
......
31 30

  
32 31
import org.apache.commons.lang.StringUtils;
33 32
import org.apache.log4j.Logger;
34
import org.springframework.context.EnvironmentAware;
35
import org.springframework.core.env.Environment;
36 33

  
37 34
/**
38 35
 * Util class for consistent access to and creation of per instance application configuration files.
......
40 37
 * @author a.mueller
41 38
 * @author a.kohlbecker
42 39
 */
43
public class CdmUtils implements EnvironmentAware {
40
public class CdmUtils {
44 41

  
45 42
    private static final Logger logger = Logger.getLogger(CdmUtils.class);
46 43

  
47
    // ============= TODO externalize into CdmFileUtils class ? ========== //
48

  
49
    private static String userHome = null;
50

  
51
    /**
52
     * The per user cdm folder name: ".cdmLibrary"
53
     */
54
    private static final String CDM_FOLDER_NAME = ".cdmLibrary";
55

  
56
    /**
57
     * The per user cdm folder "~/.cdmLibrary"
58
     */
59
    public static File perUserCdmFolder = null;
60

  
61
    /**
62
     * suggested sub folder for web app related data and configurations.
63
     * Each webapp instance should use a dedicated subfolder or file
64
     * which is named by the data source bean id.
65
     */
66
    public static final String SUBFOLDER_WEBAPP = "remote-webapp";
67

  
68
    static final String MUST_EXIST_FILE = "MUST-EXIST.txt";
69

  
70
    //folder separator
71
    static String folderSeparator;
72

  
73
    protected Environment env;
74

  
75
    @Override
76
    public void setEnvironment(Environment environment) {
77
        this.env = environment;
78
        if(userHome == null){
79
            CdmUtils.userHome = env.getRequiredProperty("user.home");
80
            CdmUtils.perUserCdmFolder = new File(userHome + File.separator + CDM_FOLDER_NAME );
81
            logger.info("user.home is set to " + CdmUtils.userHome);
82
        }
83
    }
84

  
85
    public static File getCdmHomeDir() {
86
        return new File(perUserCdmFolder + File.separator);
87
    }
88

  
89
	/**
90
	 * Returns specified the sub folder of  {@link #CDM_FOLDER_NAME}.
91
	 * If the sub folder does not exist it will be created.
92
	 *
93
	 * @param subFolderName
94
	 * @return the sub folder or null in case the folder did not exist ant the attempt to create it has failed.
95
	 *
96
	 * @see {@link #SUBFOLDER_WEBAPP}
97
	 */
98
	public static File getCdmHomeSubDir(String subFolderName) {
99

  
100
		File parentFolder = getCdmHomeDir();
101
        return ensureSubfolderExists(parentFolder, subFolderName);
102
	}
103

  
104
	/**
105
     * Returns an instance specific folder folder in  {@link #CDM_FOLDER_NAME}/<code>subFolderName</code>
106
     * Non existing folders will be created.
107
     *
108
     * @param subFolderName
109
     *      The name of a subfolded. In most cases this will be {@link #SUBFOLDER_WEBAPP}
110
     * @param instanceName
111
     *      The name of the application instance. The name should be related to the data source id.
112
     * @return the sub folder or null in case the folder did not exist ant the attempt to create it has failed.
113
     *
114
     * @see {@link #SUBFOLDER_WEBAPP}
115
     */
116
    public static File getCdmInstanceSubDir(String subFolderName, String instanceName) {
117

  
118
        File subfolder = ensureSubfolderExists(getCdmHomeDir(), subFolderName);
119
        return ensureSubfolderExists(subfolder, instanceName);
120
    }
121

  
122
    /**
123
     * @param subFolderName
124
     * @param parentFolder
125
     * @return
126
     */
127
    private static File ensureSubfolderExists(File parentFolder, String subFolderName) {
128
        if (!parentFolder.exists()){
129
            if (!parentFolder.mkdir()) {
130
                throw new RuntimeException("Parent folder could not be created: " + parentFolder.getAbsolutePath());
131
            }
132
        }
133

  
134
        File subfolder = new File(parentFolder, subFolderName);
135
		// if the directory does not exist, create it
136
		if (!subfolder.exists()) {
137
			if (!subfolder.mkdir()) {
138
				throw new RuntimeException("Subfolder could not be created: " + subfolder.getAbsolutePath());
139
			}
140
		}
141
		return subfolder;
142
    }
143

  
144
	// ============= END of CdmFileUtils ========== //
145

  
146 44
    /**
147 45
     * Returns the an InputStream for a read-only source
148 46
     * @param resourceFileName the resources path within the classpath(!)
......
169 67
    }
170 68

  
171 69

  
172
    /**
173
     * @return
174
     */
175
    static public String getFolderSeperator(){
176
        if (folderSeparator == null){
177
            URL url = CdmUtils.class.getResource("/"+ MUST_EXIST_FILE);
178
            if ( url != null && ! urlIsJarOrBundle(url) ){
179
                folderSeparator =  File.separator;
180
            }else{
181
                folderSeparator = "/";
182
            }
183
        }
184
        return folderSeparator;
185
    }
186

  
187

  
188
    /**
189
     * @param url
190
     * @return
191
     */
192
    static private boolean urlIsJarOrBundle(URL url){
193
        return url.getProtocol().startsWith("jar") || url.getProtocol().startsWith("bundleresource");
194
    }
195 70

  
196 71
    /**
197 72
     * Returns the file name for the file in which 'clazz' is to be found (helps finding according libraries)

Also available in: Unified diff