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.activemq.plugin;
018
019import javax.xml.bind.JAXBElement;
020
021import java.lang.reflect.Method;
022import java.util.HashSet;
023import java.util.LinkedList;
024import java.util.List;
025import java.util.Set;
026
027import org.apache.activemq.broker.region.virtual.FilteredDestination;
028import org.apache.activemq.command.ActiveMQQueue;
029import org.apache.activemq.command.ActiveMQTopic;
030import org.apache.activemq.schema.core.DtoFilteredDestination;
031import org.apache.activemq.schema.core.DtoTopic;
032import org.apache.activemq.schema.core.DtoQueue;
033import org.apache.activemq.schema.core.DtoAuthenticationUser;
034import org.apache.activemq.security.AuthenticationUser;
035
036public class JAXBUtils {
037
038    public static Method findSetter(Object instance, String elementName) {
039        String setter = "set" + elementName;
040        for (Method m : instance.getClass().getMethods()) {
041            if (setter.equals(m.getName())) {
042                return m;
043            }
044        }
045        return null;
046    }
047
048    public static Object inferTargetObject(Object elementContent) {
049        if (DtoTopic.class.isAssignableFrom(elementContent.getClass())) {
050            return new ActiveMQTopic();
051        } else if (DtoQueue.class.isAssignableFrom(elementContent.getClass())) {
052            return new ActiveMQQueue();
053        } else if (DtoAuthenticationUser.class.isAssignableFrom(elementContent.getClass())) {
054            return new AuthenticationUser();
055        } else if (DtoFilteredDestination.class.isAssignableFrom(elementContent.getClass())) {
056            return new FilteredDestination();            
057        } else {
058            return new Object();
059        }
060    }
061
062    public static Object matchType(List<Object> parameterValues, Class<?> aClass) {
063        Object result = parameterValues;
064        if (Set.class.isAssignableFrom(aClass)) {
065            result = new HashSet(parameterValues);
066        }
067        return result;
068    }
069
070}