1
package org.hisp.dhis.external.location;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import java.io.BufferedInputStream;
31
import java.io.BufferedOutputStream;
33
import java.io.FileInputStream;
34
import java.io.FileNotFoundException;
35
import java.io.FileOutputStream;
36
import java.io.InputStream;
37
import java.io.OutputStream;
39
import org.apache.commons.logging.Log;
40
import org.apache.commons.logging.LogFactory;
42
import static java.io.File.separator;
45
* @author Lars Helge Overland
48
public class DefaultLocationManager
49
implements LocationManager
51
private static final Log log = LogFactory.getLog( DefaultLocationManager.class );
53
private String externalDir = null;
55
private String environmentVariable;
57
public void setEnvironmentVariable( String environmentVariable )
59
this.environmentVariable = environmentVariable;
62
// -------------------------------------------------------------------------
64
// -------------------------------------------------------------------------
68
String path = System.getenv( environmentVariable );
72
log.info( "Environment variable " + environmentVariable + " points to " + path );
74
if ( directoryIsValid( new File( path ) ) )
81
log.info( "Environment variable " + environmentVariable + " not set" );
85
// -------------------------------------------------------------------------
86
// LocationManager implementation
87
// -------------------------------------------------------------------------
89
// -------------------------------------------------------------------------
91
// -------------------------------------------------------------------------
93
public InputStream getInputStream( String fileName )
94
throws LocationManagerException
96
return getInputStream( fileName, new String[0] );
99
public InputStream getInputStream( String fileName, String... directories )
100
throws LocationManagerException
102
File file = getFileForReading( fileName, directories );
106
InputStream in = new BufferedInputStream( new FileInputStream( file ) );
110
catch ( FileNotFoundException ex )
112
throw new LocationManagerException( "Could not find file", ex );
116
// -------------------------------------------------------------------------
118
// -------------------------------------------------------------------------
120
public File getFileForReading( String fileName )
121
throws LocationManagerException
123
return getFileForReading( fileName, new String[0] );
126
public File getFileForReading( String fileName, String... directories )
127
throws LocationManagerException
129
if ( externalDir == null )
131
throw new LocationManagerException( "External directory not set" );
134
StringBuffer directory = new StringBuffer();
136
directory.append( externalDir + separator );
138
for ( String dir : directories )
140
directory.append( dir + separator );
143
File file = new File( directory.toString(), fileName );
145
if ( !canReadFile( file ) )
147
throw new LocationManagerException( "File " + file.getAbsolutePath() + " cannot be read" );
153
// -------------------------------------------------------------------------
155
// -------------------------------------------------------------------------
157
public OutputStream getOutputStream( String fileName )
158
throws LocationManagerException
160
return getOutputStream( fileName, new String[0] );
163
public OutputStream getOutputStream( String fileName, String... directories )
164
throws LocationManagerException
166
File file = getFileForWriting( fileName, directories );
170
OutputStream out = new BufferedOutputStream( new FileOutputStream( file ) );
174
catch ( FileNotFoundException ex )
176
throw new LocationManagerException( "Could not find file", ex );
180
// -------------------------------------------------------------------------
182
// -------------------------------------------------------------------------
184
public File getFileForWriting( String fileName )
185
throws LocationManagerException
187
return getFileForWriting( fileName, new String[0] );
190
public File getFileForWriting( String fileName, String... directories )
191
throws LocationManagerException
193
if ( externalDir == null )
195
throw new LocationManagerException( "External directory not set" );
198
StringBuffer directoryPath = new StringBuffer();
200
directoryPath.append( externalDir + separator );
202
for ( String dir : directories )
204
directoryPath.append( dir + separator );
207
File directory = new File( directoryPath.toString() );
209
if ( !directoryIsValid( directory ) )
211
throw new LocationManagerException( "Directory " + directory.getAbsolutePath() + " cannot be created" );
214
File file = new File( directory, fileName );
219
// -------------------------------------------------------------------------
220
// External directory and environment variable
221
// -------------------------------------------------------------------------
223
public File getExternalDirectory()
224
throws LocationManagerException
226
if ( externalDir == null )
228
throw new LocationManagerException( "External directory not set" );
231
return new File( externalDir );
234
public boolean externalDirectorySet()
236
return externalDir != null;
239
public String getEnvironmentVariable()
241
return environmentVariable;
244
// -------------------------------------------------------------------------
245
// Supportive methods
246
// -------------------------------------------------------------------------
249
* Tests whether the file exists and can be read by the application.
251
private boolean canReadFile( File file )
253
if ( !file.exists() )
255
log.info( "File " + file.getAbsolutePath() + " does not exist" );
260
if ( !file.canRead() )
262
log.info( "File " + file.getAbsolutePath() + " cannot be read" );
271
* Tests whether the directory is writable by the application if the directory
272
* exists. Tries to create the directory including necessary parent directories
273
* if the directory does not exists, and tests whether the directory construction
274
* was successful and not prevented by a SecurityManager in any way.
276
private boolean directoryIsValid( File directory )
278
if ( directory.exists() )
280
if( !directory.canWrite() )
282
log.info( "Directory " + directory.getAbsolutePath() + " is not writeable" );
291
if ( !directory.mkdirs() )
293
log.info( "Directory " + directory.getAbsolutePath() + " cannot be created" );
298
catch( SecurityException ex )
300
log.info( "Directory " + directory.getAbsolutePath() + " cannot be accessed" );