Skip to content

Commit

Permalink
Bug 568632 Implement feature grant acquire/release
Browse files Browse the repository at this point in the history
Extract typed EObject from bytes reading for reuse

Signed-off-by: eparovyshnaya <[email protected]>
  • Loading branch information
eparovyshnaya committed Nov 12, 2020
1 parent 685e2a3 commit 1759f9a
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ public final class AccessMessages extends NLS {

public static String AccessPacks_failure;
public static String AccessPacks_failed_on_file;

public static String AccessPacks_failed_xmi_read;
public static String AccessPacks_unexpected_amount;
public static String AccessPacks_unexpected_type;
public static String AccessPacks_files_gaining_failed;
public static String AccessPacks_insufficient_configuration;
public static String AccessPacks_no_key_keeper;
public static String AccessPacks_no_stream_codec;

public static String EObjectFromXmiResponse_unexpected_content_type;
public static String RemoteService_no_server;

public static String RequestParameters_encoding_failed;
public static String XmiToEObject_failed_xmi_read;
public static String XmiToEObject_unexpected_amount;
public static String XmiToEObject_unexpected_type;

static {
// initialize resource bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@

AccessPacks_failure=Reading floating license access file failed: %s
AccessPacks_failed_on_file=Error on reading Floating License Access file %s
AccessPacks_failed_xmi_read=Reading xmi content failed
AccessPacks_unexpected_amount=Unexpected amount of contents in Floating License Access file: %d
AccessPacks_unexpected_type=Unexpected content: %s
AccessPacks_files_gaining_failed = Failed to locate Floating License Access files
AccessPacks_insufficient_configuration = Mandatory services are not found
AccessPacks_no_key_keeper=No key Keeper service for product %s
AccessPacks_no_stream_codec=No Stream Codec service for product %s
EObjectFromXmiResponse_unexpected_content_type=Unexpected ContentType %s instead of %s

XmiToEObject_failed_xmi_read=Reading xmi content failed
XmiToEObject_unexpected_amount=Unexpected amount of contents in Floating License Access file: %d
XmiToEObject_unexpected_type=Unexpected content: %s

RemoteService_no_server=No floating server configuration found. *.flaen file is expected to be present.
RequestParameters_encoding_failed=Failed to encode url query parameter value [%s] to UTF-8
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,14 @@
*******************************************************************************/
package org.eclipse.passage.lic.internal.hc.remote.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
import org.eclipse.passage.lic.floating.model.api.FloatingLicenseAccess;
import org.eclipse.passage.lic.internal.api.LicensedProduct;
import org.eclipse.passage.lic.internal.api.LicensingException;
Expand Down Expand Up @@ -83,7 +77,7 @@ private List<Trouble> accessPacks(KeyKeeper key, StreamCodec codec, List<Floatin
List<Trouble> failures = new ArrayList<>();
for (Path file : files) {
try {
result.add(from(content(decoded(file, key, codec))));
result.add(from(decoded(file, key, codec)));
} catch (LicensingException e) {
failures.add(new Trouble(new ServiceFailedOnMorsel(),
String.format(AccessMessages.AccessPacks_failed_on_file, file.toAbsolutePath()), e));
Expand All @@ -100,26 +94,8 @@ private byte[] decoded(Path file, KeyKeeper key, StreamCodec codec) throws Licen
}
}

private EList<EObject> content(byte[] content) throws LicensingException {
Resource resource = new XMIResourceImpl();
try (InputStream input = new ByteArrayInputStream(content)) {
resource.load(input, Collections.emptyMap());
} catch (IOException e) {
throw new LicensingException(AccessMessages.AccessPacks_failed_xmi_read, e);
}
return resource.getContents();
}

private FloatingLicenseAccess from(EList<EObject> contents) throws LicensingException {
if (contents.size() != 1) {
throw new LicensingException(String.format(AccessMessages.AccessPacks_unexpected_amount, contents.size()));
}
EObject only = contents.get(0);
if (!FloatingLicenseAccess.class.isInstance(only)) {
throw new LicensingException(
String.format(AccessMessages.AccessPacks_unexpected_type, only.eClass().getName()));
}
return FloatingLicenseAccess.class.cast(only);
private FloatingLicenseAccess from(byte[] content) throws LicensingException {
return new EObjectFromBytes<>(content, FloatingLicenseAccess.class).get();
}

private KeyKeeper key() throws LicensingException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************************************
* 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.hc.remote.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.hc.i18n.AccessMessages;

/**
* Reads xmi content from raw byte array and retrieve the single root element of
* the expected type. Throws {@code LicensingException} is case of any surprise.
*/
public final class EObjectFromBytes<T> {

private final byte[] content;
private final Class<T> cls;

public EObjectFromBytes(byte[] content, Class<T> cls) {
this.content = content;
this.cls = cls;
}

public T get() throws LicensingException {
return from(only(content()));
}

private List<EObject> content() throws LicensingException {
Resource resource = new XMIResourceImpl();
try (InputStream input = new ByteArrayInputStream(content)) {
resource.load(input, Collections.emptyMap());
} catch (IOException e) {
throw new LicensingException(AccessMessages.XmiToEObject_failed_xmi_read, e);
}
return resource.getContents();
}

private EObject only(List<EObject> contents) throws LicensingException {
if (contents.size() != 1) {
throw new LicensingException(String.format(AccessMessages.XmiToEObject_unexpected_amount, contents.size()));
}
return contents.get(0);
}

private T from(EObject only) throws LicensingException {
if (!cls.isInstance(only)) {
throw new LicensingException(
String.format(AccessMessages.XmiToEObject_unexpected_type, only.eClass().getName()));
}
return cls.cast(only);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* 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.hc.remote.impl;

import org.eclipse.passage.lic.internal.api.LicensingException;
import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType;
import org.eclipse.passage.lic.internal.hc.i18n.AccessMessages;
import org.eclipse.passage.lic.internal.hc.remote.ResponseHandler;

public final class EObjectFromXmiResponse<T> implements ResponseHandler<T> {

private final Class<T> expected;

public EObjectFromXmiResponse(Class<T> expected) {
this.expected = expected;
}

@Override
public T read(byte[] raw, String contentType) throws LicensingException {
// TODO we use `transport`s for Conditions,
// design transports here too should the need arise
contentTypeIsExpected(contentType);
return new EObjectFromBytes<>(raw, expected).get();
}

private void contentTypeIsExpected(String contentType) throws LicensingException {
if (!new ContentType.Xml().contentType().equals(contentType)) {
throw new LicensingException(String.format(AccessMessages.EObjectFromXmiResponse_unexpected_content_type,
contentType, new ContentType.Xml().contentType()));
}
}

}

0 comments on commit 1759f9a

Please sign in to comment.