com.hierynomus.smbj.SMBClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smbj Show documentation
Show all versions of smbj Show documentation
SMB2 protocol library for communication with Windows servers
/*
* Copyright (C)2016 - SMBJ Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hierynomus.smbj;
import com.hierynomus.smbj.connection.Connection;
import com.hierynomus.smbj.event.SMBEventBus;
import com.hierynomus.smbj.transport.tcp.DirectTcpTransport;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Server Message Block Client API.
*/
public class SMBClient {
/**
* The default TCP port for SMB
*/
public static final int DEFAULT_PORT = 445;
private Map connectionTable = new ConcurrentHashMap<>();
private Config config;
private SMBEventBus bus;
public SMBClient() {
this(new DefaultConfig());
}
public SMBClient(Config config) {
this.config = config;
bus = new SMBEventBus();
}
/**
* Connect to the host at hostname
on the default port (445)
* @param hostname The hostname to connect to.
* @return An established connection.
* @throws IOException If the connection could not be established.
*/
public Connection connect(String hostname) throws IOException {
return getEstablishedOrConnect(hostname, DEFAULT_PORT);
}
private Connection getEstablishedOrConnect(String hostname, int port) throws IOException {
synchronized (this) {
if (!connectionTable.containsKey(hostname)) {
Connection connection = new Connection(config, new DirectTcpTransport(), bus);
connection.connect(hostname, port);
connectionTable.put(hostname, connection);
return connection;
}
return connectionTable.get(hostname);
}
}
}