Search in sources :

Example 51 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project ExoPlayer by google.

the class DemoUtil method getHttpDataSourceFactory.

public static synchronized HttpDataSource.Factory getHttpDataSourceFactory(Context context) {
    if (httpDataSourceFactory == null) {
        if (USE_CRONET_FOR_NETWORKING) {
            context = context.getApplicationContext();
            @Nullable CronetEngine cronetEngine = CronetUtil.buildCronetEngine(context);
            if (cronetEngine != null) {
                httpDataSourceFactory = new CronetDataSource.Factory(cronetEngine, Executors.newSingleThreadExecutor());
            }
        }
        if (httpDataSourceFactory == null) {
            // We don't want to use Cronet, or we failed to instantiate a CronetEngine.
            CookieManager cookieManager = new CookieManager();
            cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
            CookieHandler.setDefault(cookieManager);
            httpDataSourceFactory = new DefaultHttpDataSource.Factory();
        }
    }
    return httpDataSourceFactory;
}
Also used : CronetEngine(org.chromium.net.CronetEngine) CronetDataSource(com.google.android.exoplayer2.ext.cronet.CronetDataSource) DefaultHttpDataSource(com.google.android.exoplayer2.upstream.DefaultHttpDataSource) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CookieManager(java.net.CookieManager)

Example 52 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project aws-xray-sdk-java by aws.

the class ContainerInsightsUtil method getClusterName.

/**
 * Return the cluster name from ContainerInsights configMap via the K8S API and the pod's system account.
 *
 * @return the name
 */
@Nullable
public static String getClusterName() {
    if (isK8s()) {
        CloseableHttpClient client = getHttpClient();
        HttpGet getRequest = new HttpGet(K8S_URL + CI_CONFIGMAP_PATH);
        String k8sCredentialHeader = getK8sCredentialHeader();
        if (k8sCredentialHeader != null) {
            getRequest.setHeader(AUTH_HEADER_NAME, k8sCredentialHeader);
        }
        try {
            CloseableHttpResponse response = client.execute(getRequest);
            try {
                HttpEntity entity = response.getEntity();
                String json = EntityUtils.toString(entity);
                ObjectMapper mapper = new ObjectMapper();
                String clusterName = mapper.readTree(json).at("/data/cluster.name").asText();
                if (logger.isDebugEnabled()) {
                    logger.debug("Container Insights Cluster Name: " + clusterName);
                }
                return clusterName;
            } catch (IOException e) {
                logger.error("Error parsing response from Kubernetes", e);
            } finally {
                response.close();
            }
            client.close();
        } catch (IOException e) {
            logger.error("Error querying for Container Insights ConfigMap", e);
        }
    }
    return null;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 53 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project aws-xray-sdk-java by aws.

the class ContainerInsightsUtil method getK8sKeystore.

@Nullable
private static KeyStore getK8sKeystore() {
    InputStream certificateFile = null;
    try {
        KeyStore k8sTrustStore = null;
        File caFile = Paths.get(K8S_CRED_FOLDER, K8S_CRED_CERT_SUFFIX).toFile();
        if (caFile.exists()) {
            k8sTrustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            k8sTrustStore.load(null, null);
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
            certificateFile = new FileInputStream(caFile);
            Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(certificateFile);
            if (certificates.isEmpty()) {
                throw new IllegalArgumentException("K8s cert file contained no certificates.");
            }
            for (Certificate certificate : certificates) {
                k8sTrustStore.setCertificateEntry("k8sca", certificate);
            }
        } else {
            logger.debug("K8s CA Cert file does not exists.");
        }
        return k8sTrustStore;
    } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) {
        logger.warn("Unable to load K8s CA certificate.", e);
        return null;
    } finally {
        if (certificateFile != null) {
            try {
                certificateFile.close();
            } catch (IOException e) {
                logger.error("Can't close K8s CA certificate file.", e);
            }
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CertificateException(java.security.cert.CertificateException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStore(java.security.KeyStore) CertificateFactory(java.security.cert.CertificateFactory) FileInputStream(java.io.FileInputStream) File(java.io.File) Certificate(java.security.cert.Certificate) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 54 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project aws-xray-sdk-java by aws.

the class ContainerInsightsUtil method getK8sCredentialHeader.

@Nullable
private static String getK8sCredentialHeader() {
    BufferedReader tokenReader = null;
    try {
        File tokenFile = Paths.get(K8S_CRED_FOLDER, K8S_CRED_TOKEN_SUFFIX).toFile();
        tokenReader = new BufferedReader(new InputStreamReader(new FileInputStream(tokenFile), StandardCharsets.UTF_8));
        return String.format("Bearer %s", tokenReader.readLine());
    } catch (IOException e) {
        logger.warn("Unable to read K8s credential file.", e);
    } finally {
        if (tokenReader != null) {
            try {
                tokenReader.close();
            } catch (IOException e) {
                logger.error("Can't close K8s credential file.", e);
            }
        }
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 55 with Nullable

use of org.checkerframework.checker.nullness.qual.Nullable in project aws-xray-sdk-java by aws.

the class AWSXRayRecorder method currentTraceId.

/**
 * @throws SegmentNotFoundException
 *             if {@code contextMissingStrategy} throws exceptions and no segment or subsegment is currently in progress
 * @return the trace ID of the {@code Segment} currently in progress, or {@code null} if {@code contextMissingStrategy}
 * suppresses exceptions and no segment or subsegment is currently in progress
 */
@Nullable
public TraceID currentTraceId() {
    SegmentContext context = getSegmentContext();
    if (context == null) {
        return null;
    }
    Entity current = context.getTraceEntity();
    if (current != null) {
        return current.getParentSegment().getTraceId();
    } else {
        contextMissingStrategy.contextMissing("Failed to get current trace ID: segment cannot be found.", SegmentNotFoundException.class);
        return null;
    }
}
Also used : SegmentContext(com.amazonaws.xray.contexts.SegmentContext) Entity(com.amazonaws.xray.entities.Entity) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

Nullable (org.checkerframework.checker.nullness.qual.Nullable)285 ArrayList (java.util.ArrayList)27 TypeElement (javax.lang.model.element.TypeElement)23 IOException (java.io.IOException)21 Map (java.util.Map)21 ExecutableElement (javax.lang.model.element.ExecutableElement)19 ServerLevel (net.minecraft.server.level.ServerLevel)19 List (java.util.List)16 Instant (org.joda.time.Instant)16 VariableElement (javax.lang.model.element.VariableElement)15 HashMap (java.util.HashMap)14 Optional (java.util.Optional)14 Element (javax.lang.model.element.Element)13 TreePath (com.sun.source.util.TreePath)12 BlockPos (net.minecraft.core.BlockPos)12 Method (java.lang.reflect.Method)11 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)11 MonotonicNonNull (org.checkerframework.checker.nullness.qual.MonotonicNonNull)10 VariableTree (com.sun.source.tree.VariableTree)8 ClassTree (com.sun.source.tree.ClassTree)7