forked from Blazebit/blaze-persistence
/
EntityViewRepositoryExtension.java
167 lines (141 loc) · 6.9 KB
/
EntityViewRepositoryExtension.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2014 - 2017 Blazebit.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.blazebit.persistence.deltaspike.data.impl;
import com.blazebit.apt.service.ServiceProvider;
import com.blazebit.persistence.deltaspike.data.impl.meta.EntityViewRepositoryComponents;
import org.apache.deltaspike.core.spi.activation.Deactivatable;
import org.apache.deltaspike.core.util.ClassDeactivationUtils;
import org.apache.deltaspike.data.api.AbstractEntityRepository;
import org.apache.deltaspike.data.api.AbstractFullEntityRepository;
import org.apache.deltaspike.data.api.Repository;
import org.apache.deltaspike.data.impl.RepositoryDefinitionException;
import org.apache.deltaspike.data.impl.meta.RepositoryComponents;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.BeforeShutdown;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Implementation is similar to {@link org.apache.deltaspike.data.impl.RepositoryExtension} but was modified to
* work with entity views.
*
* @author Moritz Becker
* @since 1.2.0
*/
@ServiceProvider(Extension.class)
public class EntityViewRepositoryExtension implements Extension, Deactivatable {
private static final Logger LOG = Logger.getLogger(EntityViewRepositoryExtension.class.getName());
private static RepositoryComponents staticComponents = new RepositoryComponents();
private static EntityViewRepositoryComponents staticEntityViewRepositoryComponents = new EntityViewRepositoryComponents();
private final List<RepositoryDefinitionException> definitionExceptions = new LinkedList<>();
private Boolean isActivated = true;
private RepositoryComponents components = new RepositoryComponents();
private EntityViewRepositoryComponents entityViewRepositoryComponents = new EntityViewRepositoryComponents();
void beforeBeanDiscovery(@Observes BeforeBeanDiscovery before) {
isActivated = ClassDeactivationUtils.isActivated(getClass());
}
@SuppressWarnings("unchecked")
<X> void processAnnotatedType(@Observes ProcessAnnotatedType<X> event) {
if (!isActivated) {
return;
}
if (isVetoed(event.getAnnotatedType())) {
event.veto();
} else if (isRepository(event.getAnnotatedType())) {
Class<X> repoClass = event.getAnnotatedType().getJavaClass();
try {
LOG.log(Level.FINER, "getHandlerClass: Repository annotation detected on {0}",
event.getAnnotatedType());
if (Deactivatable.class.isAssignableFrom(repoClass)
&& !ClassDeactivationUtils.isActivated((Class<? extends Deactivatable>) repoClass)) {
LOG.log(Level.FINER, "Class {0} is Deactivated", repoClass);
return;
}
try {
entityViewRepositoryComponents.add(repoClass);
staticEntityViewRepositoryComponents.add(repoClass);
} catch (RepositoryDefinitionException e) {
components.add(repoClass);
staticComponents.add(repoClass);
}
} catch (RepositoryDefinitionException e) {
definitionExceptions.add(e);
} catch (Exception e) {
definitionExceptions.add(new RepositoryDefinitionException(repoClass, e));
}
}
}
<X> void addDefinitionErrors(@Observes AfterBeanDiscovery afterBeanDiscovery, BeanManager beanManager) {
if (!isActivated) {
return;
}
for (RepositoryDefinitionException ex : definitionExceptions) {
afterBeanDiscovery.addDefinitionError(ex);
}
}
private <X> boolean isRepository(AnnotatedType<X> annotatedType) {
return (annotatedType.isAnnotationPresent(Repository.class) ||
annotatedType.getJavaClass().isAnnotationPresent(Repository.class)) &&
!InvocationHandler.class.isAssignableFrom(annotatedType.getJavaClass());
}
private <X> boolean isVetoed(AnnotatedType<X> annotated) {
Class<X> javaClass = annotated.getJavaClass();
return javaClass.equals(AbstractEntityRepository.class) ||
javaClass.equals(AbstractFullEntityRepository.class);
}
public RepositoryComponents getComponents() {
RepositoryComponents result = new RepositoryComponents();
if (components.getRepositories().isEmpty() && !staticComponents.getRepositories().isEmpty()) {
result.addAll(staticComponents.getRepositories());
}
if (!components.getRepositories().isEmpty()) {
result.addAll(components.getRepositories());
}
return result;
}
public EntityViewRepositoryComponents getEntityViewRepositoryComponents() {
EntityViewRepositoryComponents result = new EntityViewRepositoryComponents();
if (entityViewRepositoryComponents.getRepositories().isEmpty() && !staticEntityViewRepositoryComponents.getRepositories().isEmpty()) {
result.addAll(staticEntityViewRepositoryComponents.getRepositories());
}
if (!entityViewRepositoryComponents.getRepositories().isEmpty()) {
result.addAll(entityViewRepositoryComponents.getRepositories());
}
return result;
}
protected void cleanup(@Observes BeforeShutdown beforeShutdown) {
//we can reset it in any case,
//because every application produced a copy as application-scoped bean (see RepositoryComponentsFactory)
staticComponents.getRepositories().clear();
}
private static void setFinalField(Field field, Object instance, Object newValue) throws NoSuchFieldException, IllegalAccessException {
field.setAccessible(true);
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(instance, newValue);
}
}