Subversion Repositories Programming Utils

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 rm5248 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
package org.apache.sshd.common.io.mina;
20
 
21
import java.util.concurrent.ExecutorService;
22
import java.util.concurrent.TimeUnit;
23
 
24
import org.apache.mina.core.service.IoProcessor;
25
import org.apache.mina.core.service.SimpleIoProcessorPool;
26
import org.apache.mina.transport.socket.nio.NioProcessor;
27
import org.apache.mina.transport.socket.nio.NioSession;
28
import org.apache.sshd.common.FactoryManager;
29
import org.apache.sshd.common.io.IoAcceptor;
30
import org.apache.sshd.common.io.IoConnector;
31
import org.apache.sshd.common.io.IoHandler;
32
import org.apache.sshd.common.io.IoServiceFactory;
33
import org.apache.sshd.common.util.CloseableUtils;
34
import org.apache.sshd.common.util.ThreadUtils;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37
 
38
/**
39
 */
40
public class MinaServiceFactory extends CloseableUtils.AbstractCloseable implements IoServiceFactory {
41
 
42
    private final Logger logger = LoggerFactory.getLogger(MinaServiceFactory.class);
43
    private final FactoryManager manager;
44
    private final ExecutorService executor;
45
    private final IoProcessor<NioSession> ioProcessor;
46
 
47
    public MinaServiceFactory(FactoryManager manager) {
48
        this.manager = manager;
49
        this.executor = ThreadUtils.newCachedThreadPool(manager.toString() + "-mina");
50
        this.ioProcessor = new SimpleIoProcessorPool<NioSession>(NioProcessor.class, getNioWorkers());
51
    }
52
 
53
    public IoConnector createConnector(IoHandler handler) {
54
        return new MinaConnector(manager, handler, ioProcessor);
55
    }
56
 
57
    public IoAcceptor createAcceptor(IoHandler handler) {
58
        return new MinaAcceptor(manager, handler, ioProcessor);
59
    }
60
 
61
    @Override
62
    protected void doCloseImmediately() {
63
        try {
64
            executor.shutdownNow();
65
            executor.awaitTermination(5, TimeUnit.SECONDS);
66
        } catch (Exception e) {
67
            log.debug("Exception caught while closing executor", e);
68
        } finally {
69
            super.doCloseImmediately();
70
        }
71
    }
72
 
73
    public int getNioWorkers() {
74
        String nioWorkers = manager.getProperties().get(FactoryManager.NIO_WORKERS);
75
        if (nioWorkers != null && nioWorkers.length() > 0) {
76
            int nb = Integer.parseInt(nioWorkers);
77
            if (nb > 0) {
78
                return nb;
79
            }
80
        }
81
        return FactoryManager.DEFAULT_NIO_WORKERS;
82
    }
83
 
84
}