Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DSL for groovy and kotlin #169

Merged
merged 2 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions camel-k-loader-groovy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@
<artifactId>camel-properties</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bean</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<scope>test</scope>
</dependency>

<!-- ******************************* -->
<!-- test deps :: misc -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
*/
package org.apache.camel.k.loader.groovy.dsl

import groovy.util.logging.Slf4j
import org.apache.camel.CamelContext
import org.apache.camel.ExtendedCamelContext
import org.apache.camel.support.PropertyBindingSupport
import org.slf4j.Logger
import org.slf4j.LoggerFactory

class ComponentConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(ComponentConfiguration.class);
private final org.apache.camel.Component component
@Slf4j
class BeanConfiguration {
private final CamelContext context
private final Object target

ComponentConfiguration(org.apache.camel.Component component) {
this.component = component
BeanConfiguration(CamelContext camelContext, Object target) {
this.context = camelContext
this.target = target
}

def methodMissing(String name, arguments) {
Expand All @@ -38,47 +40,48 @@ class ComponentConfiguration {
} else if (args.length == 1) {
value = args[0]
} else {
throw new IllegalArgumentException("Unable to set property '${name}' on component '${component.class.name}'")
throw new IllegalArgumentException("Unable to set property '${name}' on target '${target.class.name}'")
}

if (value instanceof Closure<?>) {
def m = this.component.metaClass.getMetaMethod(name, Closure.class)
def m = this.target.metaClass.getMetaMethod(name, Closure.class)
if (m) {
m.invoke(component, args)
m.invoke(target, args)

// done
return
}
}

boolean bound = PropertyBindingSupport.build()
.withCamelContext(component.camelContext)
.withTarget(component)
.withCamelContext(context)
.withTarget(target)
.withProperty(name, value)
.bind()

if (!bound) {
throw new MissingMethodException(name, this.component.class, args as Object[])
throw new MissingMethodException(name, this.target.class, args as Object[])
}
}

def propertyMissing(String name, value) {
boolean bound = PropertyBindingSupport.build()
.withCamelContext(component.camelContext)
.withTarget(component)
.withCamelContext(context)
.withTarget(target)
.withProperty(name, value)
.bind()

if (!bound) {
throw new MissingPropertyException(name, this.component.class)
throw new MissingPropertyException(name, this.target.class)
}
}

def propertyMissing(String name) {
def props = new HashMap<String, Object>()
def context = component.getCamelContext().adapt(ExtendedCamelContext.class)

context.getBeanIntrospection().getProperties(component, props, null, false)
context.adapt(ExtendedCamelContext.class)
.getBeanIntrospection()
.getProperties(target, props, null, false)

return props[name]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,28 @@ package org.apache.camel.k.loader.groovy.dsl

import org.apache.camel.CamelContext

class ContextConfiguration {
class CamelConfiguration {
private final CamelContext context

ContextConfiguration(CamelContext context) {
CamelConfiguration(CamelContext context) {
this.context = context
}

def registry(@DelegatesTo(RegistryConfiguration) Closure<?> callable) {
def components(@DelegatesTo(ComponentsConfiguration) Closure<?> callable) {
callable.resolveStrategy = Closure.DELEGATE_FIRST
callable.delegate = new RegistryConfiguration(this.context.registry)
callable.delegate = new ComponentsConfiguration(context)
callable.call()
}

def components(@DelegatesTo(ComponentsConfiguration) Closure<?> callable) {
def dataFormats(@DelegatesTo(DataFormatsConfiguration) Closure<?> callable) {
callable.resolveStrategy = Closure.DELEGATE_FIRST
callable.delegate = new ComponentsConfiguration(context)
callable.delegate = new DataFormatsConfiguration(context)
callable.call()
}

def languages(@DelegatesTo(LanguagesConfiguration) Closure<?> callable) {
callable.resolveStrategy = Closure.DELEGATE_FIRST
callable.delegate = new LanguagesConfiguration(context)
callable.call()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,54 @@
*/
package org.apache.camel.k.loader.groovy.dsl

import groovy.util.logging.Slf4j
import org.apache.camel.CamelContext
import org.apache.camel.Component

@Slf4j
class ComponentsConfiguration {
private final CamelContext context

ComponentsConfiguration(CamelContext context) {
this.context = context
}

def component(String name, @DelegatesTo(ComponentConfiguration) Closure<?> callable) {
def component = context.getComponent(name, true, false)
def component(String name, Closure<?> callable) {
def target = context.getComponent(name, true, false)
if (target == null) {
throw new IllegalArgumentException("Unable to find a component with name: ${name}")
}

// Just make sure the closure context is belong to component
callable.resolveStrategy = Closure.DELEGATE_ONLY
callable.delegate = new ComponentConfiguration(component)
callable.delegate = new BeanConfiguration(context, target)
callable.call()
}

def component(String name, Class<? extends Component> type, @DelegatesTo(ComponentConfiguration) Closure <?> callable) {
def component = context.getComponent(name, true, false)
def component(String name, Class<? extends Component> type, Closure <?> callable) {
def target = context.getComponent(name, true, false)
def bind = false

if (target != null && !type.isInstance(target)) {
throw new IllegalArgumentException("Type mismatch, expected: ${type} , got: ${target.class}")
}

// if the component is not found, let's create a new one. This is
// equivalent to create a new named component, useful to create
// multiple instances of the same component but with different setup
if (component == null) {
component = context.injector.newInstance(type)

// let's the camel context be aware of the new component
context.addComponent(name, component)
if (target == null) {
target = context.injector.newInstance(type)
bind = true
}

if (type.isAssignableFrom(component.class)) {
// Just make sure the closure context is belong to component
callable.resolveStrategy = Closure.DELEGATE_ONLY
callable.delegate = new ComponentConfiguration(component)
callable.call()
return
}
// Just make sure the closure context is belong to component
callable.resolveStrategy = Closure.DELEGATE_ONLY
callable.delegate = new BeanConfiguration(context, target)
callable.call()

throw new IllegalArgumentException("Type mismatch, expected: " + type + ", got: " + component.class)
if (bind) {
context.registry.bind(name, type, target)
}
}

def methodMissing(String name, arguments) {
Expand All @@ -74,7 +82,7 @@ class ComponentsConfiguration {
def clos = args[1]

if (type instanceof Class && Component.class.isAssignableFrom(type) && clos instanceof Closure) {
return component(name,type, clos)
return component(name, type, clos)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.k.loader.groovy.dsl

import org.apache.camel.CamelContext
import org.apache.camel.spi.DataFormat

class DataFormatsConfiguration {
private final CamelContext context

DataFormatsConfiguration(CamelContext context) {
this.context = context
}

def dataFormat(String name, Closure<?> callable) {
def target = context.resolveDataFormat(name)
if (target == null) {
throw new IllegalArgumentException("Unable to find a dataformat with name: ${name}")
}

// Just make sure the closure context is belong to component
callable.resolveStrategy = Closure.DELEGATE_ONLY
callable.delegate = new BeanConfiguration(context, target)
callable.call()

// let's the camel context be aware of the new dataformat
context.registry.bind(name, DataFormat.class, target)
}

def dataFormat(String name, Class<? extends DataFormat> type, Closure <?> callable) {
def target = context.registry.lookupByNameAndType(name, type)
def bind = false

// if the dataformat is not found, let's create a new one. This is
// equivalent to create a new named dataformat, useful to create
// multiple instances of the same dataformat but with different setup
if (target == null) {
target = context.injector.newInstance(type)

bind = true
}

// Just make sure the closure context is belong to dataformat
callable.resolveStrategy = Closure.DELEGATE_ONLY
callable.delegate = new BeanConfiguration(context, target)
callable.call()

if (bind) {
// let's the camel context be aware of the new dataformat
context.registry.bind(name, type, target)
}
}

def methodMissing(String name, arguments) {
final Object[] args = arguments as Object[]

if (args != null) {
if (args.length == 1) {
def clos = args[0]

if (clos instanceof Closure) {
return dataFormat(name, clos)
}
}
if (args.length == 2) {
def type = args[0]
def clos = args[1]

if (type instanceof Class && DataFormat.class.isAssignableFrom(type) && clos instanceof Closure) {
return dataFormat(name,type, clos)
}
}
}

throw new MissingMethodException(name, this, args)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class IntegrationConfiguration extends BuilderSupport implements Support, Endpoi
callable.call()
}

def context(@DelegatesTo(ContextConfiguration) Closure<?> callable) {
def camel(@DelegatesTo(CamelConfiguration) Closure<?> callable) {
callable.resolveStrategy = Closure.DELEGATE_FIRST
callable.delegate = new ContextConfiguration(context)
callable.delegate = new CamelConfiguration(context)
callable.call()
}

Expand Down
Loading