Example usage for java.net ConnectException ConnectException

List of usage examples for java.net ConnectException ConnectException

Introduction

In this page you can find the example usage for java.net ConnectException ConnectException.

Prototype

public ConnectException(String msg) 

Source Link

Document

Constructs a new ConnectException with the specified detail message as to why the connect error occurred.

Usage

From source file:org.jboss.processFlow.knowledgeService.BaseKnowledgeSessionBean.java

public String getAllProcessesInPackage(String pkgName) throws ConnectException {
    if (!guvnorUtils.guvnorExists()) {
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append(guvnorUtils.getGuvnorProtocol());
        sBuilder.append("://");
        sBuilder.append(guvnorUtils.getGuvnorHost());
        sBuilder.append("/");
        sBuilder.append(guvnorUtils.getGuvnorSubdomain());
        sBuilder.append("/rest/packages/");
        throw new ConnectException(
                "createKnowledgeBase() cannot connect to guvnor at URL : " + sBuilder.toString());
    }/*from  w w w .  java  2 s .  co  m*/
    List<String> processes = guvnorUtils.getAllProcessesInPackage(pkgName);
    StringBuilder sBuilder = new StringBuilder("getAllProcessesInPackage() pkgName = " + pkgName);
    if (processes.isEmpty()) {
        sBuilder.append("\n\n\t :  not processes found");
        return sBuilder.toString();
    }
    for (String pDef : processes) {
        sBuilder.append("\n\t");
        sBuilder.append(pDef);
    }
    return sBuilder.toString();
}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.LDAPUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#modifyUserSuspension(java.lang.String, boolean)
 *//*from w w w.j  ava 2  s.  c  o m*/
public synchronized boolean modifyUserSuspension(final String userId, final boolean isSuspended)
        throws UserManagementException {
    final String methodName = LDAPUserManager.CNAME
            + "#modifyUserSuspension(final String userDN, final boolean isSuspended) throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", userId);
        DEBUGGER.debug("Value: {}", isSuspended);
    }

    boolean isComplete = false;
    LDAPConnection ldapConn = null;
    LDAPConnectionPool ldapPool = null;

    try {
        ldapPool = (LDAPConnectionPool) svcBean.getAuthDataSource();

        if (DEBUG) {
            DEBUGGER.debug("LDAPConnectionPool: {}", ldapPool);
        }

        if (ldapPool.isClosed()) {
            throw new ConnectException("Failed to create LDAP connection using the specified information");
        }

        ldapConn = ldapPool.getConnection();

        if (DEBUG) {
            DEBUGGER.debug("LDAPConnection: {}", ldapConn);
        }

        if (!(ldapConn.isConnected())) {
            throw new ConnectException("Failed to create LDAP connection using the specified information");
        }

        List<Modification> modifyList = new ArrayList<Modification>(Arrays.asList(new Modification(
                ModificationType.REPLACE, securityAttributes.getIsSuspended(), String.valueOf(isSuspended))));

        if (DEBUG) {
            DEBUGGER.debug("modifyList: {}", modifyList);
        }

        LDAPResult ldapResult = ldapConn.modify(
                new ModifyRequest(new StringBuilder().append(userAttributes.getUserId() + "=" + userId + ",")
                        .append(repoConfig.getRepositoryUserBase() + "," + repoConfig.getRepositoryBaseDN())
                        .toString(), modifyList));

        if (DEBUG) {
            DEBUGGER.debug("LDAPResult: {}", ldapResult);
        }

        if (ldapResult.getResultCode() == ResultCode.SUCCESS) {
            isComplete = true;
        }
    } catch (LDAPException lx) {
        throw new UserManagementException(lx.getMessage(), lx);
    } catch (ConnectException cx) {
        throw new UserManagementException(cx.getMessage(), cx);
    } finally {
        if ((ldapPool != null) && ((ldapConn != null) && (ldapConn.isConnected()))) {
            ldapConn.close();
            ldapPool.releaseConnection(ldapConn);
        }
    }

    return isComplete;
}

From source file:ipc.Client.java

/**
 * Take an IOException and the address we were trying to connect to
 * and return an IOException with the input exception as the cause.
 * The new exception provides the stack trace of the place where 
 * the exception is thrown and some extra diagnostics information.
 * If the exception is ConnectException or SocketTimeoutException, 
 * return a new one of the same type; Otherwise return an IOException.
 * /*from  w  w  w.  j a  v  a2  s . c o  m*/
 * @param addr target address
 * @param exception the relevant exception
 * @return an exception to throw
 */
private IOException wrapException(InetSocketAddress addr, IOException exception) {
    if (exception instanceof ConnectException) {
        //connection refused; include the host:port in the error
        return (ConnectException) new ConnectException(
                "Call to " + addr + " failed on connection exception: " + exception).initCause(exception);
    } else if (exception instanceof SocketTimeoutException) {
        return (SocketTimeoutException) new SocketTimeoutException(
                "Call to " + addr + " failed on socket timeout exception: " + exception).initCause(exception);
    } else {
        return (IOException) new IOException("Call to " + addr + " failed on local exception: " + exception)
                .initCause(exception);

    }
}

From source file:edu.si.services.beans.edansidora.UCT_EdanSidoraTest.java

@Test
public void edanIdsExceptionTest() throws Exception {
    String testManifestXML = readFileToString(testManifest);
    String testDatastreamsXML = readFileToString(
            new File(KARAF_HOME + "/test-data/fedoraObjectDatastreams.xml"));
    String testFusekiObjectFoundXML = readFileToString(
            new File(KARAF_HOME + "/test-data/objectFoundFusekiResponse.xml"));
    Integer minEdanRedelivery = Integer.valueOf(getExtra().getProperty("min.edan.redeliveries"));

    MockEndpoint mockResult = getMockEndpoint("mock:result");
    mockResult.expectedMessageCount(0);//from  ww  w . j  av  a2s  . c om
    mockResult.expectedHeaderReceived("redeliveryCount", minEdanRedelivery);

    MockEndpoint mockError = getMockEndpoint("mock:error");
    mockError.expectedMessageCount(1);
    mockError.message(0).exchangeProperty(Exchange.EXCEPTION_CAUGHT).isInstanceOf(EdanIdsException.class);
    mockError.expectedHeaderReceived("redeliveryCount", minEdanRedelivery);

    context.getRouteDefinition("edanUpdate").adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            weaveById("processFedoraGetDatastreams").replace().setBody().simple(testDatastreamsXML);
            weaveById("processFedoraFindParentObject").replace().setHeader("parentPid").simple("test:0001");
            weaveById("processFedoraGetManifestDatastream").replace().setBody().simple(testManifestXML);
        }
    });

    context.getRouteDefinition("edanHttpRequest").adviceWith(context, new AdviceWithRouteBuilder() {
        @Override
        public void configure() throws Exception {
            //processor used to replace sql query to test onException and retries
            final Processor processor = new Processor() {
                public void process(Exchange exchange) throws Exception {
                    Message in = exchange.getIn();
                    in.setHeader("redeliveryCount", in.getHeader(Exchange.REDELIVERY_COUNTER, Integer.class));
                    try {
                        if (in.getHeader("redeliveryCount", Integer.class) == minEdanRedelivery) {
                            throw new IOException("Outer try exception");
                        }
                        try {
                            throw new ConnectException("Inner Try Exception");
                        } catch (Exception e) {
                            throw new EdanIdsException("Simulated EdanApiBean Error!!!", e);
                        }
                    } catch (Exception e) {
                        throw new EdanIdsException(e);
                    }
                }
            };

            weaveById("edanApiSendRequest").replace().process(processor);
            weaveById("logEdanIdsException").after().to("mock:error");
            weaveAddLast().to("mock:result").stop();
        }
    });

    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setHeader("ManifestXML", readFileToString(testManifest));
    exchange.getIn().setHeader("imageid", "testRaccoonAndFox");

    template.send("direct:edanUpdate", exchange);

    assertMockEndpointsSatisfied();

}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.LDAPUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#modifyUserGroups(java.lang.String, java.lang.Object[])
 *//*w w w  .  j a  v  a2s  .c  o m*/
public synchronized boolean modifyUserGroups(final String userId, final Object[] value)
        throws UserManagementException {
    final String methodName = LDAPUserManager.CNAME
            + "#modifyUserGroups(final String userId, final Object[] value) throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("userId: {}", userId);
        DEBUGGER.debug("userGuid: {}", value);
    }

    boolean isComplete = false;
    LDAPConnection ldapConn = null;
    LDAPConnectionPool ldapPool = null;

    final StringBuilder userDN = new StringBuilder().append(userAttributes.getUserId() + "=" + userId + ",")
            .append(repoConfig.getRepositoryUserBase() + "," + repoConfig.getRepositoryBaseDN());

    try {
        ldapPool = (LDAPConnectionPool) svcBean.getAuthDataSource();

        if (DEBUG) {
            DEBUGGER.debug("LDAPConnectionPool: {}", ldapPool);
        }

        if (ldapPool.isClosed()) {
            throw new ConnectException("Failed to create LDAP connection using the specified information");
        }

        ldapConn = ldapPool.getConnection();

        if (DEBUG) {
            DEBUGGER.debug("LDAPConnection: {}", ldapConn);
        }

        if (!(ldapConn.isConnected())) {
            throw new ConnectException("Failed to create LDAP connection using the specified information");
        }

        for (Object group : value) {
            if (DEBUG) {
                DEBUGGER.debug("Group: {}", group);
            }

            StringBuilder roleDN = new StringBuilder()
                    .append(userAttributes.getCommonName() + "=" + (String) group)
                    .append(repoConfig.getRepositoryRoleBase());

            if (DEBUG) {
                DEBUGGER.debug("StringBuilder: {}", roleDN);
            }

            AddRequest addRequest = new AddRequest(roleDN.toString(),
                    new ArrayList<Attribute>(Arrays.asList(new Attribute("objectClass", "uniqueMember"),
                            new Attribute("uniqueMember", userDN.toString()))));

            if (DEBUG) {
                DEBUGGER.debug("AddRequest: {}", addRequest);
            }

            LDAPResult ldapResult = ldapConn.add(addRequest);

            if (ldapResult.getResultCode() == ResultCode.SUCCESS) {
                isComplete = true;
            }
        }
    } catch (LDAPException lx) {
        throw new UserManagementException(lx.getMessage(), lx);
    } catch (ConnectException cx) {
        throw new UserManagementException(cx.getMessage(), cx);
    } finally {
        if ((ldapPool != null) && ((ldapConn != null) && (ldapConn.isConnected()))) {
            ldapConn.close();
            ldapPool.releaseConnection(ldapConn);
        }
    }

    return isComplete;
}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.LDAPUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#modifyOlrLock(java.lang.String, boolean)
 *//*from   www  .ja va 2s. co m*/
public synchronized boolean modifyOlrLock(final String userId, final boolean isLocked)
        throws UserManagementException {
    final String methodName = LDAPUserManager.CNAME
            + "#modifyOlrLock(final String userId, final boolean isLocked) throws UserManagementException";

    if (DEBUG) {

        DEBUGGER.debug(methodName);
        DEBUGGER.debug("userId: {}", userId);
        DEBUGGER.debug("Value: {}", isLocked);
    }

    boolean isComplete = false;
    LDAPConnection ldapConn = null;
    LDAPConnectionPool ldapPool = null;

    try {
        ldapPool = (LDAPConnectionPool) svcBean.getAuthDataSource();

        if (DEBUG) {
            DEBUGGER.debug("LDAPConnectionPool: {}", ldapPool);
        }

        if (ldapPool.isClosed()) {
            throw new ConnectException("Failed to create LDAP connection using the specified information");
        }

        ldapConn = ldapPool.getConnection();

        if (DEBUG) {
            DEBUGGER.debug("LDAPConnection: {}", ldapConn);
        }

        if (!(ldapConn.isConnected())) {
            throw new ConnectException("Failed to create LDAP connection using the specified information");
        }

        List<Modification> modifyList = new ArrayList<Modification>(Arrays.asList(new Modification(
                ModificationType.REPLACE, securityAttributes.getOlrLocked(), String.valueOf(isLocked))));

        LDAPResult ldapResult = ldapConn.modify(
                new ModifyRequest(new StringBuilder().append(userAttributes.getUserId() + "=" + userId + ",")
                        .append(repoConfig.getRepositoryUserBase() + "," + repoConfig.getRepositoryBaseDN())
                        .toString(), modifyList));

        if (DEBUG) {
            DEBUGGER.debug("LDAPResult: {}", ldapResult);
        }

        isComplete = (ldapResult.getResultCode() == ResultCode.SUCCESS);

        if (DEBUG) {
            DEBUGGER.debug("isComplete: {}", isComplete);
        }
    } catch (LDAPException lx) {
        throw new UserManagementException(lx.getMessage(), lx);
    } catch (ConnectException cx) {
        throw new UserManagementException(cx.getMessage(), cx);
    } finally {
        if ((ldapPool != null) && ((ldapConn != null) && (ldapConn.isConnected()))) {
            ldapConn.close();
            ldapPool.releaseConnection(ldapConn);
        }
    }

    return isComplete;
}

From source file:com.cws.esolutions.core.utils.NetworkUtils.java

/**
 * Creates an telnet connection to a target host and port number. Silently
 * succeeds if no issues are encountered, if so, exceptions are logged and
 * re-thrown back to the requestor./*w  w  w.jav  a2 s  .  com*/
 *
 * If an exception is thrown during the <code>socket.close()</code> operation,
 * it is logged but NOT re-thrown. It's not re-thrown because it does not indicate
 * a connection failure (indeed, it means the connection succeeded) but it is
 * logged because continued failures to close the socket could result in target
 * system instability.
 * 
 * @param hostName - The target host to make the connection to
 * @param portNumber - The port number to attempt the connection on
 * @param timeout - How long to wait for a connection to establish or a response from the target
 * @param object - The serializable object to send to the target
 * @return <code>Object</code> as output from the request
 * @throws UtilityException {@link com.cws.esolutions.core.utils.exception.UtilityException} if an error occurs processing
 */
public static final synchronized Object executeTcpRequest(final String hostName, final int portNumber,
        final int timeout, final Object object) throws UtilityException {
    final String methodName = NetworkUtils.CNAME
            + "#executeTcpRequest(final String hostName, final int portNumber, final int timeout, final Object object) throws UtilityException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug(hostName);
        DEBUGGER.debug("portNumber: {}", portNumber);
        DEBUGGER.debug("timeout: {}", timeout);
        DEBUGGER.debug("object: {}", object);
    }

    Socket socket = null;
    Object resObject = null;

    try {
        synchronized (new Object()) {
            if (StringUtils.isEmpty(InetAddress.getByName(hostName).toString())) {
                throw new UnknownHostException("No host was found in DNS for the given name: " + hostName);
            }

            InetSocketAddress socketAddress = new InetSocketAddress(hostName, portNumber);

            socket = new Socket();
            socket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(timeout));
            socket.setSoLinger(false, 0);
            socket.setKeepAlive(false);
            socket.connect(socketAddress, (int) TimeUnit.SECONDS.toMillis(timeout));

            if (!(socket.isConnected())) {
                throw new ConnectException("Failed to connect to host " + hostName + " on port " + portNumber);
            }

            ObjectOutputStream objectOut = new ObjectOutputStream(socket.getOutputStream());

            if (DEBUG) {
                DEBUGGER.debug("ObjectOutputStream: {}", objectOut);
            }

            objectOut.writeObject(object);

            resObject = new ObjectInputStream(socket.getInputStream()).readObject();

            if (DEBUG) {
                DEBUGGER.debug("resObject: {}", resObject);
            }

            PrintWriter pWriter = new PrintWriter(socket.getOutputStream(), true);

            pWriter.println(NetworkUtils.TERMINATE_TELNET + NetworkUtils.CRLF);

            pWriter.flush();
            pWriter.close();
        }
    } catch (ConnectException cx) {
        throw new UtilityException(cx.getMessage(), cx);
    } catch (UnknownHostException ux) {
        throw new UtilityException(ux.getMessage(), ux);
    } catch (SocketException sx) {
        throw new UtilityException(sx.getMessage(), sx);
    } catch (IOException iox) {
        throw new UtilityException(iox.getMessage(), iox);
    } catch (ClassNotFoundException cnfx) {
        throw new UtilityException(cnfx.getMessage(), cnfx);
    } finally {
        try {
            if ((socket != null) && (!(socket.isClosed()))) {
                socket.close();
            }
        } catch (IOException iox) {
            // log it - this could cause problems later on
            ERROR_RECORDER.error(iox.getMessage(), iox);
        }
    }

    return resObject;
}

From source file:com.hortonworks.hbase.replication.bridge.HBaseClient.java

/**
 * Take an IOException and the address we were trying to connect to
 * and return an IOException with the input exception as the cause.
 * The new exception provides the stack trace of the place where
 * the exception is thrown and some extra diagnostics information.
 * If the exception is ConnectException or SocketTimeoutException,
 * return a new one of the same type; Otherwise return an IOException.
 *
 * @param addr target address//from   ww  w.j  a  v a 2 s.c om
 * @param exception the relevant exception
 * @return an exception to throw
 */
@SuppressWarnings({ "ThrowableInstanceNeverThrown" })
protected IOException wrapException(InetSocketAddress addr, IOException exception) {
    if (exception instanceof ConnectException) {
        //connection refused; include the host:port in the error
        return (ConnectException) new ConnectException(
                "Call to " + addr + " failed on connection exception: " + exception).initCause(exception);
    } else if (exception instanceof SocketTimeoutException) {
        return (SocketTimeoutException) new SocketTimeoutException(
                "Call to " + addr + " failed on socket timeout exception: " + exception).initCause(exception);
    } else {
        return (IOException) new IOException("Call to " + addr + " failed on local exception: " + exception)
                .initCause(exception);

    }
}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.LDAPUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#modifyUserLock(java.lang.String, boolean, int)
 *//* ww  w.  j a  v  a2  s.c  om*/
public synchronized boolean modifyUserLock(final String userId, final boolean isLocked, final int increment)
        throws UserManagementException {
    final String methodName = LDAPUserManager.CNAME
            + "#modifyUserLock(final String userId, final boolean isLocked, final int increment) throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("userId: {}", userId);
        DEBUGGER.debug("Value: {}", isLocked);
        DEBUGGER.debug("Value: {}", increment);
    }

    boolean isComplete = false;
    LDAPConnection ldapConn = null;
    LDAPConnectionPool ldapPool = null;

    try {
        ldapPool = (LDAPConnectionPool) svcBean.getAuthDataSource();

        if (DEBUG) {
            DEBUGGER.debug("LDAPConnectionPool: {}", ldapPool);
        }

        if (ldapPool.isClosed()) {
            throw new ConnectException("Failed to create LDAP connection using the specified information");
        }

        ldapConn = ldapPool.getConnection();

        if (DEBUG) {
            DEBUGGER.debug("LDAPConnection: {}", ldapConn);
        }

        if (!(ldapConn.isConnected())) {
            throw new ConnectException("Failed to create LDAP connection using the specified information");
        }

        List<Modification> modifyList = new ArrayList<Modification>();

        if (isLocked) {
            modifyList.add(new Modification(ModificationType.REPLACE, securityAttributes.getLockCount(), "3"));
        } else {
            modifyList.add(new Modification(ModificationType.REPLACE, securityAttributes.getLockCount(),
                    String.valueOf(increment)));
        }

        LDAPResult ldapResult = ldapConn.modify(
                new ModifyRequest(new StringBuilder().append(userAttributes.getUserId() + "=" + userId + ",")
                        .append(repoConfig.getRepositoryUserBase() + "," + repoConfig.getRepositoryBaseDN())
                        .toString(), modifyList));

        if (DEBUG) {
            DEBUGGER.debug("LDAPResult: {}", ldapResult);
        }

        isComplete = (ldapResult.getResultCode() == ResultCode.SUCCESS);

        if (DEBUG) {
            DEBUGGER.debug("isComplete: {}", isComplete);
        }
    } catch (LDAPException lx) {
        throw new UserManagementException(lx.getMessage(), lx);
    } catch (ConnectException cx) {
        throw new UserManagementException(cx.getMessage(), cx);
    } finally {
        if ((ldapPool != null) && ((ldapConn != null) && (ldapConn.isConnected()))) {
            ldapConn.close();
            ldapPool.releaseConnection(ldapConn);
        }
    }

    return isComplete;
}

From source file:com.cws.esolutions.security.dao.usermgmt.impl.LDAPUserManager.java

/**
 * @see com.cws.esolutions.security.dao.usermgmt.interfaces.UserManager#modifyUserPassword(java.lang.String, java.lang.String)
 *///from www. ja  va  2 s .c o m
public synchronized boolean modifyUserPassword(final String userId, final String newPass)
        throws UserManagementException {
    final String methodName = LDAPUserManager.CNAME
            + "#modifyUserPassword(final String userId, final String newPass) throws UserManagementException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", userId);
    }

    boolean isComplete = false;
    LDAPConnection ldapConn = null;
    LDAPConnectionPool ldapPool = null;

    try {
        ldapPool = (LDAPConnectionPool) svcBean.getAuthDataSource();

        if (DEBUG) {
            DEBUGGER.debug("LDAPConnectionPool: {}", ldapPool);
        }

        if (ldapPool.isClosed()) {
            throw new ConnectException("Failed to create LDAP connection using the specified information");
        }

        ldapConn = ldapPool.getConnection();

        if (DEBUG) {
            DEBUGGER.debug("LDAPConnection: {}", ldapConn);
        }

        if (!(ldapConn.isConnected())) {
            throw new ConnectException("Failed to create LDAP connection using the specified information");
        }

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, secConfig.getPasswordExpiration());

        List<Modification> modifyList = new ArrayList<Modification>(Arrays.asList(
                new Modification(ModificationType.REPLACE, securityAttributes.getUserPassword(), newPass),
                new Modification(ModificationType.REPLACE, securityAttributes.getExpiryDate(),
                        String.valueOf(cal.getTime()))));

        LDAPResult ldapResult = ldapConn.modify(
                new ModifyRequest(new StringBuilder().append(userAttributes.getUserId() + "=" + userId + ",")
                        .append(repoConfig.getRepositoryUserBase() + "," + repoConfig.getRepositoryBaseDN())
                        .toString(), modifyList));

        if (DEBUG) {
            DEBUGGER.debug("LDAPResult: {}", ldapResult);
        }

        isComplete = (ldapResult.getResultCode() == ResultCode.SUCCESS);

        if (DEBUG) {
            DEBUGGER.debug("isComplete: {}", isComplete);
        }
    } catch (LDAPException lx) {
        throw new UserManagementException(lx.getMessage(), lx);
    } catch (ConnectException cx) {
        throw new UserManagementException(cx.getMessage(), cx);
    } finally {
        if ((ldapPool != null) && ((ldapConn != null) && (ldapConn.isConnected()))) {
            ldapConn.close();
            ldapPool.releaseConnection(ldapConn);
        }
    }

    return isComplete;
}