View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.service;
21  
22  import java.util.Map;
23  import java.util.concurrent.ConcurrentHashMap;
24  
25  import org.apache.mina.api.IoFilter;
26  import org.apache.mina.api.IoHandler;
27  import org.apache.mina.api.IoService;
28  import org.apache.mina.api.IoSession;
29  import org.apache.mina.api.IoSessionConfig;
30  import org.apache.mina.service.executor.IoHandlerExecutor;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  /**
35   * Base implementation for {@link IoService}s.
36   * 
37   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
38   */
39  public abstract class AbstractIoService implements IoService {
40  
41      /** A logger for this class */
42      static final Logger LOG = LoggerFactory.getLogger(AbstractIoService.class);
43  
44      /** The service state */
45      private ServiceState state;
46  
47      /** The placeholder of managed open sessions */
48      private final Map<Long, IoSession> managedSessions = new ConcurrentHashMap<Long, IoSession>();
49  
50      /** the default session configuration */
51      protected IoSessionConfig config;
52  
53      /** The high level business logic */
54      private IoHandler handler;
55  
56      /** Filters chain */
57      private IoFilter[] filters = new IoFilter[0];
58  
59      /** used for executing IoHandler event in another pool of thread (not in the low level I/O one) */
60      protected final IoHandlerExecutor ioHandlerExecutor;
61  
62      /**
63       * The Service states
64       */
65      protected enum ServiceState {
66          /** Initial state */
67          NONE,
68          /** The service has been created */
69          CREATED,
70          /** The service is started */
71          ACTIVE,
72          /** The service has been suspended */
73          SUSPENDED,
74          /** The service is being stopped */
75          DISPOSING,
76          /** The service is stopped */
77          DISPOSED
78      }
79  
80      /**
81       * Create an AbstractIoService
82       * 
83       * @param eventExecutor used for executing IoHandler event in another pool of thread (not in the low level I/O one).
84       *        Use <code>null</code> if you don't want one. Be careful, the IoHandler processing will block the I/O
85       *        operations.
86       */
87      protected AbstractIoService(final IoHandlerExecutor eventExecutor) {
88          this.state = ServiceState.NONE;
89          this.ioHandlerExecutor = eventExecutor;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public Map<Long, IoSession> getManagedSessions() {
97          return this.managedSessions;
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public void setIoHandler(final IoHandler handler) {
105         this.handler = handler;
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public IoHandler getIoHandler() {
113         return handler;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public IoHandlerExecutor getIoHandlerExecutor() {
121         return ioHandlerExecutor;
122     }
123 
124     /**
125      * @return true if the IoService is active
126      */
127     public boolean isActive() {
128         return this.state == ServiceState.ACTIVE;
129     }
130 
131     /**
132      * @return true if the IoService is being disposed
133      */
134     public boolean isDisposing() {
135         return this.state == ServiceState.DISPOSING;
136     }
137 
138     /**
139      * @return true if the IoService is disposed
140      */
141     public boolean isDisposed() {
142         return this.state == ServiceState.DISPOSED;
143     }
144 
145     /**
146      * @return true if the IoService is suspended
147      */
148     public boolean isSuspended() {
149         return this.state == ServiceState.SUSPENDED;
150     }
151 
152     /**
153      * @return true if the IoService is created
154      */
155     public boolean isCreated() {
156         return this.state == ServiceState.CREATED;
157     }
158 
159     /**
160      * Sets the IoService state to CREATED.
161      */
162     protected void setCreated() {
163         this.state = ServiceState.CREATED;
164     }
165 
166     /**
167      * Sets the IoService state to ACTIVE.
168      */
169     protected void setActive() {
170         this.state = ServiceState.ACTIVE;
171     }
172 
173     /**
174      * Sets the IoService state to DISPOSED.
175      */
176     protected void setDisposed() {
177         this.state = ServiceState.DISPOSED;
178     }
179 
180     /**
181      * Sets the IoService state to DISPOSING.
182      */
183     protected void setDisposing() {
184         this.state = ServiceState.DISPOSING;
185     }
186 
187     /**
188      * Sets the IoService state to SUSPENDED.
189      */
190     protected void setSuspended() {
191         this.state = ServiceState.SUSPENDED;
192     }
193 
194     /**
195      * Initialize the IoService state
196      */
197     protected void initState() {
198         this.state = ServiceState.NONE;
199     }
200 
201     /**
202      * Inform all current the listeners of the service activation.
203      */
204     protected void fireServiceActivated() {
205         if (handler != null) {
206             handler.serviceActivated(this);
207         }
208     }
209 
210     /**
211      * Inform all current the listeners of the service desactivation.
212      */
213     protected void fireServiceInactivated() {
214         if (handler != null) {
215             handler.serviceInactivated(this);
216         }
217     }
218 
219     /**
220      * {@inheritDoc}
221      */
222     @Override
223     public IoFilter[] getFilters() {
224         return this.filters;
225     }
226 
227     /**
228      * {@inheritDoc}
229      */
230     @Override
231     public void setFilters(final IoFilter... filters) {
232         this.filters = filters;
233     }
234 }