Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make About extensible and add extension for database info #565

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions alpine-common/src/main/java/alpine/common/AboutProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file is part of Alpine.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) Steve Springett. All Rights Reserved.
*/
package alpine.common;

import java.util.Map;

/**
* @since 2.2.6
*/
public interface AboutProvider {

/**
* @return The name of the component this provider collects "about" information for
*/
String name();

/**
* @return Information about the described component
*/
Map<String, Object> collect();

}
31 changes: 31 additions & 0 deletions alpine-model/src/main/java/alpine/model/About.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
package alpine.model;

import alpine.Config;
import alpine.common.AboutProvider;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import javax.inject.Singleton;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

/**
* A value object describing the name of the application, version, and the timestamp when it was built.
Expand Down Expand Up @@ -91,4 +98,28 @@ public String getUuid() {
public String getSystemUuid() {
return SYSTEM_UUID;
}

/**
* @return Additional information provided by {@link AboutProvider}s
* @since 2.2.6
*/
@JsonAnyGetter
@JsonInclude(Include.NON_EMPTY)
@SuppressWarnings("unused") // Called by JSON serializer.
public Map<String, Object> getProviderData() {
final ServiceLoader<AboutProvider> providers = ServiceLoader.load(AboutProvider.class);

final var data = new HashMap<String, Object>();
for (final AboutProvider provider : providers) {
final String providerName = provider.name();
final Map<String, Object> providerData = provider.collect();

if (providerData != null && !providerData.isEmpty()) {
data.put(providerName, providerData);
}
}

return data;
}

}
24 changes: 24 additions & 0 deletions alpine-model/src/test/java/alpine/model/AboutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
*/
package alpine.model;

import alpine.common.AboutProvider;
import alpine.common.util.UuidUtil;
import org.junit.Assert;
import org.junit.Test;

import java.util.Map;

public class AboutTest {

@Test
Expand All @@ -36,5 +39,26 @@ public void getterTest() {
Assert.assertTrue(about.getFramework().getVersion().startsWith("2."));
Assert.assertTrue(about.getFramework().getTimestamp().startsWith("20"));
Assert.assertTrue(UuidUtil.isValidUUID(about.getFramework().getUuid()));

final Map<String, Object> providerData = about.getProviderData();
Assert.assertEquals(1, providerData.size());
Assert.assertNotNull(providerData.get("test"));
Assert.assertTrue(providerData.get("test") instanceof Map);
Assert.assertEquals("bar", ((Map<String, Object>) providerData.get("test")).get("foo"));
}

public static class TestProvider implements AboutProvider {

@Override
public String name() {
return "test";
}

@Override
public Map<String, Object> collect() {
return Map.of("foo", "bar");
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alpine.model.AboutTest$TestProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* This file is part of Alpine.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) Steve Springett. All Rights Reserved.
*/
package alpine.server.persistence;

import alpine.common.AboutProvider;
import alpine.common.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.jdo.datastore.JDOConnection;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import static alpine.server.persistence.PersistenceManagerFactory.createPersistenceManager;

/**
* An {@link AboutProvider} for database information.
*
* @since 2.2.6
*/
public class DatabaseAboutProvider implements AboutProvider {

private static final Logger LOGGER = Logger.getLogger(DatabaseAboutProvider.class);

@Override
public String name() {
return "database";
}

@Override
public Map<String, Object> collect() {
final var data = new HashMap<String, Object>();

try (final PersistenceManager pm = createPersistenceManager()) {
final JDOConnection jdoConnection = pm.getDataStoreConnection();
final var nativeConnection = (Connection) jdoConnection.getNativeConnection();
try {
final DatabaseMetaData databaseMetaData = nativeConnection.getMetaData();
data.put("productName", databaseMetaData.getDatabaseProductName());
data.put("productVersion", databaseMetaData.getDatabaseProductVersion());
} catch (SQLException e) {
LOGGER.error("Failed to retrieve database metadata", e);
} finally {
jdoConnection.close();
}
}

return data;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alpine.server.persistence.DatabaseAboutProvider