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

aspectran / aspectran / #3636

07 Aug 2024 08:48PM CUT coverage: 34.07% (-0.01%) from 34.081%
#3636

Pull #685

github

web-flow
Bump org.awaitility:awaitility from 4.2.1 to 4.2.2

Bumps [org.awaitility:awaitility](https://github.com/awaitility/awaitility) from 4.2.1 to 4.2.2.
- [Changelog](https://github.com/awaitility/awaitility/blob/master/changelog.txt)
- [Commits](https://github.com/awaitility/awaitility/compare/awaitility-4.2.1...awaitility-4.2.2)

---
updated-dependencies:
- dependency-name: org.awaitility:awaitility
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #685: Bump org.awaitility:awaitility from 4.2.1 to 4.2.2

13344 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

63.85
/daemon/src/main/java/com/aspectran/daemon/command/polling/DefaultFileCommander.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.command.polling;
17

18
import com.aspectran.core.context.config.DaemonPollingConfig;
19
import com.aspectran.daemon.Daemon;
20
import com.aspectran.daemon.command.CommandExecutor;
21
import com.aspectran.daemon.command.CommandParameters;
22
import com.aspectran.utils.FilenameUtils;
23
import com.aspectran.utils.ResourceUtils;
24
import com.aspectran.utils.annotation.jsr305.NonNull;
25
import com.aspectran.utils.annotation.jsr305.Nullable;
26
import com.aspectran.utils.apon.AponReader;
27
import com.aspectran.utils.apon.AponWriter;
28
import com.aspectran.utils.logging.Logger;
29
import com.aspectran.utils.logging.LoggerFactory;
30

31
import java.io.File;
32
import java.io.IOException;
33
import java.net.URI;
34
import java.time.LocalDateTime;
35
import java.time.format.DateTimeFormatter;
36
import java.util.Arrays;
37
import java.util.Comparator;
38

39
/**
40
 * File system-based commander.
41
 *
42
 * <p>Created: 2017. 12. 11.</p>
43
 */
44
public class DefaultFileCommander extends AbstractFileCommander {
45

46
    protected final Logger logger = LoggerFactory.getLogger(DefaultFileCommander.class);
1✔
47

48
    private static final String COMMANDS_PATH = "/cmd";
49

50
    private static final String QUEUED_PATH = COMMANDS_PATH + "/queued";
51

52
    private static final String COMPLETED_PATH = COMMANDS_PATH + "/completed";
53

54
    private static final String FAILED_PATH = COMMANDS_PATH + "/failed";
55

56
    private static final String DEFAULT_INCOMING_PATH = COMMANDS_PATH + "/incoming";
57

58
    private final Object lock = new Object();
1✔
59

60
    private final File incomingDir;
61

62
    private final File queuedDir;
63

64
    private final File completedDir;
65

66
    private final File failedDir;
67

68
    public DefaultFileCommander(Daemon daemon, DaemonPollingConfig pollingConfig) throws Exception {
69
        super(daemon, pollingConfig);
1✔
70

71
        try {
72
            File commandsDir = new File(getDaemon().getBasePath(), COMMANDS_PATH);
1✔
73
            commandsDir.mkdirs();
1✔
74

75
            File queuedDir = new File(getDaemon().getBasePath(), QUEUED_PATH);
1✔
76
            queuedDir.mkdirs();
1✔
77
            this.queuedDir = queuedDir;
1✔
78

79
            File completedDir = new File(getDaemon().getBasePath(), COMPLETED_PATH);
1✔
80
            completedDir.mkdirs();
1✔
81
            this.completedDir = completedDir;
1✔
82

83
            File failedDir = new File(getDaemon().getBasePath(), FAILED_PATH);
1✔
84
            failedDir.mkdirs();
1✔
85
            this.failedDir = failedDir;
1✔
86

87
            String incomingPath = pollingConfig.getIncoming(DEFAULT_INCOMING_PATH);
1✔
88
            File incomingDir;
89
            if (incomingPath.startsWith(ResourceUtils.FILE_URL_PREFIX)) {
1✔
90
                // Using url fully qualified paths
91
                URI uri = URI.create(incomingPath);
×
92
                incomingDir = new File(uri);
×
93
            } else {
×
94
                incomingDir = new File(getDaemon().getBasePath(), incomingPath);
1✔
95
            }
96
            incomingDir.mkdirs();
1✔
97
            this.incomingDir = incomingDir;
1✔
98

99
            File[] incomingFiles = retrieveCommandFiles(incomingDir);
1✔
100
            if (incomingFiles != null) {
1✔
101
                for (File file : incomingFiles) {
1✔
102
                    if (logger.isDebugEnabled()) {
×
103
                        logger.debug("Delete old incoming command file: " + file);
×
104
                    }
105
                    file.delete();
×
106
                }
107
            }
108
        } catch (Exception e) {
×
109
            throw new Exception("Could not create directory structure for file commander", e);
×
110
        }
1✔
111
    }
1✔
112

113
    public File getIncomingDir() {
114
        return incomingDir;
×
115
    }
116

117
    public File getQueuedDir() {
118
        return queuedDir;
×
119
    }
120

121
    public File getCompletedDir() {
122
        return completedDir;
×
123
    }
124

125
    public File getFailedDir() {
126
        return failedDir;
×
127
    }
128

129
    @Override
130
    public void requeue() {
131
        File[] queuedFiles = retrieveCommandFiles(queuedDir);
1✔
132
        if (queuedFiles != null) {
1✔
133
            if (isRequeuable()) {
1✔
134
                for (File file : queuedFiles) {
×
135
                    CommandParameters parameters = readCommandFile(file);
×
136
                    if (parameters != null) {
×
137
                        if (parameters.isRequeuable()) {
×
138
                            writeIncomingCommand(parameters, file.getName());
×
139
                        }
140
                        removeCommandFile(queuedDir, file.getName());
×
141
                    }
142
                }
143
            } else {
144
                for (File file : queuedFiles) {
1✔
145
                    removeCommandFile(queuedDir, file.getName());
×
146
                }
147
            }
148
        }
149
    }
1✔
150

151
    @Override
152
    public void polling() {
153
        File[] files = retrieveCommandFiles(incomingDir);
1✔
154
        if (files != null) {
1✔
155
            int limit = getCommandExecutor().getAvailableThreads();
1✔
156
            for (int i = 0; i < files.length && i < limit; i++) {
1✔
157
                File file = files[i];
1✔
158
                CommandParameters parameters = readCommandFile(file);
1✔
159
                if (parameters != null) {
1✔
160
                    String incomingFileName = file.getName();
1✔
161
                    String queuedFileName = writeQueuedCommand(parameters, incomingFileName);
1✔
162
                    if (queuedFileName != null) {
1✔
163
                        removeCommandFile(incomingDir, incomingFileName);
1✔
164
                        executeQueuedCommand(parameters, queuedFileName);
1✔
165
                    }
166
                }
167
            }
168
        }
169
    }
1✔
170

171
    private void executeQueuedCommand(final CommandParameters parameters, final String fileName) {
172
        if (logger.isDebugEnabled()) {
1✔
173
            logger.debug("Execute Command: " + fileName + System.lineSeparator() + parameters);
1✔
174
        }
175
        getCommandExecutor().execute(parameters, new CommandExecutor.Callback() {
1✔
176
            @Override
177
            public void success() {
178
                removeCommandFile(queuedDir, fileName);
1✔
179
                writeCompletedCommand(parameters, makeFileName());
1✔
180
                if (logger.isTraceEnabled()) {
1✔
181
                    logger.trace("Result of Completed Command: " + fileName + System.lineSeparator() + parameters);
×
182
                }
183
            }
1✔
184

185
            @Override
186
            public void failure() {
187
                removeCommandFile(queuedDir, fileName);
×
188
                writeFailedCommand(parameters, makeFileName());
×
189
                if (logger.isTraceEnabled()) {
×
190
                    logger.trace("Result of Failed Command: " + fileName + System.lineSeparator() + parameters);
×
191
                }
192
            }
×
193

194
            @NonNull
195
            private String makeFileName() {
196
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmssSSS");
1✔
197
                String datetime = formatter.format(LocalDateTime.now());
1✔
198
                return datetime + "_" + fileName;
1✔
199
            }
200
        });
201
    }
1✔
202

203
    private File[] retrieveCommandFiles(@NonNull File dir) {
204
        File[] files = dir.listFiles((file) -> (file.isFile() && file.getName().toLowerCase().endsWith(".apon")));
1✔
205
        if (files != null && files.length > 0) {
1✔
206
            Arrays.sort(files, Comparator.comparing(File::getName));
1✔
207
        }
208
        return files;
1✔
209
    }
210

211
    @Nullable
212
    private CommandParameters readCommandFile(@NonNull File file) {
213
        if (logger.isTraceEnabled()) {
1✔
214
            logger.trace("Read command file: " + file);
×
215
        }
216
        try {
217
            CommandParameters parameters = new CommandParameters();
1✔
218
            AponReader.parse(file, parameters);
1✔
219
            return parameters;
1✔
220
        } catch (Exception e) {
×
221
            logger.error("Failed to read command file: " + file, e);
×
222
            removeCommandFile(incomingDir, file.getName());
×
223
            return null;
×
224
        }
225
    }
226

227
    private void writeIncomingCommand(CommandParameters parameters, String fileName) {
228
        String written = writeCommandFile(incomingDir, fileName, parameters);
×
229
        if (logger.isDebugEnabled()) {
×
230
            logger.debug("Incoming Command: " + written + " in " + incomingDir);
×
231
        }
232
    }
×
233

234
    private String writeQueuedCommand(CommandParameters parameters, String fileName) {
235
        String written = writeCommandFile(queuedDir, fileName, parameters);
1✔
236
        if (logger.isDebugEnabled()) {
1✔
237
            logger.debug("Queued Command: " + written + " in " + queuedDir);
1✔
238
        }
239
        return written;
1✔
240
    }
241

242
    private void writeCompletedCommand(CommandParameters parameters, String fileName) {
243
        String written = writeCommandFile(completedDir, fileName, parameters);
1✔
244
        if (logger.isDebugEnabled()) {
1✔
245
            logger.debug("Completed Command: " + written + " in " + completedDir);
1✔
246
        }
247
    }
1✔
248

249
    private void writeFailedCommand(CommandParameters parameters, String fileName) {
250
        String written = writeCommandFile(failedDir, fileName, parameters);
×
251
        if (logger.isDebugEnabled()) {
×
252
            logger.debug("Failed Command: " + written + " in " + failedDir);
×
253
        }
254
    }
×
255

256
    @Nullable
257
    private String writeCommandFile(File dir, String fileName, CommandParameters parameters) {
258
        File file = null;
1✔
259
        try {
260
            synchronized (lock) {
1✔
261
                file = FilenameUtils.generateUniqueFile(new File(dir, fileName));
1✔
262
                file.createNewFile();
1✔
263
            }
1✔
264
            if (logger.isTraceEnabled()) {
1✔
265
                logger.trace("Write command file: " + file);
×
266
            }
267
            AponWriter aponWriter = new AponWriter(file).nullWritable(false);
1✔
268
            aponWriter.write(parameters);
1✔
269
            aponWriter.close();
1✔
270
            return file.getName();
1✔
271
        } catch (IOException e) {
×
272
            if (file != null) {
×
273
                logger.warn("Failed to write command file: " + file, e);
×
274
            } else {
275
                File f = new File(dir, fileName);
×
276
                logger.warn("Failed to write command file: " + f, e);
×
277
            }
278
            return null;
×
279
        }
280
    }
281

282
    private void removeCommandFile(File dir, String fileName) {
283
        File file = new File(dir, fileName);
1✔
284
        if (logger.isTraceEnabled()) {
1✔
285
            logger.trace("Delete command file: " + file);
×
286
        }
287
        if (!file.delete()) {
1✔
288
            logger.warn("Failed to delete command file: " + file);
×
289
        }
290
    }
1✔
291

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