View Javadoc

1   /*
2    * Copyright 2005-2011 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.server.endpoint.mapping;
18  
19  import java.lang.annotation.Annotation;
20  
21  import org.springframework.beans.BeansException;
22  import org.springframework.beans.factory.BeanFactoryUtils;
23  import org.springframework.core.annotation.AnnotationUtils;
24  import org.springframework.ws.server.endpoint.annotation.Endpoint;
25  
26  /**
27   * Abstract base for [email protected] org.springframework.ws.server.EndpointMapping} implementations that map classes tagged with
28   * an annotation. By default the annotation is [email protected] Endpoint}, but this can be overriden in subclasses.
29   * <p/>
30   * The methods of each bean carrying @Endpoint will be registered using [email protected] #registerMethods(String)}.
31   *
32   * @author Arjen Poutsma
33   * @since 1.0.0
34   */
35  public abstract class AbstractAnnotationMethodEndpointMapping<T> extends AbstractMethodEndpointMapping<T> {
36  
37      private boolean detectEndpointsInAncestorContexts = false;
38  
39      /**
40       * Set whether to detect endpoint beans in ancestor ApplicationContexts.
41       * <p/>
42       * Default is "false": Only endpoint beans in the current ApplicationContext will be detected, i.e. only in the
43       * context that this EndpointMapping itself is defined in (typically the current MessageDispatcherServlet's
44       * context).
45       * <p/>
46       * Switch this flag on to detect endpoint beans in ancestor contexts (typically the Spring root
47       * WebApplicationContext) as well.
48       */
49      public void setDetectEndpointsInAncestorContexts(boolean detectEndpointsInAncestorContexts) {
50          this.detectEndpointsInAncestorContexts = detectEndpointsInAncestorContexts;
51      }
52  
53      /** Returns the 'endpoint' annotation type. Default is [email protected] Endpoint}. */
54      protected Class<? extends Annotation> getEndpointAnnotationType() {
55          return Endpoint.class;
56      }
57  
58      @Override
59      protected void initApplicationContext() throws BeansException {
60          super.initApplicationContext();
61          if (logger.isDebugEnabled()) {
62              logger.debug("Looking for endpoints in application context: " + getApplicationContext());
63          }
64          String[] beanNames = (this.detectEndpointsInAncestorContexts ?
65                  BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
66                  getApplicationContext().getBeanNamesForType(Object.class));
67  
68          for (String beanName : beanNames) {
69              Class<?> endpointClass = getApplicationContext().getType(beanName);
70              if (endpointClass != null &&
71                      AnnotationUtils.findAnnotation(endpointClass, getEndpointAnnotationType()) != null) {
72                  registerMethods(beanName);
73              }
74          }
75      }
76  
77  }