1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.servlet.core;
20
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26
27 import io.undertow.servlet.UndertowServletMessages;
28 import io.undertow.servlet.api.DeploymentInfo;
29 import io.undertow.servlet.api.DeploymentManager;
30 import io.undertow.servlet.api.ServletContainer;
31
32 /**
33  * The manager for all servlet deployments.
34  *
35  * @author Stuart Douglas
36  */

37 public class ServletContainerImpl implements ServletContainer {
38
39
40     private final Map<String, DeploymentManager> deployments = Collections.synchronizedMap(new HashMap<String, DeploymentManager>());
41     private final Map<String, DeploymentManager> deploymentsByPath = Collections.synchronizedMap(new HashMap<String, DeploymentManager>());
42
43     @Override
44     public Collection<String> listDeployments() {
45         return new HashSet<>(deployments.keySet());
46     }
47
48     @Override
49     public DeploymentManager addDeployment(final DeploymentInfo deployment) {
50         final DeploymentInfo dep = deployment.clone();
51         DeploymentManager deploymentManager = new DeploymentManagerImpl(dep, this);
52         deployments.put(dep.getDeploymentName(), deploymentManager);
53         deploymentsByPath.put(dep.getContextPath(), deploymentManager);
54         return deploymentManager;
55     }
56
57     @Override
58     public DeploymentManager getDeployment(final String deploymentName) {
59         return deployments.get(deploymentName);
60     }
61
62     @Override
63     public void removeDeployment(final DeploymentInfo deploymentInfo) {
64         final DeploymentManager deploymentManager = deployments.get(deploymentInfo.getDeploymentName());
65         if (deploymentManager.getState() != DeploymentManager.State.UNDEPLOYED) {
66             throw UndertowServletMessages.MESSAGES.canOnlyRemoveDeploymentsWhenUndeployed(deploymentManager.getState());
67         }
68         deployments.remove(deploymentInfo.getDeploymentName());
69         deploymentsByPath.remove(deploymentInfo.getContextPath());
70     }
71
72     @Override
73     public DeploymentManager getDeploymentByPath(final String path) {
74
75         DeploymentManager exact = deploymentsByPath.get(path.isEmpty() ? "/" : path);
76         if (exact != null) {
77             return exact;
78         }
79         int length = path.length();
80         int pos = length;
81
82         while (pos > 1) {
83             --pos;
84             if (path.charAt(pos) == '/') {
85                 String part = path.substring(0, pos);
86                 DeploymentManager deployment = deploymentsByPath.get(part);
87                 if (deployment != null) {
88                     return deployment;
89                 }
90             }
91         }
92         return deploymentsByPath.get("/");
93     }
94 }
95