All Downloads are FREE. Search and download functionalities are using the official Maven repository.

info.kwarc.sally4.servlet.impl.ServletImpl Maven / Gradle / Ivy

The newest version!
package info.kwarc.sally4.servlet.impl;

import info.kwarc.sally4.servlet.SallyServlet;

import java.util.Dictionary;

import javax.servlet.ServletException;

import org.apache.camel.CamelContext;
import org.apache.camel.component.servlet.CamelHttpTransportServlet;
import org.apache.camel.component.servlet.ServletComponent;
import org.apache.felix.ipojo.annotations.Bind;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Property;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Updated;
import org.apache.felix.ipojo.annotations.Validate;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(managedservice="sally")
@Provides(specifications=SallyServlet.class)
@Instantiate
public class ServletImpl implements SallyServlet {

	HttpService httpSevlet;

	final static String portProperty = "org.osgi.service.http.port";

	@Property
	String SallyHost = "http://localhost";

	@Property
	String SallyWSURL = "http://localhost:61614";
	
	String port="8181";

	@Property(name="SallyDebugTemplate")
	boolean debugTemplate;
	
	String SallyURL;
	
	@Updated
	void updated(Dictionary newValues) {
		SallyHost = newValues.get("SallyHost");
		SallyWSURL = newValues.get("SallyWSURL");
		SallyURL = SallyHost + ":" + port;
	}
	
	@Bind(aggregate=true)
	public void bindService(HttpService httpSevlet, Dictionary properties) {
		Object pid = properties.get("service.pid");
		if (pid == null)
			return;
		if (!"org.ops4j.pax.web".equals(pid)) {
			return;
		}
		port = properties.get(portProperty).toString();
		this.httpSevlet = httpSevlet;
	}	

	Logger log = LoggerFactory.getLogger(getClass());
	ServletComponent servletComponent;
	
	@Validate
	public void start() throws ServletException, NamespaceException {
		CamelHttpTransportServlet srvlet = new CamelHttpTransportServlet();
		
		httpSevlet.registerServlet("/sally", srvlet, null, null);
		httpSevlet.registerResources("/sally/static", "/static", null);
		servletComponent = new ServletComponent();
		servletComponent.setServletName(srvlet.getServletName());
		log.info("SallyURL "+SallyURL+ " "); 
	}

	@Override
	public ServletController init(CamelContext context, ClassLoader loader) {
		if (context.hasComponent("sallyservlet") == null) {
			context.addComponent("sallyservlet", servletComponent);			
		}
		
		return new ServletController(context, loader).setDebugTemplating(debugTemplate);
	}
		
	@Invalidate
	public void stop() {
		httpSevlet.unregister("/sally");		
	}

	@Override
	public String getURL() {
		return SallyURL+"/sally";
	}

	@Override
	public ServletComponent getCamelComponent() {
		return servletComponent;
	}

	@Override
	public String getWebConnectURL() {
		return SallyWSURL;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy