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

com.almende.eve.protocol.jsonrpc.JSONRpcProtocolBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Copyright: Almende B.V. (2014), Rotterdam, The Netherlands
 * License: The Apache Software License, Version 2.0
 */
package com.almende.eve.protocol.jsonrpc;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.almende.eve.capabilities.AbstractCapabilityBuilder;
import com.almende.eve.capabilities.handler.Handler;
import com.almende.eve.protocol.auth.Authorizor;
import com.almende.util.TypeUtil;
import com.almende.util.uuid.UUID;

/**
 * The Class JSONRpcProtocolBuilder.
 */
public class JSONRpcProtocolBuilder extends
		AbstractCapabilityBuilder {
	private static final Logger							LOG			= Logger.getLogger(JSONRpcProtocolBuilder.class
																			.getName());
	private static final TypeUtil>		TYPEUTIL	= new TypeUtil>() {};
	private static final Map	INSTANCES	= new HashMap();

	/*
	 * (non-Javadoc)
	 * @see
	 * com.almende.eve.capabilities.CapabilityService#get(com.fasterxml.jackson.
	 * databind
	 * .JsonNode, com.almende.eve.capabilities.handler.Handler, java.lang.Class)
	 */
	@Override
	public JSONRpcProtocol build() {
		JSONRpcProtocolConfig config = new JSONRpcProtocolConfig(getParams());
		String id = config.getId();
		if (id == null) {
			id = new UUID().toString();
			LOG.warning("Parameter 'id' is required for JSONRpcProtocol. (giving temporary name: "
					+ id + ")");
		}

		JSONRpcProtocol result;
		if (INSTANCES.containsKey(id)) {
			result = INSTANCES.get(id);
			final Handler oldHandle = result.getHandle();
			oldHandle.update(TYPEUTIL.inject(getHandle()));
		} else {
			result = new JSONRpcProtocol(config, TYPEUTIL.inject(getHandle()));
		}
		INSTANCES.put(id, result);

		// Add authorizor:
		try {
			@SuppressWarnings("unchecked")
			final Class clazz = (Class) Class
					.forName(config.getAuthorizor());
			result.setAuth(clazz.newInstance());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException e) {
			LOG.log(Level.WARNING, "Couldn't instantiate authorizor class:"
					+ config.getAuthorizor(), e);
		}
		return result;
	}

	/**
	 * Delete.
	 *
	 * @param id
	 *            the id
	 */
	public static void delete(final String id){
		INSTANCES.remove(id);
	}
}