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.io.IOException;
22
23 import javax.servlet.Filter;
24 import javax.servlet.FilterChain;
25 import javax.servlet.ServletException;
26 import javax.servlet.ServletRequest;
27 import javax.servlet.ServletResponse;
28
29 import io.undertow.servlet.UndertowServletLogger;
30 import io.undertow.servlet.UndertowServletMessages;
31 import io.undertow.servlet.api.DeploymentManager;
32 import io.undertow.servlet.api.FilterInfo;
33 import io.undertow.servlet.api.InstanceHandle;
34 import io.undertow.servlet.spec.FilterConfigImpl;
35 import io.undertow.servlet.spec.ServletContextImpl;
36
37 /**
38  * @author Stuart Douglas
39  */

40 public class ManagedFilter implements Lifecycle {
41
42     private final FilterInfo filterInfo;
43     private final ServletContextImpl servletContext;
44
45     private volatile boolean started = false;
46     private volatile Filter filter;
47     private volatile InstanceHandle<? extends Filter> handle;
48
49     public ManagedFilter(final FilterInfo filterInfo, final ServletContextImpl servletContext) {
50         this.filterInfo = filterInfo;
51         this.servletContext = servletContext;
52     }
53
54     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
55         if(servletContext.getDeployment().getDeploymentState() != DeploymentManager.State.STARTED) {
56             throw UndertowServletMessages.MESSAGES.deploymentStopped(servletContext.getDeployment().getDeploymentInfo().getDeploymentName());
57         }
58         if (!started) {
59             start();
60         }
61         getFilter().doFilter(request, response, chain);
62     }
63
64     private Filter getFilter() throws ServletException {
65         if (filter == null) {
66             createFilter();
67         }
68         return filter;
69     }
70
71     public void createFilter() throws ServletException {
72         synchronized (this) {
73             if (filter == null) {
74                 try {
75                     handle = filterInfo.getInstanceFactory().createInstance();
76                 } catch (Exception e) {
77                     throw UndertowServletMessages.MESSAGES.couldNotInstantiateComponent(filterInfo.getName(), e);
78                 }
79                 Filter filter = handle.getInstance();
80                 new LifecyleInterceptorInvocation(servletContext.getDeployment().getDeploymentInfo().getLifecycleInterceptors(), filterInfo, filter, new FilterConfigImpl(filterInfo, servletContext)).proceed();
81                 this.filter = filter;
82             }
83         }
84     }
85
86     public synchronized void start() throws ServletException {
87         if (!started) {
88
89             started = true;
90         }
91     }
92
93     public synchronized void stop() {
94         started = false;
95         if (handle != null) {
96             try {
97                 new LifecyleInterceptorInvocation(servletContext.getDeployment().getDeploymentInfo().getLifecycleInterceptors(), filterInfo, filter).proceed();
98             } catch (Exception e) {
99                 UndertowServletLogger.ROOT_LOGGER.failedToDestroy(filterInfo, e);
100             }
101             handle.release();
102         }
103         filter = null;
104         handle = null;
105     }
106
107     @Override
108     public boolean isStarted() {
109         return started;
110     }
111
112     public FilterInfo getFilterInfo() {
113         return filterInfo;
114     }
115
116     @Override
117     public String toString() {
118         return "ManagedFilter{" +
119                 "filterInfo=" + filterInfo +
120                 '}';
121     }
122
123     public void forceInit() throws ServletException {
124         if (filter == null) {
125             createFilter();
126         }
127     }
128 }
129