~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-support/dhis-support-external/src/main/java/org/hisp/dhis/external/location/DefaultLocationManager.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.external.location;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
17
 *
 
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.
 
28
 */
 
29
 
 
30
import java.io.BufferedInputStream;
 
31
import java.io.BufferedOutputStream;
 
32
import java.io.File;
 
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;
 
38
 
 
39
import org.apache.commons.logging.Log;
 
40
import org.apache.commons.logging.LogFactory;
 
41
 
 
42
import static java.io.File.separator;
 
43
 
 
44
/**
 
45
 * @author Lars Helge Overland
 
46
 * @version $Id$
 
47
 */
 
48
public class DefaultLocationManager
 
49
    implements LocationManager
 
50
{
 
51
    private static final Log log = LogFactory.getLog( DefaultLocationManager.class );
 
52
 
 
53
    private String externalDir = null;
 
54
 
 
55
    private String environmentVariable;
 
56
 
 
57
    public void setEnvironmentVariable( String environmentVariable )
 
58
    {
 
59
        this.environmentVariable = environmentVariable;
 
60
    }
 
61
    
 
62
    // -------------------------------------------------------------------------
 
63
    // Init
 
64
    // -------------------------------------------------------------------------
 
65
 
 
66
    public void init()
 
67
    {
 
68
        String path = System.getenv( environmentVariable );
 
69
        
 
70
        if ( path != null )
 
71
        {
 
72
            log.info( "Environment variable " + environmentVariable + " points to " + path );
 
73
            
 
74
            if ( directoryIsValid( new File( path ) ) )
 
75
            {
 
76
                externalDir = path;
 
77
            }
 
78
        }
 
79
        else
 
80
        {
 
81
            log.info( "Environment variable " + environmentVariable + " not set" );
 
82
        }
 
83
    }
 
84
 
 
85
    // -------------------------------------------------------------------------
 
86
    // LocationManager implementation
 
87
    // -------------------------------------------------------------------------
 
88
 
 
89
    // -------------------------------------------------------------------------
 
90
    // InputStream
 
91
    // -------------------------------------------------------------------------
 
92
 
 
93
    public InputStream getInputStream( String fileName )
 
94
        throws LocationManagerException
 
95
    {
 
96
        return getInputStream( fileName, new String[0] );
 
97
    }
 
98
 
 
99
    public InputStream getInputStream( String fileName, String... directories )
 
100
        throws LocationManagerException
 
101
    {
 
102
        File file = getFileForReading( fileName, directories );
 
103
        
 
104
        try
 
105
        {
 
106
            InputStream in = new BufferedInputStream( new FileInputStream( file ) );
 
107
            
 
108
            return in;
 
109
        }
 
110
        catch ( FileNotFoundException ex )
 
111
        {
 
112
            throw new LocationManagerException( "Could not find file", ex );
 
113
        }
 
114
    }
 
115
 
 
116
    // -------------------------------------------------------------------------
 
117
    // File for reading
 
118
    // -------------------------------------------------------------------------
 
119
 
 
120
    public File getFileForReading( String fileName )
 
121
        throws LocationManagerException
 
122
    {
 
123
        return getFileForReading( fileName, new String[0] );
 
124
    }
 
125
 
 
126
    public File getFileForReading( String fileName, String... directories )
 
127
        throws LocationManagerException
 
128
    {
 
129
        if ( externalDir == null )
 
130
        {
 
131
            throw new LocationManagerException( "External directory not set" );
 
132
        }
 
133
        
 
134
        StringBuffer directory = new StringBuffer();
 
135
        
 
136
        directory.append( externalDir + separator );
 
137
        
 
138
        for ( String dir : directories )
 
139
        {
 
140
            directory.append( dir + separator );
 
141
        }
 
142
        
 
143
        File file = new File( directory.toString(), fileName );
 
144
        
 
145
        if ( !canReadFile( file ) )
 
146
        {
 
147
            throw new LocationManagerException( "File " + file.getAbsolutePath() + " cannot be read" );
 
148
        }
 
149
        
 
150
        return file;
 
151
    }
 
152
 
 
153
    // -------------------------------------------------------------------------
 
154
    // OutputStream
 
155
    // -------------------------------------------------------------------------
 
156
 
 
157
    public OutputStream getOutputStream( String fileName )
 
158
        throws LocationManagerException
 
159
    {
 
160
        return getOutputStream( fileName, new String[0] );
 
161
    }
 
162
    
 
163
    public OutputStream getOutputStream( String fileName, String... directories )
 
164
        throws LocationManagerException
 
165
    {
 
166
        File file = getFileForWriting( fileName, directories );
 
167
        
 
168
        try
 
169
        {
 
170
            OutputStream out = new BufferedOutputStream( new FileOutputStream( file ) );
 
171
            
 
172
            return out;
 
173
        }
 
174
        catch ( FileNotFoundException ex )
 
175
        {
 
176
            throw new LocationManagerException( "Could not find file", ex );
 
177
        }
 
178
    }
 
179
    
 
180
    // -------------------------------------------------------------------------
 
181
    // File for writing
 
182
    // -------------------------------------------------------------------------
 
183
 
 
184
    public File getFileForWriting( String fileName )
 
185
        throws LocationManagerException
 
186
    {
 
187
        return getFileForWriting( fileName, new String[0] );
 
188
    }
 
189
    
 
190
    public File getFileForWriting( String fileName, String... directories )
 
191
        throws LocationManagerException
 
192
    {
 
193
        if ( externalDir == null )
 
194
        {
 
195
            throw new LocationManagerException( "External directory not set" );
 
196
        }
 
197
        
 
198
        StringBuffer directoryPath = new StringBuffer();
 
199
        
 
200
        directoryPath.append( externalDir + separator );
 
201
        
 
202
        for ( String dir : directories )
 
203
        {
 
204
            directoryPath.append( dir + separator );
 
205
        }
 
206
        
 
207
        File directory = new File( directoryPath.toString() );
 
208
        
 
209
        if ( !directoryIsValid( directory ) )
 
210
        {
 
211
            throw new LocationManagerException( "Directory " + directory.getAbsolutePath() + " cannot be created" );
 
212
        }
 
213
        
 
214
        File file = new File( directory, fileName );
 
215
        
 
216
        return file;
 
217
    }
 
218
 
 
219
    // -------------------------------------------------------------------------
 
220
    // External directory and environment variable
 
221
    // -------------------------------------------------------------------------
 
222
 
 
223
    public File getExternalDirectory()
 
224
        throws LocationManagerException
 
225
    {
 
226
        if ( externalDir == null )
 
227
        {
 
228
            throw new LocationManagerException( "External directory not set" );
 
229
        }
 
230
        
 
231
        return new File( externalDir );
 
232
    }
 
233
 
 
234
    public boolean externalDirectorySet()
 
235
    {
 
236
        return externalDir != null;
 
237
    }
 
238
    
 
239
    public String getEnvironmentVariable()
 
240
    {
 
241
        return environmentVariable;
 
242
    }
 
243
    
 
244
    // -------------------------------------------------------------------------
 
245
    // Supportive methods
 
246
    // -------------------------------------------------------------------------
 
247
    
 
248
    /**
 
249
     * Tests whether the file exists and can be read by the application.
 
250
     */
 
251
    private boolean canReadFile( File file )
 
252
    {
 
253
        if ( !file.exists() )
 
254
        {
 
255
            log.info( "File " + file.getAbsolutePath() + " does not exist" );
 
256
            
 
257
            return false;
 
258
        }
 
259
        
 
260
        if ( !file.canRead() )
 
261
        {
 
262
            log.info( "File " + file.getAbsolutePath() + " cannot be read" );
 
263
            
 
264
            return false;
 
265
        }
 
266
        
 
267
        return true;
 
268
    }
 
269
    
 
270
    /**
 
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.
 
275
     */
 
276
    private boolean directoryIsValid( File directory ) 
 
277
    {
 
278
        if ( directory.exists() )
 
279
        {
 
280
            if( !directory.canWrite() ) 
 
281
            {
 
282
                log.info( "Directory " + directory.getAbsolutePath() + " is not writeable" );                
 
283
                
 
284
                return false;
 
285
            }
 
286
        }
 
287
        else 
 
288
        {
 
289
            try 
 
290
            {
 
291
                if ( !directory.mkdirs() )
 
292
                {
 
293
                    log.info( "Directory " + directory.getAbsolutePath() + " cannot be created" );
 
294
                    
 
295
                    return false;
 
296
                }
 
297
            } 
 
298
            catch( SecurityException ex ) 
 
299
            {
 
300
                log.info( "Directory " + directory.getAbsolutePath() + " cannot be accessed" );
 
301
                
 
302
                return false;                
 
303
            }
 
304
        }
 
305
        
 
306
        return true;
 
307
    }
 
308
}