Example usage for java.net UnknownHostException getCause

List of usage examples for java.net UnknownHostException getCause

Introduction

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

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

From source file:flex.helpers.NTPHelper.java

/**
 * Solicitar hora y fecha a servidor NTP.
 * //from w ww.  j  a  v a  2  s .co  m
 * @param ntpServer Direccin ip del servidor NTP
 * @return
 * @throws NTPHelperException 
 */
public static Date getDateTime(String ntpServer) throws NTPHelperException {
    if (ntpServer == null)
        throw new NTPHelperException(NTPHelperException.ERROR_NTP_EMPTY);
    if (ntpServer.isEmpty())
        throw new NTPHelperException(NTPHelperException.ERROR_NTP_EMPTY);

    Date time = null;
    NTPUDPClient client = new NTPUDPClient();
    client.setDefaultTimeout(NTP_TIMEOUT);

    try {
        client.open();
        InetAddress hostAddr = InetAddress.getByName(ntpServer);
        TimeInfo info = client.getTime(hostAddr);
        time = processResponse(info);

    } catch (UnknownHostException ex) {
        throw new NTPHelperException(NTPHelperException.ERROR_NTP_UNKNOWN_HOST, ex.getCause());

    } catch (SocketException ex) {
        throw new NTPHelperException(NTPHelperException.ERROR_NTP_SOCKET, ex.getCause());

    } catch (IOException ex) {
        throw new NTPHelperException(NTPHelperException.ERROR_NTP_READ, ex.getCause());

    } finally {
        client.close();
        System.gc();
    }

    return time;
}

From source file:onl.netfishers.netshot.RestService.java

/**
 * Sets the domain.//from w w w .jav a 2s .c o m
 *
 * @param request the request
 * @param id the id
 * @param rsDomain the rs domain
 * @return the rs domain
 * @throws WebApplicationException the web application exception
 */
@PUT
@Path("domains/{id}")
@RolesAllowed("admin")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public RsDomain setDomain(@PathParam("id") Long id, RsDomain rsDomain) throws WebApplicationException {
    logger.debug("REST request, edit domain {}.", id);
    String name = rsDomain.getName().trim();
    if (name.isEmpty()) {
        logger.warn("User posted an invalid domain name.");
        throw new NetshotBadRequestException("Invalid domain name.",
                NetshotBadRequestException.NETSHOT_INVALID_DOMAIN_NAME);
    }
    String description = rsDomain.getDescription().trim();
    Network4Address v4Address;
    try {
        v4Address = new Network4Address(rsDomain.getIpAddress());
        if (!v4Address.isNormalUnicast()) {
            logger.warn("User posted an invalid IP address");
            throw new NetshotBadRequestException("Invalid IP address",
                    NetshotBadRequestException.NETSHOT_INVALID_IP_ADDRESS);
        }
    } catch (UnknownHostException e) {
        logger.warn("Invalid IP address.", e);
        throw new NetshotBadRequestException("Malformed IP address",
                NetshotBadRequestException.NETSHOT_MALFORMED_IP_ADDRESS);
    }
    Session session = Database.getSession();
    Domain domain;
    try {
        session.beginTransaction();
        domain = (Domain) session.load(Domain.class, id);
        domain.setName(name);
        domain.setDescription(description);
        domain.setServer4Address(v4Address);
        session.update(domain);
        session.getTransaction().commit();
    } catch (ObjectNotFoundException e) {
        session.getTransaction().rollback();
        logger.error("The domain doesn't exist.", e);
        throw new NetshotBadRequestException("The domain doesn't exist.",
                NetshotBadRequestException.NETSHOT_INVALID_DOMAIN);
    } catch (HibernateException e) {
        session.getTransaction().rollback();
        logger.error("Error while editing the domain.", e);
        Throwable t = e.getCause();
        if (t != null && t.getMessage().contains("Duplicate entry")) {
            throw new NetshotBadRequestException("A domain with this name already exists.",
                    NetshotBadRequestException.NETSHOT_DUPLICATE_DOMAIN);
        }
        throw new NetshotBadRequestException("Unable to save the domain... is the name already in use?",
                NetshotBadRequestException.NETSHOT_DATABASE_ACCESS_ERROR);
    } finally {
        session.close();
    }
    return new RsDomain(domain);
}

From source file:org.jboss.tools.discovery.core.internal.connectors.xpl.RemoteExternalBundleDiscoveryStrategy.java

protected Map<File, Entry> loadRegistry(File storageDirectory, IProgressMonitor monitor) throws CoreException {

    // new SubProgressMonitor(monitor, ticksTenPercent * 3);

    final int totalTicks = 100000;
    final int ticksTenPercent = totalTicks / 10;

    monitor.beginTask("Remote discovery", totalTicks);

    Directory directory;/*from   w  w w.  j  a  v  a2  s  .c o  m*/

    try {
        final Directory[] temp = new Directory[1];
        final URI uri = new URI(directoryUrl);
        WebUtil.readResource(uri, new TextContentProcessor() {
            public void process(Reader reader) throws IOException {
                DirectoryParser parser = new DirectoryParser();
                parser.setBaseUri(uri);
                temp[0] = parser.parse(reader);
            }
        }, new SubProgressMonitor(monitor, ticksTenPercent));
        directory = temp[0];
        if (directory == null) {
            throw new IllegalStateException();
        }
    } catch (UnknownHostException e) {
        throw new CoreException(new Status(IStatus.ERROR, DiscoveryCore.ID_PLUGIN,
                NLS.bind(
                        "Cannot access {0}: unknown host: please check your Internet connection and try again.",
                        e.getMessage()),
                e));
    } catch (IOException e) {
        throw new CoreException(new Status(IStatus.ERROR, DiscoveryCore.ID_PLUGIN,
                "IO failure: cannot load discovery directory", e));
    } catch (URISyntaxException e) {
        throw new CoreException(new Status(IStatus.ERROR, DiscoveryCore.ID_PLUGIN,
                "IO failure: cannot load discovery directory", e));
    }
    if (monitor.isCanceled()) {
        return null;
    }
    if (directory.getEntries().isEmpty()) {
        throw new CoreException(
                new Status(IStatus.ERROR, DiscoveryCore.ID_PLUGIN, "Discovery directory is empty"));
    }

    Map<File, Directory.Entry> bundleFileToDirectoryEntry = new HashMap<File, Directory.Entry>();

    ExecutorService executorService = createExecutorService(directory.getEntries().size());
    try {
        List<Future<DownloadBundleJob>> futures = new ArrayList<Future<DownloadBundleJob>>();
        // submit jobs
        for (Directory.Entry entry : directory.getEntries()) {
            futures.add(executorService.submit(new DownloadBundleJob(entry, storageDirectory, monitor)));
        }
        int futureSize = ticksTenPercent * 4 / directory.getEntries().size();
        // collect job results
        for (Future<DownloadBundleJob> job : futures) {
            try {
                DownloadBundleJob bundleJob;
                for (;;) {
                    try {
                        bundleJob = job.get(1L, TimeUnit.SECONDS);
                        break;
                    } catch (TimeoutException e) {
                        if (monitor.isCanceled()) {
                            return null;
                        }
                    }
                }
                if (bundleJob.file != null) {
                    bundleFileToDirectoryEntry.put(bundleJob.file, bundleJob.entry);
                }
                monitor.worked(futureSize);
            } catch (ExecutionException e) {
                Throwable cause = e.getCause();
                if (cause instanceof OperationCanceledException) {
                    monitor.setCanceled(true);
                    return null;
                }
                IStatus status;
                if (cause instanceof CoreException) {
                    status = ((CoreException) cause).getStatus();
                } else {
                    status = new Status(IStatus.ERROR, DiscoveryCore.ID_PLUGIN, "Unexpected error", cause);
                }
                // log errors but continue on
                StatusHandler.log(status);
            } catch (InterruptedException e) {
                monitor.setCanceled(true);
                return null;
            }
        }
    } finally {
        executorService.shutdownNow();
    }
    return bundleFileToDirectoryEntry;
}