Skip to content

Commit

Permalink
Merge pull request #308 from eclipse-passage/565012
Browse files Browse the repository at this point in the history
Bug 565012 - API revision | permission | OshiPermissionEmitter
  • Loading branch information
eparovyshnaya authored Jul 12, 2020
2 parents d38cc6f + d32d689 commit 7568738
Show file tree
Hide file tree
Showing 20 changed files with 451 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@
import org.eclipse.passage.lic.api.conditions.LicensingCondition;

/**
* Interface of a service responsible for the third step of <i> access cycle</i>: condition evaluation.
* According to the contract, a service must be able to evaluate given {@link LicensingCondition}s against the current program runtime.
* As a result of evaluation, for a {@link LicensingCondition} a {@link FeaturePermission} must be emitted
* in the case all of the {@link LicensingCondition}'s terms are met at the current program runtime.
* Interface of a service responsible for the third step of <i> access
* cycle</i>: condition evaluation. According to the contract, a service must be
* able to evaluate given {@link LicensingCondition}s against the current
* program runtime. As a result of evaluation, for a {@link LicensingCondition}
* a {@link FeaturePermission} must be emitted in the case all of the
* {@link LicensingCondition}'s terms are met at the current program runtime.
*
* @see org.eclipse.passage.lic.api.conditions.ConditionMiner
* @since 0.4.0
* @deprecated use 1.0 PermissionEmittingService
*/
@Deprecated
public interface PermissionEmitter {
/**
* Evaluates the collection of {@link LicensingCondition}s to emit a collection of {@link FeaturePermission}.
* Evaluates the collection of {@link LicensingCondition}s to emit a collection
* of {@link FeaturePermission}.
*
* @param configuration general configuration for the whole <i>access cycle</i>
* @param conditions source conditions to be evaluated against the current program runtime
* @param conditions source conditions to be evaluated against the current
* program runtime
* @since 0.4.0
*/
Iterable<FeaturePermission> emitPermissions(LicensingConfiguration configuration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.api;

import org.eclipse.passage.lic.internal.api.conditions.evaluation.PermissionEmittersRegistry;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionEvaluatorsRegistry;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionPasringRegistry;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionTokenAssessorsRegistry;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.PermissionEmittersRegistry;
import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransportRegistry;
import org.eclipse.passage.lic.internal.api.conditions.mining.MinedConditionsRegistry;
import org.eclipse.passage.lic.internal.api.inspection.RuntimeEnvironmentRegistry;
import org.eclipse.passage.lic.internal.api.io.KeyKeeperRegistry;
import org.eclipse.passage.lic.internal.api.io.StreamCodecRegistry;
import org.eclipse.passage.lic.internal.api.requirements.ResolvedRequirementsRegistry;
Expand Down Expand Up @@ -45,4 +46,6 @@ public interface AccessCycleConfiguration {

ExpressionTokenAssessorsRegistry expressionAssessors();

RuntimeEnvironmentRegistry environments();

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
import oshi.software.os.OperatingSystem;
import oshi.software.os.OperatingSystemVersion;

/**
* @deprecated use 1.0 internal State for the sake of scanning and
* HardawareEnvironemnt to get a corresponsing service
*/
@Deprecated
@Component
public class OshiHardwareInspector implements HardwareInspector {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
###############################################################################
# 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
###############################################################################

HardwareAssessmentService_error_on_assessment=Error property [%s] assessment for value [%s]
HardwareAssessmentService_no_hw_inspector=There is no runtive environment inpection service for type %s
State_error_reading_hw=Error reading hardware enviroment state
HardwareAssessmentService_no_hw_inspector=There is no runtime environment inspection service for type %s
State_error_reading_hw=Error reading hardware environment state
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.oshi.tobemoved;

import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.eclipse.passage.lic.internal.api.inspection.EnvironmentProperty;
import org.eclipse.passage.lic.internal.base.inspection.hardware.Disk;

@SuppressWarnings("restriction")
final class GlanceOfState implements Supplier<String> {

private final State state;
Expand All @@ -26,7 +33,37 @@ final class GlanceOfState implements Supplier<String> {

@Override
public String get() {
return "ytbd"; //$NON-NLS-1$
StringBuilder out = new StringBuilder();
state.properties().stream() //
.collect(Collectors.groupingBy(EnvironmentProperty::family))//
.entrySet().stream() //
.forEach(e -> appendFamily(e.getKey(), e.getValue(), out));
IntStream.range(0, state.disksAmount()) //
.forEach(no -> appendDisk(no, out));
return out.toString();
}

private void appendDisk(int no, StringBuilder out) {
out.append(new Disk.Model().family())//
.append(" #")//$NON-NLS-1$
.append(no)//
.append("\r\n"); //$NON-NLS-1$
state.diskProperties(no).stream() //
.forEach(p -> appendProperty(p, state.diskValue(no, p), out));
}

private void appendFamily(String family, List<EnvironmentProperty> properties, StringBuilder out) {
out.append(family).append("\r\n"); //$NON-NLS-1$
properties.stream().forEach(p -> appendProperty(p, state.value(p), out));
}

private StringBuilder appendProperty(EnvironmentProperty property, String value, StringBuilder out) {
out.append("\t") //$NON-NLS-1$
.append(property.name()) //
.append(": ") //$NON-NLS-1$
.append(value)//
.append("\r\n"); //$NON-NLS-1$
return out;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public String state() throws LicensingException {
return new GlanceOfState(freshState()).get();
}

/**
* <p>
* It's extremely important to prevent any concurrent access to the
* {@linkplain State} reading. We synchronize it by key {@code OSHI} class
* ({@linkplain SystemInfo}) lock to be sure we have exclusive access to it
* until the hardware scanning is over.
* </p>
* <p>
* Behind all the benefits of such a sync lock choice, it's still a foreign
* class. Just beware, in case of any threading troubles revise.
* </p>
*/
private State freshState() throws LicensingException {
State state;
synchronized (SystemInfo.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;

import org.eclipse.passage.lic.internal.api.LicensingException;
Expand Down Expand Up @@ -87,6 +88,26 @@ boolean hasValue(EnvironmentProperty property, String expected) {
.orElse(false);
}

Set<EnvironmentProperty> properties() {
return hardware.keySet();
}

String value(EnvironmentProperty key) {
return hardware.get(key);
}

int disksAmount() {
return disks.size();
}

Set<EnvironmentProperty> diskProperties(int no) {
return disks.get(no).keySet();
}

String diskValue(int no, EnvironmentProperty key) {
return disks.get(no).get(key);
}

private void read() throws LicensingException {
try {
SystemInfo system = new SystemInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
import java.util.Set;

import org.eclipse.passage.lic.api.inspector.HardwareInspector;
import org.eclipse.passage.lic.internal.oshi.tobemoved.HardwareEnvironment;

/**
* @deprecated use 1.0 {@link HardwareEnvironment}'s state
*/
@Deprecated
public class OshiHal {

public static final String CONDITION_TYPE_HARDWARE = "hardware"; //$NON-NLS-1$
Expand Down
3 changes: 2 additions & 1 deletion bundles/org.eclipse.passage.seal.demo/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Require-Bundle: org.eclipse.osgi.services;bundle-version="3.8.0",
org.eclipse.passage.lic.hc;bundle-version="0.5.300",
org.eclipse.passage.lic.json;bundle-version="0.5.300",
org.eclipse.passage.lic.bc;bundle-version="0.5.100",
org.eclipse.passage.lic.licenses.migration
org.eclipse.passage.lic.licenses.migration,
org.eclipse.passage.lic.oshi
Service-Component: OSGI-INF/*.xml
Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.passage.seal.internal.demo;x-internal:=true
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@
import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.api.conditions.EvaluationType;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.PermissionEmittingService;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.PermissionEmittersRegistry;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionEvaluationService;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionEvaluatorsRegistry;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionProtocol;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionPasringRegistry;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionParsingService;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionPasringRegistry;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionProtocol;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionTokenAssessmentService;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionTokenAssessorsRegistry;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.PermissionEmittersRegistry;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.PermissionEmittingService;
import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransport;
import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransportRegistry;
import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType;
import org.eclipse.passage.lic.internal.api.conditions.mining.MinedConditions;
import org.eclipse.passage.lic.internal.api.conditions.mining.MinedConditionsRegistry;
import org.eclipse.passage.lic.internal.api.inspection.RuntimeEnvironment;
import org.eclipse.passage.lic.internal.api.inspection.RuntimeEnvironmentRegistry;
import org.eclipse.passage.lic.internal.api.io.KeyKeeper;
import org.eclipse.passage.lic.internal.api.io.KeyKeeperRegistry;
import org.eclipse.passage.lic.internal.api.io.StreamCodec;
Expand All @@ -42,8 +44,8 @@
import org.eclipse.passage.lic.internal.api.registry.StringServiceId;
import org.eclipse.passage.lic.internal.api.requirements.ResolvedRequirements;
import org.eclipse.passage.lic.internal.api.requirements.ResolvedRequirementsRegistry;
import org.eclipse.passage.lic.internal.base.conditions.evaluation.BasePermissionEmittingService;
import org.eclipse.passage.lic.internal.base.conditions.evaluation.AndsProtocolExpressionParseService;
import org.eclipse.passage.lic.internal.base.conditions.evaluation.BasePermissionEmittingService;
import org.eclipse.passage.lic.internal.base.conditions.evaluation.SimpleMapExpressionEvaluationService;
import org.eclipse.passage.lic.internal.base.conditions.mining.MiningEquipment;
import org.eclipse.passage.lic.internal.base.conditions.mining.UserHomeResidentConditions;
Expand All @@ -57,6 +59,8 @@
import org.eclipse.passage.lic.internal.hc.remote.impl.RemoteConditions;
import org.eclipse.passage.lic.internal.json.tobemoved.JsonConditionTransport;
import org.eclipse.passage.lic.internal.licenses.migration.tobemoved.XmiConditionTransport;
import org.eclipse.passage.lic.internal.oshi.tobemoved.HardwareAssessmentService;
import org.eclipse.passage.lic.internal.oshi.tobemoved.HardwareEnvironment;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;

Expand All @@ -72,7 +76,8 @@ final class SealedAccessCycleConfiguration implements AccessCycleConfiguration {
private final Registry<StringServiceId, PermissionEmittingService> emitters;
private final Registry<ExpressionProtocol, ExpressionParsingService> expressionParsers;
private final Registry<ExpressionProtocol, ExpressionEvaluationService> expressionEvaluators;
private final Registry<EvaluationType, ExpressionTokenAssessmentService> tokenEvaluators;
private final Registry<EvaluationType, ExpressionTokenAssessmentService> tokenAssessors;
private final Registry<EvaluationType, RuntimeEnvironment> environments;

SealedAccessCycleConfiguration(Supplier<LicensedProduct> product) {
alarm = LicensingException::printStackTrace;
Expand Down Expand Up @@ -114,8 +119,11 @@ final class SealedAccessCycleConfiguration implements AccessCycleConfiguration {
expressionEvaluators = new ReadOnlyRegistry<>(Arrays.asList(//
new SimpleMapExpressionEvaluationService()//
));
tokenEvaluators = new ReadOnlyRegistry<>(Arrays.asList(//
// FIXME: OSHI-based evaluator for EvaluationType.Hardware
tokenAssessors = new ReadOnlyRegistry<>(Arrays.asList(//
new HardwareAssessmentService(environments())//
));
environments = new ReadOnlyRegistry<>(Arrays.asList(//
new HardwareEnvironment()//
));
}

Expand Down Expand Up @@ -165,7 +173,12 @@ public ExpressionEvaluatorsRegistry expressionEvaluators() {

@Override
public ExpressionTokenAssessorsRegistry expressionAssessors() {
return () -> tokenEvaluators;
return () -> tokenAssessors;
}

@Override
public RuntimeEnvironmentRegistry environments() {
return () -> environments;
}

}
2 changes: 2 additions & 0 deletions tests/org.eclipse.passage.lic.api.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ Export-Package: org.eclipse.passage.lic.api.tests;x-internal:=true,
org.eclipse.passage.lic.api.tests.fakes.conditions.evaluation,
org.eclipse.passage.lic.api.tests.fakes.conditions.mining;x-internal:=true,
org.eclipse.passage.lic.api.tests.fakes.diagnostic;x-internal:=true,
org.eclipse.passage.lic.api.tests.fakes.inspection;x-internal:=true,
org.eclipse.passage.lic.api.tests.fakes.io;x-internal:=true,
org.eclipse.passage.lic.api.tests.fakes.requirements;x-internal:=true,
org.eclipse.passage.lic.api.tests.inspection;x-internal:=true,
org.eclipse.passage.lic.api.tests.registry;x-internal:=true,
org.eclipse.passage.lic.api.tests.version;x-internal:=true
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import org.eclipse.passage.lic.internal.api.Framework;
import org.eclipse.passage.lic.internal.api.conditions.evaluation.ExpressionProtocol;
import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType;
import org.eclipse.passage.lic.internal.api.inspection.RuntimeEnvironmentRegistry;
import org.eclipse.passage.lic.internal.api.registry.Registry;
import org.eclipse.passage.lic.internal.api.registry.Service;
import org.eclipse.passage.lic.internal.api.registry.ServiceId;
import org.junit.Ignore;
import org.junit.Test;

/**
Expand Down Expand Up @@ -147,7 +147,6 @@ public final void hasEvaluatorForDefaultExpressionFormat() {
}

@Test
@Ignore // yet to be implemented (OSHI-based)
public final void canAssessConditionExpressionsSegment() {
assertServiceRegistryIsFunctional(config().expressionAssessors().get());
}
Expand All @@ -157,6 +156,19 @@ public final void prohibitsExpressionSegmentAssessmentServicesExtension() {
assertTrue(readOnly(config().expressionAssessors().get()));
}

@Test
public final void hasRuntimeEnvironmentInspectorsRegistry() {
RuntimeEnvironmentRegistry environments = framework().get().accessCycleConfiguration().environments();
assertNotNull(environments);
assertNotNull(environments.get());
assertNotNull(environments.get().services()); // can legally be empty
}

@Test
public final void prohibitsRuntimeEnvironmentInspectionServicesExtension() {
assertTrue(readOnly(config().expressionAssessors().get()));
}

private <I extends ServiceId, S extends Service<I>> void assertServiceRegistryIsFunctional(
Registry<I, S> registry) {
assertNotNull(registry);
Expand Down
Loading

0 comments on commit 7568738

Please sign in to comment.