Skip to content

Commit 4064f8f

Browse files
committed
Olog ES client preferences to use @preference
1 parent f549079 commit 4064f8f

File tree

7 files changed

+98
-130
lines changed

7 files changed

+98
-130
lines changed

app/logbook/olog/client-es/pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<artifactId>app-logbook-olog-client-es</artifactId>
1111

1212
<dependencies>
13-
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-core -->
1413
<dependency>
1514
<groupId>com.sun.jersey</groupId>
1615
<artifactId>jersey-core</artifactId>
@@ -21,7 +20,6 @@
2120
<artifactId>jersey-client</artifactId>
2221
<version>1.19</version>
2322
</dependency>
24-
<!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-multipart -->
2523
<dependency>
2624
<groupId>com.sun.jersey.contribs</groupId>
2725
<artifactId>jersey-multipart</artifactId>

app/logbook/olog/client-es/src/main/java/org/phoebus/olog/es/api/OlogClient.java

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import java.io.IOException;
4545
import java.io.InputStream;
4646
import java.net.URI;
47-
import java.net.http.HttpHeaders;
4847
import java.util.ArrayList;
4948
import java.util.Arrays;
5049
import java.util.Collection;
@@ -89,13 +88,9 @@ public static class OlogClientBuilder {
8988
private final String protocol;
9089
private String username = null;
9190
private String password = null;
92-
private String connectTimeoutAsString = null;
93-
private Boolean permissiveHostnameVerifier;
94-
95-
private final OlogProperties properties = new OlogProperties();
9691

9792
private OlogClientBuilder() {
98-
this.ologURI = URI.create(this.properties.getPreferenceValue("olog_url"));
93+
this.ologURI = URI.create(Preferences.olog_url);
9994
this.protocol = this.ologURI.getScheme();
10095
}
10196

@@ -157,45 +152,29 @@ public OlogClient create() {
157152
this.clientConfig = new DefaultClientConfig();
158153
}
159154
}
160-
if(this.username == null || this.password == null){
155+
if (this.username == null || this.password == null) {
161156
ScopedAuthenticationToken scopedAuthenticationToken = getCredentialsFromSecureStore();
162-
if(scopedAuthenticationToken != null){
157+
if (scopedAuthenticationToken != null) {
163158
this.username = scopedAuthenticationToken.getUsername();
164159
this.password = scopedAuthenticationToken.getPassword();
160+
} else {
161+
this.username = Preferences.username != null ? Preferences.username : this.username;
162+
this.password = Preferences.password != null ? Preferences.password : this.password;
165163
}
166-
else{
167-
this.username = ifNullReturnPreferenceValue(this.username, "username");
168-
this.password = ifNullReturnPreferenceValue(this.password, "password");
169-
}
170-
}
171-
this.connectTimeoutAsString = ifNullReturnPreferenceValue(this.connectTimeoutAsString, "connectTimeout");
172-
int connectTimeout = 0;
173-
try {
174-
connectTimeout = Integer.parseInt(connectTimeoutAsString);
175-
} catch (NumberFormatException e) {
176-
Logger.getLogger(OlogClientBuilder.class.getPackageName())
177-
.warning("connectTimeout preference not set or invalid, using 0 (=infinite)");
178164
}
165+
166+
int connectTimeout = Preferences.connectTimeout;
179167
this.clientConfig.getProperties().put(ClientConfig.PROPERTY_CONNECT_TIMEOUT, connectTimeout);
180168

181-
this.permissiveHostnameVerifier = Boolean.parseBoolean(this.properties.getPreferenceValue("permissive_hostname_verifier"));
182-
if (this.permissiveHostnameVerifier) {
169+
if (Preferences.permissive_hostname_verifier) {
183170
HostnameVerifier allHostsValid = (hostname, session) -> true;
184171
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
185172
}
186173

187174
return new OlogClient(this.ologURI, this.clientConfig, this.withHTTPAuthentication, this.username, this.password);
188175
}
189176

190-
private String ifNullReturnPreferenceValue(String value, String key) {
191-
if (value == null) {
192-
return this.properties.getPreferenceValue(key);
193-
} else {
194-
return value;
195-
}
196-
}
197-
198-
private ScopedAuthenticationToken getCredentialsFromSecureStore(){
177+
private ScopedAuthenticationToken getCredentialsFromSecureStore() {
199178
try {
200179
SecureStore secureStore = new SecureStore();
201180
return secureStore.getScopedAuthenticationToken(AuthenticationScope.LOGBOOK);
@@ -405,24 +384,9 @@ public List<LogEntry> listLogs() {
405384
return new ArrayList<>();
406385
}
407386

408-
/**
409-
* List of level values as defined in the properties file.
410-
*/
411-
private List<String> levels;
412-
413-
/**
414-
* Service URL as configured by properties.
415-
*/
416-
private String serviceUrl;
417-
418387
@Override
419388
public Collection<String> listLevels() {
420-
if (levels == null) {
421-
OlogProperties ologProperties = new OlogProperties();
422-
String[] levelList = ologProperties.getPreferenceValue("levels").split(",");
423-
levels = Arrays.asList(levelList);
424-
}
425-
return levels;
389+
return Arrays.stream(Preferences.levels).toList();
426390
}
427391

428392
@Override
@@ -466,11 +430,7 @@ public Collection<Tag> listTags() {
466430

467431
@Override
468432
public String getServiceUrl() {
469-
if (serviceUrl == null) {
470-
OlogProperties ologProperties = new OlogProperties();
471-
serviceUrl = ologProperties.getPreferenceValue("olog_url");
472-
}
473-
return serviceUrl;
433+
return Preferences.olog_url;
474434
}
475435

476436
@Override
@@ -549,7 +509,7 @@ public String serviceInfo() {
549509
}
550510

551511
@Override
552-
public SearchResult getArchivedEntries(long id){
512+
public SearchResult getArchivedEntries(long id) {
553513
try {
554514
final OlogSearchResult ologSearchResult = OlogObjectMappers.logEntryDeserializer.readValue(
555515
service.path("logs/archived/" + id)
@@ -566,7 +526,7 @@ public SearchResult getArchivedEntries(long id){
566526
}
567527

568528
@Override
569-
public Collection<LogTemplate> getTemplates(){
529+
public Collection<LogTemplate> getTemplates() {
570530
try {
571531
return OlogObjectMappers.logEntryDeserializer.readValue(
572532
service.path("templates").accept(MediaType.APPLICATION_JSON).get(String.class),
@@ -579,7 +539,7 @@ public Collection<LogTemplate> getTemplates(){
579539
}
580540

581541
@Override
582-
public LogTemplate saveTemplate(LogTemplate template) throws LogbookException{
542+
public LogTemplate saveTemplate(LogTemplate template) throws LogbookException {
583543
ClientResponse clientResponse = service.path("templates").accept(MediaType.APPLICATION_JSON_TYPE)
584544
.header("Content-Type", MediaType.APPLICATION_JSON_TYPE)
585545
.put(ClientResponse.class, template);

app/logbook/olog/client-es/src/main/java/org/phoebus/olog/es/api/OlogProperties.java

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (C) 2024 European Spallation Source ERIC.
3+
*/
4+
5+
package org.phoebus.olog.es.api;
6+
7+
import org.phoebus.framework.preferences.AnnotatedPreferences;
8+
import org.phoebus.framework.preferences.Preference;
9+
10+
public class Preferences {
11+
12+
@Preference
13+
public static String olog_url;
14+
15+
@Preference
16+
public static int connectTimeout;
17+
18+
@Preference
19+
public static boolean permissive_hostname_verifier;
20+
21+
@Preference
22+
public static String username;
23+
24+
@Preference
25+
public static String password;
26+
27+
@Preference
28+
public static boolean debug;
29+
30+
@Preference public static String[] levels;
31+
32+
static
33+
{
34+
AnnotatedPreferences.initialize(Preferences.class, "/olog_es_preferences.properties");
35+
}
36+
}

app/logbook/olog/ui/src/main/java/org/phoebus/logbook/olog/ui/AdvancedSearchViewController.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import javafx.beans.value.ObservableValue;
2525
import javafx.fxml.FXML;
2626
import javafx.geometry.Pos;
27-
import javafx.scene.Node;
2827
import javafx.scene.control.Button;
2928
import javafx.scene.control.ComboBox;
3029
import javafx.scene.control.Label;
@@ -39,6 +38,7 @@
3938
import org.phoebus.logbook.LogClient;
4039
import org.phoebus.logbook.Logbook;
4140
import org.phoebus.logbook.Tag;
41+
import org.phoebus.olog.es.api.Preferences;
4242
import org.phoebus.ui.dialog.ListSelectionPopOver;
4343
import org.phoebus.ui.dialog.PopOver;
4444
import org.phoebus.ui.time.TimeRelativeIntervalPane;
@@ -51,7 +51,6 @@
5151
import java.util.Collections;
5252
import java.util.List;
5353
import java.util.Map;
54-
import java.util.logging.Logger;
5554
import java.util.stream.Collectors;
5655

5756
import static org.phoebus.logbook.olog.ui.LogbookQueryUtil.Keys;
@@ -62,8 +61,6 @@
6261
*/
6362
public class AdvancedSearchViewController {
6463

65-
static final Logger logger = Logger.getLogger(AdvancedSearchViewController.class.getName());
66-
6764
@FXML
6865
Label levelLabel;
6966

@@ -108,7 +105,7 @@ public class AdvancedSearchViewController {
108105
@FXML
109106
private TextField attachmentTypes;
110107

111-
private SearchParameters searchParameters;
108+
private final SearchParameters searchParameters;
112109

113110
private final SimpleBooleanProperty sortAscending = new SimpleBooleanProperty(false);
114111
private final SimpleBooleanProperty requireAttachments = new SimpleBooleanProperty(false);
@@ -254,7 +251,7 @@ public void initialize() {
254251
if (tagSearchPopover.isShowing()) {
255252
tagSearchPopover.hide();
256253
} else {
257-
List<String> selectedTags = Arrays.stream( searchParameters.tagsProperty().getValueSafe().split(","))
254+
List<String> selectedTags = Arrays.stream(searchParameters.tagsProperty().getValueSafe().split(","))
258255
.map(String::trim)
259256
.filter(it -> !it.isEmpty())
260257
.collect(Collectors.toList());
@@ -272,7 +269,7 @@ public void initialize() {
272269
if (logbookSearchPopover.isShowing()) {
273270
logbookSearchPopover.hide();
274271
} else {
275-
List<String> selectedLogbooks = Arrays.stream( searchParameters.logbooksProperty().getValueSafe().split(","))
272+
List<String> selectedLogbooks = Arrays.stream(searchParameters.logbooksProperty().getValueSafe().split(","))
276273
.map(String::trim)
277274
.filter(it -> !it.isEmpty())
278275
.collect(Collectors.toList());
@@ -286,7 +283,7 @@ public void initialize() {
286283
}
287284
});
288285

289-
List<String> levelList = logClient.listLevels().stream().collect(Collectors.toList());
286+
List<String> levelList = Arrays.stream(Preferences.levels).toList();
290287
levelSelector.getItems().add("");
291288
levelSelector.getItems().addAll(levelList);
292289

@@ -307,15 +304,15 @@ public AnchorPane getPane() {
307304
/**
308305
* Updates non-text field controls so that search parameter values are correctly rendered.
309306
*
310-
* @param queryString
307+
* @param queryString Query string containing search terms and values
311308
*/
312309
private void updateControls(String queryString) {
313310
Map<String, String> queryStringParameters = LogbookQueryUtil.parseHumanReadableQueryString(queryString);
314311
queryStringParameters.entrySet().stream().forEach(entry -> {
315312
Keys keys = Keys.findKey(entry.getKey());
316313
if (keys != null) {
317314
if (keys.equals(Keys.LEVEL)) {
318-
List<String> levels = logClient.listLevels().stream().collect(Collectors.toList());
315+
List<String> levels = Arrays.stream(Preferences.levels).toList();
319316
if (levels.contains(entry.getValue())) {
320317
searchParameters.levelProperty().setValue(entry.getValue());
321318
} else {
@@ -371,7 +368,7 @@ protected List<String> getValidatedTagsSelection(String tags) {
371368
return validatedLogbookNames;
372369
}
373370

374-
public SimpleBooleanProperty getSortAscending(){
371+
public SimpleBooleanProperty getSortAscending() {
375372
return sortAscending;
376373
}
377374

0 commit comments

Comments
 (0)