<commit_before>package org.ow2.chameleon.rose.jsonrpc;

import static java.util.Arrays.asList;
import static org.osgi.service.log.LogService.LOG_ERROR;

import java.util.Dictionary;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.ipojo.annotations.Validate;
import org.jabsorb.JSONRPCBridge;
import org.jabsorb.JSONRPCServlet;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.service.log.LogService;
import org.osgi.service.remoteserviceadmin.EndpointDescription;
import org.ow2.chameleon.rose.AbstractExporterComponent;
import org.ow2.chameleon.rose.ExporterService;
import org.ow2.chameleon.rose.registry.ExportRegistryProvisioning;

@Component(name="RoSe.endpoint_creator.jsonrpc")
@Provides(specifications=ExporterService.class)
@Instantiate(name="RoSe.endpoint_creator.jsonrpc-default")
public class EndpointCreator extends AbstractExporterComponent implements ExporterService {
	
	/**
	 * Property containing the URL of the JSONRPC orb.
	 */
	private final static String PROP_JABSORB_URL = "org.jabsorb.url";
	
	/**
	 * Configuration supported by this component
	 */
	@ServiceProperty(name=ENDPOINT_CONFIG_PREFIX,mandatory=true,value="{json-rpc,jsonrpc,org.jabsorb}")
	private String[] configs;
	
    /**
     * Name of the Property needed by JSONRPCServlet, the gzip threshold.
     */
    private static final String PROP_GZIP_THRESHOLD = "gzip.threshold";

    /**
     * Values of the Property needed by JSONRPCServlet, the gzip threshold.
     */
    @ServiceProperty(name=PROP_GZIP_THRESHOLD,mandatory=true,value="200")
    private String gzip_threshold;
    
    /**
     * The Servlet name of the JSON-RPC bridge.
     */
    @ServiceProperty(name="jsonrpc.servlet.name",mandatory=true,value="/JSONRPC")
    private String servletname;
    
    /**
     * This class implements a bridge that unmarshalls JSON objects in JSON-RPC
     * request format, invokes a method on the exported object, and then
     * marshalls the resulting Java objects to JSON objects in JSON-RPC result
     * format.
     */
    private JSONRPCBridge jsonbridge;
    
    /**
     * Set which contains the names of the endpoint created by this factory.
     */
    private Set<String> endpointIds = new HashSet<String>();
    
	@Requires(optional=true)
	private LogService logger;
	
	@Requires(optional=true)
	private EventAdmin eventadmin;
	
	@Requires(optional=false)
	private HttpService httpservice;
	
	@Requires(optional=false)
	private ExportRegistryProvisioning registry;
	
	private final BundleContext context;
	
	public EndpointCreator(BundleContext pContext) {
		context = pContext;
	}
	
	/*--------------------------*
	 *  Component LifeCycle     *
	 *--------------------------*/
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#start()
	 */
	@Override
	@Validate
	protected void start() {
		super.start();

        Dictionary<String, String> properties = new Hashtable<String, String>();
        properties.put(PROP_GZIP_THRESHOLD, gzip_threshold);

        try {
            // Registered the JSONRPCServlet
            httpservice.registerServlet(servletname, new JSONRPCServlet(), properties, null);
        } catch (NamespaceException e) {
            logger.log(LOG_ERROR, e.getMessage(), e);
        } catch (Exception e) {
            logger.log(LOG_ERROR, e.getMessage(), e);
        }

        // Set the bridge to a global bridge. (HttpSession are not managed here)
        // TODO support the HttpSession
        jsonbridge = JSONRPCBridge.getGlobalBridge();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#stop()
	 */
	@Invalidate
	@Override
	protected void stop() {
		super.stop();
		
        try { //Unregister the jsonrpc server.
            if (httpservice != null) {
                httpservice.unregister(servletname);
            }
        } catch (RuntimeException re) {
            logger.log(LogService.LOG_ERROR, re.getMessage(), re);
        }
	}

	/*----------------------------*
	 *  EndpointCreator methods   *
	 *----------------------------*/
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#createEndpoint(org.osgi.framework.ServiceReference, java.util.Map)
	 */
	protected EndpointDescription createEndpoint(ServiceReference sref,
			Map<String, Object> extraProperties) {

		//Set the url property
		//extraProperties.put(PROP_JABSORB_URL, ""); //FIXME
		
		//create the endpoint description
		EndpointDescription desc = new EndpointDescription(sref, extraProperties);
		
        String id = desc.getId();
        
        Object service = context.getService(sref);
        
        //Check if the name is valid
        if (endpointIds.contains(id)){
            throw new IllegalArgumentException("An endpoint of id: "+id+" has already been created. Please chose a different id.");
        }
        
        if (desc.getInterfaces().size() > 1) {
            // Export all !
            jsonbridge.registerObject(id, service);
        }

        //only the specified interface
        else {
            String itf = desc.getInterfaces().get(0);

            try {
                jsonbridge.registerObject(id, service, sref.getBundle().loadClass(itf));
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("Cannot load the service interface " + itf + " from the service classloader.", e);
            }
        }
        
        endpointIds.add(id); //add the id to the list
		return desc;
	}

	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#destroyEndpoint(org.osgi.service.remoteserviceadmin.EndpointDescription)
	 */
	protected void destroyEndpoint(EndpointDescription endesc) {
        //Destroy the endpoint
		if (endpointIds.remove(endesc.getId())){
			jsonbridge.unregisterObject(endesc.getId());
		}
        
	}
	
	
	/*-----------------*
	 * Service Getter  *
	 *-----------------*/
	
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#getLogService()
	 */
	protected LogService getLogService() {
		return logger;
	}

	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#getEventAdmin()
	 */
	protected EventAdmin getEventAdmin() {
		return eventadmin;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractExporterComponent#getExportRegistry()
	 */
	@Override
	protected ExportRegistryProvisioning getExportRegistry() {
		return registry;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.introspect.EndpointCreatorIntrospection#getConfigPrefix()
	 */
	public List<String> getConfigPrefix() {
		return asList(configs);
	}
}

<commit_msg>supress unused warning on static property name.<commit_after>package org.ow2.chameleon.rose.jsonrpc;

import static java.util.Arrays.asList;
import static org.osgi.service.log.LogService.LOG_ERROR;

import java.util.Dictionary;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.Provides;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.ServiceProperty;
import org.apache.felix.ipojo.annotations.Validate;
import org.jabsorb.JSONRPCBridge;
import org.jabsorb.JSONRPCServlet;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;
import org.osgi.service.log.LogService;
import org.osgi.service.remoteserviceadmin.EndpointDescription;
import org.ow2.chameleon.rose.AbstractExporterComponent;
import org.ow2.chameleon.rose.ExporterService;
import org.ow2.chameleon.rose.registry.ExportRegistryProvisioning;

@Component(name="RoSe.endpoint_creator.jsonrpc")
@Provides(specifications=ExporterService.class)
@Instantiate(name="RoSe.endpoint_creator.jsonrpc-default")
public class EndpointCreator extends AbstractExporterComponent implements ExporterService {
	
	/**
	 * Property containing the URL of the JSONRPC orb.
	 */
	@SuppressWarnings("unused")
	private final static String PROP_JABSORB_URL = "org.jabsorb.url";
	
	/**
	 * Configuration supported by this component
	 */
	@ServiceProperty(name=ENDPOINT_CONFIG_PREFIX,mandatory=true,value="{json-rpc,jsonrpc,org.jabsorb}")
	private String[] configs;
	
    /**
     * Name of the Property needed by JSONRPCServlet, the gzip threshold.
     */
    private static final String PROP_GZIP_THRESHOLD = "gzip.threshold";

    /**
     * Values of the Property needed by JSONRPCServlet, the gzip threshold.
     */
    @ServiceProperty(name=PROP_GZIP_THRESHOLD,mandatory=true,value="200")
    private String gzip_threshold;
    
    /**
     * The Servlet name of the JSON-RPC bridge.
     */
    @ServiceProperty(name="jsonrpc.servlet.name",mandatory=true,value="/JSONRPC")
    private String servletname;
    
    /**
     * This class implements a bridge that unmarshalls JSON objects in JSON-RPC
     * request format, invokes a method on the exported object, and then
     * marshalls the resulting Java objects to JSON objects in JSON-RPC result
     * format.
     */
    private JSONRPCBridge jsonbridge;
    
    /**
     * Set which contains the names of the endpoint created by this factory.
     */
    private Set<String> endpointIds = new HashSet<String>();
    
	@Requires(optional=true)
	private LogService logger;
	
	@Requires(optional=true)
	private EventAdmin eventadmin;
	
	@Requires(optional=false)
	private HttpService httpservice;
	
	@Requires(optional=false)
	private ExportRegistryProvisioning registry;
	
	private final BundleContext context;
	
	public EndpointCreator(BundleContext pContext) {
		context = pContext;
	}
	
	/*--------------------------*
	 *  Component LifeCycle     *
	 *--------------------------*/
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#start()
	 */
	@Override
	@Validate
	protected void start() {
		super.start();

        Dictionary<String, String> properties = new Hashtable<String, String>();
        properties.put(PROP_GZIP_THRESHOLD, gzip_threshold);

        try {
            // Registered the JSONRPCServlet
            httpservice.registerServlet(servletname, new JSONRPCServlet(), properties, null);
        } catch (NamespaceException e) {
            logger.log(LOG_ERROR, e.getMessage(), e);
        } catch (Exception e) {
            logger.log(LOG_ERROR, e.getMessage(), e);
        }

        // Set the bridge to a global bridge. (HttpSession are not managed here)
        // TODO support the HttpSession
        jsonbridge = JSONRPCBridge.getGlobalBridge();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#stop()
	 */
	@Invalidate
	@Override
	protected void stop() {
		super.stop();
		
        try { //Unregister the jsonrpc server.
            if (httpservice != null) {
                httpservice.unregister(servletname);
            }
        } catch (RuntimeException re) {
            logger.log(LogService.LOG_ERROR, re.getMessage(), re);
        }
	}

	/*----------------------------*
	 *  EndpointCreator methods   *
	 *----------------------------*/
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#createEndpoint(org.osgi.framework.ServiceReference, java.util.Map)
	 */
	protected EndpointDescription createEndpoint(ServiceReference sref,
			Map<String, Object> extraProperties) {

		//Set the url property
		//extraProperties.put(PROP_JABSORB_URL, ""); //FIXME
		
		//create the endpoint description
		EndpointDescription desc = new EndpointDescription(sref, extraProperties);
		
        String id = desc.getId();
        
        Object service = context.getService(sref);
        
        //Check if the name is valid
        if (endpointIds.contains(id)){
            throw new IllegalArgumentException("An endpoint of id: "+id+" has already been created. Please chose a different id.");
        }
        
        if (desc.getInterfaces().size() > 1) {
            // Export all !
            jsonbridge.registerObject(id, service);
        }

        //only the specified interface
        else {
            String itf = desc.getInterfaces().get(0);

            try {
                jsonbridge.registerObject(id, service, sref.getBundle().loadClass(itf));
            } catch (ClassNotFoundException e) {
                throw new IllegalArgumentException("Cannot load the service interface " + itf + " from the service classloader.", e);
            }
        }
        
        endpointIds.add(id); //add the id to the list
		return desc;
	}

	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#destroyEndpoint(org.osgi.service.remoteserviceadmin.EndpointDescription)
	 */
	protected void destroyEndpoint(EndpointDescription endesc) {
        //Destroy the endpoint
		if (endpointIds.remove(endesc.getId())){
			jsonbridge.unregisterObject(endesc.getId());
		}
        
	}
	
	
	/*-----------------*
	 * Service Getter  *
	 *-----------------*/
	
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#getLogService()
	 */
	protected LogService getLogService() {
		return logger;
	}

	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractEndpointCreator#getEventAdmin()
	 */
	protected EventAdmin getEventAdmin() {
		return eventadmin;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.AbstractExporterComponent#getExportRegistry()
	 */
	@Override
	protected ExportRegistryProvisioning getExportRegistry() {
		return registry;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ow2.chameleon.rose.introspect.EndpointCreatorIntrospection#getConfigPrefix()
	 */
	public List<String> getConfigPrefix() {
		return asList(configs);
	}
}


{'fraction_non_alphanumeric': 0.1254551700446582, 'fraction_numerical': 0.0023359670216420475, 'mean_word_length': 3.045580878265703, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '685277', 'n_tokens_mistral': 4502, 'n_tokens_neox': 4257, 'n_words': 893}