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: 10351 $
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                    registerClient(aClass);
074                } else if (!Modifier.isAbstract(aClass.getModifiers())) {
075                    list.add(createBeanEndpoint(aClass));
076                }
077            }
078        }
079    
080        public void destroy() throws Exception {
081        }
082    
083        /**
084         * Should the bean be ignored?
085         */
086        protected boolean shouldIgnoreBean(Class type) {
087            Map beans = applicationContext.getBeansOfType(type, true, true);
088            if (beans == null || beans.isEmpty()) {
089                return false;
090            }
091            // TODO apply some filter?
092            return true;
093        }
094    
095        /**
096         * Returns true if the interface is a client side interface
097         */
098        protected boolean isClient(Class type) {
099            return type.isInterface() || Modifier.isAbstract(type.getModifiers());
100        }
101    
102        protected void registerClient(Class type) {
103            /** TODO */
104        }
105    
106    
107        protected BeanEndpoint createBeanEndpoint(Class serviceType) {
108            Endpoint endpointAnnotation = (Endpoint) serviceType.getAnnotation(Endpoint.class);
109            if (endpointAnnotation == null) {
110                throw new IllegalArgumentException("Could not find endpoint annotation on type: " + serviceType);
111            }
112            BeanEndpoint answer = new BeanEndpoint();
113            answer.setBeanType(serviceType);
114            answer.setEndpoint(endpointAnnotation.name());
115            QName service = createQName(endpointAnnotation.serviceName(), endpointAnnotation.targetNamespace());
116            if (service == null) {
117                service = beanComponent.getEPRServiceName();
118            }
119            answer.setService(service);
120            return answer;
121        }
122    
123        protected QName createQName(String localName, String uri) {
124            if (isNotNullOrBlank(localName)) {
125                if (isNotNullOrBlank(uri)) {
126                    return new QName(uri, localName);
127                }
128                return new QName(localName);
129            }
130            return null;
131        }
132    
133    
134        protected boolean isNotNullOrBlank(String text) {
135            return text != null && text.trim().length() > 0;
136        }
137    }