void AmqpOutboundEndpointInstance::wire(
		const SmartPtrIAppContext& appContext,
		const SmartPtrIChannelResolver& channelResolver) {
	CAF_CM_FUNCNAME("wire");
	CAF_CM_PRECOND_ISINITIALIZED(_isInitialized);

	const SmartPtrIMessageChannel errorMessageChannel =
		channelResolver->resolveChannelName("errorChannel");

	std::string elementVal = _configSection->findOptionalAttribute("amqp-template");
	if (!elementVal.length()) {
		elementVal = "amqpTemplate";
		CAF_CM_LOG_DEBUG_VA0("Using default amqp-template reference value 'amqpTemplate'");
	}
	SmartPtrIIntegrationObject amqpTemplateObj = _context->getIntegrationObject(elementVal);
	SmartPtrAmqpTemplate amqpTemplate;
	amqpTemplate.QueryInterface(amqpTemplateObj, false);
	if (!amqpTemplate) {
		CAF_CM_EXCEPTIONEX_VA1(
				NoSuchInterfaceException,
				0,
				"Bean '%s' is not of type AmqpTemplate",
				elementVal.c_str());
	}

	SmartPtrIAppConfig appConfig = getAppConfig();
	SmartPtrAmqpOutboundEndpoint outboundEndpoint;
	outboundEndpoint.CreateInstance();
	elementVal = _configSection->findOptionalAttribute("exchange-name");
	outboundEndpoint->setExchangeName(appConfig->resolveValue(elementVal));
	elementVal = _configSection->findOptionalAttribute("exchange-name-expression");
	outboundEndpoint->setExchangeNameExpression(elementVal);
	elementVal = _configSection->findOptionalAttribute("routing-key");
	outboundEndpoint->setRoutingKey(appConfig->resolveValue(elementVal));
	elementVal = _configSection->findOptionalAttribute("routing-key-expression");
	outboundEndpoint->setRoutingKeyExpression(elementVal);
	elementVal = _configSection->findOptionalAttribute("mapped-request-headers");
	outboundEndpoint->setMappedRequestHeadersExpression(appConfig->resolveValue(elementVal));
	outboundEndpoint->init(
			amqpTemplate,
			appConfig,
			appContext);

	elementVal = _configSection->findRequiredAttribute("channel");
	SmartPtrIMessageChannel inputChannel = channelResolver->resolveChannelName(elementVal);
	SmartPtrIIntegrationObject inputChannelObj;
	inputChannelObj.QueryInterface(inputChannel);


	_messagingTemplate.CreateInstance();
	_messagingTemplate->initialize(
			channelResolver,
			inputChannelObj,
			errorMessageChannel,
			SmartPtrIMessageChannel(),
			outboundEndpoint);
}
void ExchangeInstance::initialize(
	const IBean::Cargs& ctorArgs,
	const IBean::Cprops& properties,
	const SmartPtrIDocument& configSection) {
	CAF_CM_FUNCNAME("initialize");

	_id = CStringUtils::createRandomUuid();

	SmartPtrIAppConfig appConfig = getAppConfig();

	const std::string exchange = appConfig->resolveValue(configSection->findRequiredAttribute("name"));
	const std::string durableStr = configSection->findOptionalAttribute("durable");
	if (durableStr.length()) {
		if ((durableStr != "true") && (durableStr != "false")) {
			CAF_CM_EXCEPTIONEX_VA3(
					InvalidArgumentException,
					0,
					"Invalid 'durable' value (%s) for %s '%s'. "
					"Value must be either 'true' or 'false'.",
					durableStr.c_str(),
					configSection->getName().c_str(),
					exchange.c_str());
		}
	}
	const bool durable = !durableStr.length() || (durableStr == "true");

	if (configSection->getName() == "rabbit-direct-exchange") {
		_exchange = createDirectExchange(exchange, durable);
	} else if (configSection->getName() == "rabbit-topic-exchange") {
		_exchange = createTopicExchange(exchange, durable);
	} else if (configSection->getName() == "rabbit-headers-exchange") {
		_exchange = createHeadersExchange(exchange, durable);
	} else if (configSection->getName() == "rabbit-fanout-exchange") {
		_exchange = createFanoutExchange(exchange, durable);
	} else {
		CAF_CM_EXCEPTIONEX_VA1(
				InvalidArgumentException,
				0,
				"Invalid exchange type (%s)",
				configSection->getName().c_str());
	}

	SmartPtrIDocument bindingsSection = configSection->findOptionalChild("rabbit-bindings");
	if (bindingsSection) {
		IDocument::SmartPtrCChildCollection bindingSections =
				bindingsSection->getAllChildren();
		for (TSmartConstMapIterator<IDocument::CChildCollection> bindingSection(*bindingSections);
				bindingSection;
				bindingSection++) {
			if (bindingSection->getName() != "rabbit-binding") {
				CAF_CM_EXCEPTIONEX_VA2(
						InvalidArgumentException,
						0,
						"Invalid tag (%s) found in bindings section of "
						"exchange declaration (name=%s)",
						bindingSection->getName().c_str(),
						exchange.c_str());
			}
			const std::string queue = appConfig->resolveValue(bindingSection->findRequiredAttribute("queue"));
			const std::string key = appConfig->resolveValue(bindingSection->findRequiredAttribute("key"));
			CAF_CM_LOG_DEBUG_VA3(
					"Adding binding declaration [queue id=%s][exchange name=%s][key=%s]",
					queue.c_str(),
					exchange.c_str(),
					key.c_str());
			SmartPtrBindingInstance binding;
			binding.CreateInstance();
			binding->setBindingInternal(createBinding(queue, exchange, key));
			_bindings.push_back(binding);
		}
	}
}
void CExpressionHandler::init(
		const SmartPtrIAppConfig& appConfig,
		const SmartPtrIAppContext& appContext,
		const std::string& expression) {
	CAF_CM_FUNCNAME("init");
	CAF_CM_PRECOND_ISNOTINITIALIZED(_isInitialized);
	CAF_CM_VALIDATE_INTERFACE(appConfig);
	CAF_CM_VALIDATE_INTERFACE(appContext);
	CAF_CM_VALIDATE_STRING(expression);

	CCafRegex regex;
	regex.initialize("\\@(\\w+)\\.(\\w+)\\((.*)\\)");
	if (!regex.isMatched(expression)) {
		CAF_CM_EXCEPTIONEX_VA1(
				InvalidArgumentException,
				0,
				"Invalid expression syntax '%s'",
				expression.c_str());
	}
	_beanName = regex.match(expression, 1);
	_methodName = regex.match(expression, 2);
	if (!_beanName.length() || !_methodName.length()) {
		CAF_CM_EXCEPTIONEX_VA1(
				InvalidArgumentException,
				0,
				"Invalid expression syntax '%s'",
				expression.c_str());
	}

	SmartPtrIBean bean = appContext->getBean(_beanName);
	_invoker.QueryInterface(bean, false);
	if (!_invoker) {
		CAF_CM_EXCEPTIONEX_VA1(
				NoSuchInterfaceException,
				0,
				"bean '%s' does not support the IExpressionInvoker interface",
				_beanName.c_str());
	}

	const std::string paramSubstr = regex.match(expression, 3);
	Cdeqstr paramExprs = CStringUtils::split(paramSubstr, ',');
	CCafRegex regexLiteral;
	regexLiteral.initialize("'(.*)'");
	for (TConstIterator<Cdeqstr> paramExpr(paramExprs); paramExpr; paramExpr++) {
		std::string trimmedExpr = CStringUtils::trim(*paramExpr);
		if (regexLiteral.isMatched(trimmedExpr)) {
			std::string val = regexLiteral.match(trimmedExpr, 1);
			if (val.length()) {
				_methodParams.push_back(appConfig->resolveValue(val));
			} else {
				_methodParams.push_back("");
			}
		} else {
			CAF_CM_EXCEPTIONEX_VA1(
					InvalidArgumentException,
					0,
					"Invalid expression syntax '%s'",
					expression.c_str());
		}
	}
	_isInitialized = true;
}
Beispiel #4
0
void CApplicationContext::initializeBeans(
		CBeanCollection& beanCollection,
		CBeanGraph::ClistVertexEdges& beanTopologySort) const {
	CAF_CM_FUNCNAME("initializeBeans");

	for (TSmartIterator<CBeanGraph::ClistVertexEdges> beanNode(beanTopologySort);
			beanNode;
			beanNode++) {
		CAF_CM_LOG_DEBUG_VA1("Initializing bean %s", beanNode->_id.c_str());

		// The bean should not have been initialized
		if (beanNode->_isInitialized) {
			CAF_CM_EXCEPTIONEX_VA1(
					IllegalStateException,
					0,
					"Internal error: Bean [%s] has already been initialized.",
					beanNode->_id.c_str());
		}

		// Iterate the contructor-args and build a collection to
		// pass to the bean initializer
		IBean::Cargs beanInitArgs;
		for (TConstMapIterator<CBeanCtorArgCollection> ctorArg(beanNode->_ctorArgs);
				ctorArg;
				ctorArg++) {
			switch (ctorArg->_type) {
				case CBeanCtorArg::REFERENCE: {
						CBeanCollection::const_iterator bean = beanCollection.find(ctorArg->_value);
						if (!bean->second->_isInitialized) {
							CAF_CM_EXCEPTIONEX_VA2(
									NullPointerException,
									0,
									"Internal error: Referenced bean not initialized. "
									"[bean id=%s][constructor-arg ref=%s]",
									beanNode->_id.c_str(),
									ctorArg->_value.c_str());
						}
						beanInitArgs.push_back(IBean::CArg(bean->second->_bean));
						CAF_CM_LOG_DEBUG_VA1(
								"constructor-arg ref=%s",
								ctorArg->_value.c_str());
					}
					break;

				case CBeanCtorArg::VALUE:
					beanInitArgs.push_back(IBean::CArg(ctorArg->_value));
					CAF_CM_LOG_DEBUG_VA1(
							"constructor-arg value=%s",
							ctorArg->_value.c_str());
					break;

				default:
					CAF_CM_EXCEPTIONEX_VA2(
							InvalidArgumentException,
							0,
							"Internal error: Bean constructor-arg is not a ref or value "
							"[bean id=%s][constructor-arg index=%d]",
							beanNode->_id.c_str(),
							ctorArg.getKey());
			}
		}

		// Iterate the bean properties and resolve value references
		SmartPtrIAppConfig appConfig = getAppConfig();
		Cmapstrstr properties = beanNode->_properties;

		for (TMapIterator<Cmapstrstr> property(properties);
				property;
				property++) {
			*property = appConfig->resolveValue(*property);
		}

		// Initialize the bean
		beanNode->_bean->initializeBean(beanInitArgs, properties);
		beanNode->_isInitialized = true;
	}
}