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

Bug 564420 conditions | move HC condition miner to new interfaces #269

Merged
merged 2 commits into from
Jun 27, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -28,19 +28,19 @@
import org.eclipse.passage.lic.internal.base.io.PathFromLicensedProduct;
import org.eclipse.passage.lic.internal.equinox.i18n.EquinoxMessages;
import org.eclipse.passage.lic.internal.equinox.io.ConfigurationPath;
import org.eclipse.passage.lic.internal.equinox.io.InstalationPath;
import org.eclipse.passage.lic.internal.equinox.io.InstallationPath;

/**
* @deprecated in the favor of {@code lic.interal.base.io} Path suppliers
* accompanied with {@linkplain InstalationPath} and
* accompanied with {@linkplain InstallationPath} and
* {@linkplain ConfigurationPath}
*/
@Deprecated
@SuppressWarnings("restriction")
public class EquinoxPaths {

/**
* @deprecated use {@link InstalationPath} in decoration with
* @deprecated use {@link InstallationPath} in decoration with
* {@link PathFromLicensedProduct}
*/
@Deprecated
@@ -50,7 +50,7 @@ public static Path resolveInstallConfigurationPath(LicensingConfiguration config
}

/**
* @deprecated use {@link InstalationPath} in decoration with
* @deprecated use {@link InstallationPath} in decoration with
*/
@Deprecated
public static Path resolveInstallBasePath() {
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@
* </p>
*/
@SuppressWarnings("restriction")
public final class InstalationPath implements Supplier<Path> {
public final class InstallationPath implements Supplier<Path> {

@Override
public Path get() {
Original file line number Diff line number Diff line change
@@ -18,14 +18,11 @@ public class HcMessages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.passage.lic.internal.hc.i18n.HcMessages"; //$NON-NLS-1$

public static String DecryptedConditions_no_transport_for_content_type;

public static String DecryptedConditions_reading_error;

public static String HttpClient_final_error_message;
public static String HttpClient_not_ok_response;

public static String LicensingServerCoordinates_settings_not_found;

static {
NLS.initializeMessages(BUNDLE_NAME, HcMessages.class);
}
Original file line number Diff line number Diff line change
@@ -15,4 +15,3 @@ DecryptedConditions_no_transport_for_content_type=Unknown content type: no regis
DecryptedConditions_reading_error=Error reading remote condition data
HttpClient_final_error_message=Error mining conditions
HttpClient_not_ok_response=Mining connection responded not OK: %d ($s)
LicensingServerCoordinates_settings_not_found=%s is not found
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.hc.remote;
package org.eclipse.passage.lic.internal.net;

import java.nio.file.Path;
import java.util.Map;
@@ -20,9 +20,7 @@
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.base.StringNamedData;
import org.eclipse.passage.lic.internal.base.io.Settings;
import org.eclipse.passage.lic.internal.hc.i18n.HcMessages;
import org.eclipse.passage.lic.internal.net.LicensingServerHost;
import org.eclipse.passage.lic.internal.net.LicensingServerPort;
import org.eclipse.passage.lic.internal.net.i18n.NetMessages;

/**
* <p>
@@ -51,19 +49,23 @@ public LicensingServerCoordinates(Supplier<Path> residence) {
* @throws LicensingException in case of errors during file reading or setting
* analysis
*/
public String get() throws LicensingException {
public HostPort get() throws LicensingException {
Map<String, Object> properties = new Settings(settings, this::necessaryPropertiesExist).get();
return String.format(//
"%s:%s", //$NON-NLS-1$
return new HostPort(//
value(new LicensingServerHost(properties)), //
value(new LicensingServerPort(properties)));
}

private String value(StringNamedData data) throws LicensingException {
Optional<String> value = data.get();
if (value.isPresent()) {
throw new LicensingException(
String.format(HcMessages.LicensingServerCoordinates_settings_not_found, data.key()));
if (!value.isPresent()) {
throw new LicensingException(//
String.format(NetMessages.LicensingServerCoordinates_settings_not_found, data.key()));
}
if (value.get().trim().isEmpty()) {
throw new LicensingException(//
String.format(NetMessages.LicensingServerCoordinates_settings_are_blank, data.key()));

}
return value.get();
}
@@ -73,4 +75,22 @@ private boolean necessaryPropertiesExist(Map<String, Object> properties) {
new LicensingServerPort(properties).get().isPresent();
}

public static final class HostPort {
private final String host;
private final String port;

public HostPort(String host, String port) {
this.host = host;
this.port = port;
}

public String host() {
return host;
}

public String port() {
return port;
}

}
}
Original file line number Diff line number Diff line change
@@ -23,6 +23,15 @@ public LicensingServerHost(Map<String, Object> container) {
super(container);
}

public LicensingServerHost(String value) {
super(value);
}

@Override
public String entrySeparator() {
return "\n"; //$NON-NLS-1$
}

@Override
public String key() {
return "licensing.server.host"; //$NON-NLS-1$
Original file line number Diff line number Diff line change
@@ -23,6 +23,15 @@ public LicensingServerPort(Map<String, Object> container) {
super(container);
}

public LicensingServerPort(String value) {
super(value);
}

@Override
public String entrySeparator() {
return "\n"; //$NON-NLS-1$
}

@Override
public String key() {
return "licensing.server.port"; //$NON-NLS-1$
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.internal.net.i18n;
ruspl-afed marked this conversation as resolved.
Show resolved Hide resolved

import org.eclipse.osgi.util.NLS;

public class NetMessages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.passage.lic.internal.net.i18n.NetMessages"; //$NON-NLS-1$
public static String LicensingServerCoordinates_settings_are_blank;
public static String LicensingServerCoordinates_settings_not_found;
static {
// initialize resource bundle
NLS.initializeMessages(BUNDLE_NAME, NetMessages.class);
}

private NetMessages() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
###############################################################################
# Copyright (c) 2020 ArSysOp and others
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0/.
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# ArSysOp - initial API and implementation
###############################################################################

LicensingServerCoordinates_settings_are_blank=%s cannot be blank
ruspl-afed marked this conversation as resolved.
Show resolved Hide resolved
LicensingServerCoordinates_settings_not_found=%s is not found
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* https://www.eclipse.org/legal/epl-2.0/.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lic.net.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.base.NamedData;
import org.eclipse.passage.lic.internal.base.io.PassageFileExtension;
import org.eclipse.passage.lic.internal.net.LicensingServerCoordinates;
import org.eclipse.passage.lic.internal.net.LicensingServerCoordinates.HostPort;
import org.eclipse.passage.lic.internal.net.LicensingServerHost;
import org.eclipse.passage.lic.internal.net.LicensingServerPort;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

@SuppressWarnings("restriction")
public final class LicensingServerCoordinatesTest {

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void existingSettingsAreFound() throws IOException, LicensingException {
writeSettingsFile("some.server.ho", "1234"); //$NON-NLS-1$//$NON-NLS-2$
assertEquals("some.server.ho:1234", coordinates()); //$NON-NLS-1$
}

@Test
public void canDefineHostAndPortInDifferentFiles() throws IOException, LicensingException {
writeSettingsFile("some.server", null); //$NON-NLS-1$
writeSettingsFile(null, "56789"); //$NON-NLS-1$
assertEquals("some.server:56789", coordinates()); //$NON-NLS-1$
}

@Test(expected = LicensingException.class)
public void absentHostCausesFailure() throws IOException, LicensingException {
writeSettingsFile(null, "8080"); //$NON-NLS-1$
coordinates();
}

@Test(expected = LicensingException.class)
public void absentPortCausesFailure() throws IOException, LicensingException {
writeSettingsFile("192.168.0.1", null); //$NON-NLS-1$
coordinates();
}

@Test(expected = LicensingException.class)
public void emptyHostCausesFailure() throws IOException, LicensingException {
writeSettingsFile("", "2212"); //$NON-NLS-1$ //$NON-NLS-2$
coordinates();
}

@Test(expected = LicensingException.class)
public void emptyPortCausesFailure() throws IOException, LicensingException {
writeSettingsFile("server.address", ""); //$NON-NLS-1$ //$NON-NLS-2$
coordinates();
}

@Test
public void valuesAreNotVerified() throws IOException {
writeSettingsFile("(absolutely)$invald+server&*%.address", "not-a-port-value"); //$NON-NLS-1$ //$NON-NLS-2$
try {
coordinates();
} catch (LicensingException e) {
fail("Values verification is not the part of settings retrieval"); //$NON-NLS-1$
}
}

private String coordinates() throws LicensingException {
HostPort coords = new LicensingServerCoordinates(folder.getRoot()::toPath).get();
return String.format("%s:%s", coords.host(), coords.port()); //$NON-NLS-1$
}

private void writeSettingsFile(String host, String port) throws IOException {
File file = folder.newFile(Long.toHexString(System.nanoTime()) + new PassageFileExtension.Settings().get());
StringBuilder out = new StringBuilder();
if (host != null) {
new NamedData.Writable<String>(new LicensingServerHost(host)).write(out);
}
if (port != null) {
new NamedData.Writable<String>(new LicensingServerPort(port)).write(out);
}
try (PrintWriter writer = new PrintWriter(file)) {
writer.print(out.toString());
}
}

}