Skip to content

Commit

Permalink
Added GitHub ResolutionStrategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
ysrbo committed Mar 28, 2024
1 parent 7d37c9a commit 3f924a9
Show file tree
Hide file tree
Showing 17 changed files with 359 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,6 @@ protected Class<?> findClass( final String name ) {
private static <T> Class<T> defineAndLoad( final QualifiedName className, final ClassLoader classLoader ) {
try {
return (Class<T>) classLoader.loadClass( className.toString() );
// final List<String> compilerOptions = List.of( "-classpath", System.getProperty( "java.class.path" ) );
// compiler.getTask( null, manager, diagnosticListener, compilerOptions, null, compilerInput ).call();
// return loadOrder.stream().collect( Collectors.toMap( Function.identity(), qualifiedName ->
// defineAndLoad( qualifiedName, manager ) ) );
// } catch ( IOException e ) {
// throw new RuntimeException( e );
// }
// }
//
// @SuppressWarnings( "unchecked" )
// private static <T> Class<T> defineAndLoad( final QualifiedName className, final InMemoryClassFileManager manager ) {
// try {
// return (Class<T>) new ClassLoader() {
// @Override
// protected Class<?> findClass( final String name ) {
// final byte[] classBytes = manager.getOutput( name ).getBytes();
// return defineClass( name, classBytes, 0, classBytes.length );
// }
// }.loadClass( className.toString() );
} catch ( final ClassNotFoundException exception ) {
throw new RuntimeException( exception );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,6 @@ public static Try<AspectModelUrn> fileToUrn( final File inputFile ) {
* @return the URN of the model element that corresponds to the file name and its location inside the models root
*/
public static AspectModelUrn urnFromModel( final VersionedModel model, final File file ) {
// TODO replace File file to URI uri
final String aspectName = FilenameUtils.removeExtension( file.getName() );
return Streams.stream( model.getRawModel().listSubjects() ).filter( s -> aspectName.equals( s.getLocalName() ) )
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Robert Bosch Manufacturing Solutions GmbH
* Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH
*
* See the AUTHORS file(s) distributed with this work for additional
* information regarding authorship.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import java.net.URI;
import java.util.function.Function;

import org.apache.jena.rdf.model.Model;
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;

import io.vavr.control.Try;
import org.apache.jena.rdf.model.Model;

/**
* Represents one way to load and resolve an Aspect model from a given source.
Expand All @@ -37,5 +37,7 @@ default Try<Model> applyUri( URI uri ) {
return Try.of( () -> AspectModelUrn.fromUrn( uri ) ).flatMap( this );
}

InputStream read( URI uri ) throws Exception;
default InputStream read( URI uri ) throws Exception {
return uri.toURL().openStream();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand Down Expand Up @@ -52,7 +51,7 @@ public StructuredModelsRoot( final URI resource ) {

private static Try<URI> forUri( final URI uri ) {
return getModelRoot( uri )
.orElse( () -> Try.failure( new ModelResolutionException( "Could not locate models root directory for " + uri ) ) );
.recoverWith( x -> Try.failure( new ModelResolutionException( "Could not locate models root directory for " + uri, x ) ) );
}

@Override
Expand Down Expand Up @@ -81,13 +80,8 @@ public static Try<URI> getModelRoot( final URI input ) {
}

private static Try<URI> findModelsRootFromUrl( URI input ) {
return Try.of( input::toURL )
//change path to parent
.mapTry( url -> {
var path = Paths.get( url.getPath(), "..", "..", "..", "." );
return new URL( url.getProtocol(), url.getHost(), url.getPort(), path.toString() );
} )
.mapTry( URL::toURI );
return Try.of(() -> input.resolve( "../.." ) )
.map( URI::normalize );
}

private static Try<URI> findModelsRoot( File file ) {
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<module>core/esmf-test-aspect-models</module>
<module>core/esmf-test-resources</module>
<module>tools/esmf-aspect-model-maven-plugin</module>
<module>tools/esmf-aspect-model-github-resolver</module>
<module>tools/samm-cli</module>
<module>documentation</module>
</modules>
Expand Down Expand Up @@ -104,6 +105,11 @@
<artifactId>esmf-aspect-model-resolver</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.esmf</groupId>
<artifactId>esmf-aspect-model-github-resolver</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.esmf</groupId>
<artifactId>esmf-aspect-meta-model-types</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions tools/esmf-aspect-model-github-resolver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Aspect Model GitHub Resolver

The plugin for the Aspect Model Resolver provides functionality to resolve aspect models from GitHub.
85 changes: 85 additions & 0 deletions tools/esmf-aspect-model-github-resolver/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2024 Robert Bosch Manufacturing Solutions GmbH
~
~ See the AUTHORS file(s) distributed with this work for additional
~ information regarding authorship.
~
~ This Source Code Form is subject to the terms of the Mozilla Public
~ License, v. 2.0. If a copy of the MPL was not distributed with this
~ file, You can obtain one at https://mozilla.org/MPL/2.0/.
~
~ SPDX-License-Identifier: MPL-2.0
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.eclipse.esmf</groupId>
<artifactId>esmf-sdk-parent</artifactId>
<version>DEV-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>esmf-aspect-model-github-resolver</artifactId>
<name>ESMF Aspect Model GitHub Resolver</name>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.eclipse.esmf</groupId>
<artifactId>esmf-aspect-model-resolver</artifactId>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.esmf</groupId>
<artifactId>esmf-aspect-model-urn</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.esmf</groupId>
<artifactId>esmf-semantic-aspect-meta-model</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.esmf</groupId>
<artifactId>esmf-aspect-meta-model-resolver</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-vavr</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.esmf</groupId>
<artifactId>esmf-test-aspect-models</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.esmf</groupId>
<artifactId>esmf-test-resources</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.eclipse.esmf.aspectmodel.resolver.github;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import org.apache.jena.rdf.model.Model;
import org.eclipse.esmf.aspectmodel.resolver.AspectModelResolver;
import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategy;
import org.eclipse.esmf.aspectmodel.resolver.fs.StructuredModelsRoot;
import org.eclipse.esmf.aspectmodel.resolver.services.TurtleLoader;
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;

import io.vavr.control.Try;

public class GitHubResolutionStrategy implements ResolutionStrategy {
private final StructuredModelsRoot root;

public static void install() {
AspectModelResolver.register( null, uri -> Try.of( () -> new GitHubResolutionStrategy( uri ) ) );
}

public GitHubResolutionStrategy( URI baseUri ) {
if ( !isUrl( baseUri ) || !isGithubUrl( baseUri ) )
throw new UnsupportedOperationException( "GitHub resolution strategy does not support URN: " + baseUri );
else {
this.root = new StructuredModelsRoot( transformGithubUrlToContentLink( baseUri ).mapTry( URL::toURI ).get() );
}
}

@Override
public Try<Model> apply( AspectModelUrn urn ) {
return applyUri( root.determineAspectModelFile( urn ) );
}

@Override
public Try<Model> applyUri( URI uri ) {
return AspectModelUrn.from( uri ).flatMap( this::apply )
.recoverWith( __ ->
toURL( uri ).flatMap( GitHubResolutionStrategy::loadModel )
)
.orElse( Try.failure( new UnsupportedOperationException( "GitHub resolution strategy does not support URI: " + uri ) ) );
}

@Override
public InputStream read( URI uri ) {
return Try.success( uri )
.filter( GitHubResolutionStrategy::isGithubUrl )
.flatMap( GitHubResolutionStrategy::toURL )
.flatMap( GitHubResolutionStrategy::read )
.getOrElse( (InputStream) null );
}

static Try<URL> toURL( URI uri ) {
if ( isGithubUrl( uri ) )
return transformGithubUrlToContentLink( uri );
else
return Try.of( uri::toURL );
}

static boolean isUrl( URI uri ) {
try {
//noinspection ResultOfMethodCallIgnored
uri.toURL();
return true;
} catch ( MalformedURLException e ) {
return false;
}
}

static boolean isGithubUrl( URI uri ) {
try {
String host = uri.toURL().getHost();
return "github.com".equals( host )
|| "raw.githubusercontent.com".equals( host );
} catch ( Exception e ) {
return false;
}
}

static Try<Model> loadModel( URL link ) {
return read( link )
.flatMap( TurtleLoader::loadTurtle );
}

static Try<InputStream> read( URL link ) {
return Try.withResources( link::openStream )
.of( InputStream::readAllBytes )
.map( ByteArrayInputStream::new );
}

static Try<URL> transformGithubUrlToContentLink( URI githubUrl ) {
String url = githubUrl.toString()
.replace( "github.com", "raw.githubusercontent.com" )
.replace( "/blob/", "/" );
return Try.of( () -> new URL( url ) );
}

public String toString() {
return "GitHubStrategy(root=" + root + ')';
}
}
Loading

0 comments on commit 3f924a9

Please sign in to comment.