Skip to content

Commit

Permalink
remove usage of the map inside AppVersionHandlerMap
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <[email protected]>
  • Loading branch information
lachlan-roberts committed Aug 1, 2023
1 parent 1a348aa commit 202e119
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand All @@ -46,29 +47,25 @@
*/
public class AppVersionHandlerMap extends AbstractHandlerContainer {
private final AppVersionHandlerFactory appVersionHandlerFactory;
private final Map<AppVersionKey, AppVersion> appVersionMap;
private final Map<AppVersionKey, Handler> handlerMap;
private AppVersion appVersion;
private Handler handler;

public AppVersionHandlerMap(AppVersionHandlerFactory appVersionHandlerFactory) {
this.appVersionHandlerFactory = appVersionHandlerFactory;
this.appVersionMap = new HashMap<>();
this.handlerMap = new HashMap<>();
}

public void addAppVersion(AppVersion appVersion) {
appVersionMap.put(appVersion.getKey(), appVersion);
if (appVersion != null) {
throw new IllegalStateException("Already have an AppVersion " + this.appVersion);
}
this.appVersion = appVersion;
}

public void removeAppVersion(AppVersionKey appVersionKey) {
appVersionMap.remove(appVersionKey);
}

public int getNumAppVersions() {
return appVersionMap.size();
}

public String getAppVersions() {
return appVersionMap.keySet().toString();
if (Objects.equals(appVersionKey, appVersion.getKey()))
throw new IllegalArgumentException("AppVersionKey does not match AppVersion " + appVersion.getKey());
this.appVersion = null;
}

/**
Expand All @@ -85,12 +82,9 @@ public void setSessionStoreFactory(SessionStoreFactory factory) {
* Returns the {@code Handler} that will handle requests for the specified application version.
*/
public synchronized Handler getHandler(AppVersionKey appVersionKey) throws ServletException {
Handler handler = handlerMap.get(appVersionKey);
if (handler == null) {
AppVersion appVersion = appVersionMap.get(appVersionKey);
if (appVersion != null) {
handler = appVersionHandlerFactory.createHandler(appVersion);
handlerMap.put(appVersionKey, handler);
}
}
return handler;
Expand Down Expand Up @@ -155,7 +149,7 @@ public void setServer(Server server) {

@Override
public Handler[] getHandlers() {
return handlerMap.values().toArray(new Handler[0]);
return new Handler[]{ handler };
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@ public void stop() {

@Override
public void addAppVersion(AppVersion appVersion) throws FileNotFoundException {

if (appVersionHandlerMap.getNumAppVersions() > 0)
throw new IllegalStateException("Too many AppVersions: " + appVersionHandlerMap.getAppVersions());
appVersionHandlerMap.addAppVersion(appVersion);
}

Expand Down

0 comments on commit 202e119

Please sign in to comment.