Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e30910702c878586e5981c0f77f12f7d82a18892 (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
/******************************************************************************* 
 * Copyright (c) 2010-2011 Naumen. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Pavel Samolisov - initial API and implementation
 *******************************************************************************/
package org.eclipse.ecf.internal.tests.remoteservice.rpc;

import javax.servlet.http.HttpServlet;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpService;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

@SuppressWarnings("rawtypes")
public class HttpServiceConnector extends ServiceTracker implements ServiceTrackerCustomizer {

	private String path;

    private HttpServlet servlet;

    @SuppressWarnings("unchecked")
	public HttpServiceConnector(BundleContext context, String path, HttpServlet servlet) {
        super(context, HttpService.class.getName(), null);
        this.path = path;
        this.servlet = servlet;
        
        open();
    }

    public Object addingService(ServiceReference reference) {
        @SuppressWarnings("unchecked")
		HttpService httpService = (HttpService) super.addingService(reference);
        if (httpService == null)
          return null;

        try {
          httpService.registerServlet(path, servlet, null, null);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        
        return httpService;
    }

    @SuppressWarnings("unchecked")
	public void removedService(ServiceReference reference, Object service) {
        HttpService httpService = (HttpService) service;
        httpService.unregister(path);

        super.removedService(reference, service);
    }
}

Back to the top