Skip to main content
summaryrefslogtreecommitdiffstats
blob: d23c4b96c40477edc032c5ca34c3ec71c6cece2f (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
/*******************************************************************************
 * Copyright (c) Feb. 2, 2019 Liferay, Inc.
 *
 * 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:
 *    Liferay, Inc. - initial API and implementation and/or initial
 *                    documentation
 ******************************************************************************/

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

import static org.osgi.service.http.whiteboard.HttpWhiteboardConstants.HTTP_WHITEBOARD_PREPROCESSOR_INIT_PARAM_PREFIX;

import java.util.concurrent.atomic.AtomicReference;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import org.eclipse.equinox.http.servlet.internal.context.ServiceHolder;
import org.eclipse.equinox.http.servlet.internal.error.HttpWhiteboardFailureException;
import org.eclipse.equinox.http.servlet.internal.registration.PreprocessorRegistration;
import org.eclipse.equinox.http.servlet.internal.servlet.FilterConfigImpl;
import org.eclipse.equinox.http.servlet.internal.util.ServiceProperties;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.runtime.dto.*;
import org.osgi.service.http.whiteboard.Preprocessor;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

/**
 * @author Raymond Augé
 */
public class PreprocessorCustomizer
	implements ServiceTrackerCustomizer<Preprocessor, AtomicReference<PreprocessorRegistration>> {

	public PreprocessorCustomizer(HttpServiceRuntimeImpl httpServiceRuntime) {
		this.httpServiceRuntime = httpServiceRuntime;
	}

	@Override
	public AtomicReference<PreprocessorRegistration> addingService(
		ServiceReference<Preprocessor> serviceReference) {

		AtomicReference<PreprocessorRegistration> result = new AtomicReference<PreprocessorRegistration>();
		if (!httpServiceRuntime.matches(serviceReference)) {
			return result;
		}

		try {
			removeFailed(serviceReference);

			result.set(addPreprocessorRegistration(serviceReference));
		}
		catch (HttpWhiteboardFailureException hwfe) {
			httpServiceRuntime.debug(hwfe.getMessage(), hwfe);

			recordFailed(serviceReference, hwfe.getFailureReason());
		}
		catch (Exception e) {
			httpServiceRuntime.error(e.getMessage(), e);

			recordFailed(serviceReference, DTOConstants.FAILURE_REASON_EXCEPTION_ON_INIT);
		}
		finally {
			httpServiceRuntime.incrementServiceChangecount();
		}

		return result;
	}

	@Override
	public void modifiedService(
		ServiceReference<Preprocessor> serviceReference, AtomicReference<PreprocessorRegistration> reference) {

		removedService(serviceReference, reference);
		AtomicReference<PreprocessorRegistration> added = addingService(serviceReference);
		reference.set(added.get());
	}

	@Override
	public void removedService(
		ServiceReference<Preprocessor> serviceReference, AtomicReference<PreprocessorRegistration> reference) {
		try {
			PreprocessorRegistration registration = reference.get();
			if (registration != null) {
				registration.destroy();
			}

			removeFailed(serviceReference);
		}
		finally {
			httpServiceRuntime.incrementServiceChangecount();
		}
	}

	void removeFailed(ServiceReference<Preprocessor> serviceReference) {
		httpServiceRuntime.removeFailedPreprocessorDTO(serviceReference);
	}

	private void recordFailed(
		ServiceReference<Preprocessor> serviceReference, int failureReason) {

		FailedPreprocessorDTO failedPreprocessorDTO = new FailedPreprocessorDTO();

		failedPreprocessorDTO.failureReason = failureReason;
		failedPreprocessorDTO.initParams = ServiceProperties.parseInitParams(
			serviceReference, HTTP_WHITEBOARD_PREPROCESSOR_INIT_PARAM_PREFIX);
		failedPreprocessorDTO.serviceId = (Long)serviceReference.getProperty(Constants.SERVICE_ID);

		httpServiceRuntime.recordFailedPreprocessorDTO(serviceReference, failedPreprocessorDTO);
	}

	private PreprocessorRegistration addPreprocessorRegistration(
		ServiceReference<Preprocessor> preprocessorRef) throws ServletException {

		ServiceHolder<Preprocessor> preprocessorHolder = new ServiceHolder<Preprocessor>(httpServiceRuntime.getConsumingContext().getServiceObjects(preprocessorRef));
		Preprocessor preprocessor = preprocessorHolder.get();
		PreprocessorRegistration registration = null;
		boolean addedRegisteredObject = false;
		try {
			if (preprocessor == null) {
				throw new IllegalArgumentException("Preprocessor cannot be null"); //$NON-NLS-1$
			}
			addedRegisteredObject = httpServiceRuntime.getRegisteredObjects().add(preprocessor);
			if (!addedRegisteredObject) {
				throw new HttpWhiteboardFailureException("Multiple registration of instance detected. Prototype scope is recommended: " + preprocessorRef, DTOConstants.FAILURE_REASON_SERVICE_IN_USE); //$NON-NLS-1$
			}
			registration = doAddPreprocessorRegistration(preprocessorHolder, preprocessorRef);
		} finally {
			if (registration == null) {
				preprocessorHolder.release();
				if (addedRegisteredObject) {
					httpServiceRuntime.getRegisteredObjects().remove(preprocessor);
				}
			}
		}
		return registration;
	}

	private PreprocessorRegistration doAddPreprocessorRegistration(
		ServiceHolder<Preprocessor> preprocessorHolder,
		ServiceReference<Preprocessor> preprocessorRef) throws ServletException {

		PreprocessorDTO preprocessorDTO = new PreprocessorDTO();

		preprocessorDTO.initParams = ServiceProperties.parseInitParams(
			preprocessorRef, HTTP_WHITEBOARD_PREPROCESSOR_INIT_PARAM_PREFIX);
		preprocessorDTO.serviceId = preprocessorHolder.getServiceId();

		PreprocessorRegistration newRegistration = new PreprocessorRegistration(
			preprocessorHolder, preprocessorDTO, httpServiceRuntime);
		FilterConfig filterConfig = new FilterConfigImpl(
			preprocessorHolder.get().getClass().getCanonicalName(),
			preprocessorDTO.initParams,
			httpServiceRuntime.getParentServletContext());

		newRegistration.init(filterConfig);

		httpServiceRuntime.getPreprocessorRegistrations().put(preprocessorHolder.getServiceReference(), newRegistration);

		return newRegistration;
	}

	private final HttpServiceRuntimeImpl httpServiceRuntime;

}

Back to the top