-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor WorkspaceProcessingService to allow registering custom proce…
…ssors
- Loading branch information
Showing
6 changed files
with
158 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 0 additions & 47 deletions
47
recaf-core/src/main/java/software/coley/recaf/services/workspace/WorkspaceProcessing.java
This file was deleted.
Oops, something went wrong.
20 changes: 20 additions & 0 deletions
20
...core/src/main/java/software/coley/recaf/services/workspace/WorkspaceProcessingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package software.coley.recaf.services.workspace; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import software.coley.recaf.config.BasicConfigContainer; | ||
import software.coley.recaf.config.ConfigGroups; | ||
import software.coley.recaf.services.ServiceConfig; | ||
|
||
/** | ||
* Config for {@link WorkspaceProcessingService}. | ||
* | ||
* @author Matt Coley | ||
*/ | ||
@ApplicationScoped | ||
public class WorkspaceProcessingConfig extends BasicConfigContainer implements ServiceConfig { | ||
@Inject | ||
public WorkspaceProcessingConfig() { | ||
super(ConfigGroups.SERVICE_TRANSFORM, WorkspaceProcessingService.SERVICE_ID + CONFIG_SUFFIX); | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
...ore/src/main/java/software/coley/recaf/services/workspace/WorkspaceProcessingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package software.coley.recaf.services.workspace; | ||
|
||
import jakarta.annotation.Nonnull; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.enterprise.inject.spi.Bean; | ||
import jakarta.inject.Inject; | ||
import org.slf4j.Logger; | ||
import software.coley.collections.Unchecked; | ||
import software.coley.recaf.Bootstrap; | ||
import software.coley.recaf.analytics.logging.Logging; | ||
import software.coley.recaf.cdi.EagerInitialization; | ||
import software.coley.recaf.services.Service; | ||
import software.coley.recaf.workspace.model.Workspace; | ||
|
||
import java.util.IdentityHashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Applies all discovered {@link WorkspaceProcessor} instances to {@link Workspace} instances upon loading them via | ||
* {@link WorkspaceManager#setCurrent(Workspace)}. | ||
* | ||
* @author Matt Coley | ||
* @see WorkspaceProcessor Processor type to implement. | ||
*/ | ||
@EagerInitialization | ||
@ApplicationScoped | ||
public class WorkspaceProcessingService implements Service { | ||
public static final String SERVICE_ID = "workspace-processing"; | ||
private static final Logger logger = Logging.get(WorkspaceProcessingService.class); | ||
private final Map<Class<? extends WorkspaceProcessor>, Supplier<WorkspaceProcessor>> processorSuppliers = new IdentityHashMap<>(); | ||
private final WorkspaceProcessingConfig config; | ||
|
||
/** | ||
* @param workspaceManager | ||
* Manager to facilitate listening to new opened workspaces. | ||
* @param config | ||
* Service config. | ||
* @param processors | ||
* Discovered processors to apply. | ||
*/ | ||
@Inject | ||
public WorkspaceProcessingService(@Nonnull WorkspaceManager workspaceManager, | ||
@Nonnull WorkspaceProcessingConfig config, | ||
@Nonnull Instance<WorkspaceProcessor> processors) { | ||
this.config = config; | ||
for (Instance.Handle<WorkspaceProcessor> handle : processors.handles()) { | ||
Bean<WorkspaceProcessor> bean = handle.getBean(); | ||
Class<? extends WorkspaceProcessor> processorClass = Unchecked.cast(bean.getBeanClass()); | ||
processorSuppliers.put(processorClass, () -> { | ||
// Even though our processors may be @Dependent scoped, we need to do a new lookup each time we want | ||
// a new instance to get our desired scope behavior. If we re-use the instance handle that is injected | ||
// here then even @Dependent scoped beans will yield the same instance again and again. | ||
return Bootstrap.get().get(processorClass); | ||
}); | ||
} | ||
|
||
// Apply processors when new workspace is opened | ||
workspaceManager.addWorkspaceOpenListener(this::processWorkspace); | ||
} | ||
|
||
/** | ||
* @param processorClass | ||
* Class of processor to register. | ||
* @param processorSupplier | ||
* Supplier of processor instances. | ||
* @param <T> | ||
* Processor type. | ||
*/ | ||
public <T extends WorkspaceProcessor> void register(@Nonnull Class<T> processorClass, @Nonnull Supplier<T> processorSupplier) { | ||
processorSuppliers.put(Unchecked.cast(processorClass), Unchecked.cast(processorSupplier)); | ||
} | ||
|
||
/** | ||
* @param processorClass | ||
* Class of processor to unregister. | ||
* @param <T> | ||
* Processor type. | ||
*/ | ||
public <T extends WorkspaceProcessor> void unregister(@Nonnull Class<T> processorClass) { | ||
processorSuppliers.remove(Unchecked.cast(processorClass)); | ||
} | ||
|
||
/** | ||
* Applies all processors to the given workspace. | ||
* | ||
* @param workspace | ||
* Workspace to process. | ||
*/ | ||
public void processWorkspace(@Nonnull Workspace workspace) { | ||
for (Supplier<WorkspaceProcessor> processorSupplier : processorSuppliers.values()) { | ||
WorkspaceProcessor processor = processorSupplier.get(); | ||
|
||
logger.trace("Applying workspace processor: {}", processor.name()); | ||
processor.processWorkspace(workspace); | ||
} | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public String getServiceId() { | ||
return SERVICE_ID; | ||
} | ||
|
||
@Nonnull | ||
@Override | ||
public WorkspaceProcessingConfig getServiceConfig() { | ||
return config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters