001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.spi; 018 019import org.apache.camel.util.StringHelper; 020 021/** 022 * Represents the data type URN which is used for message data type contract. 023 * Java class doesn't always explain the data type completely, for example XML and JSON 024 * data format is sometimes serialized as a {@code String}, {@code InputStream} or etc. 025 * The {@link DataTypeAware} message stores the DataType as a part of the message to carry 026 * those data type information even if it's marshaled, so that it could be 027 * leveraged to detect required {@link Transformer} and {@link Validator}. 028 * DataType consists of two parts, 'model' and 'name'. Its string representation is 029 * 'model:name' connected with colon. For example 'java:com.example.Order', 'xml:ABCOrder' 030 * or 'json:XYZOrder'. These type name other than java class name allows the message to 031 * carry the name of the message data structure even if it's marshaled. 032 * 033 * @see {@link DataTypeAware} {@link Transformer} {@link Validator} 034 */ 035public class DataType { 036 037 public static final String JAVA_TYPE_PREFIX = "java"; 038 039 private String model; 040 private String name; 041 private boolean isJavaType; 042 private String typeString; 043 044 public DataType(String urn) { 045 if (urn != null) { 046 String split[] = StringHelper.splitOnCharacter(urn, ":", 2); 047 model = split[0]; 048 isJavaType = model.equals(JAVA_TYPE_PREFIX); 049 if (split.length > 1) { 050 name = split[1]; 051 } 052 } 053 } 054 055 public DataType(Class<?> clazz) { 056 model = JAVA_TYPE_PREFIX; 057 isJavaType = true; 058 name = clazz.getName(); 059 } 060 061 public String getModel() { 062 return model; 063 } 064 065 public String getName() { 066 return name; 067 } 068 069 public boolean isJavaType() { 070 return isJavaType; 071 } 072 073 @Override 074 public String toString() { 075 if (this.typeString == null) { 076 this.typeString = name != null && !name.isEmpty() ? model + ":" + name : model; 077 } 078 return this.typeString; 079 } 080 081 @Override 082 public boolean equals(Object target) { 083 if (target instanceof DataType) { 084 DataType targetdt = (DataType)target; 085 String targetModel = targetdt.getModel(); 086 String targetName = targetdt.getName(); 087 if (targetModel == null) { 088 return false; 089 } else if (targetName == null) { 090 return targetModel.equals(getModel()) && getName() == null; 091 } else { 092 return targetModel.equals(getModel()) && targetName.equals(getName()); 093 } 094 } 095 return false; 096 } 097 098 @Override 099 public int hashCode() { 100 return toString().hashCode(); 101 } 102}