Search in sources :

Example 1 with Connect

use of org.openmuc.framework.driver.annotation.Connect in project OpenMUC by isc-konstanz.

the class OpcConnection method connect.

@Connect
public void connect() throws ConnectionException {
    try {
        Path securityTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "security");
        Files.createDirectories(securityTempDir);
        if (!Files.exists(securityTempDir)) {
            throw new ConnectionException("Unable to create security dir: " + securityTempDir);
        }
        logger.debug("Security temp dir: {}", securityTempDir.toAbsolutePath());
        KeyStoreLoader loader = new KeyStoreLoader().load(securityTempDir);
        if (!address.contains("opc.tcp://")) {
            host = address.split(":")[0];
            port = Integer.parseInt(address.split(":")[1]);
            address = "opc.tcp://" + address;
        } else {
            host = address.split("//|:")[1];
            port = Integer.parseInt(address.split("//|:")[2]);
        }
        List<EndpointDescription> endpoints = DiscoveryClient.getEndpoints(address).get();
        EndpointDescription endpoint = endpoints.stream().filter(e -> true).findFirst().orElseThrow(() -> new UaException(StatusCodes.Bad_ConfigurationError, "No endpoint selected"));
        logger.info("OPC Client connecting to {}.", address);
        endpoint = EndpointUtil.updateUrl(endpoints.get(0), host, port);
        OpcUaClientConfigBuilder clientBuilder = new OpcUaClientConfigBuilder().setEndpoint(endpoint).setApplicationName(LocalizedText.english("OpenMUC OPC UA Client")).setApplicationUri("urn:openmuc:client").setCertificate(loader.getClientCertificate()).setKeyPair(loader.getClientKeyPair()).setIdentityProvider(new AnonymousProvider()).setRequestTimeout(uint(5000));
        client = OpcUaClient.create(clientBuilder.build());
        client.connect().get();
        // Get a typed reference to the Server object: ServerNode
        ServerTypeNode serverNode = client.getAddressSpace().getObjectNode(Identifiers.Server, ServerTypeNode.class).get();
        if (namespaceUri != null && !namespaceUri.isEmpty()) {
            try {
                namespaceIndex = Integer.parseInt(namespaceUri);
            } catch (NumberFormatException e) {
                namespaceIndex = Arrays.asList(serverNode.getNamespaceArray().get()).indexOf(namespaceUri);
            }
        }
    } catch (Exception e) {
        logger.error("OPC connection to server failed {}", e);
    }
}
Also used : Path(java.nio.file.Path) OpcUaClientConfigBuilder(org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder) ServerTypeNode(org.eclipse.milo.opcua.sdk.client.model.nodes.objects.ServerTypeNode) UaException(org.eclipse.milo.opcua.stack.core.UaException) AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException) Connect(org.openmuc.framework.driver.annotation.Connect)

Example 2 with Connect

use of org.openmuc.framework.driver.annotation.Connect in project OpenMUC by isc-konstanz.

the class SqlClient method connect.

@Connect
protected void connect() throws ArgumentSyntaxException, ConnectionException {
    logger.info("Initializing SQL connection \"{}\"", url);
    try {
        if (source != null) {
            source.close();
        }
        source = new ComboPooledDataSource();
        source.setDriverClass(driver);
        source.setJdbcUrl(url);
        source.setUser(user);
        source.setPassword(password);
        readTables();
    } catch (Exception e) {
        throw new ConnectionException(e);
    }
}
Also used : ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) SQLException(java.sql.SQLException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) ArgumentSyntaxException(org.openmuc.framework.config.ArgumentSyntaxException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) Connect(org.openmuc.framework.driver.annotation.Connect)

Example 3 with Connect

use of org.openmuc.framework.driver.annotation.Connect in project OpenMUC by isc-konstanz.

the class RestRemote method connect.

@Connect
public void connect() throws ConnectionException {
    if (url.startsWith("https://")) {
        TrustManager[] trustManager = getTrustManager();
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustManager, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (KeyManagementException e) {
            throw new ConnectionException(e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            throw new ConnectionException(e.getMessage());
        }
        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = getHostnameVerifier();
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    }
    logger.info("Connecting to remote OpenMUC: {}", url);
    // This is only used to verify the existence of the remote OpenMUC
    connection = new RestConnection(url, authorization, timeout);
    connection.connect();
}
Also used : SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) ConnectionException(org.openmuc.framework.driver.spi.ConnectionException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) Connect(org.openmuc.framework.driver.annotation.Connect)

Aggregations

Connect (org.openmuc.framework.driver.annotation.Connect)3 ConnectionException (org.openmuc.framework.driver.spi.ConnectionException)3 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 Path (java.nio.file.Path)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 SQLException (java.sql.SQLException)1 ExecutionException (java.util.concurrent.ExecutionException)1 HostnameVerifier (javax.net.ssl.HostnameVerifier)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManager (javax.net.ssl.TrustManager)1 X509TrustManager (javax.net.ssl.X509TrustManager)1 OpcUaClientConfigBuilder (org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfigBuilder)1 AnonymousProvider (org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider)1 ServerTypeNode (org.eclipse.milo.opcua.sdk.client.model.nodes.objects.ServerTypeNode)1 UaException (org.eclipse.milo.opcua.stack.core.UaException)1 EndpointDescription (org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription)1 ArgumentSyntaxException (org.openmuc.framework.config.ArgumentSyntaxException)1