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.HashMap;
22 import java.util.Map;
23
24 import io.undertow.servlet.api.ServletInfo;
25 import io.undertow.servlet.handlers.ServletHandler;
26 import io.undertow.servlet.handlers.ServletPathMatches;
27 import io.undertow.util.CopyOnWriteMap;
28
29 /**
30  * Runtime representation of servlets. Basically a container for {@link ManagedServlet} instances
31  *
32  * @author Stuart Douglas
33  */

34 public class ManagedServlets {
35
36     private final Map<String, ServletHandler> managedServletMap = new CopyOnWriteMap<>();
37     private final DeploymentImpl deployment;
38     private final ServletPathMatches servletPaths;
39
40     public ManagedServlets(final DeploymentImpl deployment, final ServletPathMatches servletPaths) {
41         this.deployment = deployment;
42         this.servletPaths = servletPaths;
43     }
44
45     public ServletHandler addServlet(final ServletInfo servletInfo) {
46         ManagedServlet managedServlet = new ManagedServlet(servletInfo, deployment.getServletContext());
47         ServletHandler servletHandler = new ServletHandler(managedServlet);
48         managedServletMap.put(servletInfo.getName(), servletHandler);
49         deployment.addLifecycleObjects(managedServlet);
50         this.servletPaths.invalidate();
51
52         return servletHandler;
53     }
54
55     public ManagedServlet getManagedServlet(final String name) {
56         ServletHandler servletHandler = managedServletMap.get(name);
57         if(servletHandler == null) {
58             return null;
59         }
60         return servletHandler.getManagedServlet();
61     }
62
63     public ServletHandler getServletHandler(final String name) {
64         return managedServletMap.get(name);
65     }
66
67     public Map<String, ServletHandler> getServletHandlers() {
68         return new HashMap<>(managedServletMap);
69     }
70
71 }
72