use crate::channel::Channel;
use crate::dsl::component_builders::ServiceProcessor;
use crate::service_activator_processor::ServiceActivatorProcessor;
use crate::Filter;
use crate::HttpInboundAdapter;
use crate::HttpOutboundAdapter;
use std::sync::Arc;
#[derive(Debug)]
pub struct AlloraRuntime {
channels: Vec<Arc<dyn Channel>>,
filters: Vec<Filter>,
services: Vec<ServiceProcessor>,
service_activator_processors: Vec<ServiceActivatorProcessor>,
http_inbound_adapters: Vec<HttpInboundAdapter>,
http_outbound_adapters: Vec<HttpOutboundAdapter>,
}
impl AlloraRuntime {
pub fn new(channels: Vec<Box<dyn Channel>>) -> Self {
let channels_arc = channels.into_iter().map(|c| Arc::from(c)).collect();
Self {
channels: channels_arc,
filters: Vec::new(),
services: Vec::new(),
service_activator_processors: Vec::new(),
http_inbound_adapters: Vec::new(),
http_outbound_adapters: Vec::new(),
}
}
pub fn with_filters(mut self, filters: Vec<Filter>) -> Self {
self.filters = filters;
self
}
pub fn with_services(mut self, services: Vec<ServiceProcessor>) -> Self {
self.services = services;
self
}
pub fn with_service_processors(mut self, proc: Vec<ServiceActivatorProcessor>) -> Self {
self.service_activator_processors = proc;
self
}
pub fn with_http_inbound_adapters(mut self, adapters: Vec<HttpInboundAdapter>) -> Self {
self.http_inbound_adapters = adapters;
self
}
pub fn with_http_outbound_adapters(mut self, adapters: Vec<HttpOutboundAdapter>) -> Self {
self.http_outbound_adapters = adapters;
self
}
pub fn channels(&self) -> impl Iterator<Item = &dyn Channel> {
self.channels.iter().map(|c| c.as_ref())
}
pub fn channels_slice(&self) -> &[Arc<dyn Channel>] {
&self.channels
}
pub fn filters(&self) -> &[Filter] {
&self.filters
}
pub fn services(&self) -> &[ServiceProcessor] {
&self.services
}
pub fn into_channels(self) -> Vec<Arc<dyn Channel>> {
self.channels
}
pub fn into_filters(self) -> Vec<Filter> {
self.filters
}
pub fn into_services(self) -> Vec<ServiceProcessor> {
self.services
}
pub fn channel_by_id(&self, id: &str) -> Option<&dyn Channel> {
self.channels
.iter()
.find(|c| c.id() == id)
.map(|c| c.as_ref())
}
pub fn channel_ref_by_id(&self, id: &str) -> Option<Arc<dyn Channel>> {
self.channels
.iter()
.find(|c| c.id() == id)
.map(|c| Arc::clone(c))
}
pub fn channel_typed<T: Channel + 'static>(&self, id: &str) -> Option<&T> {
self.channels
.iter()
.find(|c| c.id() == id)
.and_then(|c| c.as_any().downcast_ref::<T>())
}
pub fn channel<T: Channel + 'static>(&self, id: &str) -> &T {
for c in &self.channels {
if c.id() == id {
if let Some(t) = c.as_any().downcast_ref::<T>() {
return t;
} else {
panic!(
"channel '{}' exists with kind '{}' but does not match expected type '{}'",
id,
c.kind(),
std::any::type_name::<T>()
);
}
}
}
panic!(
"channel '{}' not found (expected type '{}')",
id,
std::any::type_name::<T>()
);
}
pub fn channel_is<T: Channel + 'static>(&self, id: &str) -> bool {
self.channel_typed::<T>(id).is_some()
}
pub fn channel_count(&self) -> usize {
self.channels.len()
}
pub fn filter_count(&self) -> usize {
self.filters.len()
}
pub fn service_count(&self) -> usize {
self.services.len()
}
pub fn service_processor_count(&self) -> usize {
self.service_activator_processors.len()
}
pub fn service_activator_processors(&self) -> &[ServiceActivatorProcessor] {
&self.service_activator_processors
}
pub fn service_activator_processor_by_id(
&self,
id: &str,
) -> Option<&ServiceActivatorProcessor> {
self.service_activator_processors
.iter()
.find(|p| p.id() == id)
}
pub fn service_activator_processor_mut_by_id(
&mut self,
id: &str,
) -> Option<&mut ServiceActivatorProcessor> {
self.service_activator_processors
.iter_mut()
.find(|p| p.id() == id)
}
pub fn http_inbound_adapters(&self) -> &[HttpInboundAdapter] {
&self.http_inbound_adapters
}
pub fn http_inbound_adapter_count(&self) -> usize {
self.http_inbound_adapters.len()
}
pub fn http_outbound_adapters(&self) -> &[HttpOutboundAdapter] {
&self.http_outbound_adapters
}
pub fn http_outbound_adapter_count(&self) -> usize {
self.http_outbound_adapters.len()
}
}