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 544385 - [Passage] "Licensing" dialog shall show "expired" status #217

Merged
merged 2 commits into from
May 27, 2020
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
2 changes: 1 addition & 1 deletion bundles/org.eclipse.passage.lic.base/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Automatic-Module-Name: org.eclipse.passage.lic.api.base
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.passage.lic.base
Bundle-Version: 0.6.1.qualifier
Bundle-Version: 0.6.4.qualifier
Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ public Iterable<FeaturePermission> evaluateConditions(LicensingConfiguration con
licensingReporter.postResult(createEvent(AccessEvents.CONDITIONS_EVALUATED, empty, error));
return empty;
}
List<LicensingCondition> invalid = new ArrayList<>();
for (LicensingCondition condition : conditions) {
if (condition == null) {
String message = BaseMessages.getString("BaseAccessManager_evaluation_error_invalid_condition"); //$NON-NLS-1$
Expand All @@ -219,26 +218,30 @@ public Iterable<FeaturePermission> evaluateConditions(LicensingConfiguration con
licensingReporter.logResult(error);
continue;
}
List<LicensingCondition> mappedConditions = Collections.singletonList(condition);
try {
Iterable<FeaturePermission> permissions = emitter.emitPermissions(configuration, mappedConditions);
Iterable<FeaturePermission> permissions = emitter.emitPermissions(configuration,
Collections.singletonList(condition));
for (FeaturePermission permission : permissions) {
result.add(permission);
}
} catch (LicensingException e) {
LicensingResult error = e.getResult();
licensingReporter.logResult(error);
licensingReporter
.postResult(createEvent(ConditionEvents.CONDITIONS_NOT_VALID, mappedConditions, error));
licensingReporter.postResult(createEvent(ConditionEvents.CONDITIONS_NOT_VALID,
Collections.singletonList(condition), error));
} catch (Throwable e) {
LicensingResult error = LicensingResults.createError(BaseMessages.getString("BaseAccessManager_permission_emitter_failure"), source, e); //$NON-NLS-1$
LicensingResult error = LicensingResults.createError(
BaseMessages.getString("BaseAccessManager_permission_emitter_failure"), source, e); //$NON-NLS-1$
licensingReporter.logResult(error);
licensingReporter
.postResult(createEvent(ConditionEvents.CONDITIONS_NOT_VALID, mappedConditions, error));
licensingReporter.postResult(createEvent(ConditionEvents.CONDITIONS_NOT_VALID,
Collections.singletonList(condition), error));
}
} else {
licensingReporter.logResult(validate);
invalid.add(condition);
// FIXME: we need to use "post", but the only receiver of this event
// ("Licensing Status" dialog) will need to much rework
licensingReporter.sendResult(createEvent(ConditionEvents.CONDITIONS_NOT_VALID,
Collections.singletonList(condition), validate));
}
}
List<FeaturePermission> unmodifiable = Collections.unmodifiableList(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
*******************************************************************************/
package org.eclipse.passage.lic.base.conditions;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.passage.lic.api.LicensingResult;
import org.eclipse.passage.lic.api.conditions.LicensingCondition;
import org.eclipse.passage.lic.base.BaseLicensingResult;
import org.eclipse.passage.lic.base.LicensingResults;
import org.eclipse.passage.lic.internal.base.i18n.BaseMessages;

Expand Down Expand Up @@ -62,28 +65,31 @@ public static LicensingCondition create(String identifier, String version, Strin
}

public static LicensingResult validate(LicensingCondition condition, String source) {
List<LicensingResult> details = new ArrayList<>();
Map<String, Object> data = new HashMap<String, Object>();
data.put(LicensingCondition.class.getName(), condition);
Date validFrom = condition.getValidFrom();
if (validFrom == null) {
String format = BaseMessages.getString("LicensingConditions_validation_no_from"); //$NON-NLS-1$
String message = String.format(format, condition);
return LicensingResults.createError(message, source);
return new BaseLicensingResult(LicensingResult.ERROR, message, 400, source, null, details, data);
}
Date now = new Date();
if (validFrom.after(now)) {
String format = BaseMessages.getString("LicensingConditions_validation_invalid_from"); //$NON-NLS-1$
String message = String.format(format, condition);
return LicensingResults.createError(message, source);
return new BaseLicensingResult(LicensingResult.ERROR, message, 416, source, null, details, data);
}
Date validUntil = condition.getValidUntil();
if (validUntil == null) {
String format = BaseMessages.getString("LicensingConditions_validation_no_until"); //$NON-NLS-1$
String message = String.format(format, condition);
return LicensingResults.createError(message, source);
return new BaseLicensingResult(LicensingResult.ERROR, message, 400, source, null, details, data);
eparovyshnaya marked this conversation as resolved.
Show resolved Hide resolved
}
if (validUntil.before(now)) {
String format = BaseMessages.getString("LicensingConditions_validation_invalid_until"); //$NON-NLS-1$
String message = String.format(format, condition);
return LicensingResults.createError(message, source);
return new BaseLicensingResult(LicensingResult.ERROR, message, 417, source, null, details, data);
}
return LicensingResults.createOK("", source); //$NON-NLS-1$
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static org.eclipse.passage.lic.base.LicensingProperties.LICENSING_FEATURE_VERSION;
import static org.eclipse.passage.lic.base.LicensingProperties.LICENSING_RESTRICTION_LEVEL;

import java.util.Objects;

import org.eclipse.passage.lic.api.requirements.LicensingRequirement;

class BaseLicensingRequirement implements LicensingRequirement {
Expand Down Expand Up @@ -78,4 +80,42 @@ public String toString() {
return sb.toString();
}

@Override
public int hashCode() {
return Objects.hash(featureIdentifier, featureName, featureProvider, featureVersion, restrictionLevel, source);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
BaseLicensingRequirement other = (BaseLicensingRequirement) obj;
if (!Objects.equals(featureIdentifier, other.featureIdentifier)) {
return false;
}
if (!Objects.equals(featureName, other.featureName)) {
return false;
}
if (!Objects.equals(featureProvider, other.featureProvider)) {
return false;
}
if (!Objects.equals(featureVersion, other.featureVersion)) {
return false;
}
if (!Objects.equals(restrictionLevel, other.restrictionLevel)) {
return false;
}
if (!Objects.equals(source, other.source)) {
return false;
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.passage.lic.internal.equinox.inspector.EquinoxFeatureInspector">
<property name="event.topics" value="org/eclipse/passage/lic/api/AccessEvents/*"/>
<property name="event.topics">org/eclipse/passage/lic/api/AccessEvents/*
org/eclipse/passage/lic/api/ConditionEvents/*
</property>
<service>
<provide interface="org.eclipse.passage.lic.api.inspector.FeatureInspector"/>
<provide interface="org.osgi.service.event.EventHandler"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public static Event extractEvent(LicensingResult result) {
if (attachmentTopic instanceof String) {
String topic = (String) attachmentTopic;
Object data = result.getAttachment(LicensingEvents.PROPERTY_DATA);
return createEvent(topic, data);
Map<String, Object> properties = new HashMap<>();
properties.put(PROPERTY_DATA, data);
// FIXME: we may need more attributes from LicensingResult
properties.put("org.eclipse.passage.lic.api.event.code", result.getCode()); //$NON-NLS-1$
return new Event(topic, properties);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,101 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.equinox.inspector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import org.eclipse.passage.lic.api.LicensingConfiguration;
import org.eclipse.passage.lic.api.conditions.LicensingCondition;
import org.eclipse.passage.lic.api.inspector.FeatureCase;
import org.eclipse.passage.lic.api.requirements.LicensingRequirement;
import org.eclipse.passage.lic.api.restrictions.RestrictionVerdict;

public class EquinoxFeatureCase implements FeatureCase {

private final class NoRestriction implements RestrictionVerdict {

private final LicensingRequirement requirement;
private final LicensingConfiguration configuration;

private NoRestriction(LicensingRequirement requirement, LicensingConfiguration configuration) {
this.requirement = requirement;
this.configuration = configuration;
}

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

@Override
public int getRestrictionCode() {
return 0;
}

@Override
public LicensingRequirement getLicensingRequirement() {
return requirement;
}

@Override
public LicensingConfiguration getLicensingConfiguration() {
return this.configuration;
}
}

private final class WrappedVerdict implements RestrictionVerdict {
private final RestrictionVerdict verdict;
private final Integer code;

private WrappedVerdict(RestrictionVerdict verdict, Integer code) {
this.verdict = verdict;
this.code = code;
}

@Override
public String getRestrictionLevel() {
return verdict.getRestrictionLevel();
}

@Override
public int getRestrictionCode() {
return code.intValue();
}

@Override
public LicensingRequirement getLicensingRequirement() {
return verdict.getLicensingRequirement();
}

@Override
public LicensingConfiguration getLicensingConfiguration() {
return verdict.getLicensingConfiguration();
}
}

private final EquinoxFeatureInspector inspector;
private final List<LicensingRequirement> requirements;
private final List<RestrictionVerdict> verdicts;
private final Set<Class<?>> query;
private final Map<String, Integer> failures;

private final Iterable<String> featureIdentifiers;

EquinoxFeatureCase(EquinoxFeatureInspector inspector, Iterable<String> features) {
this.inspector = inspector;
this.featureIdentifiers = features;
requirements = new ArrayList<>();
verdicts = new ArrayList<>();
failures = new HashMap<>();
query = new HashSet<Class<?>>();
query.add(LicensingRequirement.class);
query.add(RestrictionVerdict.class);
}

@Override
Expand All @@ -34,17 +116,61 @@ public Iterable<String> getFeatureIdentifiers() {

@Override
public Iterable<LicensingRequirement> getRequirements() {
return inspector.getRequirements(featureIdentifiers);
if (query.contains(LicensingRequirement.class)) {
inspector.getRequirements(featureIdentifiers).forEach(requirements::add);
query.remove(LicensingRequirement.class);
}
return requirements;
}

@Override
public Iterable<RestrictionVerdict> getRestrictions() {
return inspector.getRestrictions(featureIdentifiers);
if (query.contains(RestrictionVerdict.class)) {
inspector.getRestrictions(featureIdentifiers).forEach(verdicts::add);
query.remove(RestrictionVerdict.class);
}
return verdicts;
}

@Override
public void close() {
query.clear();
requirements.clear();
verdicts.clear();
inspector.close(this);
}

public void reset() {
failures.clear();
query.add(LicensingRequirement.class);
query.add(RestrictionVerdict.class);
}

void conditionNotValid(LicensingCondition condition, int code) {
failures.put(condition.getFeatureIdentifier(), code);
}

public List<RestrictionVerdict> status() {
Map<LicensingRequirement, RestrictionVerdict> base = StreamSupport
.stream(getRestrictions().spliterator(), false)//
.collect(Collectors.toMap(v -> v.getLicensingRequirement(), v -> v));
return StreamSupport.stream(getRequirements().spliterator(), false)//
.map(r -> wrap(base, r))//
.collect(Collectors.toList());
}

private RestrictionVerdict wrap(Map<LicensingRequirement, RestrictionVerdict> base,
LicensingRequirement requirement) {
RestrictionVerdict verdict = base.get(requirement);
if (verdict == null) {
return new NoRestriction(requirement, inspector.getLicensingConfiguration());
} else {
Integer code = failures.get(requirement.getFeatureIdentifier());
if (code == null) {
return verdict;
} else {
return new WrappedVerdict(verdict, code);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@
import org.eclipse.passage.lic.api.access.AccessEvents;
import org.eclipse.passage.lic.api.access.AccessManager;
import org.eclipse.passage.lic.api.access.FeaturePermission;
import org.eclipse.passage.lic.api.conditions.ConditionEvents;
import org.eclipse.passage.lic.api.conditions.LicensingCondition;
import org.eclipse.passage.lic.api.inspector.FeatureCase;
import org.eclipse.passage.lic.api.inspector.FeatureInspector;
import org.eclipse.passage.lic.api.requirements.LicensingRequirement;
import org.eclipse.passage.lic.api.restrictions.RestrictionVerdict;
import org.eclipse.passage.lic.equinox.ApplicationConfigurations;
import org.eclipse.passage.lic.internal.equinox.EquinoxEvents;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;

@Component(property = EventConstants.EVENT_TOPIC + "=" + AccessEvents.TOPIC_ALL)
@Component(property = { EventConstants.EVENT_TOPIC + "=" + AccessEvents.TOPIC_ALL,
EventConstants.EVENT_TOPIC + "=" + ConditionEvents.TOPIC_ALL, })
public class EquinoxFeatureInspector implements FeatureInspector, EventHandler {

private final Map<String, List<EquinoxFeatureCase>> cases = new HashMap<>();
Expand All @@ -57,6 +60,25 @@ public void unbindAccessManager(AccessManager manager) {

@Override
public void handleEvent(Event event) {
String topic = event.getTopic();
if (ConditionEvents.CONDITIONS_NOT_VALID.equals(topic)) {
Object data = event.getProperty(EquinoxEvents.PROPERTY_DATA);
if (data instanceof List<?>) {
List<?> list = (List<?>) data;
for (Object object : list) {
if (object instanceof LicensingCondition) {
LicensingCondition condition = (LicensingCondition) object;
String id = condition.getFeatureIdentifier();
Object code = event.getProperty("org.eclipse.passage.lic.api.event.code"); //$NON-NLS-1$
if (code instanceof Integer) {
cases.getOrDefault(id, Collections.emptyList())//
.forEach(c -> c.conditionNotValid(condition, (Integer) code));
}
}
}
}

}
// FIXME: should update cached info
}

Expand Down
Loading