Example usage for org.eclipse.jdt.core IJavaModelStatusConstants CANNOT_RETRIEVE_ATTACHED_JAVADOC_TIMEOUT

List of usage examples for org.eclipse.jdt.core IJavaModelStatusConstants CANNOT_RETRIEVE_ATTACHED_JAVADOC_TIMEOUT

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaModelStatusConstants CANNOT_RETRIEVE_ATTACHED_JAVADOC_TIMEOUT.

Prototype

int CANNOT_RETRIEVE_ATTACHED_JAVADOC_TIMEOUT

To view the source code for org.eclipse.jdt.core IJavaModelStatusConstants CANNOT_RETRIEVE_ATTACHED_JAVADOC_TIMEOUT.

Click Source Link

Document

Status constant indicating that the attached javadoc content cannot be retrieved due to timeout

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.JavaElement.java

License:Open Source License

protected String getURLContents(URL baseLoc, String docUrlValue) throws JavaModelException {
    InputStream stream = null;//from w w w .j  ava  2s. c  om
    JarURLConnection connection2 = null;
    try {
        URL docUrl = new URL(docUrlValue);
        URLConnection connection = docUrl.openConnection();
        Class[] parameterTypes = new Class[] { int.class };
        Integer timeoutVal = new Integer(10000);
        // set the connect and read timeouts using reflection since these methods are not available in java 1.4
        Class URLClass = connection.getClass();
        try {
            Method connectTimeoutMethod = URLClass.getDeclaredMethod("setConnectTimeout", parameterTypes); //$NON-NLS-1$
            Method readTimeoutMethod = URLClass.getDeclaredMethod("setReadTimeout", parameterTypes); //$NON-NLS-1$
            connectTimeoutMethod.invoke(connection, new Object[] { timeoutVal });
            readTimeoutMethod.invoke(connection, new Object[] { timeoutVal });
        } catch (SecurityException e) {
            // ignore
        } catch (IllegalArgumentException e) {
            // ignore
        } catch (NoSuchMethodException e) {
            // ignore
        } catch (IllegalAccessException e) {
            // ignore
        } catch (InvocationTargetException e) {
            // ignore
        }

        if (connection instanceof JarURLConnection) {
            connection2 = (JarURLConnection) connection;
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=156307
            connection.setUseCaches(false);
        }
        try {
            stream = new BufferedInputStream(connection.getInputStream());
        } catch (IllegalArgumentException e) {
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=304316
            return null;
        } catch (NullPointerException e) {
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=304316
            return null;
        }
        String encoding = connection.getContentEncoding();
        byte[] contents = org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsByteArray(stream,
                connection.getContentLength());
        if (encoding == null) {
            int index = getIndexOf(contents, META_START, 0, -1);
            if (index != -1) {
                int end = getIndexOf(contents, META_END, index, -1);
                if (end != -1) {
                    if ((end + 1) <= contents.length)
                        end++;
                    int charsetIndex = getIndexOf(contents, CHARSET_HTML5, index, end);
                    if (charsetIndex == -1) {
                        charsetIndex = getIndexOf(contents, CHARSET, index, end);
                        if (charsetIndex != -1)
                            charsetIndex = charsetIndex + CHARSET.length;
                    } else {
                        charsetIndex = charsetIndex + CHARSET_HTML5.length;
                    }
                    if (charsetIndex != -1) {
                        end = getIndexOf(contents, CLOSING_DOUBLE_QUOTE, charsetIndex, end);
                        encoding = new String(contents, charsetIndex, end - charsetIndex,
                                org.eclipse.jdt.internal.compiler.util.Util.UTF_8);
                    }
                }
            }
        }
        try {
            if (encoding == null) {
                encoding = getJavaProject().getProject().getDefaultCharset();
            }
        } catch (CoreException e) {
            // ignore
        }
        if (contents != null) {
            if (encoding != null) {
                return new String(contents, encoding);
            } else {
                // platform encoding is used
                return new String(contents);
            }
        }
    } catch (SocketTimeoutException e) {
        throw new JavaModelException(
                new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC_TIMEOUT, this));
    } catch (MalformedURLException e) {
        throw new JavaModelException(
                new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC, this));
    } catch (FileNotFoundException e) {
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=403154
        validateAndCache(baseLoc, e);
    } catch (SocketException e) {
        // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=247845 &
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=400060
        throw new JavaModelException(e, IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC);
    } catch (UnknownHostException e) {
        // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=247845 &
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=400060
        throw new JavaModelException(e, IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC);
    } catch (ProtocolException e) {
        // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=247845 &
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=400060
        throw new JavaModelException(e, IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC);
    } catch (IOException e) {
        throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // ignore
            }
        }
        if (connection2 != null) {
            try {
                connection2.getJarFile().close();
            } catch (IOException e) {
                // ignore
            } catch (IllegalStateException e) {
                /*
                * ignore. Can happen in case the stream.close() did close the jar file
                * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=140750
                */
            }
        }
    }
    return null;
}

From source file:com.codenvy.ide.ext.java.server.javadoc.JavaDocLocations.java

License:Open Source License

/**
 * Handles the exception thrown from JDT Core when the attached Javadoc
 * cannot be retrieved due to accessibility issues or location URL issue. This exception is not
 * logged but the exceptions occurred due to other reasons are logged.
 *
 * @param e the exception thrown when retrieving the Javadoc fails
 * @return the String message for why the Javadoc could not be retrieved
 * @since 3.9/*ww w  . j  a v  a  2  s . c o  m*/
 */
public static String handleFailedJavadocFetch(CoreException e) {
    IStatus status = e.getStatus();
    if (JavaCore.PLUGIN_ID.equals(status.getPlugin())) {
        Throwable cause = e.getCause();
        int code = status.getCode();
        // See bug 120559, bug 400060 and bug 400062
        if (code == IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC_TIMEOUT
                || (code == IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC
                        && (cause instanceof FileNotFoundException || cause instanceof SocketException
                                || cause instanceof UnknownHostException
                                || cause instanceof ProtocolException)))
            return CorextMessages.JavaDocLocations_error_gettingAttachedJavadoc;
    }
    LOG.error(e.getMessage(), e);
    return CorextMessages.JavaDocLocations_error_gettingJavadoc;
}