Skip to content
This repository has been archived by the owner on Sep 10, 2020. It is now read-only.

Commit

Permalink
first step to FacesConverter/Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
tandraschko committed Aug 19, 2019
1 parent e944e39 commit 3ac6736
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2019 JBoss by Red Hat.
*
* 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 io.quarkus.myfaces.deployment;

import java.util.Arrays;
import java.util.List;

import javax.faces.convert.FacesConverter;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Type;

import io.quarkus.arc.deployment.BeanRegistrarBuildItem;
import io.quarkus.arc.processor.BeanRegistrar;
import io.quarkus.deployment.annotations.BuildProducer;

public class FacesConverterExtension {
public static void register(BuildProducer<BeanRegistrarBuildItem> beanConfigurators,
ClassInfo clazz,
Type forClass,
String converterId) {
System.err.println("FacesConverterExtension: " + clazz + ", " + forClass + ", " + converterId);

if (converterId == null) {
converterId = "";
}
if (forClass == null) {
forClass = Type.create(DotName.createSimple(Object.class.getName()), Type.Kind.CLASS);
}

List<AnnotationValue> qualifierAttributes = Arrays.asList(
AnnotationValue.createClassValue("forClass", forClass),
AnnotationValue.createStringValue("value", converterId),
AnnotationValue.createBooleanValue("managed", true));

AnnotationInstance qualifier = AnnotationInstance.create(
DotName.createSimple(FacesConverter.class.getName()),
null,
qualifierAttributes);

beanConfigurators.produce(new BeanRegistrarBuildItem(new BeanRegistrar() {
@Override
public void register(BeanRegistrar.RegistrationContext registrationContext) {
registrationContext
.configure(clazz.name())
.qualifiers(qualifier)
.types(Type.create(clazz.name(), Type.Kind.CLASS))
.creator(mc -> mc.returnValue(mc.loadClass(clazz.name().toString())));
}
}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2019 JBoss by Red Hat.
*
* 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 io.quarkus.myfaces.deployment;

import java.util.Arrays;
import java.util.List;

import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Type;

import io.quarkus.arc.deployment.BeanRegistrarBuildItem;
import io.quarkus.arc.processor.BeanRegistrar;
import io.quarkus.arc.processor.BuiltinScope;
import io.quarkus.deployment.annotations.BuildProducer;

public class FacesValidatorExtension {
public static void register(BuildProducer<BeanRegistrarBuildItem> beanConfigurators,
ClassInfo clazz,
String validatorId) {
System.err.println("FacesValidatorExtension: " + clazz + ", " + validatorId);

if (validatorId == null) {
validatorId = "";
}

List<AnnotationValue> qualifierAttributes = Arrays.asList(
AnnotationValue.createStringValue("value", validatorId),
AnnotationValue.createBooleanValue("managed", true));

AnnotationInstance qualifier = AnnotationInstance.create(
DotName.createSimple(FacesValidator.class.getName()),
null,
qualifierAttributes);

beanConfigurators.produce(new BeanRegistrarBuildItem(new BeanRegistrar() {
@Override
public void register(BeanRegistrar.RegistrationContext registrationContext) {
registrationContext
.configure(clazz.name())
.qualifiers(qualifier)
.scope(BuiltinScope.DEPENDENT.getInfo())
.types(Type.create(DotName.createSimple(Validator.class.getName()), Type.Kind.CLASS),
Type.create(clazz.name(), Type.Kind.CLASS))
.creator(mc -> mc.returnValue(mc.loadClass(clazz.name().toString())))
.done();
}
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@
import org.apache.myfaces.push.cdi.WebsocketViewBean;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.DotName;

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanDefiningAnnotationBuildItem;
import io.quarkus.arc.deployment.BeanRegistrarBuildItem;
import io.quarkus.arc.deployment.ContextRegistrarBuildItem;
import io.quarkus.arc.processor.ContextRegistrar;
import io.quarkus.deployment.annotations.BuildProducer;
Expand Down Expand Up @@ -245,4 +248,45 @@ private Optional<String> resolveProjectStage(Config config) {
}
return projectStage;
}

@BuildStep
void asdas(BuildProducer<BeanRegistrarBuildItem> beanConfigurators,
CombinedIndexBuildItem combinedIndex) throws IOException {

for (AnnotationInstance ai : combinedIndex.getIndex()
.getAnnotations(DotName.createSimple(FacesConverter.class.getName()))) {
AnnotationValue managed = ai.value("managed");
if (managed != null && managed.asBoolean()) {
AnnotationValue forClass = ai.value("forClass");
if (forClass != null) {
if (!Object.class.getName().equals(forClass.asClass().name().toString())) {
FacesConverterExtension.register(beanConfigurators,
ai.target().asClass(), forClass.asClass(), null);
}
}

AnnotationValue value = ai.value("value");
if (value != null) {
if (value.asString() != null && value.asString().length() > 0) {
FacesConverterExtension.register(beanConfigurators,
ai.target().asClass(), null, value.asString());
}
}
}
}

for (AnnotationInstance ai : combinedIndex.getIndex()
.getAnnotations(DotName.createSimple(FacesValidator.class.getName()))) {
AnnotationValue managed = ai.value("managed");
if (managed != null && managed.asBoolean()) {
AnnotationValue value = ai.value("value");
if (value != null) {
if (value.asString() != null && value.asString().length() > 0) {
FacesValidatorExtension.register(beanConfigurators,
ai.target().asClass(), value.asString());
}
}
}
}
}
}

0 comments on commit 3ac6736

Please sign in to comment.