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     */
017    package org.apache.servicemix.bean;
018    
019    import java.lang.reflect.Modifier;
020    import java.util.List;
021    import java.util.Map;
022    import java.util.Set;
023    
024    import javax.xml.namespace.QName;
025    
026    import org.apache.servicemix.bean.support.ResolverUtil;
027    import org.springframework.beans.BeansException;
028    import org.springframework.context.ApplicationContext;
029    import org.springframework.context.ApplicationContextAware;
030    
031    /**
032     * Used to find POJOs on the classpath to be auto-exposed as endpoints
033     *
034     * @version $Revision: 3702 $
035     */
036    public class EndpointFinder implements ApplicationContextAware {
037        private ApplicationContext applicationContext;
038        private ResolverUtil resolver = new ResolverUtil();
039        private String[] packages = {};
040        private BeanComponent beanComponent;
041    
042        public EndpointFinder(BeanComponent beanComponent) {
043            this.beanComponent = beanComponent;
044            this.packages = beanComponent.getSearchPackages();
045            this.applicationContext = beanComponent.getApplicationContext();
046        }
047    
048        public String[] getPackages() {
049            return packages;
050        }
051    
052        public void setPackages(String[] packages) {
053            this.packages = packages;
054        }
055    
056        public ApplicationContext getApplicationContext() {
057            return applicationContext;
058        }
059    
060        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
061            this.applicationContext = applicationContext;
062        }
063    
064    
065        public void addEndpoints(List list) {
066            resolver.findAnnotated(Endpoint.class, packages);
067            Set<Class> classes = resolver.getClasses();
068            for (Class aClass : classes) {
069                if (shouldIgnoreBean(aClass)) {
070                    continue;
071                }
072                if (!isClient(aClass)) {
073                    list.add(createBeanEndpoint(aClass));
074                }
075            }
076        }
077    
078        /**
079         * Should the bean be ignored?
080         */
081        protected boolean shouldIgnoreBean(Class type) {
082            Map beans = applicationContext.getBeansOfType(type, true, true);
083            if (beans == null || beans.isEmpty()) {
084                return false;
085            }
086            // TODO apply some filter?
087            return true;
088        }
089    
090        /**
091         * Returns true if the interface is a client side interface
092         */
093        protected boolean isClient(Class type) {
094            return type.isInterface() || Modifier.isAbstract(type.getModifiers());
095        }
096    
097        protected BeanEndpoint createBeanEndpoint(Class serviceType) {
098            Endpoint endpointAnnotation = (Endpoint) serviceType.getAnnotation(Endpoint.class);
099            if (endpointAnnotation == null) {
100                throw new IllegalArgumentException("Could not find endpoint annotation on type: " + serviceType);
101            }
102            BeanEndpoint answer = new BeanEndpoint();
103            answer.setBeanType(serviceType);
104            answer.setEndpoint(endpointAnnotation.name());
105            QName service = createQName(endpointAnnotation.serviceName(), endpointAnnotation.targetNamespace());
106            if (service == null) {
107                service = beanComponent.getEPRServiceName();
108            }
109            answer.setService(service);
110            return answer;
111        }
112    
113        protected QName createQName(String localName, String uri) {
114            if (isNotNullOrBlank(localName)) {
115                if (isNotNullOrBlank(uri)) {
116                    return new QName(uri, localName);
117                }
118                return new QName(localName);
119            }
120            return null;
121        }
122    
123    
124        protected boolean isNotNullOrBlank(String text) {
125            return text != null && text.trim().length() > 0;
126        }
127    }