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

aspectran / aspectran / #4075

19 Feb 2025 12:58PM CUT coverage: 35.181% (-0.004%) from 35.185%
#4075

push

github

topframe
Update

20 of 155 new or added lines in 9 files covered. (12.9%)

5 existing lines in 3 files now uncovered.

14252 of 40510 relevant lines covered (35.18%)

0.35 hits per line

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

0.0
/shell/src/main/java/com/aspectran/shell/service/AbstractShellService.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.shell.service;
17

18
import com.aspectran.core.adapter.SessionAdapter;
19
import com.aspectran.core.component.session.DefaultSessionManager;
20
import com.aspectran.core.component.session.SessionAgent;
21
import com.aspectran.core.context.asel.token.Token;
22
import com.aspectran.core.context.asel.token.TokenEvaluator;
23
import com.aspectran.core.context.asel.token.TokenParser;
24
import com.aspectran.core.context.config.AcceptableConfig;
25
import com.aspectran.core.context.config.AspectranConfig;
26
import com.aspectran.core.context.config.SessionManagerConfig;
27
import com.aspectran.core.context.config.ShellConfig;
28
import com.aspectran.core.service.CoreServiceException;
29
import com.aspectran.core.service.DefaultCoreService;
30
import com.aspectran.core.service.RequestAcceptor;
31
import com.aspectran.shell.adapter.ShellSessionAdapter;
32
import com.aspectran.shell.console.ShellConsole;
33
import com.aspectran.utils.Assert;
34
import com.aspectran.utils.StringUtils;
35
import com.aspectran.utils.annotation.jsr305.NonNull;
36
import com.aspectran.utils.logging.Logger;
37
import com.aspectran.utils.logging.LoggerFactory;
38

39
/**
40
 * Abstract base class for {@code ShellService} implementations.
41
 *
42
 * <p>Created: 2017. 10. 30.</p>
43
 */
44
public abstract class AbstractShellService extends DefaultCoreService implements ShellService {
45

46
    private static final Logger logger = LoggerFactory.getLogger(AbstractShellService.class);
×
47

48
    private final ShellConsole console;
49

50
    private DefaultSessionManager sessionManager;
51

52
    private SessionAgent sessionAgent;
53

54
    /** If verbose mode is on, a detailed description is printed each time the command is executed. */
55
    private boolean verbose;
56

57
    private String greetings;
58

59
    private Token[] greetingsTokens;
60

61
    AbstractShellService(ShellConsole console) {
62
        super();
×
63

64
        Assert.notNull(console, "console must not be null");
×
65
        this.console = console;
×
66
    }
×
67

68
    @Override
69
    public void afterContextLoaded() throws Exception {
70
        super.afterContextLoaded();
×
71
        parseGreetings();
×
72
    }
×
73

74
    @Override
75
    public ShellConsole getConsole() {
76
        return console;
×
77
    }
78

79
    /**
80
     * Tests if the verbose mode is enabled.
81
     * If verbose mode is on, a detailed description is printed each time the command is executed.
82
     * Returns a flag indicating whether to show the description or not.
83
     * @return true if the verbose mode is enabled
84
     */
85
    @Override
86
    public boolean isVerbose() {
87
        return verbose;
×
88
    }
89

90
    /**
91
     * Enables or disables the verbose mode.
92
     * If verbose mode is on, a detailed description is printed each time the command is executed.
93
     * Sets a flag indicating whether to show the description or not.
94
     * @param verbose true to enable the verbose mode; false to disable
95
     */
96
    @Override
97
    public void setVerbose(boolean verbose) {
98
        this.verbose = verbose;
×
99
    }
×
100

101
    @Override
102
    public String getGreetings() {
103
        return greetings;
×
104
    }
105

106
    @Override
107
    public void setGreetings(String greetings) {
108
        this.greetings = greetings;
×
109
    }
×
110

111
    @Override
112
    public void printGreetings() {
113
        if (greetingsTokens != null) {
×
114
            TokenEvaluator evaluator = getDefaultActivity().getTokenEvaluator();
×
115
            String message = evaluator.evaluateAsString(greetingsTokens);
×
116
            if (console.isReading()) {
×
117
                console.clearLine();
×
118
            }
119
            console.writeLine(message);
×
120
        } else if (greetings != null) {
×
121
            if (console.isReading()) {
×
122
                console.clearLine();
×
123
            }
124
            console.writeLine(greetings);
×
125
        }
126
    }
×
127

128
    private void parseGreetings() {
129
        if (StringUtils.hasText(greetings)) {
×
130
            greetingsTokens = TokenParser.makeTokens(greetings, true);
×
131
            if (greetingsTokens != null) {
×
132
                try {
133
                    for (Token token : greetingsTokens) {
×
NEW
134
                        Token.resolveValueProvider(token, getServiceClassLoader());
×
135
                    }
136
                } catch (Exception e) {
×
137
                    greetingsTokens = null;
×
138
                    logger.error("Failed to parse greetings", e);
×
139
                }
×
140
            }
141
        }
142
    }
×
143

144
    @Override
145
    public void printHelp() {
146
        if (isVerbose()) {
×
147
            String description = getActivityContext().getDescription();
×
148
            if (description != null) {
×
149
                console.writeLine(description);
×
150
            }
151
        }
152
    }
×
153

154
    @Override
155
    public SessionAdapter newSessionAdapter() {
156
        if (sessionAgent != null) {
×
157
            return new ShellSessionAdapter(sessionAgent);
×
158
        } else {
159
            return null;
×
160
        }
161
    }
162

163
    protected void createSessionManager() {
164
        Assert.state(this.sessionManager == null,
×
165
                "Session Manager is already exists for " + getServiceName());
×
166
        ShellConfig shellConfig = getAspectranConfig().getShellConfig();
×
167
        if (shellConfig != null) {
×
168
            SessionManagerConfig sessionManagerConfig = shellConfig.getSessionManagerConfig();
×
169
            if (sessionManagerConfig != null && sessionManagerConfig.isEnabled()) {
×
170
                try {
171
                    DefaultSessionManager sessionManager = new DefaultSessionManager();
×
172
                    sessionManager.setActivityContext(getActivityContext());
×
173
                    sessionManager.setSessionManagerConfig(sessionManagerConfig);
×
174
                    sessionManager.initialize();
×
175
                    this.sessionManager = sessionManager;
×
176
                    this.sessionAgent = new SessionAgent(sessionManager);
×
177
                } catch (Exception e) {
×
178
                    throw new CoreServiceException("Failed to create session manager for " + getServiceName(), e);
×
179
                }
×
180
            }
181
        }
182
    }
×
183

184
    protected void destroySessionManager() {
185
        if (sessionAgent != null) {
×
186
            sessionAgent.invalidate();
×
187
            sessionAgent = null;
×
188
        }
189
        if (sessionManager != null) {
×
190
            sessionManager.destroy();
×
191
            sessionManager = null;
×
192
        }
193
    }
×
194

195
    @Override
196
    public void restart(String message) throws Exception {
197
        if (StringUtils.hasText(message)) {
×
198
            console.setStyle(console.getDangerStyle());
×
199
            console.writeAbove(message);
×
200
            console.resetStyle();
×
201
        }
202
        if (!isBusy() && console.confirmRestart()) {
×
203
            try {
204
                super.restart(message);
×
205
            } catch (Exception e) {
×
206
                logger.error("Shell restart failed", e);
×
207
                console.setStyle(console.getDangerStyle());
×
208
                console.writeAbove("Shell restart failed!");
×
209
                console.resetStyle();
×
210
            }
×
211
            if (console.isReading()) {
×
212
                console.redrawLine();
×
213
            }
214
        }
215
    }
×
216

217
    @Override
218
    public boolean isBusy() {
219
        return console.isReading();
×
220
    }
221

222
    @Override
223
    protected void configure(@NonNull AspectranConfig aspectranConfig) {
224
        ShellConfig shellConfig = aspectranConfig.getShellConfig();
×
225
        if (shellConfig != null) {
×
226
            configure(shellConfig);
×
227
        }
228
        super.configure(aspectranConfig);
×
229
    }
×
230

231
    private void configure(@NonNull ShellConfig shellConfig) {
232
        setVerbose(shellConfig.isVerbose());
×
233
        setGreetings(shellConfig.getGreetings());
×
234
        AcceptableConfig acceptableConfig = shellConfig.getAcceptableConfig();
×
235
        if (acceptableConfig != null) {
×
236
            setRequestAcceptor(new RequestAcceptor(acceptableConfig));
×
237
        }
238
    }
×
239

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