• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aspectran / aspectran / #3986

15 Jan 2025 04:27AM CUT coverage: 35.056% (+0.05%) from 35.004%
#3986

push

github

topframe
Update

7 of 40 new or added lines in 6 files covered. (17.5%)

2 existing lines in 2 files now uncovered.

14194 of 40489 relevant lines covered (35.06%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/core/src/main/java/com/aspectran/core/context/builder/reload/ContextReloadingTask.java
1
/*
2
 * Copyright (c) 2008-2025 The Aspectran Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.aspectran.core.context.builder.reload;
17

18
import com.aspectran.core.service.ServiceLifeCycle;
19
import com.aspectran.utils.StringUtils;
20
import com.aspectran.utils.logging.Logger;
21
import com.aspectran.utils.logging.LoggerFactory;
22

23
import java.io.File;
24
import java.net.URI;
25
import java.net.URL;
26
import java.util.Enumeration;
27
import java.util.HashMap;
28
import java.util.Map;
29
import java.util.TimerTask;
30

31
import static com.aspectran.utils.ResourceUtils.JAR_URL_SEPARATOR;
32
import static com.aspectran.utils.ResourceUtils.URL_PROTOCOL_JAR;
33

34
public class ContextReloadingTask extends TimerTask {
35

NEW
36
    private static final Logger logger = LoggerFactory.getLogger(ContextReloadingTask.class);
×
37

38
    private final ServiceLifeCycle serviceLifeCycle;
39

40
    private final Map<String, Long> modifiedTimeMap = new HashMap<>();
×
41

42
    private boolean modified = false;
×
43

NEW
44
    public ContextReloadingTask(ServiceLifeCycle serviceLifeCycle) {
×
45
        this.serviceLifeCycle = serviceLifeCycle;
×
46
    }
×
47

48
    public void setResources(Enumeration<URL> resources) {
49
        if (resources != null) {
×
50
            while (resources.hasMoreElements()) {
×
51
                URL url = resources.nextElement();
×
52
                try {
53
                    File file;
54
                    if (URL_PROTOCOL_JAR.equals(url.getProtocol())) {
×
55
                        URL fileUrl = new URI(url.getFile()).toURL();
×
56
                        String[] parts = StringUtils.split(fileUrl.getFile(), JAR_URL_SEPARATOR);
×
57
                        file = new File(parts[0]);
×
58
                    } else {
×
59
                        file = new File(url.getFile());
×
60
                    }
61
                    String filePath = file.getAbsolutePath();
×
62
                    modifiedTimeMap.put(filePath, file.lastModified());
×
63
                } catch (Exception e) {
×
64
                    logger.error(e);
×
65
                }
×
66
            }
×
67
        }
68
    }
×
69

70
    @Override
71
    public void run() {
72
        if (modified || modifiedTimeMap.isEmpty()) {
×
73
            return;
×
74
        }
75
        for (Map.Entry<String, Long> entry : modifiedTimeMap.entrySet()) {
×
76
            String filePath = entry.getKey();
×
77
            long prevLastModifiedTime = entry.getValue();
×
78
            File file = new File(filePath);
×
79
            long lastModifiedTime = file.lastModified();
×
80
            if (prevLastModifiedTime != lastModifiedTime) {
×
81
                modified = true;
×
82
                modifiedTimeMap.put(filePath, lastModifiedTime);
×
83
                if (logger.isDebugEnabled()) {
×
84
                    logger.debug("Detected modified resource: " + filePath);
×
85
                }
86
            }
87
        }
×
88
        if (modified) {
×
89
            restartService();
×
90
        }
91
    }
×
92

93
    private void restartService() {
94
        try {
95
            String message = "Some resource file changes have been detected.";
×
96
            serviceLifeCycle.restart(message);
×
97
        } catch (Exception e) {
×
98
            // ignore
99
        } finally {
100
            modified = false;
×
101
        }
102
    }
×
103

104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc