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
019
020import java.lang.reflect.Method;
021import java.lang.reflect.Modifier;
022import java.util.Collection;
023import java.util.HashSet;
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.DtoAuthenticationUser;
031import org.apache.activemq.schema.core.DtoFilteredDestination;
032import org.apache.activemq.schema.core.DtoQueue;
033import org.apache.activemq.schema.core.DtoSslContext;
034import org.apache.activemq.schema.core.DtoTopic;
035import org.apache.activemq.security.AuthenticationUser;
036import org.apache.activemq.spring.SpringSslContext;
037
038public class JAXBUtils {
039
040    public static Method findSetter(Object instance, String elementName) {
041        String setter = "set" + elementName;
042        for (Method m : instance.getClass().getMethods()) {
043            if (setter.equals(m.getName())) {
044                return m;
045            }
046        }
047        return null;
048    }
049
050    public static void ensureAccessible(Method m) {
051        if ((!Modifier.isPublic(m.getModifiers()) || !Modifier.isPublic(m.getDeclaringClass().getModifiers())) && !m.isAccessible()) {
052            m.setAccessible(true);
053        }
054    }
055
056    public static Object inferTargetObject(Object elementContent) {
057        if (DtoTopic.class.isAssignableFrom(elementContent.getClass())) {
058            return new ActiveMQTopic();
059        } else if (DtoQueue.class.isAssignableFrom(elementContent.getClass())) {
060            return new ActiveMQQueue();
061        } else if (DtoAuthenticationUser.class.isAssignableFrom(elementContent.getClass())) {
062            return new AuthenticationUser();
063        } else if (DtoFilteredDestination.class.isAssignableFrom(elementContent.getClass())) {
064            return new FilteredDestination();
065        } else if (DtoSslContext.class.isAssignableFrom(elementContent.getClass())) {
066            return new SpringSslContext();
067        } else {
068            return new Object();
069        }
070    }
071
072    public static Object matchType(List<Object> parameterValues, Class<?> aClass) {
073        Object result = parameterValues;
074        if (Set.class.isAssignableFrom(aClass)) {
075            result = new HashSet(parameterValues);
076        } else if (!Collection.class.isAssignableFrom(aClass)) {
077            result = parameterValues.get(0);
078        }
079        return result;
080    }
081
082}