View Javadoc

1   /*
2    * Copyright 2007 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 {@link org.springframework.ws.server.EndpointMapping} implementations that map classes tagged with
28   * an annotation. By default the annotation is {@link Endpoint}, but this can be overriden in subclasses.
29   * <p/>
30   * The methods of each bean carrying @Endpoint will be registered using {@link #registerMethods(String)}.
31   *
32   * @author Arjen Poutsma
33   * @since 1.0.0
34   */
35  public abstract class AbstractAnnotationMethodEndpointMapping extends AbstractMethodEndpointMapping {
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 {@link Endpoint}. */
54      protected Class<? extends Annotation> getEndpointAnnotationType() {
55          return Endpoint.class;
56      }
57  
58      protected final void initApplicationContext() throws BeansException {
59          if (logger.isDebugEnabled()) {
60              logger.debug("Looking for endpoints in application context: " + getApplicationContext());
61          }
62          String[] beanNames = (this.detectEndpointsInAncestorContexts ?
63                  BeanFactoryUtils.beanNamesForTypeIncludingAncestors(getApplicationContext(), Object.class) :
64                  getApplicationContext().getBeanNamesForType(Object.class));
65  
66          for (int i = 0; i < beanNames.length; i++) {
67              String beanName = beanNames[i];
68              Class endpointClass = getApplicationContext().getType(beanName);
69              if (endpointClass != null &&
70                      AnnotationUtils.findAnnotation(endpointClass, getEndpointAnnotationType()) != null) {
71                  registerMethods(beanName);
72              }
73          }
74      }
75  
76  }