/
TemplateBean.java
126 lines (102 loc) · 3.75 KB
/
TemplateBean.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* 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.DatabaseQualifier;
import org.jnosql.artemis.DatabaseType;
import org.jnosql.artemis.document.DocumentTemplate;
import org.jnosql.artemis.document.DocumentTemplateProducer;
import org.jnosql.artemis.spi.AbstractBean;
import org.jnosql.diana.api.document.DocumentCollectionManager;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.InjectionPoint;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.Set;
class TemplateBean extends AbstractBean<DocumentTemplate> {
private final Set<Type> types;
private final String provider;
private final Set<Annotation> qualifiers;
/**
* Constructor
*
* @param beanManager the beanManager
* @param provider the provider name, that must be a
*/
public TemplateBean(BeanManager beanManager, String provider) {
super(beanManager);
this.types = Collections.singleton(DocumentTemplate.class);
this.provider = provider;
this.qualifiers = Collections.singleton(DatabaseQualifier.ofDocument(provider));
}
@Override
public Class<?> getBeanClass() {
return DocumentTemplate.class;
}
@Override
public Set<InjectionPoint> getInjectionPoints() {
return Collections.emptySet();
}
@Override
public boolean isNullable() {
return false;
}
@Override
public DocumentTemplate create(CreationalContext<DocumentTemplate> creationalContext) {
DocumentTemplateProducer producer = getInstance(DocumentTemplateProducer.class);
DocumentCollectionManager manager = getManager();
return producer.get(manager);
}
private DocumentCollectionManager getManager() {
Bean<DocumentCollectionManager> bean = (Bean<DocumentCollectionManager>) getBeanManager().getBeans(DocumentCollectionManager.class,
DatabaseQualifier.ofDocument(provider) ).iterator().next();
CreationalContext<DocumentCollectionManager> ctx = getBeanManager().createCreationalContext(bean);
return (DocumentCollectionManager) getBeanManager().getReference(bean, DocumentCollectionManager.class, ctx);
}
@Override
public void destroy(DocumentTemplate instance, CreationalContext<DocumentTemplate> creationalContext) {
}
@Override
public Set<Type> getTypes() {
return types;
}
@Override
public Set<Annotation> getQualifiers() {
return qualifiers;
}
@Override
public Class<? extends Annotation> getScope() {
return ApplicationScoped.class;
}
@Override
public String getName() {
return null;
}
@Override
public Set<Class<? extends Annotation>> getStereotypes() {
return Collections.emptySet();
}
@Override
public boolean isAlternative() {
return false;
}
@Override
public String getId() {
return DocumentTemplate.class.getName() + DatabaseType.COLUMN + "-" + provider;
}
}