
org.jnosql.artemis.document.spi.DocumentCollectionProducerExtension Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of artemis-document Show documentation
Show all versions of artemis-document Show documentation
Eclipse JNoSQL Mapping, Artemis API, to document NoSQL databases
The newest version!
/*
* Copyright (c) 2017 Otávio Santana and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.jnosql.artemis.document.spi;
import org.jnosql.artemis.ConfigurationUnit;
import org.jnosql.artemis.DatabaseMetadata;
import org.jnosql.artemis.Databases;
import org.jnosql.artemis.Repository;
import org.jnosql.artemis.RepositoryAsync;
import org.jnosql.artemis.document.query.RepositoryAsyncDocumentBean;
import org.jnosql.artemis.document.query.RepositoryDocumentBean;
import org.jnosql.artemis.util.ConfigurationUnitUtils;
import org.jnosql.artemis.util.RepositoryUnit;
import org.jnosql.diana.api.document.DocumentCollectionManager;
import org.jnosql.diana.api.document.DocumentCollectionManagerAsync;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.ProcessInjectionPoint;
import javax.enterprise.inject.spi.ProcessProducer;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import static org.jnosql.artemis.DatabaseType.DOCUMENT;
/**
* Extension to start up the DocumentTemplate, DocumentTemplateAsync, Repository and RepositoryAsync
* from the {@link org.jnosql.artemis.Database} qualifier
*/
public class DocumentCollectionProducerExtension implements Extension {
private static final Logger LOGGER = Logger.getLogger(DocumentCollectionProducerExtension.class.getName());
private final Set databases = new HashSet<>();
private final Set databasesAsync = new HashSet<>();
private final Collection> crudTypes = new HashSet<>();
private final Collection> crudAsyncTypes = new HashSet<>();
private final Collection repositoryUnits = new HashSet<>();
private final Collection repositoryAsyncUnits = new HashSet<>();
void observes(@Observes final ProcessAnnotatedType repo) {
Class javaClass = repo.getAnnotatedType().getJavaClass();
if (Repository.class.equals(javaClass)) {
return;
}
if (Arrays.asList(javaClass.getInterfaces()).contains(Repository.class)
&& Modifier.isInterface(javaClass.getModifiers())) {
LOGGER.info("Adding a new Repository as discovered on document: " + javaClass);
crudTypes.add(javaClass);
}
}
void observesAsync(@Observes final ProcessAnnotatedType repo) {
Class javaClass = repo.getAnnotatedType().getJavaClass();
if (RepositoryAsync.class.equals(javaClass)) {
return;
}
if (Arrays.asList(javaClass.getInterfaces()).contains(RepositoryAsync.class)
&& Modifier.isInterface(javaClass.getModifiers())) {
LOGGER.info("Adding a new RepositoryAsync as discovered on document: " + javaClass);
crudAsyncTypes.add(javaClass);
}
}
void observes(@Observes final ProcessProducer pp) {
Databases.addDatabase(pp, DOCUMENT, databases);
}
void observesAsync(@Observes final ProcessProducer pp) {
Databases.addDatabase(pp, DOCUMENT, databasesAsync);
}
> void observes(@Observes ProcessInjectionPoint event) {
observes(event.getInjectionPoint(), repositoryUnits);
}
> void observesAsync(@Observes ProcessInjectionPoint event) {
observes(event.getInjectionPoint(), repositoryAsyncUnits);
}
private void observes(InjectionPoint injectionPoint, Collection repositoryAsyncUnits) {
if (ConfigurationUnitUtils.hasConfigurationUnit(injectionPoint)) {
ConfigurationUnit configurationUnit = ConfigurationUnitUtils.getConfigurationUnit(injectionPoint);
Type type = injectionPoint.getType();
RepositoryUnit unitRepository = RepositoryUnit.of((Class>) type, configurationUnit);
if (unitRepository.isDocument()) {
LOGGER.info(String.format("Found Repository to configuration unit document to configuration name %s fileName %s database: %s repository: %s",
configurationUnit.name(), configurationUnit.fileName(), configurationUnit.database(), type.toString()));
repositoryAsyncUnits.add(unitRepository);
}
}
}
void onAfterBeanDiscovery(@Observes final AfterBeanDiscovery afterBeanDiscovery, final BeanManager beanManager) {
LOGGER.info(String.format("Starting to process on documents: %d databases crud %d and crudAsync %d",
databases.size(), crudTypes.size(), crudAsyncTypes.size()));
databases.forEach(type -> {
final TemplateBean bean = new TemplateBean(beanManager, type.getProvider());
afterBeanDiscovery.addBean(bean);
});
databasesAsync.forEach(type -> {
final TemplateAsyncBean bean = new TemplateAsyncBean(beanManager, type.getProvider());
afterBeanDiscovery.addBean(bean);
});
crudTypes.forEach(type -> {
if (!databases.contains(DatabaseMetadata.DEFAULT_DOCUMENT)) {
afterBeanDiscovery.addBean(new RepositoryDocumentBean(type, beanManager, ""));
}
databases.forEach(database -> {
final RepositoryDocumentBean bean = new RepositoryDocumentBean(type, beanManager, database.getProvider());
afterBeanDiscovery.addBean(bean);
});
});
crudAsyncTypes.forEach(type -> {
if (!databases.contains(DatabaseMetadata.DEFAULT_DOCUMENT)) {
afterBeanDiscovery.addBean(new RepositoryAsyncDocumentBean(type, beanManager, ""));
}
databasesAsync.forEach(database -> {
final RepositoryAsyncDocumentBean bean = new RepositoryAsyncDocumentBean(type, beanManager,
database.getProvider());
afterBeanDiscovery.addBean(bean);
});
});
repositoryUnits.forEach(type -> afterBeanDiscovery.addBean(new RepositoryUnitDocumentBean(beanManager, type)));
repositoryAsyncUnits.forEach(type -> afterBeanDiscovery.addBean(new RepositoryAsyncUnitDocumentBean(beanManager, type)));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy