-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 572833 protect the product with common-use license
Protect floating license issuing with `.full` feature Signed-off-by: eparovyshnaya <[email protected]>
- Loading branch information
1 parent
c27ef44
commit 3cdb507
Showing
4 changed files
with
194 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...c/org/eclipse/passage/loc/internal/licenses/core/issue/FeatureGrantCapacityReduction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 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.loc.internal.licenses.core.issue; | ||
|
||
import org.eclipse.passage.lic.licenses.model.api.FeatureGrant; | ||
|
||
@SuppressWarnings("restriction") | ||
final class FeatureGrantCapacityReduction implements Reduction<FeatureGrant> { | ||
|
||
private final int capacity = 3; | ||
|
||
@Override | ||
public void accept(FeatureGrant grant) { | ||
grant.setCapacity(Math.min(capacity, grant.getCapacity())); | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
...rg/eclipse/passage/loc/internal/licenses/core/issue/FloatingLicenseIssuingProtection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 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.loc.internal.licenses.core.issue; | ||
|
||
import java.time.ZoneId; | ||
import java.time.ZonedDateTime; | ||
import java.util.Arrays; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
|
||
import org.eclipse.passage.lic.internal.base.conditions.BaseValidityPeriodClosed; | ||
import org.eclipse.passage.lic.internal.equinox.EquinoxPassage; | ||
import org.eclipse.passage.lic.licenses.model.api.FeatureGrant; | ||
import org.eclipse.passage.lic.licenses.model.api.FloatingLicensePack; | ||
import org.eclipse.passage.lic.licenses.model.api.ValidityPeriod; | ||
import org.eclipse.passage.lic.licenses.model.api.ValidityPeriodClosed; | ||
import org.eclipse.passage.lic.licenses.model.meta.LicensesFactory; | ||
|
||
@SuppressWarnings("restriction") | ||
public final class FloatingLicenseIssuingProtection implements Consumer<FloatingLicensePack> { | ||
|
||
private final String feature = "org.eclipse.passage.loc.operator.issue.floating.full"; //$NON-NLS-1$ | ||
private final List<Reduction<FeatureGrant>> featureReductions; | ||
private final List<Reduction<FloatingLicensePack>> licReductions; | ||
|
||
public FloatingLicenseIssuingProtection() { | ||
this.featureReductions = Arrays.asList(// | ||
new ClosedValidityPeriodReduction<FeatureGrant>(this::validGet, this::validSet), // | ||
new FeatureGrantCapacityReduction()// | ||
); | ||
this.licReductions = Arrays.asList(// | ||
new ClosedValidityPeriodReduction<FloatingLicensePack>(this::validGet, this::validSet), // | ||
new UserGrantsAmountReduction()// | ||
); | ||
} | ||
|
||
@Override | ||
public void accept(FloatingLicensePack license) { | ||
if (new EquinoxPassage().canUse(feature)) { | ||
return; | ||
} | ||
// TODO: log diminishing | ||
diminish(license); | ||
} | ||
|
||
private void diminish(FloatingLicensePack license) { | ||
diminishFeatureGrants(license); | ||
diminishLicense(license); | ||
} | ||
|
||
private void diminishFeatureGrants(FloatingLicensePack license) { | ||
license.getFeatures().forEach(this::diminishGrant); | ||
} | ||
|
||
private void diminishLicense(FloatingLicensePack lic) { | ||
licReductions.forEach(r -> r.accept(lic)); | ||
} | ||
|
||
private void diminishGrant(FeatureGrant grant) { | ||
featureReductions.forEach(r -> r.accept(grant)); | ||
} | ||
|
||
private Optional<org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed> validGet( | ||
FloatingLicensePack lic) { | ||
return validGet(lic.getLicense().getValid()); | ||
} | ||
|
||
private Optional<org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed> validGet( | ||
FeatureGrant grant) { | ||
return validGet(grant.getValid()); | ||
} | ||
|
||
private Optional<org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed> validGet( | ||
ValidityPeriod period) { | ||
if (!(period instanceof ValidityPeriodClosed)) { | ||
return Optional.empty(); // nothing we can reduce for now | ||
} | ||
ValidityPeriodClosed closed = (ValidityPeriodClosed) period; | ||
return Optional.of(new BaseValidityPeriodClosed(date(closed.getFrom()), date(closed.getUntil()))); | ||
} | ||
|
||
private void validSet(FloatingLicensePack lic, | ||
org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed period) { | ||
lic.getLicense().setValid(convert(period)); | ||
} | ||
|
||
private void validSet(FeatureGrant grant, | ||
org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed period) { | ||
grant.setValid(convert(period)); | ||
} | ||
|
||
private ValidityPeriodClosed convert(org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed period) { | ||
org.eclipse.passage.lic.licenses.model.api.ValidityPeriodClosed valid = LicensesFactory.eINSTANCE | ||
.createValidityPeriodClosed(); | ||
valid.setFrom(Date.from(period.from().toInstant())); | ||
valid.setUntil(Date.from(period.to().toInstant())); | ||
return valid; | ||
} | ||
|
||
private ZonedDateTime date(Date date) { | ||
return ZonedDateTime.from(date.toInstant().atZone(ZoneId.systemDefault())); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...e/src/org/eclipse/passage/loc/internal/licenses/core/issue/UserGrantsAmountReduction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 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.loc.internal.licenses.core.issue; | ||
|
||
import org.eclipse.emf.common.util.EList; | ||
import org.eclipse.passage.lic.licenses.model.api.FloatingLicensePack; | ||
import org.eclipse.passage.lic.licenses.model.api.UserGrant; | ||
|
||
@SuppressWarnings("restriction") | ||
final class UserGrantsAmountReduction implements Reduction<FloatingLicensePack> { | ||
|
||
private final int amount = 1; | ||
|
||
@Override | ||
public void accept(FloatingLicensePack license) { | ||
EList<UserGrant> users = license.getUsers(); | ||
if (users.size() > amount) { | ||
for (int i = users.size() - 1; i >= amount; i--) { | ||
users.remove(i); | ||
} | ||
} | ||
} | ||
|
||
} |