From ea1082899ad341f890708273bd28e63eaa365d1a Mon Sep 17 00:00:00 2001 From: Bosanac Dejan Date: Wed, 5 Aug 2009 12:50:04 +0000 Subject: [PATCH] returning xstream factory bean (a bit modified), mistekanly deleted yesterday git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@801197 13f79535-47bb-0310-9956-ffa450edef68 --- .../activemq/util/XStreamFactoryBean.java | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 activemq-core/src/main/java/org/apache/activemq/util/XStreamFactoryBean.java diff --git a/activemq-core/src/main/java/org/apache/activemq/util/XStreamFactoryBean.java b/activemq-core/src/main/java/org/apache/activemq/util/XStreamFactoryBean.java new file mode 100644 index 0000000000..081a09b97c --- /dev/null +++ b/activemq-core/src/main/java/org/apache/activemq/util/XStreamFactoryBean.java @@ -0,0 +1,122 @@ +/** + * 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.activemq.util; + +import java.util.Iterator; +import java.util.Map; + +import org.springframework.beans.factory.FactoryBean; +import org.springframework.beans.propertyeditors.ClassEditor; +import org.springframework.util.Assert; + +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.converters.Converter; +import com.thoughtworks.xstream.converters.ConverterMatcher; +import com.thoughtworks.xstream.converters.SingleValueConverter; + +public class XStreamFactoryBean implements FactoryBean { + + XStream xstream = new XStream(); + + /** + * Sets the Converters or SingleValueConverters to be registered with the + * XStream instance. + * + * @see Converter + * @see SingleValueConverter + */ + public void setConverters(ConverterMatcher[] converters) { + for (int i = 0; i < converters.length; i++) { + if (converters[i] instanceof Converter) { + xstream.registerConverter((Converter) converters[i], i); + } + else if (converters[i] instanceof SingleValueConverter) { + xstream.registerConverter((SingleValueConverter) converters[i], i); + } + else { + throw new IllegalArgumentException("Invalid ConverterMatcher [" + converters[i] + "]"); + } + } + } + + /** + * Set a alias/type map, consisting of string aliases mapped to Class instances (or Strings to be + * converted to Class instances). + * + * @see org.springframework.beans.propertyeditors.ClassEditor + */ + public void setAliases(Map aliases) { + for (Iterator iterator = aliases.entrySet().iterator(); iterator.hasNext();) { + Map.Entry entry = (Map.Entry) iterator.next(); + // Check whether we need to convert from String to Class. + Class type; + if (entry.getValue() instanceof Class) { + type = (Class) entry.getValue(); + } + else { + ClassEditor editor = new ClassEditor(); + editor.setAsText(String.valueOf(entry.getValue())); + type = (Class) editor.getValue(); + } + xstream.alias((String) entry.getKey(), type); + } + } + + /** + * Sets the XStream mode. + * + * @see XStream#XPATH_REFERENCES + * @see XStream#ID_REFERENCES + * @see XStream#NO_REFERENCES + */ + public void setMode(int mode) { + xstream.setMode(mode); + } + + /** + * Sets the classes, for which mappings will be read from class-level JDK 1.5+ annotation metadata. + * + * @see Annotations#configureAliases(XStream, Class[]) + */ + public void setAnnotatedClass(Class annotatedClass) { + Assert.notNull(annotatedClass, "'annotatedClass' must not be null"); + xstream.processAnnotations(annotatedClass); + } + + /** + * Sets annotated classes, for which aliases will be read from class-level JDK 1.5+ annotation metadata. + * + * @see Annotations#configureAliases(XStream, Class[]) + */ + public void setAnnotatedClasses(Class[] annotatedClasses) { + Assert.notEmpty(annotatedClasses, "'annotatedClasses' must not be empty"); + xstream.processAnnotations(annotatedClasses); + } + + public Object getObject() throws Exception { + return xstream; + } + + public Class getObjectType() { + return XStream.class; + } + + public boolean isSingleton() { + return true; + } + +}