Package org.eclipse.equinox.jsp.jstl.examples

Source Code of org.eclipse.equinox.jsp.jstl.examples.Activator$HttpServiceTracker

/*
*  Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
*  WSO2 Inc. licenses this file to you under the Apache License,
*  Version 2.0 (the "License"); you may not use this file except
*  in compliance with the License.
*  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.eclipse.equinox.jsp.jstl.examples;

import org.eclipse.equinox.http.helper.BundleEntryHttpContext;
import org.eclipse.equinox.http.helper.ContextPathServletAdaptor;
import org.eclipse.equinox.jsp.jasper.JspServlet;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.util.tracker.ServiceTracker;

import javax.servlet.Servlet;

public class Activator implements BundleActivator {

    private ServiceTracker httpServiceTracker;

    public void start(BundleContext context) throws Exception {
        httpServiceTracker = new HttpServiceTracker(context);
        httpServiceTracker.open();
    }

    public void stop(BundleContext context) throws Exception {
        httpServiceTracker.open();
    }

    private class HttpServiceTracker extends ServiceTracker {

        public HttpServiceTracker(BundleContext context) {
            super(context, HttpService.class.getName(), null);
        }

        public Object addingService(ServiceReference reference) {
            final HttpService httpService = (HttpService) context.getService(reference);
            try {
                HttpContext commonContext =
                        new BundleEntryHttpContext(context.getBundle(), "/web"); //$NON-NLS-1$
                httpService.registerResources("/jstl-examples", "/",
                                              commonContext); //$NON-NLS-1$ //$NON-NLS-2$

                Servlet adaptedJspServlet = new ContextPathServletAdaptor(
                        new JspServlet(context.getBundle(), "/web"),
                        "/jstl-examples")//$NON-NLS-1$//$NON-NLS-2$
                httpService.registerServlet("/jstl-examples/*.jsp", adaptedJspServlet, null,
                                            commonContext); //$NON-NLS-1$
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return httpService;
        }

        public void removedService(ServiceReference reference, Object service) {
            final HttpService httpService = (HttpService) service;
            httpService.unregister("/jstl-examples"); //$NON-NLS-1$
            httpService.unregister("/jstl-examples/*.jsp"); //$NON-NLS-1$
            super.removedService(reference, service);
        }
    }
}
TOP

Related Classes of org.eclipse.equinox.jsp.jstl.examples.Activator$HttpServiceTracker

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.