Skip to content

Commit

Permalink
#5: Improve access method for CommandletManager (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille authored Nov 7, 2023
1 parent ef506bc commit cbe3008
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
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);
}

}

0 comments on commit cbe3008

Please sign in to comment.