Skip to content

Commit

Permalink
Elide 5.x dynamic config standalone (#1259)
Browse files Browse the repository at this point in the history
* Fix method call, Swagger Doc Update

Co-authored-by: AvaniMakwana <[email protected]>

* Swagger doc update

Co-authored-by: AvaniMakwana <[email protected]>

* update pom.xml

Co-authored-by: AvaniMakwana <[email protected]>

* update pom.xml

Co-authored-by: AvaniMakwana <[email protected]>

* review comments

Co-authored-by: AvaniMakwana <[email protected]>

* Update ElideStandaloneSettings.java

* Update pom.xml

* Update pom.xml

Co-authored-by: moiz arafat <[email protected]>
  • Loading branch information
2 people authored and aklish committed Dec 11, 2020
1 parent 7e8b807 commit 95b3031
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 207 deletions.
15 changes: 7 additions & 8 deletions elide-contrib/elide-dynamic-config-helpers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

<properties>
<hjson.version>3.0.0</hjson.version>
<jackson.version>2.10.2</jackson.version>
<handlebars.version>4.2.0</handlebars.version>
<json-schema-validator.version>2.2.12</json-schema-validator.version>
<mdkt.compiler.version>1.3.0</mdkt.compiler.version>
Expand All @@ -55,7 +54,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
<version>${version.jackson}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand All @@ -65,11 +64,11 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down Expand Up @@ -117,7 +116,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependency>
</dependencies>

<build>
Expand Down
2 changes: 0 additions & 2 deletions elide-datastore/elide-datastore-aggregation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

Expand All @@ -111,7 +110,6 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,8 @@

package com.yahoo.elide.example;

import com.yahoo.elide.contrib.swagger.SwaggerBuilder;
import com.yahoo.elide.contrib.swagger.resources.DocEndpoint;
import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.example.models.Comment;
import com.yahoo.elide.example.models.Post;
import com.yahoo.elide.example.models.User;
import com.yahoo.elide.standalone.config.ElideStandaloneSettings;
import io.swagger.models.Info;
import io.swagger.models.Swagger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
Expand All @@ -35,20 +24,8 @@ public int getPort() {
}

@Override
public List<DocEndpoint.SwaggerRegistration> enableSwagger() {
EntityDictionary dictionary = new EntityDictionary(new HashMap());

dictionary.bindEntity(User.class);
dictionary.bindEntity(Post.class);
dictionary.bindEntity(Comment.class);
Info info = new Info().title("Test Service").version("1.0");

SwaggerBuilder builder = new SwaggerBuilder(dictionary, info).withLegacyFilterDialect(false);
Swagger swagger = builder.build();

List<DocEndpoint.SwaggerRegistration> docs = new ArrayList<>();
docs.add(new DocEndpoint.SwaggerRegistration("test", swagger));
return docs;
public boolean enableSwagger() {
return true;
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions elide-standalone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@

<dependencies>
<!-- Elide dependency -->
<dependency>
<groupId>com.yahoo.elide</groupId>
<artifactId>elide-datastore-aggregation</artifactId>
<version>5.0.0-pr10-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>com.yahoo.elide</groupId>
<artifactId>elide-datastore-jpa</artifactId>
Expand All @@ -78,6 +84,12 @@
<version>5.0.0-pr10-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>com.yahoo.elide</groupId>
<artifactId>elide-dynamic-config-helpers</artifactId>
<version>5.0.0-pr10-SNAPSHOT</version>
</dependency>

<!-- JPA -->
<dependency>
<groupId>javax.persistence</groupId>
Expand Down Expand Up @@ -239,6 +251,10 @@
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public Map<String, Class<? extends Check>> getCheckMappings() {
}
/**
* Start the Elide service.
*
* This method blocks until the server exits.
*
* @throws Exception Exception thrown
*/
public void start() throws Exception {
start(true);
Expand All @@ -72,6 +73,7 @@ public void start() throws Exception {
* Start the Elide service.
*
* @param block - Whether or not to wait for the server to shutdown.
* @throws Exception Exception thrown
*/
public void start(boolean block) throws Exception {
ServletContextHandler context = new ServletContextHandler();
Expand Down Expand Up @@ -115,7 +117,7 @@ public void start(boolean block) throws Exception {
context.addServlet(AdminServlet.class, "/stats/*");
}

if (!elideStandaloneSettings.enableSwagger().isEmpty()) {
if (elideStandaloneSettings.enableSwagger()) {
ServletHolder jerseyServlet = context.addServlet(ServletContainer.class,
elideStandaloneSettings.getSwaggerPathSpec());
jerseyServlet.setInitOrder(0);
Expand Down Expand Up @@ -144,6 +146,8 @@ public void start(boolean block) throws Exception {

/**
* Stop the Elide service.
*
* @throws Exception Exception thrown
*/
public void stop() throws Exception {
jettyServer.stop();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
package com.yahoo.elide.standalone;

import com.yahoo.elide.async.models.AsyncQuery;
import com.yahoo.elide.contrib.dynamicconfighelpers.compile.ElideDynamicEntityCompiler;
import com.yahoo.elide.datastores.jpa.PersistenceUnitInfoImpl;
import com.yahoo.elide.utils.ClassScanner;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.internal.PersistenceUnitInfoDescriptor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;

Expand All @@ -24,8 +31,8 @@
*/
public class Util {

public static EntityManagerFactory getEntityManagerFactory(String modelPackageName, boolean includeAsyncModel,
Properties options) {
public static EntityManagerFactory getEntityManagerFactory(String modelPackageName, boolean includeAsyncModel,
Optional<ElideDynamicEntityCompiler> optionalCompiler, Properties options) {

// Configure default options for example service
if (options.isEmpty()) {
Expand Down Expand Up @@ -53,28 +60,57 @@ public static EntityManagerFactory getEntityManagerFactory(String modelPackageNa
options.put("javax.persistence.jdbc.password", "elide123");
}

ClassLoader classLoader = null;
//Bind entity classes from classpath to Persistence Unit
ArrayList<Class> loadedClasses = new ArrayList<>();

if (optionalCompiler.isPresent()) {
ElideDynamicEntityCompiler compiler = optionalCompiler.get();
classLoader = compiler.getClassLoader();
Collection<ClassLoader> classLoaders = new ArrayList<>();
classLoaders.add(classLoader);
options.put(AvailableSettings.CLASSLOADERS, classLoaders);

try {
loadedClasses.addAll(compiler.findAnnotatedClasses(Entity.class));
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}

PersistenceUnitInfo persistenceUnitInfo = new PersistenceUnitInfoImpl("elide-stand-alone",
combineModelEntities(modelPackageName, includeAsyncModel), options);
combineModelEntities(optionalCompiler, modelPackageName, includeAsyncModel),
options, classLoader);

return new EntityManagerFactoryBuilderImpl(
new PersistenceUnitInfoDescriptor(persistenceUnitInfo), new HashMap<>())
new PersistenceUnitInfoDescriptor(persistenceUnitInfo), new HashMap<>(), classLoader)
.build();
}

/**
* Combine the model entities with Async model.
* Combine the model entities with Async and Dynamic models.
*
* @param optionalCompiler optional dynamicCompiler
* @param modelPackageName Package name
* @param includeAsyncModel Include Async model package Name
* @return All entities combined from both package.
*/
public static List<String> combineModelEntities(String modelPackageName, boolean includeAsyncModel) {
public static List<String> combineModelEntities(Optional<ElideDynamicEntityCompiler> optionalCompiler,
String modelPackageName, boolean includeAsyncModel) {

List<String> modelEntities = getAllEntities(modelPackageName);

if (includeAsyncModel) {
modelEntities.addAll(getAllEntities(AsyncQuery.class.getPackage().getName()));
}

if (optionalCompiler.isPresent()) {
try {
modelEntities.addAll(optionalCompiler.get().findAnnotatedClassNames(Entity.class));
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
return modelEntities;
}

Expand Down
Loading

0 comments on commit 95b3031

Please sign in to comment.