本文整理汇总了Java中org.apache.camel.TypeConverter类的典型用法代码示例。如果您正苦于以下问题:Java TypeConverter类的具体用法?Java TypeConverter怎么用?Java TypeConverter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
TypeConverter类属于org.apache.camel包,在下文中一共展示了TypeConverter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertToMethodSet
import org.apache.camel.TypeConverter; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private Set<Method> convertToMethodSet(Object value, TypeConverter typeConverter) {
if (value instanceof Set) {
return (Set<Method>) value;
}
Set<Method> set = new LinkedHashSet<>();
Iterator it = ObjectHelper.createIterator(value);
while (it.hasNext()) {
Object next = it.next();
String text = typeConverter.tryConvertTo(String.class, next);
if (text != null) {
Method method = Method.valueOf(text.trim()); // creates new instance only if no matching instance exists
set.add(method);
}
}
return set;
}
示例2: doRun
import org.apache.camel.TypeConverter; //导入依赖的package包/类
private void doRun(final Exchange exchange, final AsyncCallback callback, IgniteCompute compute) throws Exception {
Object job = exchange.getIn().getBody();
if (Collection.class.isAssignableFrom(job.getClass())) {
Collection<?> col = (Collection<?>) job;
TypeConverter tc = exchange.getContext().getTypeConverter();
Collection<IgniteRunnable> runnables = new ArrayList<>(col.size());
for (Object o : col) {
runnables.add(tc.mandatoryConvertTo(IgniteRunnable.class, o));
}
compute.run(runnables);
} else if (IgniteRunnable.class.isAssignableFrom(job.getClass())) {
compute.run((IgniteRunnable) job);
} else {
throw new RuntimeCamelException(String.format(
"Ignite Compute endpoint with RUN executionType is only " + "supported for IgniteRunnable payloads, or collections of them. The payload type was: %s.", job.getClass().getName()));
}
}
示例3: convert
import org.apache.camel.TypeConverter; //导入依赖的package包/类
private static Object convert(TypeConverter typeConverter, Class<?> type, Object value)
throws URISyntaxException, NoTypeConversionAvailableException {
if (typeConverter != null) {
return typeConverter.mandatoryConvertTo(type, value);
}
if (type == URI.class) {
return new URI(value.toString());
}
PropertyEditor editor = PropertyEditorManager.findEditor(type);
if (editor != null) {
// property editor is not thread safe, so we need to lock
Object answer;
synchronized (LOCK) {
editor.setAsText(value.toString());
answer = editor.getValue();
}
return answer;
}
return null;
}
示例4: doCall
import org.apache.camel.TypeConverter; //导入依赖的package包/类
@SuppressWarnings({ "unchecked", "rawtypes" })
private void doCall(final Exchange exchange, final AsyncCallback callback, IgniteCompute compute) throws Exception {
Object job = exchange.getIn().getBody();
IgniteReducer<Object, Object> reducer = exchange.getIn().getHeader(IgniteConstants.IGNITE_COMPUTE_REDUCER, IgniteReducer.class);
if (Collection.class.isAssignableFrom(job.getClass())) {
Collection<?> col = (Collection<?>) job;
TypeConverter tc = exchange.getContext().getTypeConverter();
Collection<IgniteCallable<?>> callables = new ArrayList<>(col.size());
for (Object o : col) {
callables.add(tc.mandatoryConvertTo(IgniteCallable.class, o));
}
if (reducer != null) {
compute.call((Collection) callables, reducer);
} else {
compute.call((Collection) callables);
}
} else if (IgniteCallable.class.isAssignableFrom(job.getClass())) {
compute.call((IgniteCallable<Object>) job);
} else {
throw new RuntimeCamelException(String.format(
"Ignite Compute endpoint with CALL executionType is only " + "supported for IgniteCallable payloads, or collections of them. The payload type was: %s.", job.getClass().getName()));
}
}
示例5: addFallbackTypeConverter
import org.apache.camel.TypeConverter; //导入依赖的package包/类
@Override
public void addFallbackTypeConverter(TypeConverter typeConverter, boolean canPromote) {
log.trace("Adding fallback type converter: {} which can promote: {}", typeConverter, canPromote);
// add in top of fallback as the toString() fallback will nearly always be able to convert
// the last one which is add to the FallbackTypeConverter will be called at the first place
fallbackConverters.add(0, new FallbackTypeConverter(typeConverter, canPromote));
if (typeConverter instanceof TypeConverterAware) {
TypeConverterAware typeConverterAware = (TypeConverterAware) typeConverter;
typeConverterAware.setTypeConverter(this);
}
if (typeConverter instanceof CamelContextAware) {
CamelContextAware camelContextAware = (CamelContextAware) typeConverter;
if (camelContext != null) {
camelContextAware.setCamelContext(camelContext);
}
}
}
示例6: getTypeConverter
import org.apache.camel.TypeConverter; //导入依赖的package包/类
public TypeConverter getTypeConverter() {
if (typeConverter == null) {
synchronized (this) {
// we can synchronize on this as there is only one instance
// of the camel context (its the container)
typeConverter = createTypeConverter();
try {
// must add service eager and force start it
addService(typeConverter, true, true);
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
}
}
return typeConverter;
}
示例7: getMandatoryBody
import org.apache.camel.TypeConverter; //导入依赖的package包/类
public <T> T getMandatoryBody(Class<T> type) throws InvalidPayloadException {
// eager same instance type test to avoid the overhead of invoking the type converter
// if already same type
if (type.isInstance(body)) {
return type.cast(body);
}
Exchange e = getExchange();
if (e != null) {
TypeConverter converter = e.getContext().getTypeConverter();
try {
return converter.mandatoryConvertTo(type, e, getBody());
} catch (Exception cause) {
throw new InvalidPayloadException(e, type, this, cause);
}
}
throw new InvalidPayloadException(e, type, this);
}
示例8: testFallbackPromote
import org.apache.camel.TypeConverter; //导入依赖的package包/类
public void testFallbackPromote() throws Exception {
MyCoolBean cool = new MyCoolBean();
cool.setCool("Camel rocks");
TypeConverter tc = context.getTypeConverterRegistry().lookup(String.class, MyCoolBean.class);
assertNull("No regular type converters", tc);
String s = context.getTypeConverter().convertTo(String.class, cool);
assertEquals("This is cool: Camel rocks", s);
cool.setCool("It works");
s = context.getTypeConverter().convertTo(String.class, cool);
assertEquals("This is cool: It works", s);
tc = context.getTypeConverterRegistry().lookup(String.class, MyCoolBean.class);
assertNotNull("Should have been promoted", tc);
}
示例9: toInputStream
import org.apache.camel.TypeConverter; //导入依赖的package包/类
@Converter
public static InputStream toInputStream(Response response, Exchange exchange) {
Object obj = response.getEntity();
if (obj == null) {
return null;
}
if (obj instanceof InputStream) {
// short circuit the lookup
return (InputStream)obj;
}
TypeConverterRegistry registry = exchange.getContext().getTypeConverterRegistry();
TypeConverter tc = registry.lookup(InputStream.class, obj.getClass());
if (tc != null) {
return tc.convertTo(InputStream.class, exchange, obj);
}
return null;
}
示例10: extractOffset
import org.apache.camel.TypeConverter; //导入依赖的package包/类
private static long extractOffset(String now, TypeConverter typeConverter) throws NoTypeConversionAvailableException {
Matcher matcher = NOW_PATTERN.matcher(now);
if (matcher.matches()) {
String op = matcher.group(1);
String remainder = matcher.group(2);
// convert remainder to a time millis (eg we have a String -> long converter that supports
// syntax with hours, days, minutes: eg 5h30m for 5 hours and 30 minutes).
long offset = typeConverter.mandatoryConvertTo(long.class, remainder);
if ("+".equals(op)) {
return offset;
} else {
return -1 * offset;
}
}
return 0;
}
示例11: create
import org.apache.camel.TypeConverter; //导入依赖的package包/类
@Override
public Writable create(Object value, TypeConverter typeConverter, Holder<Integer> size) {
InputStream is = null;
try {
is = typeConverter.convertTo(InputStream.class, value);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOUtils.copyBytes(is, bos, HdfsConstants.DEFAULT_BUFFERSIZE, false);
BytesWritable writable = new BytesWritable();
writable.set(bos.toByteArray(), 0, bos.toByteArray().length);
size.value = bos.toByteArray().length;
return writable;
} catch (IOException ex) {
throw new RuntimeCamelException(ex);
} finally {
IOHelper.close(is);
}
}
示例12: convertTo
import org.apache.camel.TypeConverter; //导入依赖的package包/类
@FallbackConverter
@SuppressWarnings("unchecked")
public static <T extends Payload> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) throws IOException {
Class<?> sourceType = value.getClass();
if (GenericFile.class.isAssignableFrom(sourceType)) {
GenericFile<?> genericFile = (GenericFile<?>) value;
if (genericFile.getFile() != null) {
Class<?> genericFileType = genericFile.getFile().getClass();
TypeConverter converter = registry.lookup(Payload.class, genericFileType);
if (converter != null) {
return (T) converter.convertTo(Payload.class, genericFile.getFile());
}
}
}
return null;
}
示例13: testFallbackConverterWithoutObjectFactory
import org.apache.camel.TypeConverter; //导入依赖的package包/类
@Test
public void testFallbackConverterWithoutObjectFactory() throws Exception {
TypeConverter converter = context.getTypeConverter();
Foo foo = converter.convertTo(Foo.class, "<foo><zot name=\"bar1\" value=\"value\" otherValue=\"otherValue\"/></foo>");
assertNotNull("foo should not be null", foo);
assertEquals("value", foo.getBarRefs().get(0).getValue());
foo.getBarRefs().clear();
Bar bar = new Bar();
bar.setName("myName");
bar.setValue("myValue");
foo.getBarRefs().add(bar);
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
String value = converter.convertTo(String.class, exchange, foo);
assertTrue("Should get a right marshalled string", value.indexOf("<bar name=\"myName\" value=\"myValue\"/>") > 0);
}
示例14: testConverter
import org.apache.camel.TypeConverter; //导入依赖的package包/类
@Test
public void testConverter() throws Exception {
TypeConverter converter = context.getTypeConverter();
PersonType person = converter.convertTo(PersonType.class, "<Person><firstName>FOO</firstName><lastName>BAR</lastName></Person>");
assertNotNull("Person should not be null ", person);
assertEquals("Get the wrong first name ", "FOO", person.getFirstName());
assertEquals("Get the wrong second name ", "BAR", person.getLastName());
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
String value = converter.convertTo(String.class, exchange, person);
assertTrue("Should get a right marshalled string", value.indexOf("<lastName>BAR</lastName>") > 0);
byte[] buffers = "<Person><firstName>FOO</firstName><lastName>BAR\u0008</lastName></Person>".getBytes("UTF-8");
InputStream is = new ByteArrayInputStream(buffers);
try {
converter.convertTo(PersonType.class, exchange, is);
fail("Should have thrown exception");
} catch (TypeConversionException e) {
// expected
}
}
示例15: testFilteringConverter
import org.apache.camel.TypeConverter; //导入依赖的package包/类
@Test
public void testFilteringConverter() throws Exception {
byte[] buffers = "<Person><firstName>FOO</firstName><lastName>BAR\u0008</lastName></Person>".getBytes("UTF-8");
InputStream is = new ByteArrayInputStream(buffers);
Exchange exchange = new DefaultExchange(context);
exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
exchange.setProperty(Exchange.FILTER_NON_XML_CHARS, true);
TypeConverter converter = context.getTypeConverter();
PersonType person = converter.convertTo(PersonType.class, exchange, is);
assertNotNull("Person should not be null ", person);
assertEquals("Get the wrong first name ", "FOO", person.getFirstName());
assertEquals("Get the wrong second name ", "BAR ", person.getLastName());
person.setLastName("BAR\u0008\uD8FF");
String value = converter.convertTo(String.class, exchange, person);
assertTrue("Didn't filter the non-xml chars", value.indexOf("<lastName>BAR </lastName>") > 0);
exchange.setProperty(Exchange.FILTER_NON_XML_CHARS, false);
value = converter.convertTo(String.class, exchange, person);
assertTrue("Should not filter the non-xml chars", value.indexOf("<lastName>BAR\uD8FF</lastName>") > 0);
}