-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arc - add built in beans for BM, Event, Instance. Add automated tests.
- Loading branch information
Showing
7 changed files
with
445 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
independent-projects/arc/runtime/src/main/java/io/quarkus/arc/BeanManagerBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.quarkus.arc; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import javax.enterprise.context.spi.CreationalContext; | ||
import javax.enterprise.inject.spi.BeanManager; | ||
|
||
public class BeanManagerBean extends BuiltInBean<BeanManager> { | ||
|
||
private static final Set<Type> BM_TYPES = Collections | ||
.unmodifiableSet(new HashSet<>(Arrays.asList(Object.class, BeanManager.class))); | ||
|
||
@Override | ||
public Set<Type> getTypes() { | ||
return BM_TYPES; | ||
} | ||
|
||
@Override | ||
public BeanManager get(CreationalContext<BeanManager> creationalContext) { | ||
BeanManager instance = new BeanManagerProvider<>().get(creationalContext); | ||
CreationalContextImpl.addDependencyToParent((InjectableBean<BeanManager>) InjectionPointProvider.get().getBean(), | ||
instance, creationalContext); | ||
return instance; | ||
} | ||
|
||
@Override | ||
public Class<?> getBeanClass() { | ||
return BeanManagerImpl.class; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
independent-projects/arc/runtime/src/main/java/io/quarkus/arc/BuiltInBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.quarkus.arc; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import javax.enterprise.context.Dependent; | ||
import javax.enterprise.context.spi.CreationalContext; | ||
|
||
/** | ||
* Common class for all built-in beans. | ||
* | ||
*/ | ||
public abstract class BuiltInBean<T> implements InjectableBean<T> { | ||
|
||
@Override | ||
public String getIdentifier() { | ||
return sha1(toString()); | ||
} | ||
|
||
@Override | ||
public Class<? extends Annotation> getScope() { | ||
return Dependent.class; | ||
} | ||
|
||
@Override | ||
public T create(CreationalContext<T> creationalContext) { | ||
return get(creationalContext); | ||
} | ||
|
||
// copy of io.quarkus.arc.processor.Hashes#sha1 | ||
private String sha1(String value) { | ||
try { | ||
MessageDigest md = MessageDigest.getInstance("SHA-1"); | ||
byte[] digest = md.digest(value.getBytes(StandardCharsets.UTF_8)); | ||
StringBuilder sb = new StringBuilder(40); | ||
for (int i = 0; i < digest.length; ++i) { | ||
sb.append(Integer.toHexString((digest[i] & 0xFF) | 0x100).substring(1, 3)); | ||
} | ||
return sb.toString(); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
independent-projects/arc/runtime/src/main/java/io/quarkus/arc/EventBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.quarkus.arc; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import javax.enterprise.context.spi.CreationalContext; | ||
import javax.enterprise.event.Event; | ||
import javax.enterprise.inject.spi.InjectionPoint; | ||
import javax.enterprise.util.TypeLiteral; | ||
|
||
public class EventBean extends BuiltInBean<Event<?>> { | ||
|
||
public static final Set<Type> EVENT_TYPES = Collections | ||
.unmodifiableSet(new HashSet<>(Arrays.asList(Event.class, new TypeLiteral<Event<Object>>() { | ||
}.getType(), Object.class))); | ||
|
||
@Override | ||
public Set<Type> getTypes() { | ||
return EVENT_TYPES; | ||
} | ||
|
||
@Override | ||
public Event<?> get(CreationalContext<Event<?>> creationalContext) { | ||
// Obtain current IP to get the required type and qualifiers | ||
InjectionPoint ip = InjectionPointProvider.get(); | ||
EventImpl<?> instance = new EventImpl<>(ip.getType(), ip.getQualifiers()); | ||
CreationalContextImpl.addDependencyToParent((InjectableBean<Event<?>>) ip.getBean(), instance, creationalContext); | ||
return instance; | ||
} | ||
|
||
@Override | ||
public Class<?> getBeanClass() { | ||
return EventImpl.class; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
independent-projects/arc/runtime/src/main/java/io/quarkus/arc/InstanceBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.quarkus.arc; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import javax.enterprise.context.spi.CreationalContext; | ||
import javax.enterprise.inject.Instance; | ||
import javax.enterprise.inject.spi.InjectionPoint; | ||
import javax.enterprise.util.TypeLiteral; | ||
|
||
public class InstanceBean extends BuiltInBean<Instance<?>> { | ||
|
||
public static final Set<Type> INSTANCE_TYPES = Collections | ||
.unmodifiableSet(new HashSet<>(Arrays.asList(Instance.class, new TypeLiteral<Instance<Object>>() { | ||
}.getType(), Object.class))); | ||
|
||
@Override | ||
public Set<Type> getTypes() { | ||
return INSTANCE_TYPES; | ||
} | ||
|
||
@Override | ||
public Class<?> getBeanClass() { | ||
return InstanceImpl.class; | ||
} | ||
|
||
@Override | ||
public Instance<?> get(CreationalContext<Instance<?>> creationalContext) { | ||
// Obtain current IP to get the required type and qualifiers | ||
InjectionPoint ip = InjectionPointProvider.get(); | ||
InstanceImpl<Instance<?>> instance = new InstanceImpl<Instance<?>>((InjectableBean<?>) ip.getBean(), ip.getType(), | ||
ip.getQualifiers(), (CreationalContextImpl<?>) creationalContext, Collections.EMPTY_SET, ip.getMember(), 0); | ||
CreationalContextImpl.addDependencyToParent((InjectableBean<Instance<?>>) ip.getBean(), instance, creationalContext); | ||
return instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.