View Javadoc

1   /*
2    * Copyright 2008 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.client.support.destination;
18  
19  import java.net.URI;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  /**
25   * Abstract base class for {@link DestinationProvider} implementations that cache destination URI.
26   * <p/>
27   * Caching can be disabled by setting the {@link #setCache(boolean) cache} property to <code>false</code>; forcing a
28   * destination lookup for every call.
29   *
30   * @author Arjen Poutsma
31   * @since 1.5.4
32   */
33  public abstract class AbstractCachingDestinationProvider implements DestinationProvider {
34  
35      /** Logger available to subclasses. */
36      protected final Log logger = LogFactory.getLog(getClass());
37  
38      private URI cachedUri;
39  
40      private boolean cache = true;
41  
42      /**
43       * Set whether to cache resolved destinations. Default is <code>true</code>. This flag can be turned off to
44       * re-lookup a destination for each operation, which allows for hot restarting of destinations. This is mainly
45       * useful during development.
46       */
47      public void setCache(boolean cache) {
48          this.cache = cache;
49      }
50  
51      public final URI getDestination() {
52          if (cache) {
53              if (cachedUri == null) {
54                  cachedUri = lookupDestination();
55              }
56              return cachedUri;
57          }
58          else {
59              return lookupDestination();
60          }
61      }
62  
63      /**
64       * Abstract template method that looks up the URI.
65       * <p/>
66       * If {@linkplain #setCache(boolean) caching}  is enabled, this method will only be called once.
67       *
68       * @return the destination URI
69       */
70      protected abstract URI lookupDestination();
71  }