Skip to content

Commit

Permalink
[#1334] Net emf objects transfer should rely on domain resource factory
Browse files Browse the repository at this point in the history
Signed-off-by: eparovyshnaya <[email protected]>
  • Loading branch information
eparovyshnaya committed Apr 4, 2024
1 parent 847a034 commit 49a06a3
Show file tree
Hide file tree
Showing 38 changed files with 230 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Collections;
import java.util.Map;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.passage.lbc.internal.base.api.RawRequest;
import org.eclipse.passage.lic.api.LicensedProduct;
Expand All @@ -27,7 +28,6 @@
import org.eclipse.passage.lic.internal.net.io.SafePayload;
import org.eclipse.passage.lic.licenses.model.api.GrantAcqisition;
import org.eclipse.passage.lic.licenses.model.meta.LicensesPackage;
import org.eclipse.passage.lic.licenses.model.util.LicensesResourceImpl;

public abstract class DecodedRequest<T extends EObject> {

Expand All @@ -41,10 +41,10 @@ protected DecodedRequest(RawRequest request, Map<String, Object> options) {

public final T get() throws IOException, LicensingException {
// FIXME:AF: should be done via factory
return new EObjectFromBytes<>(decoded(request.content()), target(), LicensesResourceImpl::new).get(options);
return new EObjectFromBytes<T>(decoded(request.content()), target()).get(options);
}

protected abstract Class<T> target();
protected abstract EClass target();

private byte[] decoded(byte[] raw) throws LicensingException {
LicensedProduct product = new ProductUserRequest<RawRequest>(request).product().get();
Expand All @@ -58,8 +58,8 @@ public GrantAck(RawRequest request) {
}

@Override
protected Class<GrantAcqisition> target() {
return GrantAcqisition.class;
protected EClass target() {
return LicensesPackage.eINSTANCE.getGrantAcqisition();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.eclipse.passage.lic.internal.emf.EObjectFromBytes;
import org.eclipse.passage.lic.licenses.model.api.FloatingLicensePack;
import org.eclipse.passage.lic.licenses.model.meta.LicensesPackage;
import org.eclipse.passage.lic.licenses.model.util.LicensesResourceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -68,7 +67,8 @@ private Optional<FloatingLicensePack> pack(Path license) {
FloatingLicensePack pack;
try {
// FIXME:AF: should be done via factory
pack = new EObjectFromBytes<>(decoded(license), FloatingLicensePack.class, LicensesResourceImpl::new)//
pack = new EObjectFromBytes<FloatingLicensePack>(decoded(license),
LicensesPackage.eINSTANCE.getFloatingLicensePack())//
.get(Collections.singletonMap(LicensesPackage.eNS_URI, LicensesPackage.eINSTANCE));
} catch (LicensingException e) {
log.error("failed: ", e); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.passage.lic.licenses.model.api.FloatingLicensePack;
import org.eclipse.passage.lic.licenses.model.api.ProductRef;
import org.eclipse.passage.lic.licenses.model.meta.LicensesPackage;
import org.eclipse.passage.lic.licenses.model.util.LicensesResourceImpl;

final class Pack {

Expand Down Expand Up @@ -63,11 +62,9 @@ final class Resolved {
}

private FloatingLicensePack read(byte[] bytes) throws LicensingException {
// FIXME:AF: should be done via factory
return new EObjectFromBytes<>(//
return new EObjectFromBytes<FloatingLicensePack>(//

Check warning on line 65 in bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/interaction/Pack.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/interaction/Pack.java#L65

Added line #L65 was not covered by tests
bytes, //
FloatingLicensePack.class, //
LicensesResourceImpl::new//
LicensesPackage.eINSTANCE.getFloatingLicensePack()//

Check warning on line 67 in bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/interaction/Pack.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lbc.base/src/org/eclipse/passage/lbc/internal/base/interaction/Pack.java#L67

Added line #L67 was not covered by tests
).get(Collections.singletonMap(LicensesPackage.eNAME, LicensesPackage.eINSTANCE));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.eclipse.passage.lic.licenses.model.api.FloatingLicensePack;
import org.eclipse.passage.lic.licenses.model.api.ProductRef;
import org.eclipse.passage.lic.licenses.model.meta.LicensesPackage;
import org.eclipse.passage.lic.licenses.model.util.LicensesResourceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -94,11 +93,9 @@ private boolean productFits(ProductRef ref) {
}

private FloatingLicensePack pack(Path source) throws LicensingException {
// FIXME:AF: should be done via factory
return new EObjectFromBytes<>(//
return new EObjectFromBytes<FloatingLicensePack>(//
decoded(source), //
FloatingLicensePack.class, //
LicensesResourceImpl::new//
LicensesPackage.eINSTANCE.getFloatingLicensePack()//
).get(Collections.singletonMap(LicensesPackage.eNS_URI, LicensesPackage.eINSTANCE));
}

Expand Down
2 changes: 1 addition & 1 deletion bundles/org.eclipse.passage.lbc.jetty/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="0.0.0",
org.eclipse.passage.lic.net;bundle-version="0.0.0"
Bundle-ActivationPolicy: lazy
Bundle-Activator: org.eclipse.passage.lbc.internal.jetty.FlsJettyActivator
Export-Package: org.eclipse.passage.lbc.jetty
Export-Package: org.eclipse.passage.lbc.jetty;x-internal:=true
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.passage.lic.internal.net.connect.Storage;
import org.osgi.framework.BundleContext;

@SuppressWarnings("restriction")
public final class FlsCommands {

public void register(BundleContext context, String name, Storage storage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ output.. = bin/
bin.includes = META-INF/,\
OSGI-INF/,\
model/,\
about.html
about.html,\
.
src.includes = model/
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.eclipse.passage.lic.emf.resource;

import org.eclipse.emf.ecore.resource.Resource;

public interface BlindResourceFactory {

Resource createResource();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.eclipse.passage.lic.internal.emf;

import java.util.Objects;
import java.util.function.Supplier;

import org.eclipse.emf.ecore.EClass;
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.emf.resource.BlindResourceFactory;

public final class BlindResource implements Supplier<Resource> {

private final EClass type;

public BlindResource(EClass type) {
this.type = Objects.requireNonNull(type);
}

public BlindResource(EObject source) {
this(Objects.requireNonNull(source).eClass());
}

@Override
public Resource get() {
String domain = type.getEPackage().getName();
Object factory = Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().get(domain);
if (factory instanceof BlindResourceFactory) {
return ((BlindResourceFactory) factory).createResource();

Check warning on line 30 in bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/internal/emf/BlindResource.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/internal/emf/BlindResource.java#L29-L30

Added lines #L29 - L30 were not covered by tests
}
return new XMIResourceImpl();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import java.util.function.Supplier;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;

public class EObjectFromBytes<T extends EObject> extends EObjectFromStream<T> {

private final byte[] content;

public EObjectFromBytes(byte[] content, Class<T> expected, Supplier<Resource> factory) {
super(expected, factory);
public EObjectFromBytes(byte[] content, EClass expected) {
super(expected);
Objects.requireNonNull(content, "EObjectFromBytes::content"); //$NON-NLS-1$
this.content = content;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Objects;
import java.util.function.Supplier;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;

public class EObjectFromFile<T extends EObject> extends EObjectFromStream<T> {

private final File file;

public EObjectFromFile(Path path, Class<T> expected, Supplier<Resource> factory) {
this(path.toFile(), expected, factory);
public EObjectFromFile(Path path, EClass expected) {
this(path.toFile(), expected);
}

public EObjectFromFile(File file, Class<T> expected, Supplier<Resource> factory) {
super(expected, factory);
public EObjectFromFile(File file, EClass expected) {
super(expected);

Check warning on line 34 in bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/internal/emf/EObjectFromFile.java

View check run for this annotation

Codecov / codecov/patch

bundles/org.eclipse.passage.lic.emf/src/org/eclipse/passage/lic/internal/emf/EObjectFromFile.java#L34

Added line #L34 was not covered by tests
Objects.requireNonNull(file, "EObjectFromFile::file"); //$NON-NLS-1$
this.file = file;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.passage.lic.api.LicensingException;
import org.eclipse.passage.lic.internal.emf.i18n.EmfMessages;

public abstract class EObjectFromStream<T extends EObject> {

private final Class<T> expected;
private final Supplier<Resource> factory;
private final EClass expected;

public EObjectFromStream(EClass expected) {
this.expected = Objects.requireNonNull(expected, getClass().getSimpleName() + "::expected"); //$NON-NLS-1$

public EObjectFromStream(Class<T> expected, Supplier<Resource> factory) {
Objects.requireNonNull(expected, getClass().getSimpleName() + "::expected"); //$NON-NLS-1$
Objects.requireNonNull(factory, getClass().getSimpleName() + "::factory"); //$NON-NLS-1$
this.expected = expected;
this.factory = factory;
}

public T get() throws LicensingException {
Expand All @@ -48,7 +45,7 @@ public T get(Map<?, ?> options) throws LicensingException {
protected abstract InputStream stream() throws IOException;

private List<EObject> content(Map<?, ?> options) throws LicensingException {
Resource resource = factory.get();
Resource resource = new BlindResource(expected).get();
try (InputStream input = stream()) {
resource.load(input, options);
} catch (IOException e) {
Expand All @@ -64,12 +61,13 @@ private EObject only(List<EObject> contents) throws LicensingException {
return contents.get(0);
}

@SuppressWarnings("unchecked")
private T from(EObject only) throws LicensingException {
if (!expected.isInstance(only)) {
if (expected != only.eClass()) {
throw new LicensingException(String.format(EmfMessages.XmiToEObject_unexpected_type,
only.getClass().getName(), expected.getName()));
}
return expected.cast(only);
return (T) expected.getInstanceClass().cast(only);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
Expand All @@ -27,21 +26,17 @@
public final class EObjectToBytes {

private final EObject source;
private final Supplier<Resource> factory;

public EObjectToBytes(EObject source, Supplier<Resource> factory) {
Objects.requireNonNull(source, getClass().getSimpleName() + "::source"); //$NON-NLS-1$
Objects.requireNonNull(factory, getClass().getSimpleName() + "::factory"); //$NON-NLS-1$
this.source = source;
this.factory = factory;
public EObjectToBytes(EObject source) {
this.source = Objects.requireNonNull(source, getClass().getSimpleName() + "::source"); //$NON-NLS-1$
}

public byte[] get() throws LicensingException {
return get(Collections.emptyMap());
}

public byte[] get(Map<?, ?> options) throws LicensingException {
Resource resource = factory.get();
Resource resource = new BlindResource(source).get();
resource.getContents().add(source);
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
resource.save(stream, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.eclipse.passage.lic.internal.hc.i18n.AccessMessages;
import org.eclipse.passage.lic.licenses.model.api.FloatingLicenseAccess;
import org.eclipse.passage.lic.licenses.model.meta.LicensesPackage;
import org.eclipse.passage.lic.licenses.model.util.LicensesResourceImpl;

/**
*
Expand Down Expand Up @@ -106,8 +105,8 @@ private byte[] decoded(Path file, KeyKeeper key, StreamCodec codec) throws Licen
}

private FloatingLicenseAccess from(byte[] content) throws LicensingException {
// FIXME:AF: should be done via factory
return new EObjectFromBytes<>(content, FloatingLicenseAccess.class, LicensesResourceImpl::new)//
return new EObjectFromBytes<FloatingLicenseAccess>(content,
LicensesPackage.eINSTANCE.getFloatingLicenseAccess())//
.get(Collections.singletonMap(LicensesPackage.eNS_URI, LicensesPackage.eINSTANCE));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.passage.lic.hc.remote.impl;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.passage.lic.api.LicensingException;
import org.eclipse.passage.lic.api.conditions.mining.ContentType;
Expand All @@ -20,27 +21,25 @@
import org.eclipse.passage.lic.internal.emf.EObjectFromBytes;
import org.eclipse.passage.lic.internal.hc.i18n.AccessMessages;
import org.eclipse.passage.lic.internal.net.io.SafePayload;
import org.eclipse.passage.lic.licenses.model.util.LicensesResourceImpl;

/**
*
* @since 1.1
*/
public final class EObjectFromXmiResponse<T extends EObject> implements ResponseHandler<T> {

private final Class<T> expected;
private final EClass expected;
private final Equipment equipment;

public EObjectFromXmiResponse(Class<T> expected, Equipment equipment) {
public EObjectFromXmiResponse(EClass expected, Equipment equipment) {
this.expected = expected;
this.equipment = equipment;
}

@Override
public T read(ResultsTransfered results, RequestContext context) throws LicensingException {
contentTypeIsExpected(results);
// FIXME:AF: should be done via factory
return new EObjectFromBytes<T>(decoded(results.data(), context), expected, LicensesResourceImpl::new).get();
return new EObjectFromBytes<T>(decoded(results.data(), context), expected).get();
}

private byte[] decoded(byte[] raw, RequestContext context) throws LicensingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
import org.eclipse.passage.lic.hc.remote.impl.Equipment;
import org.eclipse.passage.lic.hc.remote.impl.RemoteRequest;
import org.eclipse.passage.lic.hc.remote.impl.RemoteServiceData;
import org.eclipse.passage.lic.hc.remote.impl.RemoteServiceData.OfFeature;
import org.eclipse.passage.lic.hc.remote.impl.RequestParameters;
import org.eclipse.passage.lic.hc.remote.impl.ResultsTransfered;
import org.eclipse.passage.lic.hc.remote.impl.ServiceAny;
import org.eclipse.passage.lic.hc.remote.impl.RemoteServiceData.OfFeature;
import org.eclipse.passage.lic.internal.licenses.convert.PGrantAcquisition;
import org.eclipse.passage.lic.licenses.model.api.FloatingLicenseAccess;
import org.eclipse.passage.lic.licenses.model.meta.LicensesPackage;

final class RemoteAcquire<C extends Connection> extends ServiceAny<C, GrantAcquisition, RemoteServiceData.OfFeature> {

Expand All @@ -49,8 +50,8 @@ protected RemoteRequest<C> request(OfFeature params, FloatingLicenseAccess acces
@Override
protected ResponseHandler<GrantAcquisition> handler(FloatingLicenseAccess access) {
return new Response(//
new EObjectFromXmiResponse<>(//
org.eclipse.passage.lic.licenses.model.api.GrantAcqisition.class, //
new EObjectFromXmiResponse<org.eclipse.passage.lic.licenses.model.api.GrantAcqisition>(//
LicensesPackage.eINSTANCE.getGrantAcqisition(), //
equipment));
}

Expand Down
Loading

0 comments on commit 49a06a3

Please sign in to comment.