Project

General

Profile

Download (4.82 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.IOException;
13
import java.net.URI;
14
import java.net.URISyntaxException;
15
import java.util.Arrays;
16
import java.util.Date;
17

    
18
import org.apache.commons.httpclient.util.DateParseException;
19
import org.apache.commons.httpclient.util.DateUtil;
20
import org.apache.http.Header;
21
import org.apache.http.HttpResponse;
22
import org.apache.http.client.ClientProtocolException;
23
import org.apache.http.client.methods.HttpHead;
24
import org.apache.http.impl.client.CloseableHttpClient;
25
import org.apache.http.impl.client.HttpClientBuilder;
26
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28

    
29
/**
30
 * @author a.kohlbecker
31
 * @date Oct 28, 2015
32
 *
33
 */
34
public class Neo4jStoreUpdater {
35

    
36
    protected Logger logger = LoggerFactory.getLogger(Neo4jStoreUpdater.class);
37

    
38
    private final URI testUrl;
39
    private final Neo4jStore store;
40
    private long interval_ms;
41

    
42
    private String[] resources = new String[0];
43

    
44
    public Neo4jStoreUpdater(Neo4jStore store, String testUrl) throws URISyntaxException {
45
        this.testUrl = new URI(testUrl);
46
        this.store = store;
47
    }
48

    
49
    /**
50
     * @param lastModified
51
     * @return
52
     */
53
    private Date getRemoteLastModified() {
54

    
55
        Date lastModified = null;
56
        CloseableHttpClient client = HttpClientBuilder.create().build();
57
        HttpHead request = new HttpHead(testUrl);
58
        try {
59
            HttpResponse response = client.execute(request);
60
            Header lastModifiedH = response.getFirstHeader("Last-Modified");
61
            lastModified = DateUtil.parseDate(lastModifiedH.getValue());
62
            logger.debug("Last-Modified: " + lastModifiedH.getValue());
63

    
64
        } catch (ClientProtocolException e) {
65
            logger.error("ClientProtocolException, if this problem persists it will block from updating neo4jStore", e);
66
        } catch (DateParseException e) {
67
            logger.error("Could not parse Last-Modified value from HTTP response, if this problem persists it will block from updating neo4jStore");
68
        } catch (IOException e) {
69
            logger.error("IOException, if this problem persists it will block from updating the neo4jStore", e);
70
        } finally {
71
            try {
72
                client.close();
73
            } catch (IOException e) {
74
                // IGNORE //
75
            }
76
        }
77
        return lastModified;
78
    }
79

    
80
    private Date checkNewerVersion() {
81

    
82
        logger.info("polling for updates at " + testUrl.toString());
83
        Date lastModified = getRemoteLastModified();
84
        if(store.getLastModified() == null || lastModified != null && lastModified.after(store.getLastModified())) {
85
            logger.info("remote resource is more recent:  " + DateUtil.formatDate(lastModified));
86
            return lastModified;
87
        }
88
        return null;
89
    }
90

    
91
    public void addResources(String ... resources) throws URISyntaxException {
92
       this.resources = resources;
93
    }
94

    
95
    public void watch(int intervalMinutes) {
96

    
97
        if(isRunningAsTest()) {
98
            updateIfNeeded();
99
        } else {
100
            this.interval_ms = 1000 * 60 * intervalMinutes;
101
            Thread updateThread = new Thread() {
102

    
103
                @Override
104
                public void run() {
105
                    updateIfNeeded();
106
                    try {
107
                        sleep(interval_ms);
108
                    } catch (InterruptedException e) {
109
                        logger.info("Neo4jStoreUpdater has been interrupted");
110
                    }
111
                }
112
            };
113
            updateThread.start();
114
        }
115

    
116

    
117
    }
118

    
119

    
120
    /**
121
     * @return
122
     */
123
    private boolean isRunningAsTest() {
124
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
125
        for (StackTraceElement stackTraceElement : stackTrace) {
126
            if(stackTraceElement.getClassName().startsWith("org.junit.runners")) {
127
                return true;
128
            }
129
        }
130
        return false;
131
    }
132

    
133
    /**
134
     * @param neo4jStore
135
     */
136
    public void updateStore(Date lastModified) {
137

    
138
        logger.info("Starting store update");
139

    
140
        try {
141
            store.loadIntoStore(resources);
142
            store.setLastModified(lastModified);
143
        } catch (Exception e) {
144
            throw new RuntimeException("Loading "
145
                    + Arrays.toString(resources) +
146
                    " into Neo4jStore failed",  e);
147
        }
148

    
149

    
150
        logger.info("Store update done.");
151
    }
152

    
153
    /**
154
     *
155
     */
156
    private void updateIfNeeded() {
157
        Date lastModified = checkNewerVersion();
158
        if(lastModified != null) {
159
            updateStore(lastModified);
160
        }
161
    }
162

    
163
}
(2-2/4)