Custom Spring Integration Splitter throwing error - spring

Hi I am Trying to extract multiple messages from a single message and split those into multiple messages using a splitter element. But my Splitter is throwing the below error
2015-10-26 15:09:06,257 [[ACTIVE] ExecuteThread: '0' for queue: 'weblogic.kernel.Default (self-tuning)'] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.handler.MessageHandlerChain#1': Cannot create inner bean 'org.springframework.integration.config.SplitterFactoryBean#20e2ef61' of type [org.springframework.integration.config.SplitterFactoryBean] while setting bean property 'handlers' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.integration.config.SplitterFactoryBean#20e2ef61': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Found ambiguous parameter type [class java.lang.String] for method match: [public java.util.List com.salesorder.payment.util.PoslogToIsellRequests.poslogTransformer(java.lang.String), public java.util.List com.salesorder.payment.util.PoslogToIsellRequests.split(java.util.List)]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:282)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:121)
at
The splitter block:
<si:splitter>
<bean
class="com.ikea.pip.salesorder.payment.util.PoslogToIsellRequests"
p:mapTaxRateCategory="${common.map.tax.rate.category}" p:buCode="${common.country.code.iso}"
p:sourceRegion="${common.isell.order.source}" p:countryCode ="${common.country.code.iso}" />
</si:splitter>
Java Code of the class:
public class PoslogToIsellRequests implements Splitter {
private static final Logger LOG = LoggerFactory
.getLogger(PoslogToIsellRequests.class);
private static final String ORDER= "SpecialOrderNumber";
private static XMLInputFactory inputFactory;
private final SimpleXslt transformPoslogXslt = new SimpleXslt(
"xsl/POSLogToAddPaymentRequest.xsl");
private String sourceRegion;
private String buCode;
private String mapTaxRateCategory;
private String countryCode;
static {
inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE,
Boolean.TRUE);
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
#Override
public List<String> split(List<String> xmlToSplit) {
List<String> splittedPayload = new ArrayList<String>();
for (String xml : xmlToSplit) {
splittedPayload.addAll(split(xml));
}
return splittedPayload;
}
public void setSourceRegion(String sourceRegion) {
this.sourceRegion = sourceRegion;
}
public void setBuCode(String buCode) {
this.buCode = buCode;
}
public void setMapTaxRateCategory(String mapTaxRateCategory) {
this.mapTaxRateCategory = mapTaxRateCategory;
}
/* #param xmlToSplit
* #return
* #throws Exception
*/
#Override
public List<String> split(String xmlToSplit) {
List<String> resultSet=new ArrayList<String>();
resultSet=poslogTransformer(xmlToSplit);
return resultSet;
}
public List<String> poslogTransformer(String xmlToSplit) {
List<String> resultSet=null;
Set<String> orderNos=new HashSet<String>();
String payload = xmlToSplit;
try{
orderNos= parseOrderNos(payload);
resultSet=new ArrayList<String>();
}
catch (XMLStreamException e) {
LOG.warn("Could not parse Transaction");
}
for(String orderno:orderNos){
Map<String, String> parameters = createParams(orderno);
String result = transformPoslogXslt.transform(payload,
parameters);
resultSet.add(result);
}
return resultSet;
}
private Map<String, String> createParams(String orderNo) {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("sourceRegion", sourceRegion);
parameters.put("buCode",buCode );
parameters.put("mapTaxRateCategory",mapTaxRateCategory );
parameters.put("orderNo",orderNo );
parameters.put("countryCode", countryCode);
return parameters;
}
private Set<String> parseOrderNos(String payload) throws XMLStreamException {
Set<String> orders=new HashSet<String>();
XMLEventReader reader;
reader = inputFactory.createXMLEventReader(new StringReader(payload));
String currentElement = "";
try {
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
currentElement = event.asStartElement().getName()
.getLocalPart();
} else if (currentElement.equals(ORDER)
&& event.isCharacters()) {
String value = event.asCharacters().getData();
if(StringUtils.isNotBlank(value)){
orders.add(value);}
}
}
} finally {
reader.close();
}
return orders;
}
}
The Splitter interface is just contains two split method. Is method overloading not allowed in spring Integration?
One additional query can I have string input as a parameter instead of message?

If you don't explicity identify the method name in the splitter, the framework will detect multiple method candidates and won't be able to determine which one to call.
Hence:
Found ambiguous parameter type [class java.lang.String] for method match: [public java.util.List com.salesorder.payment.util.PoslogToIsellRequests.poslogTransformer(java.lang.String), public java.util.List com.salesorder.payment.util.PoslogToIsellRequests.split(java.util.List)]
All public methods are considered for POJO method matching unless you specify method="split".
If you add that, the framework will invoke the appropriate split method, depending on the payload type of the inbound message.

Related

i have an problem while start up the application, In the process of encrypting some values in my database table

This is the problem i get while i run my application:
Error creating bean with name 'aesEncryptor': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'aes.encryption.key' in value "${aes.encryption.key}"
Below is the code which is the class file in the application:
#Configuration
public class AesEncryptor implements AttributeConverter<Object, String> {
#Value("${aes.encryption.key}")
private String encryptionKey;
private final String encryptionCipher = "AES";
private Key key;
private Cipher cipher;
private Key getKey() {
if (key == null)
key = new SecretKeySpec(encryptionKey.getBytes(), encryptionCipher);
return key;
}
private Cipher getCipher() throws GeneralSecurityException {
if (cipher == null)
cipher = Cipher.getInstance(encryptionCipher);
return cipher;
}
private void initCipher(int encryptMode) throws GeneralSecurityException {
getCipher().init(encryptMode, getKey());
}
#SneakyThrows
#Override
public String convertToDatabaseColumn(Object attribute) {
if (attribute == null)
return null;
initCipher(Cipher.ENCRYPT_MODE);
byte[] bytes = SerializationUtils.serialize(attribute);
return Base64.getEncoder().encodeToString(getCipher().doFinal(bytes));
}
#SneakyThrows
#Override
public Object convertToEntityAttribute(String dbData) {
if (dbData == null)
return null;
initCipher(Cipher.DECRYPT_MODE);
byte[] bytes = getCipher().doFinal(Base64.getDecoder().decode(dbData));
return SerializationUtils.deserialize(bytes);
}
}

java.lang.IllegalArgumentException: Could not find field 'isBoolean' of type [class java.lang.Boolean] on target object

When I run test then it failed at this point ReflectionTestUtils.setField(service, SeRepositoryImpl.class, "isBoolean",true,Boolean.class) complains about Could not find field 'isBoolean' of type not found. Error trace as below.
I am not sure why because my repositoryImpl class has isBoolean variable defined.
java.lang.IllegalArgumentException: Could not find field 'isBoolean' of type [class java.lang.Boolean] on target object [lautomation.repository.impl.SaRepositoryImpl#4a178d1e] or target class [lautomation.repository.impl.SaRepositoryImpl]
at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:175)
test class looks like
#MockBean(name = "seRepository")
PolicyRepository seRepository;
#InjectMocks
private SeRepositoryImpl service;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
#Test
public void testUpdateStatus() throws Exception{
ReflectionTestUtils.setField(service, SeRepositoryImpl.class, "isBoolean",true,Boolean.class);
List<Policy> policies = Arrays.asList(new Policy[] {new Policy() });
service.updateIssuedStatus(Mockito.any(Policy.class));
Mockito.verify(seRepository, Mockito.times(1)).updateIssuedStatus(Mockito.any(Policy.class));
}
}
Respository implementation class SeRepositoryImpl has isBoolean variable defined
#Repository("seRepository")
#Log4j
public class SeRepositoryImpl implements PolicyRepository {
#Value("${change-db}")
private boolean isBoolean;
#Autowired
#Qualifier("jdbcDbName")
private NamedParameterJdbcTemplate jdbcTemplate;
#Override
public void updateIssuedStatus(final Policy policy) {
if(!isBoolean) {
log.warn("isBoolean is set to false - skipping db write.");
return;
}
final HashMap<String, String> params = new HashMap<>();
params.put("issued",
new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
params.put("id", Integer.toString(policy.getId()));
jdbcTemplate.update(updateIssuedStatus, params);
String currDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
log.info("params:"+Integer.toString(policy.getId())+" Date:"+currDate);
final String sql = "call usp_updateDatabase(:policy,:currDate)";
MapSqlParameterSource value = new MapSqlParameterSource();
value.addValue("id",Integer.toString(policy.getId()));
value.addValue("stop_dt",new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
jdbcTemplate.update(sql, value);
}
}

java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;

I am trying to access list of friends of given username using hibernate. Here is my Service class in which fetchListOfFriends function is used to convert the generic list to an Arraylist of type of FriendsDetails.
#Service
#Transactional
public class DetailsServiceImpl implements DetailsService {
#Autowired
private DetailsDao detailsDao;
#Override
public List<FriendsDetails> fetchListOfFriends(String name) {
#SuppressWarnings("rawtypes")
List listOfFriends=detailsDao.fetchListOfFriends(name);
List<FriendsDetails> friendList= fetchListOfFriendss(listOfFriends);
if(listOfFriends==null){
System.out.println("Empty and null list");
}
System.out.println("size of friendList" + listOfFriends.size());
return friendList;
}
private List<FriendsDetails> fetchListOfFriendss(#SuppressWarnings("rawtypes") List genericList) {
#SuppressWarnings("unchecked")
List<Object> result = (List<Object>) genericList;
Iterator<Object> itr = result.iterator();
List<FriendsDetails> listOfFriend= new ArrayList<FriendsDetails>();
while(itr.hasNext()){
Object[] obj = (Object[]) itr.next();
System.out.println(obj.toString());
String userName = String.valueOf(obj[0]);
FriendsDetails obj1= new FriendsDetails();
obj1.setFriendName(userName);
listOfFriend.add(obj1);
}
return listOfFriend;
}
DetailsDaoImpl.java
#Autowired
private SessionFactory sessionFactory;
#SuppressWarnings("rawtypes")
#Override
public List fetchListOfFriends(String name) {
Session session=sessionFactory.getCurrentSession();
String queryToFetchFriends="Select name,presenceStatus from UserPresence where name in (Select friendName from Friends where name='"+name+"')";
List listOfFriends=session.createSQLQuery(queryToFetchFriends).list();
return listOfFriends;
}
Logs.txt
May 22, 2016 1:24:11 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [springmvc] in context with path [/Testing] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;] with root cause
java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
at com.tcs.Service.FriendServiceImpl.searchFriend(FriendServiceImpl.java:61)
at com.tcs.Service.FriendServiceImpl.searchFriend(FriendServiceImpl.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Everything should work correctly.
The problem, obviously, that List listOfFriends is List<String>, not List<Object[]>. It is strange, because of Select name,presenceStatus from UserPresence should return List<Object[]>.
Maybe Spring set an incorrect implementation of #Autowired
private DetailsDao detailsDao;.
Try this code. This should work for List<String>
private List<FriendsDetails> fetchListOfFriendss(List<?> genericList) {
Iterator<?> itr = genericList.iterator();
List<FriendsDetails> listOfFriend = new ArrayList<FriendsDetails>();
while (itr.hasNext()) {
Object obj = itr.next();
String userName = String.valueOf(obj);
System.out.println(userName);
FriendsDetails obj1 = new FriendsDetails();
obj1.setFriendName(userName);
listOfFriend.add(obj1);
}
return listOfFriend;
}

Spring implementation of Flyweight pattern

I have a Flyweight pattern implementation in java programming without Spring. Please suggest how do I make this as a Spring managed Bean. It has static method calls and Inner class. The purpose is to initially load all the OptionSets from CRM to share the same OptionSets in the application. This prevents expensive calls to CRM. Users are restricted to create any new OptionSets hence Inner class. First need to implement as a Spring bean and then get it to be ApplicationContextAware to reference other beans. I am primarily using Spring XML configuration for bean definition.
public class OptionSetFactory{
private static Map <String, Object>optionSets = new HashMap();
//Inner class to restrict users creating OptionSet
private class OptionSet implements IOptionSet{
private String entityName;
private String attributeName;
private Hashtable<Integer, String> options;
private IOrganizationService service;
private static final String GUID_EMPTY = "00000000-0000-0000-0000-000000000000";
private ApplicationContext context;
OptionSet(String entityName, String attributeName){
this.entityName = entityName;
this.attributeName = attributeName;
//this.options = options;
OrganizationRequest request = new OrganizationRequest();
request.setRequestName("RetrieveAttribute");
Guid guid = new Guid();
guid.setValue(GUID_EMPTY);
ParameterCollection paramCol = new ParameterCollection();
KeyValuePairOfstringanyType kv0 = new KeyValuePairOfstringanyType();
kv0.setKey("MetadataId");
kv0.setValue(guid);
paramCol.getKeyValuePairOfstringanyTypes().add(kv0);
KeyValuePairOfstringanyType kv1 = new KeyValuePairOfstringanyType();
kv1.setKey("EntityLogicalName");
kv1.setValue(entityName);
paramCol.getKeyValuePairOfstringanyTypes().add(kv1);
KeyValuePairOfstringanyType kv2 = new KeyValuePairOfstringanyType();
kv2.setKey("LogicalName");
kv2.setValue(attributeName);
paramCol.getKeyValuePairOfstringanyTypes().add(kv2);
KeyValuePairOfstringanyType kv3 = new KeyValuePairOfstringanyType();
kv3.setKey("RetrieveAsIfPublished");
kv3.setValue(true);
paramCol.getKeyValuePairOfstringanyTypes().add(kv3);
request.setParameters(paramCol);
try {
OrganizationResponse response=service.execute(request);
PicklistAttributeMetadata pickListAttrMetadata = (PicklistAttributeMetadata)response.getResults().getKeyValuePairOfstringanyTypes().get(0).getValue();
OptionSetMetadata optionSetMetadata = pickListAttrMetadata.getOptionSet();
for(OptionMetadata optionMetaData : optionSetMetadata.getOptions().getOptionMetadatas()){
//TODO populate OptionSet from optionsetMetadata
}
} catch (IOrganizationServiceExecuteOrganizationServiceFaultFaultFaultMessage e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public String getEntityName() {
return entityName;
}
#Override
public String getAttributeName() {
return attributeName;
}
#Override
public Hashtable<Integer, String> getOptions() {
return options;
}
}
//static block to load predefined OptionSets in HashMap
static{
OptionSetFactory factory = new OptionSetFactory();
optionSets.put("dsl_type", factory.new OptionSet("dsl_type", "dsl_operationalstructure"));
}
//Static method calls for user to fetch OptionSet based on inputs
public static IOptionSet getOptionSet(String entityName, String attributeName){
return (IOptionSet) optionSets.get(entityName+"."+attributeName);
}
public static IOptionSet getOptionSet(String attributeName){
return (IOptionSet) optionSets.get(attributeName.toLowerCase());
}
}

Hibernate CompositeUserType mapping has wrong number of columns

I am new to Hibernate. Writing a CompositeUserType. When I run the code I am getting error.
property
mapping has wrong number of columns:
Please help me what am I missing?
My CompositeUserType goes as follows
public class EncryptedAsStringType implements CompositeUserType {
#Override
public String[] getPropertyNames() {
return new String[] { "stockId", "stockCode", "stockName","stockDescription" };
}
#Override
public Type[] getPropertyTypes() {
//stockId, stockCode,stockName,modifiedDate
return new Type[] {
Hibernate.INTEGER, Hibernate.STRING, Hibernate.STRING,Hibernate.STRING
};
}
#Override
public Object getPropertyValue(final Object component, final int property)
throws HibernateException {
Object returnValue = null;
final Stock auditData = (Stock) component;
if (0 == property) {
returnValue = auditData.getStockId();
} else if (1 == property) {
returnValue = auditData.getStockCode();
} else if (2 == property) {
returnValue = auditData.getStockName();
} return returnValue;
}
#Override
public void setPropertyValue(final Object component, final int property,
final Object setValue) throws HibernateException {
final Stock auditData = (Stock) component;
}
#Override
public Object nullSafeGet(final ResultSet resultSet,
final String[] names,
final SessionImplementor paramSessionImplementor, final Object paramObject)
throws HibernateException, SQLException {
//owner here is of type TestUser or the actual owning Object
Stock auditData = null;
final Integer createdBy = resultSet.getInt(names[0]);
//Deferred check after first read
if (!resultSet.wasNull()) {
auditData = new Stock();
System.out.println(">>>>>>>>>>>>"+resultSet.getInt(names[1]));
System.out.println(">>>>>>>>>>>>"+resultSet.getString(names[2]));
System.out.println(">>>>>>>>>>>>"+resultSet.getString(names[3]));
System.out.println(">>>>>>>>>>>>"+resultSet.getString(names[4]));
}
return auditData;
}
#Override
public void nullSafeSet(final PreparedStatement preparedStatement,
final Object value, final int property,
final SessionImplementor sessionImplementor)
throws HibernateException, SQLException {
if (null == value) {
} else {
final Stock auditData = (Stock) value;
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStockCode());
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStockDescription());
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStockId());
System.out.println("::::::::::::::::::::::::::::::::"+auditData.getStatus());
}
}
My Domain class Stock has five attributes. (stockId,stockCode,StockName,Status , Stock
Description)
I need to declare the field Stock description as Composite field Type.
private Integer stockId;
private String stockCode;
private String stockName;
private String status;
private String stockDescription;
//Constructors
#Column(name = "STOCK_CC", unique = true, nullable = false, length = 20)
#Type(type="com.mycheck.EncryptedAsStringType")
#Columns(columns = { #Column(name="STOCK_ID"),
#Column(name="STOCK_CODE"),
#Column(name="STOCK_NAME")
})
public String getStockDescription() {
return stockDescription;
}
}
When I try to execute a insert for Stock. I am getting the error Error creating bean with name
'sessionFactory' defined in class path resource [spring/config/../database/Hibernate.xml]:
Invocation of init method failed. nested exception is org.hibernate.MappingException:
property mapping has wrong number of columns: com.stock.model.Stock.stockDescription type:
com.mycheck.EncryptedAsStringType
Where am I going wrong ?
One can extract the answer from the code samples and the comments to the original question, but to save everyone some reading, I've compiled a quick summary.
If you declare a CompositeUserType that maps a type to n columns, you have to declare n columns in #Columns besides the #Type annotation. Example:
public class EncryptedAsStringType implements CompositeUserType {
#Override
public String[] getPropertyNames() {
return new String[] { "stockId", "stockCode", "stockName","stockDescription" };
}
// ...
}
This CompositeUserType maps to 4 separate columns, therefore 4 separate #Column annotations have to be declared:
#Type(type="com.mycheck.EncryptedAsStringType")
#Columns(columns = {
#Column(name="STOCK_ID"),
#Column(name="STOCK_CODE"),
#Column(name="STOCK_NAME"),
#Column(name="STOCK_DESCRIPTION")
})
public String getStockDescription() {
return stockDescription;
}
That's it and Hibernate is happy.

Resources