Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#5: Improve access method for CommandletManager #129

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,29 @@ public Commandlet getCommandletByFirstKeyword(String keyword) {
}

/**
* This method gives global access to the {@link CommandletManager} instance. Typically you should have access to
* {@link IdeContext} and use {@link IdeContext#getCommandletManager()} to access the proper instance of
* {@link CommandletManager}. Only in very specific cases where there is no {@link IdeContext} available, you may use
* this method to access it (e.g. from {@link com.devonfw.tools.ide.property.CommandletProperty})
*
* @return the static instance of this {@link CommandletManager} implementation that has already been initialized.
* @throws IllegalStateException if the instance has not been previously initialized via
* {@link #getOrCreate(IdeContext)}.
*/
public static CommandletManager get() {

return getOrCreate(null);
}

/**
* This method has to be called initially from {@link IdeContext} to create the instance of this
* {@link CommandletManager} implementation. It will store that instance internally in a static variable so it can
* later be retrieved with {@link #get()}.
*
* @param context the {@link IdeContext}.
* @return the {@link CommandletManager}.
*/
public static CommandletManager of(IdeContext context) {
public static CommandletManager getOrCreate(IdeContext context) {

if (context == null) {
if (INSTANCE == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public AbstractIdeContext(IdeLogLevel minLogLevel, Function<IdeLogLevel, IdeSubL
this.loggers.put(level, logger);
}
this.systemInfo = new SystemInfoImpl();
this.commandletManager = CommandletManagerImpl.of(this);
this.commandletManager = CommandletManagerImpl.getOrCreate(this);
this.fileAccess = new FileAccessImpl(this);
String workspace = WORKSPACE_MAIN;
if (userDir == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected String format(Commandlet valueToFormat) {
public Commandlet parse(String valueAsString) {

// needs to be initialized before calling this...
Commandlet commandlet = CommandletManagerImpl.of(null).getCommandlet(valueAsString);
Commandlet commandlet = CommandletManagerImpl.get().getCommandlet(valueAsString);
if (commandlet == null) {
throw new IllegalArgumentException(valueAsString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected String format(ToolCommandlet valueToFormat) {
public ToolCommandlet parse(String valueAsString) {

// needs to be initialized before calling this...
return CommandletManagerImpl.of(null).getToolCommandlet(valueAsString);
return CommandletManagerImpl.get().getToolCommandlet(valueAsString);
}

}