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.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 {@link 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 {@link #activate()} when the BeanFactory initializes the receiver instance. */
64 public void afterPropertiesSet() throws Exception {
65 activate();
66 }
67
68 /** Calls {@link #shutdown()} when the BeanFactory destroys the receiver instance. */
69 public void destroy() {
70 shutdown();
71 }
72
73 /**
74 * Initialize this server. Starts the server if {@link #setAutoStartup(boolean) autoStartup} hasn't been turned
75 * off.
76 */
77 public final void activate() throws Exception {
78 synchronized (lifecycleMonitor) {
79 active = true;
80 }
81 onActivate();
82 if (autoStartup) {
83 start();
84 }
85 }
86
87 /** Start this server. */
88 public final void start() {
89 synchronized (lifecycleMonitor) {
90 running = true;
91 }
92 onStart();
93 }
94
95 /** Stop this server. */
96 public final void stop() {
97 synchronized (lifecycleMonitor) {
98 running = false;
99 }
100 onStop();
101 }
102
103 /** Shut down this server. */
104 public final void shutdown() {
105 synchronized (lifecycleMonitor) {
106 running = false;
107 active = false;
108 }
109 onShutdown();
110 }
111
112 /**
113 * Template method invoked when {@link #activate()} is invoked.
114 *
115 * @throws Exception in case of errors
116 */
117 protected abstract void onActivate() throws Exception;
118
119 /** Template method invoked when {@link #start()} is invoked. */
120 protected abstract void onStart();
121
122 /** Template method invoked when {@link #stop()} is invoked. */
123 protected abstract void onStop();
124
125 /** Template method invoked when {@link #shutdown()} is invoked. */
126 protected abstract void onShutdown();
127 }