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.net.SocketAddress;
22
 
23
import org.apache.mina.core.future.ConnectFuture;
24
import org.apache.mina.core.future.IoFutureListener;
25
import org.apache.mina.core.service.IoConnector;
26
import org.apache.mina.core.service.IoHandler;
27
import org.apache.mina.core.service.IoProcessor;
28
import org.apache.mina.core.session.IoSessionConfig;
29
import org.apache.mina.transport.socket.nio.NioSession;
30
import org.apache.mina.transport.socket.nio.NioSocketConnector;
31
import org.apache.sshd.common.FactoryManager;
32
import org.apache.sshd.common.future.DefaultSshFuture;
33
import org.apache.sshd.common.io.IoConnectFuture;
34
 
35
/**
36
 */
37
public class MinaConnector extends MinaService implements org.apache.sshd.common.io.IoConnector, IoHandler {
38
 
39
    protected volatile IoConnector connector;
40
    protected IoSessionConfig sessionConfig;
41
 
42
    public MinaConnector(FactoryManager manager, org.apache.sshd.common.io.IoHandler handler, IoProcessor<NioSession> ioProcessor) {
43
        super(manager, handler, ioProcessor);
44
    }
45
 
46
    protected IoConnector createConnector() {
47
        NioSocketConnector connector = new NioSocketConnector(ioProcessor);
48
        if (sessionConfig != null) {
49
            connector.getSessionConfig().setAll(sessionConfig);
50
        }
51
        return connector;
52
    }
53
 
54
    protected IoConnector getConnector() {
55
        if (connector == null) {
56
            synchronized (this) {
57
                if (connector == null) {
58
                    connector = createConnector();
59
                    connector.setHandler(this);
60
                }
61
            }
62
        }
63
        return connector;
64
    }
65
 
66
    @Override
67
    protected org.apache.mina.core.service.IoService getIoService() {
68
        return getConnector();
69
    }
70
 
71
    public IoConnectFuture connect(SocketAddress address) {
72
        class Future extends DefaultSshFuture<IoConnectFuture> implements IoConnectFuture {
73
            Future(Object lock) {
74
                super(lock);
75
            }
76
 
77
            public org.apache.sshd.common.io.IoSession getSession() {
78
                Object v = getValue();
79
                return v instanceof org.apache.sshd.common.io.IoSession ? (org.apache.sshd.common.io.IoSession) v : null;
80
            }
81
 
82
            public Throwable getException() {
83
                Object v = getValue();
84
                return v instanceof Throwable ? (Throwable) v : null;
85
            }
86
 
87
            public boolean isConnected() {
88
                return getValue() instanceof org.apache.sshd.common.io.IoSession;
89
            }
90
 
91
            public void setSession(org.apache.sshd.common.io.IoSession session) {
92
                setValue(session);
93
            }
94
 
95
            public void setException(Throwable exception) {
96
                setValue(exception);
97
            }
98
        }
99
        final IoConnectFuture future = new Future(null);
100
        getConnector().connect(address).addListener(new IoFutureListener<ConnectFuture>() {
101
            public void operationComplete(ConnectFuture cf) {
102
                if (cf.getException() != null) {
103
                    future.setException(cf.getException());
104
                } else if (cf.isCanceled()) {
105
                    future.cancel();
106
                } else {
107
                    future.setSession(getSession(cf.getSession()));
108
                }
109
            }
110
        });
111
        return future;
112
    }
113
 
114
}