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

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

Introduction

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

Prototype

int CANNOT_RETRIEVE_ATTACHED_JAVADOC

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

Click Source Link

Document

Status constant indicating that the attached javadoc content cannot be retrieved due to multiple reasons: invalid url, incorrect proxy, wrong authentication,...

Usage

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

License:Open Source License

protected static URL getLibraryJavadocLocation(IClasspathEntry entry) throws JavaModelException {
    switch (entry.getEntryKind()) {
    case IClasspathEntry.CPE_LIBRARY:
    case IClasspathEntry.CPE_VARIABLE:
        break;/* w  ww .j  av  a2 s  .c om*/
    default:
        throw new IllegalArgumentException("Entry must be of kind CPE_LIBRARY or CPE_VARIABLE"); //$NON-NLS-1$
    }

    IClasspathAttribute[] extraAttributes = entry.getExtraAttributes();
    for (int i = 0; i < extraAttributes.length; i++) {
        IClasspathAttribute attrib = extraAttributes[i];
        if (IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME.equals(attrib.getName())) {
            String value = attrib.getValue();
            try {
                return new URL(value);
            } catch (MalformedURLException e) {
                throw new JavaModelException(
                        new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC, value));
            }
        }
    }
    return null;
}

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

License:Open Source License

protected void validateAndCache(URL baseLoc, FileNotFoundException e) throws JavaModelException {
    String url = baseLoc.toString();
    if (validURLs != null && validURLs.contains(url))
        return;//from w w w .ja v a  2  s .  co m

    if (invalidURLs != null && invalidURLs.contains(url))
        throw new JavaModelException(e, IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC);

    InputStream input = null;
    try {
        URLConnection connection = baseLoc.openConnection();
        input = connection.getInputStream();
        if (validURLs == null) {
            validURLs = new HashSet<String>(1);
        }
        validURLs.add(url);
    } catch (Exception e1) {
        if (invalidURLs == null) {
            invalidURLs = new HashSet<String>(1);
        }
        invalidURLs.add(url);
        throw new JavaModelException(e, IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (Exception e1) {
                // Ignore
            }
        }
    }
}

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 a v  a2 s.  com
    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//from ww  w  .  j a v  a 2s. 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;
}

From source file:org.eclipse.jdt.internal.core.PackageFragment.java

License:Open Source License

public String getAttachedJavadoc(IProgressMonitor monitor) throws JavaModelException {
    PerProjectInfo projectInfo = JavaModelManager.getJavaModelManager()
            .getPerProjectInfoCheckExistence(getJavaProject().getProject());
    String cachedJavadoc = null;//  w w w. j  a va  2 s .co m
    synchronized (projectInfo.javadocCache) {
        cachedJavadoc = (String) projectInfo.javadocCache.get(this);
    }
    if (cachedJavadoc != null) {
        return cachedJavadoc;
    }
    URL baseLocation = getJavadocBaseLocation();
    if (baseLocation == null) {
        return null;
    }
    StringBuffer pathBuffer = new StringBuffer(baseLocation.toExternalForm());

    if (!(pathBuffer.charAt(pathBuffer.length() - 1) == '/')) {
        pathBuffer.append('/');
    }
    String packPath = getElementName().replace('.', '/');
    pathBuffer.append(packPath).append('/').append(JavadocConstants.PACKAGE_FILE_NAME);

    if (monitor != null && monitor.isCanceled())
        throw new OperationCanceledException();
    final String contents = getURLContents(String.valueOf(pathBuffer));
    if (monitor != null && monitor.isCanceled())
        throw new OperationCanceledException();
    if (contents == null)
        throw new JavaModelException(
                new JavaModelStatus(IJavaModelStatusConstants.CANNOT_RETRIEVE_ATTACHED_JAVADOC, this));
    synchronized (projectInfo.javadocCache) {
        projectInfo.javadocCache.put(this, contents);
    }
    return contents;
}