Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 52044f9775f62502d46f8db50e269dffb491ac6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************************
 * Copyright (c) 2014, 2019 Raymond Augé and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Raymond Augé <raymond.auge@liferay.com> - Bug 436698
 ******************************************************************************/

package org.eclipse.equinox.http.servlet.internal.registration;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.equinox.http.servlet.internal.context.ContextController;
import org.eclipse.equinox.http.servlet.internal.context.ServiceHolder;
import org.eclipse.equinox.http.servlet.internal.servlet.Match;
import org.osgi.dto.DTO;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.wiring.BundleWiring;
import org.osgi.service.http.context.ServletContextHelper;

/**
 * @author Raymond Augé
 */
public abstract class EndpointRegistration<D extends DTO>
	extends MatchableRegistration<Servlet, D> implements Comparable<EndpointRegistration<?>>{

	protected final ServiceHolder<Servlet> servletHolder;
	private final ServletContextHelper servletContextHelper; //The context used during the registration of the servlet
	private final ContextController contextController;
	private final ClassLoader classLoader;

	public EndpointRegistration(
		ServiceHolder<Servlet> servletHolder, D d, ServletContextHelper servletContextHelper,
		ContextController contextController) {

		super(servletHolder.get(), d);
		this.servletHolder = servletHolder;
		this.servletContextHelper = servletContextHelper;
		this.contextController = contextController;
		if (servletHolder.getLegacyTCCL() != null) {
			// legacy registrations used the current TCCL at registration time
			classLoader = servletHolder.getLegacyTCCL();
		} else {
			classLoader = servletHolder.getBundle().adapt(BundleWiring.class).getClassLoader();
		}
		createContextAttributes();
	}

	public void destroy() {
		ClassLoader original = Thread.currentThread().getContextClassLoader();
		try {
			Thread.currentThread().setContextClassLoader(classLoader);

			contextController.getEndpointRegistrations().remove(this);
			contextController.getHttpServiceRuntime().getRegisteredObjects().remove(this.getT());
			contextController.ungetServletContextHelper(servletHolder.getBundle());

			super.destroy();
			getT().destroy();
		}
		finally {
			destroyContextAttributes();
			Thread.currentThread().setContextClassLoader(original);
			servletHolder.release();
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof EndpointRegistration)) {
			return false;
		}

		EndpointRegistration<?> endpointRegistration = (EndpointRegistration<?>)obj;

		return getD().equals(endpointRegistration.getD());
	}

	@Override
	public int hashCode() {
		return getD().hashCode();
	}

	//Delegate the init call to the actual servlet
	public void init(ServletConfig servletConfig) throws ServletException {
		ClassLoader original = Thread.currentThread().getContextClassLoader();
		try {
			Thread.currentThread().setContextClassLoader(classLoader);

			getT().init(servletConfig);
		}
		finally {
			Thread.currentThread().setContextClassLoader(original);
		}
	}

	public abstract String getName();

	public abstract String[] getPatterns();

	public abstract long getServiceId();

	public ServletContext getServletContext() {
		return getT().getServletConfig().getServletContext();
	}

	public ServletContextHelper getServletContextHelper() {
		return servletContextHelper;
	}

	public abstract ServiceReference<?> getServiceReference();

	@Override
	public String match(
		String name, String servletPath, String pathInfo, String extension,
		Match match) {

		if (match == Match.ERROR) {
			return null;
		}

		if (name != null) {
			if (getName().equals(name)) {
				return name;
			}

			return null;
		}

		String[] patterns = getPatterns();

		if (patterns == null) {
			return null;
		}

		for (String pattern : patterns) {
			if (doMatch(pattern, servletPath, pathInfo, extension, match)) {
				return pattern;
			}
		}

		return null;
	}

	//Delegate the handling of the request to the actual servlet
	public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		ClassLoader original = Thread.currentThread().getContextClassLoader();
		try {
			Thread.currentThread().setContextClassLoader(classLoader);
			getT().service(req, resp);
		} finally {
			Thread.currentThread().setContextClassLoader(original);
		}
	}

	private void createContextAttributes() {
		contextController.getProxyContext().createContextAttributes(
			contextController);
	}

	private void destroyContextAttributes() {
		contextController.getProxyContext().destroyContextAttributes(
			contextController);
	}

	@Override
	public int compareTo(EndpointRegistration<?> o) {
		int result = servletHolder.compareTo(o.servletHolder);
		if (result == 0) {
			result = Long.compare(getD().hashCode(), o.getD().hashCode());
		}
		return result;
	}

	@Override
	public String toString() {
		String toString = _toString;

		if (toString == null) {
			toString = SIMPLE_NAME + '[' + getD().toString() + ']';

			_toString = toString;
		}

		return toString;
	}

	private static final String SIMPLE_NAME =
		EndpointRegistration.class.getSimpleName();

	private String _toString;
}

Back to the top