90 private final List<Socket> socketList = new ArrayList<>(); |
90 private final List<Socket> socketList = new ArrayList<>(); |
91 private ServerSocket serverSocket; |
91 private ServerSocket serverSocket; |
92 private ExecutorService workingPool; |
92 private ExecutorService workingPool; |
93 private ConnectionHandler connectionHandler; |
93 private ConnectionHandler connectionHandler; |
94 private SessionHandler sessionHandler; |
94 private SessionHandler sessionHandler; |
95 private boolean useDaemonThread = false; |
|
96 |
95 |
97 enum DebugLevel { |
96 enum DebugLevel { |
98 FULL, // all debug message will be printed |
97 FULL, // all debug message will be printed |
99 NONE, // none of debug message will be printed |
98 NONE, // none of debug message will be printed |
100 CUSTOMIZE // only specified class debug message will be printed |
99 CUSTOMIZE // only specified class debug message will be printed |
112 public BaseLdapServer() throws IOException { |
111 public BaseLdapServer() throws IOException { |
113 this(new ServerSocket(0, 0, InetAddress.getLoopbackAddress())); |
112 this(new ServerSocket(0, 0, InetAddress.getLoopbackAddress())); |
114 } |
113 } |
115 |
114 |
116 /** |
115 /** |
117 * BaseLdapServer overload constructor with given server socket. |
|
118 * |
|
119 * @param serverSocket given server socket |
|
120 */ |
|
121 public BaseLdapServer(ServerSocket serverSocket) { |
|
122 this(serverSocket, false); |
|
123 } |
|
124 |
|
125 /** |
|
126 * BaseLdapServer constructor with given server socket and specify whether |
116 * BaseLdapServer constructor with given server socket and specify whether |
127 * use daemon for each accept connection handling thread. |
117 * use daemon for each accept connection handling thread. |
128 * |
118 * |
129 * @param serverSocket given server socket |
119 * @param serverSocket given server socket |
130 * @param useDaemonThread <tt>true</tt> if use daemon thread |
120 */ |
131 */ |
121 public BaseLdapServer(ServerSocket serverSocket) { |
132 public BaseLdapServer(ServerSocket serverSocket, boolean useDaemonThread) { |
|
133 this.serverSocket = Objects.requireNonNull(serverSocket); |
122 this.serverSocket = Objects.requireNonNull(serverSocket); |
134 this.useDaemonThread = useDaemonThread; |
123 workingPool = Executors.newCachedThreadPool(); |
135 if (useDaemonThread) { |
|
136 workingPool = Executors |
|
137 .newCachedThreadPool(new DefaultDaemonThreadFactory()); |
|
138 } else { |
|
139 workingPool = Executors.newCachedThreadPool(); |
|
140 } |
|
141 try { |
124 try { |
142 stackWalker = StackWalker.getInstance(RETAIN_CLASS_REFERENCE); |
125 stackWalker = StackWalker.getInstance(RETAIN_CLASS_REFERENCE); |
143 } catch (SecurityException se) { |
126 } catch (SecurityException se) { |
144 // just ignore |
127 // just ignore |
145 } |
128 } |
199 public void stopServer() { |
182 public void stopServer() { |
200 debug("INFO: Stopping Server."); |
183 debug("INFO: Stopping Server."); |
201 isRunning = false; |
184 isRunning = false; |
202 workingPool.shutdown(); |
185 workingPool.shutdown(); |
203 cleanupClosableRes(serverSocket); |
186 cleanupClosableRes(serverSocket); |
204 if (!useDaemonThread) { |
187 // let's cleanup thread pool |
205 // let's cleanup thread pool |
188 synchronized (socketList) { |
206 synchronized (socketList) { |
189 socketList.forEach(BaseLdapServer::cleanupClosableRes); |
207 socketList.forEach(BaseLdapServer::cleanupClosableRes); |
190 } |
208 } |
191 try { |
209 try { |
192 if (!workingPool.awaitTermination(10, TimeUnit.SECONDS)) { |
210 if (!workingPool.awaitTermination(10, TimeUnit.SECONDS)) { |
|
211 workingPool.shutdownNow(); |
|
212 } |
|
213 } catch (InterruptedException e) { |
|
214 workingPool.shutdownNow(); |
193 workingPool.shutdownNow(); |
215 Thread.currentThread().interrupt(); |
194 } |
216 } |
195 } catch (InterruptedException e) { |
|
196 workingPool.shutdownNow(); |
|
197 Thread.currentThread().interrupt(); |
217 } |
198 } |
218 } |
199 } |
219 |
200 |
220 /** |
201 /** |
221 * Return local port which server is listening. |
202 * Return local port which server is listening. |