Search in sources :

Example 11 with Singleton

use of jakarta.inject.Singleton in project micronaut-views by micronaut-projects.

the class ThymeleafFactory method templateEngine.

/**
 * Constructs the template engine.
 *
 * @param templateResolver The template resolver
 * @param engineContextFactory The engine context factory
 * @param linkBuilder The link builder
 * @param messageSourceMessageResolver The message resolver
 * @return The template engine
 */
@Singleton
public TemplateEngine templateEngine(ITemplateResolver templateResolver, IEngineContextFactory engineContextFactory, ILinkBuilder linkBuilder, MessageSourceMessageResolver messageSourceMessageResolver) {
    TemplateEngine engine = new TemplateEngine();
    engine.setEngineContextFactory(engineContextFactory);
    engine.setLinkBuilder(linkBuilder);
    engine.setTemplateResolver(templateResolver);
    engine.addDialect(new Java8TimeDialect());
    engine.addMessageResolver(messageSourceMessageResolver);
    return engine;
}
Also used : TemplateEngine(org.thymeleaf.TemplateEngine) Java8TimeDialect(org.thymeleaf.extras.java8time.dialect.Java8TimeDialect) Singleton(jakarta.inject.Singleton)

Example 12 with Singleton

use of jakarta.inject.Singleton in project micronaut-graphql by micronaut-projects.

the class GraphQLFactory method graphQL.

@Bean
@Singleton
public GraphQL graphQL(ResourceResolver resourceResolver, HelloDataFetcher helloDataFetcher) {
    // <2>
    SchemaParser schemaParser = new SchemaParser();
    SchemaGenerator schemaGenerator = new SchemaGenerator();
    // Parse the schema.
    TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();
    typeRegistry.merge(schemaParser.parse(new BufferedReader(new InputStreamReader(resourceResolver.getResourceAsStream("classpath:schema.graphqls").get()))));
    // Create the runtime wiring.
    RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring().type("Query", typeWiring -> typeWiring.dataFetcher("hello", helloDataFetcher)).build();
    // Create the executable schema.
    GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
    // Return the GraphQL bean.
    return GraphQL.newGraphQL(graphQLSchema).build();
}
Also used : Bean(io.micronaut.context.annotation.Bean) ResourceResolver(io.micronaut.core.io.ResourceResolver) GraphQL(graphql.GraphQL) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaParser(graphql.schema.idl.SchemaParser) Factory(io.micronaut.context.annotation.Factory) GraphQLSchema(graphql.schema.GraphQLSchema) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Singleton(jakarta.inject.Singleton) SchemaGenerator(graphql.schema.idl.SchemaGenerator) BufferedReader(java.io.BufferedReader) InputStreamReader(java.io.InputStreamReader) InputStreamReader(java.io.InputStreamReader) RuntimeWiring(graphql.schema.idl.RuntimeWiring) SchemaGenerator(graphql.schema.idl.SchemaGenerator) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) BufferedReader(java.io.BufferedReader) SchemaParser(graphql.schema.idl.SchemaParser) GraphQLSchema(graphql.schema.GraphQLSchema) Singleton(jakarta.inject.Singleton) Bean(io.micronaut.context.annotation.Bean)

Example 13 with Singleton

use of jakarta.inject.Singleton in project core by weld.

the class GetContextUtilMethodsTest method getActiveContextsTest.

@Test
public void getActiveContextsTest() {
    try (WeldContainer container = new Weld().initialize()) {
        // TheLoneBean is just to have some bean in the archive
        container.select(TheLoneBean.class).get().ping();
        WeldManager manager = container.select(WeldManager.class).get();
        // there are 7 scopes by default in SE, only 3 have active contexts by default
        // it is dependent, singleton and application
        Collection<Context> activeContexts = manager.getActiveContexts();
        Assert.assertEquals(3, activeContexts.size());
        Set<Class<? extends Annotation>> scopes = activeContexts.stream().map(t -> t.getScope()).collect(Collectors.toSet());
        Assert.assertTrue(scopes.contains(Dependent.class));
        Assert.assertTrue(scopes.contains(Singleton.class));
        Assert.assertTrue(scopes.contains(ApplicationScoped.class));
    }
}
Also used : Context(jakarta.enterprise.context.spi.Context) WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext) Dependent(jakarta.enterprise.context.Dependent) Arquillian(org.jboss.arquillian.junit.Arquillian) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) RunWith(org.junit.runner.RunWith) WeldManager(org.jboss.weld.manager.api.WeldManager) BeanArchive(org.jboss.shrinkwrap.api.BeanArchive) TheLoneBean(org.jboss.weld.environment.se.test.weldManager.contextActive.TheLoneBean) RequestContextController(jakarta.enterprise.context.control.RequestContextController) Weld(org.jboss.weld.environment.se.Weld) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) Utils(org.jboss.weld.test.util.Utils) Collection(java.util.Collection) Set(java.util.Set) Singleton(jakarta.inject.Singleton) Context(jakarta.enterprise.context.spi.Context) Test(org.junit.Test) Archive(org.jboss.shrinkwrap.api.Archive) ClassPath(org.jboss.arquillian.container.se.api.ClassPath) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Collectors(java.util.stream.Collectors) Deployment(org.jboss.arquillian.container.test.api.Deployment) WeldAlterableContext(org.jboss.weld.context.WeldAlterableContext) Annotation(java.lang.annotation.Annotation) Assert(org.junit.Assert) RequestScoped(jakarta.enterprise.context.RequestScoped) Singleton(jakarta.inject.Singleton) WeldContainer(org.jboss.weld.environment.se.WeldContainer) Dependent(jakarta.enterprise.context.Dependent) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) Annotation(java.lang.annotation.Annotation) Weld(org.jboss.weld.environment.se.Weld) WeldManager(org.jboss.weld.manager.api.WeldManager) Test(org.junit.Test)

Example 14 with Singleton

use of jakarta.inject.Singleton in project micronaut-kafka by micronaut-projects.

the class OptimizationStream method optimizationOn.

@Singleton
@Named(STREAM_OPTIMIZATION_ON)
KStream<String, String> optimizationOn(@Named(STREAM_OPTIMIZATION_ON) ConfiguredStreamBuilder builder) {
    // set default serdes
    Properties props = builder.getConfiguration();
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    KTable<String, String> table = builder.table(OPTIMIZATION_ON_INPUT, Materialized.as(OPTIMIZATION_ON_STORE));
    return table.toStream();
}
Also used : Properties(java.util.Properties) Named(jakarta.inject.Named) Singleton(jakarta.inject.Singleton)

Example 15 with Singleton

use of jakarta.inject.Singleton in project micronaut-gcp by micronaut-projects.

the class GoogleCredentialsFactory method defaultGoogleCredentials.

/**
 * Method used to return the default {@link GoogleCredentials} and provide it as a bean.
 *
 * It will determine which credential in the following way:
 * <ol>
 *     <li>If <pre>gcp.credentials.location</pre> is specified, use its location</li>
 *     <li>Otherwise, if <pre>gcp.credentials.encodedKey</pre> is specified, decode it and use its content</li>
 *     <li>None of the 2 properties were specified, use Application Default credential resolution. See
 *     <a href="https://github.com/googleapis/google-cloud-java#authentication">Google Cloud Java authentication</a>.
 *     This will resolve credential in the following order:
 *       <ol>
 *           <li>The credentials file pointed to by the <pre>GOOGLE_APPLICATION_CREDENTIALS</pre> environment variable</li>
 *           <li>Credentials provided by the Google Cloud SDK <pre>gcloud auth application-default login</pre> command</li>
 *           <li>Google App Engine built-in credentials when running inside of Google App Engine</li>
 *           <li>Google Cloud Shell built-in credentials when running inside of Google Cloud Shell</li>
 *           <li>Google Compute Engine built-in credentials when running inside of Google Compute Engine or Kubernetes Engine</li>
 *       </ol>
 *     </li>
 * </ol>
 *
 * @return The {@link GoogleCredentials}
 * @throws IOException An exception if an error occurs
 */
@Requires(missingBeans = GoogleCredentials.class)
@Requires(classes = com.google.auth.oauth2.GoogleCredentials.class)
@Primary
@Singleton
protected GoogleCredentials defaultGoogleCredentials() throws IOException {
    final List<String> scopes = configuration.getScopes().stream().map(URI::toString).collect(Collectors.toList());
    GoogleCredentials credentials;
    if (configuration.getLocation().isPresent() && configuration.getEncodedKey().isPresent()) {
        throw new ConfigurationException("Please specify only one of gcp.credentials.location or gcp.credentials.encodedKey");
    } else if (configuration.getLocation().isPresent()) {
        LOG.info("Google Credentials from gcp.credentials.location = " + configuration.getLocation());
        FileInputStream fis = new FileInputStream(configuration.getLocation().get());
        credentials = GoogleCredentials.fromStream(fis);
        fis.close();
    } else if (configuration.getEncodedKey().isPresent()) {
        LOG.info("Google Credentials from gcp.credentials.encodedKey");
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] bytes = decoder.decode(configuration.getEncodedKey().get());
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
        credentials = GoogleCredentials.fromStream(is);
        is.close();
    } else {
        LOG.info("Google Credentials from Application Default Credentials");
        credentials = GoogleCredentials.getApplicationDefault();
    }
    return credentials.createScoped(scopes);
}
Also used : Base64(java.util.Base64) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) FileInputStream(java.io.FileInputStream) Requires(io.micronaut.context.annotation.Requires) Singleton(jakarta.inject.Singleton) Primary(io.micronaut.context.annotation.Primary)

Aggregations

Singleton (jakarta.inject.Singleton)26 Bean (io.micronaut.context.annotation.Bean)7 Factory (io.micronaut.context.annotation.Factory)6 Properties (java.util.Properties)6 GraphQLSchema (graphql.schema.GraphQLSchema)5 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 GraphQL (graphql.GraphQL)4 RuntimeWiring (graphql.schema.idl.RuntimeWiring)4 SchemaGenerator (graphql.schema.idl.SchemaGenerator)4 SchemaParser (graphql.schema.idl.SchemaParser)4 TypeDefinitionRegistry (graphql.schema.idl.TypeDefinitionRegistry)4 ResourceResolver (io.micronaut.core.io.ResourceResolver)4 Named (jakarta.inject.Named)4 Test (org.junit.Test)3 ConfiguredStreamBuilder (io.micronaut.configuration.kafka.streams.ConfiguredStreamBuilder)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Arrays (java.util.Arrays)2 Locale (java.util.Locale)2 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)2