View Javadoc

1   /*
2    * Copyright 2005-2010 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.transport.support;
18  
19  import org.springframework.beans.factory.DisposableBean;
20  import org.springframework.context.Lifecycle;
21  
22  /**
23   * Abstract base class for standalone, server-side transport objects. Provides a basic, thread-safe implementation of
24   * the [email protected] Lifecycle} interface, and various template methods to be implemented by concrete sub classes.
25   *
26   * @author Arjen Poutsma
27   * @since 1.5.0
28   */
29  public abstract class AbstractStandaloneMessageReceiver extends SimpleWebServiceMessageReceiverObjectSupport
30          implements Lifecycle, DisposableBean {
31  
32      private volatile boolean active = false;
33  
34      private boolean autoStartup = true;
35  
36      private boolean running = false;
37  
38      private final Object lifecycleMonitor = new Object();
39  
40      /** Return whether this server is currently active, that is, whether it has been set up but not shut down yet. */
41      public final boolean isActive() {
42          synchronized (lifecycleMonitor) {
43              return active;
44          }
45      }
46  
47      /** Return whether this server is currently running, that is, whether it has been started and not stopped yet. */
48      public final boolean isRunning() {
49          synchronized (lifecycleMonitor) {
50              return running;
51          }
52      }
53  
54      /**
55       * Set whether to automatically start the receiver after initialization.
56       * <p/>
57       * Default is <code>true</code>; set this to <code>false</code> to allow for manual startup.
58       */
59      public void setAutoStartup(boolean autoStartup) {
60          this.autoStartup = autoStartup;
61      }
62  
63      /** Calls [email protected] #activate()} when the BeanFactory initializes the receiver instance. */
64      @Override
65      public void afterPropertiesSet() throws Exception {
66          activate();
67      }
68  
69      /** Calls [email protected] #shutdown()} when the BeanFactory destroys the receiver instance. */
70      public void destroy() {
71          shutdown();
72      }
73  
74      /**
75       * Initialize this server. Starts the server if [email protected] #setAutoStartup(boolean) autoStartup} hasn't been turned
76       * off.
77       */
78      public final void activate() throws Exception {
79          synchronized (lifecycleMonitor) {
80              active = true;
81          }
82          onActivate();
83          if (autoStartup) {
84              start();
85          }
86      }
87  
88      /** Start this server. */
89      public final void start() {
90          synchronized (lifecycleMonitor) {
91              running = true;
92          }
93          onStart();
94      }
95  
96      /** Stop this server. */
97      public final void stop() {
98          synchronized (lifecycleMonitor) {
99              running = false;
100         }
101         onStop();
102     }
103 
104     /** Shut down this server. */
105     public final void shutdown() {
106         synchronized (lifecycleMonitor) {
107             running = false;
108             active = false;
109         }
110         onShutdown();
111     }
112 
113     /**
114      * Template method invoked when [email protected] #activate()} is invoked.
115      *
116      * @throws Exception in case of errors
117      */
118     protected abstract void onActivate() throws Exception;
119 
120     /** Template method invoked when [email protected] #start()} is invoked. */
121     protected abstract void onStart();
122 
123     /** Template method invoked when [email protected] #stop()} is invoked. */
124     protected abstract void onStop();
125 
126     /** Template method invoked when [email protected] #shutdown()} is invoked. */
127     protected abstract void onShutdown();
128 }