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 571147 extend 'lic.net' with common facilities #650

Merged
merged 1 commit into from
Feb 14, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.eclipse.passage.lic.internal.api.EvaluationInstructions;
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.net.handle.Chore;
import org.eclipse.passage.lic.internal.net.handle.Failure;
import org.eclipse.passage.lic.internal.net.handle.NetResponse;

abstract class ChoreDraft implements Chore {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,21 @@
*******************************************************************************/
package org.eclipse.passage.lbc.internal.base;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import org.eclipse.passage.lbc.internal.base.api.RawRequest;
import org.eclipse.passage.lic.internal.api.PassageAction;
import org.eclipse.passage.lic.internal.net.LicensingAction;
import org.eclipse.passage.lic.internal.net.handle.Chore;
import org.eclipse.passage.lic.internal.net.handle.Chores;
import org.eclipse.passage.lic.internal.net.handle.NetResponse;
import org.eclipse.passage.lic.internal.net.handle.NetServices;

final class FloatingCycle implements Chores<RawRequest> {
final class FloatingCycle extends NetServices<RawRequest> {

private final Map<PassageAction, Function<RawRequest, Chore>> chores = new HashMap<>();

FloatingCycle() {
@Override
protected void defineChores(Map<PassageAction, Function<RawRequest, Chore>> chores) {
chores.put(new PassageAction.Mine(), Mine::new);
chores.put(new PassageAction.Acquire(), Acquire::new);
chores.put(new PassageAction.Release(), Release::new);
}

@Override
public NetResponse workOut(RawRequest request) {
LicensingAction action = action(request);
return chores//
.getOrDefault(//
action.get().get(), //
unknown -> new Failing(action))//
.apply(request)//
.getDone();
}

private LicensingAction action(RawRequest request) {
return new LicensingAction(key -> new PassageAction.Of(String.valueOf(request.parameter(key))));
}

private final class Failing implements Chore {
private final LicensingAction actual;

Failing(LicensingAction actual) {
this.actual = actual;
}

@Override
public NetResponse getDone() {
return new Failure.BadRequestUnknownAction(actual.get().get().name());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.passage.lbc.internal.base.EObjectTransfer;
import org.eclipse.passage.lbc.internal.base.Failure;
import org.eclipse.passage.lbc.internal.base.Failure.NoGrantsAvailable;
import org.eclipse.passage.lbc.internal.base.api.Grants;
import org.eclipse.passage.lbc.internal.base.PlainSuceess;
import org.eclipse.passage.lbc.internal.base.ProductUserRequest;
import org.eclipse.passage.lbc.internal.base.api.Grants;
import org.eclipse.passage.lic.floating.model.api.GrantAcqisition;
import org.eclipse.passage.lic.floating.model.meta.FloatingPackage;
import org.eclipse.passage.lic.internal.api.PassageAction;
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.api.PassageAction;
import org.eclipse.passage.lic.internal.base.FeatureIdentifier;
import org.eclipse.passage.lic.internal.emf.EObjectFromBytes;
import org.eclipse.passage.lic.internal.net.handle.Failure;
import org.eclipse.passage.lic.internal.net.handle.NetResponse;

public final class Acquisition {
Expand Down Expand Up @@ -70,7 +69,7 @@ public NetResponse returnBack() throws LicensingException {
}
boolean released = grants().release(data.product().get(), acquisition);
if (!released) {
return new Failure.NotReleased(data.product().get(), acquisition);
return new NotReleased(data.product().get(), acquisition);
}
return new PlainSuceess();

Expand All @@ -93,8 +92,8 @@ private Grants grants() {
return data.raw().state().grants();
}

private NoGrantsAvailable noGrants(String feature) {
return new Failure.NoGrantsAvailable(data.product().get(), feature);
private Failure noGrants(String feature) {
return new NoGrantsAvailable(data.product().get(), feature);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2020, 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.lbc.internal.base.acquire;

import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.net.handle.Failure;

/**
* Public only for test purposes
*/
public final class NoGrantsAvailable extends Failure {

public NoGrantsAvailable(LicensedProduct product, String feature) {
super(611, String.format("No license grants available for feature %s of product %s", feature, product)); //$NON-NLS-1$
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2020, 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.lbc.internal.base.acquire;

import org.eclipse.passage.lic.floating.model.api.GrantAcqisition;
import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.net.handle.Failure;

/**
* Public only for test purposes
*/
public final class NotReleased extends Failure {

public NotReleased(LicensedProduct product, GrantAcqisition acqisition) {
super(612, String.format("Failed to release grant %s acquisition for feature %s of product %s", //$NON-NLS-1$
acqisition.getGrant(), acqisition.getFeature(), product));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.passage.lbc.internal.base.EObjectTransfer;
import org.eclipse.passage.lbc.internal.base.Failure;
import org.eclipse.passage.lbc.internal.base.ProductUserRequest;
import org.eclipse.passage.lic.floating.FloatingFileExtensions;
import org.eclipse.passage.lic.internal.api.PassageAction;
import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.PassageAction;
import org.eclipse.passage.lic.internal.api.ServiceInvocationResult;
import org.eclipse.passage.lic.internal.api.conditions.ConditionMiningTarget;
import org.eclipse.passage.lic.internal.api.conditions.ConditionPack;
Expand All @@ -34,6 +33,7 @@
import org.eclipse.passage.lic.internal.base.io.LicensingFolder;
import org.eclipse.passage.lic.internal.base.io.PathFromLicensedProduct;
import org.eclipse.passage.lic.internal.base.io.UserHomePath;
import org.eclipse.passage.lic.internal.net.handle.Failure;
import org.eclipse.passage.lic.internal.net.handle.NetResponse;
import org.eclipse.passage.lic.licenses.model.api.LicensePack;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.lbc.internal.base;
package org.eclipse.passage.lic.internal.net.handle;

import org.eclipse.passage.lic.floating.model.api.GrantAcqisition;
import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType;
import org.eclipse.passage.lic.internal.net.handle.NetResponse;

public abstract class Failure implements NetResponse {

Expand Down Expand Up @@ -123,20 +120,4 @@ public OperationFailed(String name, String details) {

}

public static final class NoGrantsAvailable extends Failure {

public NoGrantsAvailable(LicensedProduct product, String feature) {
super(611, String.format("No license grants available for feature %s of product %s", feature, product)); //$NON-NLS-1$
}

}

public static final class NotReleased extends Failure {

public NotReleased(LicensedProduct product, GrantAcqisition acqisition) {
super(612, String.format("Failed to release grant %s acquisition for feature %s of product %s", //$NON-NLS-1$
acqisition.getGrant(), acqisition.getFeature(), product));
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*******************************************************************************
* 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.lic.internal.net.handle;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import org.eclipse.passage.lic.internal.api.PassageAction;
import org.eclipse.passage.lic.internal.net.LicensingAction;

public abstract class NetServices<R extends NetRequest> implements Chores<R> {

private final Map<PassageAction, Function<R, Chore>> chores = new HashMap<>();

protected NetServices() {
defineChores(chores);
}

protected abstract void defineChores(Map<PassageAction, Function<R, Chore>> services);

@Override
public NetResponse workOut(R request) {
LicensingAction action = action(request);
return chores//
.getOrDefault(//
action.get().get(), //
unknown -> new Failing(action))//
.apply(request)//
.getDone();
}

private LicensingAction action(R request) {
return new LicensingAction(key -> new PassageAction.Of(String.valueOf(request.parameter(key))));
}

private final class Failing implements Chore {

private final LicensingAction actual;

Failing(LicensingAction actual) {
this.actual = actual;
}

@Override
public NetResponse getDone() {
return new Failure.BadRequestUnknownAction(actual.get().get().name());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import static org.junit.Assert.assertTrue;

import org.eclipse.passage.lbc.internal.base.BaseFlotingRequestHandled;
import org.eclipse.passage.lbc.internal.base.Failure;
import org.eclipse.passage.lbc.internal.base.api.RawRequest;
import org.eclipse.passage.lic.internal.api.PassageAction;
import org.eclipse.passage.lic.internal.net.handle.Failure;
import org.eclipse.passage.lic.internal.net.handle.NetResponse;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import java.util.stream.IntStream;

import org.eclipse.passage.lbc.internal.base.EagerFloatingState;
import org.eclipse.passage.lbc.internal.base.Failure;
import org.eclipse.passage.lbc.internal.base.ProductUserRequest;
import org.eclipse.passage.lbc.internal.base.acquire.Acquisition;
import org.eclipse.passage.lbc.internal.base.acquire.NoGrantsAvailable;
import org.eclipse.passage.lbc.internal.base.api.FloatingState;
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.api.PassageAction;
Expand Down Expand Up @@ -63,7 +63,7 @@ private Set<Future<NetResponse>> runConcurrentAcquireRequest(int amount) throws
private int[] countGainsAndLates(Set<Future<NetResponse>> futures) throws InterruptedException {
int gains = 0;
int lates = 0;
int none = new Failure.NoGrantsAvailable(data.product(), data.feature()).error().code();
int none = new NoGrantsAvailable(data.product(), data.feature()).error().code();
for (Future<NetResponse> future : futures) {
NetResponse response;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
import java.util.stream.IntStream;

import org.eclipse.passage.lbc.internal.base.EagerFloatingState;
import org.eclipse.passage.lbc.internal.base.Failure;
import org.eclipse.passage.lbc.internal.base.ProductUserRequest;
import org.eclipse.passage.lbc.internal.base.acquire.Acquisition;
import org.eclipse.passage.lbc.internal.base.acquire.NoGrantsAvailable;
import org.eclipse.passage.lbc.internal.base.api.FloatingState;
import org.eclipse.passage.lic.floating.model.api.GrantAcqisition;
import org.eclipse.passage.lic.floating.model.meta.FloatingPackage;
Expand All @@ -45,7 +45,7 @@
public final class ExtensiveReleaseTest {

private final TestData data = new TestData();
private final int noGrants = new Failure.NoGrantsAvailable(data.product(), data.feature()).error().code();
private final int noGrants = new NoGrantsAvailable(data.product(), data.feature()).error().code();

@Test
public void concurrentAcquireAndRelease() throws InterruptedException, ExecutionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

import org.eclipse.emf.ecore.EObject;
import org.eclipse.passage.lbc.internal.base.BaseFlotingRequestHandled;
import org.eclipse.passage.lbc.internal.base.Failure;
import org.eclipse.passage.lbc.internal.base.acquire.NoGrantsAvailable;
import org.eclipse.passage.lbc.internal.base.acquire.NotReleased;
import org.eclipse.passage.lbc.internal.base.api.RawRequest;
import org.eclipse.passage.lic.floating.model.api.GrantAcqisition;
import org.eclipse.passage.lic.floating.model.meta.FloatingFactory;
Expand Down Expand Up @@ -50,7 +51,7 @@ public void mineNothing() throws LicensingException, IOException {
public void acquireNothing() throws LicensingException, IOException {
NetResponse response = new BaseFlotingRequestHandled(request(new PassageAction.Acquire())).get();
assertTrue(response.failed());
assertEquals(new Failure.NoGrantsAvailable(product, feature).error().code(), response.error().code());
assertEquals(new NoGrantsAvailable(product, feature).error().code(), response.error().code());
}

@Test
Expand All @@ -59,7 +60,7 @@ public void releaseInVain() throws LicensingException, IOException {
NetResponse response = new BaseFlotingRequestHandled(//
request(new PassageAction.Release(), Optional.of(acqisition))).get();
assertTrue(response.failed());
assertEquals(new Failure.NotReleased(product, acqisition).error().code(), response.error().code());
assertEquals(new NotReleased(product, acqisition).error().code(), response.error().code());
}

private RawRequest request(PassageAction action) throws LicensingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import java.util.Arrays;

import org.eclipse.passage.lbc.internal.base.BaseFlotingRequestHandled;
import org.eclipse.passage.lbc.internal.base.Failure;
import org.eclipse.passage.lic.floating.model.net.ServerAuthenticationExpression;
import org.eclipse.passage.lic.floating.model.net.ServerAuthenticationType;
import org.eclipse.passage.lic.internal.api.EvaluationType;
import org.eclipse.passage.lic.internal.api.PassageAction;
import org.eclipse.passage.lic.internal.base.ProductIdentifier;
import org.eclipse.passage.lic.internal.base.ProductVersion;
import org.eclipse.passage.lic.internal.base.StringNamedData;
import org.eclipse.passage.lic.internal.net.handle.Failure;
import org.eclipse.passage.lic.internal.net.handle.NetResponse;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import static org.junit.Assert.assertTrue;

import org.eclipse.passage.lbc.internal.base.BaseFlotingRequestHandled;
import org.eclipse.passage.lbc.internal.base.Failure;
import org.eclipse.passage.lic.floating.model.net.ServerAuthenticationExpression;
import org.eclipse.passage.lic.floating.model.net.ServerAuthenticationType;
import org.eclipse.passage.lic.internal.api.EvaluationType;
import org.eclipse.passage.lic.internal.api.PassageAction;
import org.eclipse.passage.lic.internal.base.StringNamedData;
import org.eclipse.passage.lic.internal.net.handle.Failure;
import org.eclipse.passage.lic.internal.net.handle.NetResponse;
import org.junit.Test;

Expand Down