Project

General

Profile

Download (4.8 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.cybertaxonomy.utis.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
            logger.debug("new store location created");
49
        }
50
        initStoreEngine();
51
    }
52

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

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

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

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

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

    
106
    /**
107
     *
108
     * @param rdfFileUri
109
     *  the location of the file to load the rdf triples from
110
     * @throws Exception
111
     */
112
    public void loadIntoStore(String ... rdfFileUri) throws Exception {
113
        stopStoreEngine();
114
        clear();
115
        initStoreEngine();
116
        for (String uri : rdfFileUri) {
117
            File localF = downloadAndExtract(uri);
118
            load(localF);
119
        }
120
    }
121

    
122
    protected abstract void initStoreEngine() throws Exception;
123

    
124
    protected abstract void stopStoreEngine() throws Exception;
125

    
126
    protected abstract void load(File rdfFile) throws Exception;
127

    
128
    protected void clear() throws IOException {
129

    
130
        if(storeLocation.exists()) {
131
            FileUtils.cleanDirectory(storeLocation);
132
        } else {
133
            storeLocation.mkdirs();
134
        }
135
    }
136

    
137
}
(2-2/3)