All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hierynomus.smbj.SMBClient Maven / Gradle / Ivy

There is a newer version: 0.13.0
Show newest version
/*
 * 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); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy