Skip to content

Commit

Permalink
Scope: fix involving scoped import overrides, a new ScopePruningModul…
Browse files Browse the repository at this point in the history
…e is now responsible for pruning unsolicited scopes
  • Loading branch information
eledhwen committed Dec 27, 2021
1 parent 78a38ce commit 113761e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Add the following in your `pom.xml`:
<dependency>
<groupId>com.noleme</groupId>
<artifactId>noleme-vault</artifactId>
<version>0.16.2</version>
<version>0.16.3</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.noleme</groupId>
<artifactId>noleme-vault</artifactId>
<version>0.16.2</version>
<version>0.16.3</version>
<packaging>jar</packaging>

<name>Noleme Vault</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ public class ScopedDefinitions extends Definitions
{
private final String scope;
private final String uid;
private int activeReferenceCount;

public ScopedDefinitions(String scope)
{
this.scope = scope;
this.uid = UUID.randomUUID().toString();
this.activeReferenceCount = 0;
}

public String scope()
Expand All @@ -33,4 +35,15 @@ public ScopedDefinitions applyScope()

return this;
}

public ScopedDefinitions incrementActiveReferenceCount()
{
this.activeReferenceCount++;
return this;
}

public boolean isActivelyReferenced()
{
return this.activeReferenceCount > 0;
}
}
10 changes: 6 additions & 4 deletions src/main/java/com/noleme/vault/parser/VaultCompositeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.noleme.commons.container.Lists;
import com.noleme.json.Json;
import com.noleme.json.JsonException;
import com.noleme.vault.container.register.Definitions;
import com.noleme.vault.exception.VaultParserException;
import com.noleme.vault.exception.VaultStructureException;
import com.noleme.vault.parser.adjuster.VaultAdjuster;
import com.noleme.vault.parser.module.*;
import com.noleme.vault.parser.module.scope.ScopeModule;
import com.noleme.vault.parser.module.scope.ScopePruningModule;
import com.noleme.vault.parser.preprocessor.VaultPreprocessor;
import com.noleme.vault.parser.resolver.FlexibleResolver;
import com.noleme.vault.parser.resolver.VaultResolver;
import com.noleme.vault.parser.resolver.source.Source;
import com.noleme.commons.container.Lists;
import com.noleme.json.Json;
import com.noleme.json.JsonException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -69,7 +70,8 @@ public static List<VaultModule> defaultPostModules()
new VariableReplacementModule(),
new ScopeModule(),
new TagModule(),
new ServiceModule()
new ServiceModule(),
new ScopePruningModule()
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.noleme.vault.parser.module.scope;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.noleme.vault.container.register.Definitions;
import com.noleme.vault.container.register.ScopedDefinitions;
import com.noleme.vault.exception.VaultParserException;
import com.noleme.vault.parser.module.VaultModule;

/**
* @author Pierre LECERF ([email protected])
* Created on 27/12/2021
*/
public class ScopePruningModule implements VaultModule
{
@Override
public String identifier()
{
return "scopes";
}

@Override
public void process(ObjectNode node, Definitions definitions) throws VaultParserException
{
/* We loop over all scoped definitions and check if they are still actively referenced (ie. the ScopedImportExtractor did find services using them) */
for (ScopedDefinitions scope : definitions.scopes().values())
{
if (scope.isActivelyReferenced())
continue;

for (String inactiveScopedService : scope.services().keys())
definitions.services().remove(inactiveScopedService);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public ServiceDefinition extract(ObjectNode json, Definitions definitions) throw
if (!scope.services().has(expectedIdentifier))
throw new VaultParserException("Service "+identifier+" makes a reference to a non-existing "+use+" service in scope "+from);

scope.incrementActiveReferenceCount();

Reference ref = scope.services().reference(expectedIdentifier);

ServiceScopedImport def = new ServiceScopedImport(identifier, from, ref);
Expand Down

0 comments on commit 113761e

Please sign in to comment.