Package org.eclipse.equinox.struts.examples

Source Code of org.eclipse.equinox.struts.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.struts.examples;

import org.apache.struts.action.ActionServlet;
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;
import java.util.Dictionary;
import java.util.Hashtable;

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.close();
  }

  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");
        httpService.registerResources("/struts-examples", "/", commonContext); //$NON-NLS-1$ //$NON-NLS-2$
       
        Servlet adaptedJspServlet = new ContextPathServletAdaptor(new JspServlet(context.getBundle(), "/web"), "/struts-examples"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        httpService.registerServlet("/struts-examples/*.jsp", adaptedJspServlet, null, commonContext); //$NON-NLS-1$
       
        Dictionary initparams = new Hashtable();
        initparams.put("servlet-name", "action"); //Note: requires servlet-name support in Http Service Implementation
        initparams.put("config", "/WEB-INF/struts-config.xml");
        initparams.put("config/exercise", "/WEB-INF/exercise/struts-config.xml");
        initparams.put("config/upload", "/WEB-INF/upload/struts-config.xml");
        initparams.put("config/validator", "/WEB-INF/validator/struts-config.xml");
        initparams.put("config/dispatch", "/WEB-INF/dispatch/struts-config.xml");
        initparams.put("debug", "2");
        initparams.put("detail", "2");
        Servlet adaptedActionServlet = new ContextPathServletAdaptor(new ActionServlet(), "/struts-examples");
        httpService.registerServlet("/struts-examples/*.do", adaptedActionServlet, initparams, commonContext);
      } 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("/struts-examples"); //$NON-NLS-1$
      httpService.unregister("/struts-examples/*.jsp"); //$NON-NLS-1$
      httpService.unregister("/struts-examples/*.do"); //$NON-NLS-1$     
      super.removedService(reference, service);
    }
  }
}
TOP

Related Classes of org.eclipse.equinox.struts.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.