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

aspectran / aspectran / #3716

20 Sep 2024 01:10AM CUT coverage: 34.073% (-0.008%) from 34.081%
#3716

push

github

topframe
Bump commons-io:commons-io from 2.16.1 to 2.17.0

Bumps commons-io:commons-io from 2.16.1 to 2.17.0.

---
updated-dependencies:
- dependency-name: commons-io:commons-io
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

13345 of 39166 relevant lines covered (34.07%)

0.34 hits per line

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

82.95
/daemon/src/main/java/com/aspectran/daemon/AbstractDaemon.java
1
/*
2
 * Copyright (c) 2008-2024 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.daemon;
17

18
import com.aspectran.core.context.config.AspectranConfig;
19
import com.aspectran.core.context.config.DaemonConfig;
20
import com.aspectran.core.context.config.DaemonExecutorConfig;
21
import com.aspectran.core.context.config.DaemonPollingConfig;
22
import com.aspectran.core.util.Aspectran;
23
import com.aspectran.daemon.command.CommandExecutor;
24
import com.aspectran.daemon.command.CommandRegistry;
25
import com.aspectran.daemon.command.DaemonCommandRegistry;
26
import com.aspectran.daemon.command.builtins.QuitCommand;
27
import com.aspectran.daemon.command.polling.DefaultFileCommander;
28
import com.aspectran.daemon.command.polling.FileCommander;
29
import com.aspectran.daemon.service.DaemonService;
30
import com.aspectran.daemon.service.DefaultDaemonService;
31
import com.aspectran.daemon.service.DefaultDaemonServiceBuilder;
32
import com.aspectran.utils.StringUtils;
33
import com.aspectran.utils.annotation.jsr305.NonNull;
34
import com.aspectran.utils.annotation.jsr305.Nullable;
35
import com.aspectran.utils.apon.AponParseException;
36
import com.aspectran.utils.apon.AponReader;
37

38
import java.io.File;
39

40
/**
41
 * The Abstract Daemon.
42
 *
43
 * <p>Created: 2017. 12. 11.</p>
44
 *
45
 * @since 5.1.0
46
 */
47
public class AbstractDaemon implements Daemon {
1✔
48

49
    private String name;
50

51
    private DefaultDaemonService daemonService;
52

53
    private CommandExecutor commandExecutor;
54

55
    private FileCommander fileCommander;
56

57
    private CommandRegistry commandRegistry;
58

59
    private boolean waiting;
60

61
    private Thread thread;
62

63
    private volatile boolean active;
64

65
    @Override
66
    public String getName() {
67
        return name;
×
68
    }
69

70
    @Override
71
    public void setName(String name) {
72
        this.name = name;
×
73
    }
×
74

75
    @Override
76
    public String getBasePath() {
77
        return (daemonService != null ? daemonService.getBasePath() : null);
1✔
78
    }
79

80
    @Override
81
    public DaemonService getDaemonService() {
82
        return daemonService;
1✔
83
    }
84

85
    @Override
86
    public CommandExecutor getCommandExecutor() {
87
        return commandExecutor;
1✔
88
    }
89

90
    @Override
91
    public FileCommander getFileCommander() {
92
        return fileCommander;
1✔
93
    }
94

95
    @Override
96
    public CommandRegistry getCommandRegistry() {
97
        return commandRegistry;
1✔
98
    }
99

100
    @Override
101
    public boolean isWaiting() {
102
        return waiting;
×
103
    }
104

105
    @Override
106
    public boolean isActive() {
107
        return active;
×
108
    }
109

110
    protected void prepare(@Nullable String basePath, @Nullable File aspectranConfigFile) throws Exception {
111
        AspectranConfig aspectranConfig = new AspectranConfig();
1✔
112
        if (aspectranConfigFile != null) {
1✔
113
            try {
114
                AponReader.parse(aspectranConfigFile, aspectranConfig);
1✔
115
            } catch (AponParseException e) {
×
116
                throw new IllegalArgumentException("Failed to parse aspectran config file: " +
×
117
                    aspectranConfigFile, e);
118
            }
1✔
119
        }
120
        prepare(basePath, aspectranConfig);
1✔
121
    }
1✔
122

123
    protected void prepare(@Nullable String basePath, @NonNull AspectranConfig aspectranConfig) throws Exception {
124
        if (StringUtils.hasText(basePath)) {
1✔
125
            aspectranConfig.touchContextConfig().setBasePath(basePath);
1✔
126
        }
127

128
        startDaemonService(aspectranConfig);
1✔
129
        initDaemon(aspectranConfig.touchDaemonConfig());
1✔
130
    }
1✔
131

132
    private void initDaemon(DaemonConfig daemonConfig) throws Exception {
133
        Aspectran.printPrettyAboutMe(System.out);
1✔
134

135
        try {
136
            DaemonExecutorConfig executorConfig = daemonConfig.touchExecutorConfig();
1✔
137
            this.commandExecutor = new CommandExecutor(this, executorConfig);
1✔
138

139
            DaemonPollingConfig pollingConfig = daemonConfig.touchPollingConfig();
1✔
140
            this.fileCommander = new DefaultFileCommander(this, pollingConfig);
1✔
141

142
            DaemonCommandRegistry commandRegistry = new DaemonCommandRegistry(this);
1✔
143
            commandRegistry.addCommand(daemonConfig.getCommands());
1✔
144
            if (commandRegistry.getCommand(QuitCommand.class) == null) {
1✔
145
                commandRegistry.addCommand(QuitCommand.class);
1✔
146
            }
147
            this.commandRegistry = commandRegistry;
1✔
148
        } catch (Exception e) {
×
149
            throw new Exception("Failed to initialize daemon", e);
×
150
        }
1✔
151
    }
1✔
152

153
    private void startDaemonService(AspectranConfig aspectranConfig) throws Exception {
154
        try {
155
            this.daemonService = DefaultDaemonServiceBuilder.build(aspectranConfig);
1✔
156
            this.daemonService.start();
1✔
157
        } catch (Exception e) {
×
158
            throw new Exception("Failed to start daemon service", e);
×
159
        }
1✔
160
    }
1✔
161

162
    protected void start() throws Exception {
163
        start(false);
×
164
    }
×
165

166
    protected void start(boolean wait) throws Exception {
167
        if (wait) {
1✔
168
            start(0L);
×
169
        } else {
170
            start(-1L);
1✔
171
        }
172
    }
1✔
173

174
    protected void start(long waitTimeoutMillis) throws Exception {
175
        if (!active) {
1✔
176
            this.waiting = (waitTimeoutMillis >= 0L);
1✔
177
            if (name == null) {
1✔
178
                name = this.getClass().getSimpleName();
1✔
179
            }
180

181
            Runnable runnable = () -> {
1✔
182
                if (!active) {
1✔
183
                    active = true;
1✔
184
                    if (fileCommander != null) {
1✔
185
                        fileCommander.requeue();
1✔
186
                        while (active) {
1✔
187
                            try {
188
                                if (fileCommander != null) {
1✔
189
                                    fileCommander.polling();
1✔
190
                                    Thread.sleep(fileCommander.getPollingInterval());
×
191
                                }
192
                            } catch (InterruptedException ie) {
1✔
193
                                active = false;
1✔
194
                            }
1✔
195
                        }
196
                    }
197
                }
198
            };
1✔
199

200
            thread = new Thread(runnable, name);
1✔
201
            thread.start();
1✔
202
            if (waitTimeoutMillis >= 0L) {
1✔
203
                thread.join(waitTimeoutMillis);
1✔
204
            }
205
        }
206
    }
1✔
207

208
    @Override
209
    public void stop() {
210
        if (active) {
1✔
211
            active = false;
1✔
212
            if (thread != null) {
1✔
213
                if (thread.getState() == Thread.State.TIMED_WAITING) {
1✔
214
                    thread.interrupt();
1✔
215
                }
216
                thread = null;
1✔
217
            }
218
        }
219
    }
1✔
220

221
    @Override
222
    public void destroy() {
223
        stop();
1✔
224

225
        if (commandExecutor != null) {
1✔
226
            commandExecutor.shutdown();
1✔
227
        }
228
        if (fileCommander != null) {
1✔
229
            fileCommander = null;
1✔
230
        }
231
        if (daemonService != null) {
1✔
232
            daemonService.stop();
1✔
233
            daemonService = null;
1✔
234
        }
235
    }
1✔
236

237
}
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