Project

General

Profile

Download (4.6 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2015 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package org.bgbm.biovel.drf.store;
11

    
12
import java.io.File;
13
import java.io.FileInputStream;
14
import java.io.FileOutputStream;
15
import java.io.IOException;
16
import java.io.InputStream;
17

    
18
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
19
import org.apache.commons.compress.compressors.gzip.GzipUtils;
20
import org.apache.commons.compress.utils.IOUtils;
21
import org.apache.commons.io.FileUtils;
22
import org.apache.commons.io.FilenameUtils;
23
import org.apache.http.client.ClientProtocolException;
24
import org.apache.http.client.methods.CloseableHttpResponse;
25
import org.apache.http.client.methods.HttpGet;
26
import org.apache.http.impl.client.CloseableHttpClient;
27
import org.apache.http.impl.client.HttpClients;
28
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30

    
31
/**
32
 * @author a.kohlbecker
33
 * @date Oct 20, 2015
34
 *
35
 */
36
public abstract class Store {
37

    
38
    protected Logger logger = LoggerFactory.getLogger(Neo4jStore.class);
39
    private static final File tmpDir = new File(System.getProperty("java.io.tmpdir"));
40
    private static final File userHomeDir = new File(System.getProperty("user.home"));
41
    private static final File utisHome = new File(userHomeDir, ".utis");
42
    protected File storeLocation = null;
43

    
44
    public Store() throws Exception {
45
        storeLocation = new File(utisHome, storeName() + File.separator);
46
        if( !storeLocation.exists()) {
47
            storeLocation.mkdirs();
48
        }
49
        initStoreEngine();
50
    }
51

    
52
    /**
53
     * @return
54
     */
55
    protected abstract String storeName();
56

    
57
    /**
58
     * @param rdfFileUri
59
    *
60
    */
61
    protected File downloadAndExtract(String rdfFileUri) {
62
           File expandedFile = null;
63
           CloseableHttpClient httpClient = HttpClients.createDefault();
64
           CloseableHttpResponse response;
65
           try {
66
               // 1. download and store in local filesystem in TMP
67
               logger.debug("downloading rdf file from " + rdfFileUri);
68
               HttpGet httpGet = new HttpGet(rdfFileUri);
69
               response = httpClient.execute(httpGet);
70
               String archiveFileName = FilenameUtils.getName(httpGet.getURI().getRawPath());
71
               File archiveFile = new File(tmpDir, archiveFileName);
72
               FileOutputStream fout = new FileOutputStream(archiveFile);
73
               IOUtils.copy(response.getEntity().getContent(), new FileOutputStream(archiveFile));
74
               fout.close();
75
               logger.debug(archiveFile.length() + " bytes downloaded to " + archiveFile.getCanonicalPath());
76

    
77
               // 2. extract the archive
78
               FileInputStream fin = new FileInputStream(archiveFile);
79
               InputStream ain = null;
80

    
81
               if(GzipUtils.isCompressedFilename(archiveFileName)) {
82
                   logger.debug("Extracting GZIP file " + archiveFile.getCanonicalPath());
83
                   ain = new GzipCompressorInputStream(fin);
84
               } else {
85
                   // TO UNZIP
86
                   //ArchiveInputStream ain = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, fin);
87
               }
88

    
89
               expandedFile = new File(tmpDir, GzipUtils.getUncompressedFilename(archiveFileName));
90
               fout = new FileOutputStream(expandedFile);
91
               IOUtils.copy(ain, fout);
92
               fout.close();
93
               fin.close();
94
               logger.debug("Extracted to " + expandedFile.getCanonicalPath());
95
           } catch (ClientProtocolException e) {
96
               // TODO Auto-generated catch block
97
               e.printStackTrace();
98
           } catch (IOException e) {
99
               // TODO Auto-generated catch block
100
               e.printStackTrace();
101
           }
102
           return expandedFile;
103
       }
104

    
105
    /**
106
     *
107
     * @param rdfFileUri
108
     *  the location of the file to load the rdf triples from
109
     * @throws Exception
110
     */
111
    public void loadIntoStore(String rdfFileUri) throws Exception {
112
        File rdfFile = downloadAndExtract(rdfFileUri);
113
        clear();
114
        initStoreEngine();
115
        load(rdfFile);
116
    }
117

    
118
    protected abstract void initStoreEngine() throws Exception;
119

    
120
    protected abstract void load(File rdfFile) throws Exception;
121

    
122
    protected void clear() throws IOException {
123

    
124
        if(storeLocation.exists()) {
125
            FileUtils.cleanDirectory(storeLocation);
126
        } else {
127
            storeLocation.mkdirs();
128
        }
129
    }
130

    
131
}
(2-2/3)