Skip to content

Commit

Permalink
FORGE-2764: CDI global enablement with Priority (#615)
Browse files Browse the repository at this point in the history
- allow to enable interceptors/decorators/alternatives globally using javax.annotation.Priority
  • Loading branch information
mkouba authored and gastaldi committed Oct 5, 2017
1 parent df247a8 commit 0517bb7
Show file tree
Hide file tree
Showing 10 changed files with 602 additions and 68 deletions.
@@ -0,0 +1,90 @@
/**
* Copyright 2017 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.addon.javaee.cdi.ui;

import java.util.concurrent.Callable;

import javax.annotation.Priority;
import javax.inject.Inject;

import org.jboss.forge.addon.javaee.cdi.CDIFacet;
import org.jboss.forge.addon.javaee.cdi.CDIFacet_1_1;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UIValidationContext;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.roaster.model.source.JavaClassSource;

/**
* @author Martin Kouba
*/
public abstract class AbstractEnablementCDICommand extends AbstractCDICommand<JavaClassSource>
{

@Inject
@WithAttributes(label = "Enabled", description = "Enables for the bean archive (adds to beans.xml)")
private UIInput<Boolean> enabled;

@Inject
@WithAttributes(label = "Priority", description = "Enables globally (for the application)")
private UIInput<Integer> priority;

@Override
protected Class<JavaClassSource> getSourceType()
{
return JavaClassSource.class;
}

// Note that enablement should be initialized last
protected void initializeEnablementUI(UIBuilder builder) {
enabled.setEnabled(hasEnablement());
if (getSelectedProject(builder).hasFacet(CDIFacet_1_1.class))
{
priority.setEnabled(hasEnablement());
builder.add(priority);
}
else
{
priority.setEnabled(false);
}
builder.add(enabled);
}

protected Callable<Boolean> hasEnablement()
{
return () -> true;
}

@Override
public JavaClassSource decorateSource(UIExecutionContext context, Project project,
JavaClassSource source) throws Exception
{
if (enabled.getValue())
{
// TODO: Create a parent BeansDescriptor
enable(project.getFacet(CDIFacet.class), source);
}
if (priority.isEnabled() && priority.hasValue())
{
source.addAnnotation(Priority.class).setLiteralValue(String.valueOf(priority.getValue()));
}
return source;
}

protected abstract void enable(CDIFacet<?> facet, JavaClassSource source);

protected void checkEnablementConflict(UIValidationContext validator, String message)
{
if (enabled.getValue() && (priority.isEnabled() && priority.hasValue()))
{
validator.addValidationWarning(priority, message);
}
}

}
Expand Up @@ -13,6 +13,8 @@
import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.forge.addon.javaee.cdi.CDIFacet;
import org.jboss.forge.addon.javaee.cdi.CDIFacet_1_0;
import org.jboss.forge.addon.javaee.cdi.CDIFacet_1_1;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.ui.context.UIBuilder;
Expand All @@ -24,14 +26,15 @@
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.roaster.model.source.JavaClassSource;
import org.jboss.shrinkwrap.descriptor.api.beans10.BeansDescriptor;

/**
* Creates a new CDI Bean with a specific scope
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public class CDINewBeanCommand extends AbstractCDICommand<JavaClassSource>
public class CDINewBeanCommand extends AbstractEnablementCDICommand
{
@Inject
@WithAttributes(label = "Scope", defaultValue = "DEPENDENT")
Expand Down Expand Up @@ -67,12 +70,6 @@ protected String getType()
return "CDI Bean";
}

@Override
protected Class<JavaClassSource> getSourceType()
{
return JavaClassSource.class;
}

@Override
public void initializeUI(UIBuilder builder) throws Exception
{
Expand All @@ -87,12 +84,20 @@ public Boolean call() throws Exception
};
customScopeAnnotation.setEnabled(customScopeSelected).setRequired(customScopeSelected);
builder.add(scoped).add(customScopeAnnotation).add(qualifier).add(alternative).add(withNamed);
initializeEnablementUI(builder);
}

@Override
protected Callable<Boolean> hasEnablement()
{
return () -> alternative.getValue();
}

@Override
public JavaClassSource decorateSource(UIExecutionContext context, Project project, JavaClassSource source)
throws Exception
{
super.decorateSource(context, project, source);
BeanScope scopedValue = scoped.getValue();
if (BeanScope.CUSTOM == scopedValue)
{
Expand Down Expand Up @@ -126,4 +131,24 @@ else if (BeanScope.DEPENDENT == scopedValue && project.hasFacet(CDIFacet_1_1.cla
}
return source;
}

@Override
protected void enable(CDIFacet<?> facet, JavaClassSource source)
{
if (facet instanceof CDIFacet_1_0)
{
CDIFacet_1_0 cdiFacet_1_0 = (CDIFacet_1_0) facet;
BeansDescriptor bd = cdiFacet_1_0.getConfig();
bd.getOrCreateAlternatives().clazz(source.getQualifiedName());
cdiFacet_1_0.saveConfig(bd);
}
else if (facet instanceof CDIFacet_1_1)
{
CDIFacet_1_1 cdiFacet_1_1 = (CDIFacet_1_1) facet;
org.jboss.shrinkwrap.descriptor.api.beans11.BeansDescriptor bd = cdiFacet_1_1.getConfig();
bd.getOrCreateAlternatives().clazz(source.getQualifiedName());
cdiFacet_1_1.saveConfig(bd);
}
}

}
Expand Up @@ -17,6 +17,7 @@
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UIValidationContext;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.util.Metadata;
Expand All @@ -28,17 +29,14 @@
* Creates a new CDI Decorator
*
* @author <a href="antonio.goncalves@gmail.com">Antonio Goncalves</a>
* @author Martin Kouba
*/
public class CDINewDecoratorCommand extends AbstractCDICommand<JavaClassSource>
public class CDINewDecoratorCommand extends AbstractEnablementCDICommand
{
@Inject
@WithAttributes(label = "Interface to delegate", required = true)
private UIInput<String> delegate;

@Inject
@WithAttributes(label = "Enabled", description = "Adds to beans.xml")
private UIInput<Boolean> enabled;

@Override
public Metadata getMetadata(UIContext context)
{
Expand All @@ -53,47 +51,51 @@ protected String getType()
return "CDI Decorator";
}

@Override
protected Class<JavaClassSource> getSourceType()
{
return JavaClassSource.class;
}

@Override
public void initializeUI(UIBuilder builder) throws Exception
{
super.initializeUI(builder);
builder.add(delegate).add(enabled);
builder.add(delegate);
initializeEnablementUI(builder);
}

@Override
public JavaClassSource decorateSource(UIExecutionContext context, Project project,
JavaClassSource decorator) throws Exception
{
super.decorateSource(context, project, decorator);
decorator.setAbstract(true).addInterface(delegate.getValue()).addAnnotation(Decorator.class);
// Fields
FieldSource<?> field = decorator.addField().setPrivate().setName("delegate").setType(delegate.getValue());
field.addAnnotation(Inject.class);
field.addAnnotation(Delegate.class);
if (enabled.getValue())
return decorator;
}

@Override
public void validate(UIValidationContext validator)
{
super.validate(validator);
checkEnablementConflict(validator,
"Decorator enabled for both the application and the bean archive (beans.xml) will only be invoked in the @Priority part of the chain");
}

@Override
protected void enable(CDIFacet<?> facet, JavaClassSource source)
{
if (facet instanceof CDIFacet_1_0)
{
CDIFacet<?> facet = project.getFacet(CDIFacet.class);
// TODO: Create a parent BeansDescriptor
if (facet instanceof CDIFacet_1_0)
{
CDIFacet_1_0 cdiFacet_1_0 = (CDIFacet_1_0) facet;
BeansDescriptor bd = cdiFacet_1_0.getConfig();
bd.getOrCreateDecorators().clazz(decorator.getQualifiedName());
cdiFacet_1_0.saveConfig(bd);
}
else if (facet instanceof CDIFacet_1_1)
{
CDIFacet_1_1 cdiFacet_1_1 = (CDIFacet_1_1) facet;
org.jboss.shrinkwrap.descriptor.api.beans11.BeansDescriptor bd = cdiFacet_1_1.getConfig();
bd.getOrCreateDecorators().clazz(decorator.getQualifiedName());
cdiFacet_1_1.saveConfig(bd);
}
CDIFacet_1_0 cdiFacet_1_0 = (CDIFacet_1_0) facet;
BeansDescriptor bd = cdiFacet_1_0.getConfig();
bd.getOrCreateDecorators().clazz(source.getQualifiedName());
cdiFacet_1_0.saveConfig(bd);
}
else if (facet instanceof CDIFacet_1_1)
{
CDIFacet_1_1 cdiFacet_1_1 = (CDIFacet_1_1) facet;
org.jboss.shrinkwrap.descriptor.api.beans11.BeansDescriptor bd = cdiFacet_1_1.getConfig();
bd.getOrCreateDecorators().clazz(source.getQualifiedName());
cdiFacet_1_1.saveConfig(bd);
}
return decorator;
}
}
Expand Up @@ -19,6 +19,7 @@
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UIValidationContext;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.util.Metadata;
Expand All @@ -29,17 +30,14 @@
* Creates a new CDI Interceptor
*
* @author <a href="antonio.goncalves@gmail.com">Antonio Goncalves</a>
* @author Martin Kouba
*/
public class CDINewInterceptorCommand extends AbstractCDICommand<JavaClassSource>
public class CDINewInterceptorCommand extends AbstractEnablementCDICommand
{
@Inject
@WithAttributes(label = "Interceptor Binding", required = true)
private UIInput<String> interceptorBinding;

@Inject
@WithAttributes(label = "Enabled", description = "Adds to beans.xml")
private UIInput<Boolean> enabled;

@Override
public Metadata getMetadata(UIContext context)
{
Expand All @@ -53,7 +51,8 @@ public void initializeUI(UIBuilder builder) throws Exception
{
super.initializeUI(builder);
interceptorBinding.setValueConverter(new PackageRootConverter(getProjectFactory(), builder));
builder.add(interceptorBinding).add(enabled);
builder.add(interceptorBinding);
initializeEnablementUI(builder);
}

@Override
Expand All @@ -62,16 +61,11 @@ protected String getType()
return "CDI Interceptor";
}

@Override
protected Class<JavaClassSource> getSourceType()
{
return JavaClassSource.class;
}

@Override
public JavaClassSource decorateSource(UIExecutionContext context, Project project,
JavaClassSource interceptor) throws Exception
{
super.decorateSource(context, project, interceptor);
interceptor.addImport(interceptorBinding.getValue());
interceptor.addAnnotation(interceptorBinding.getValue());
interceptor.addAnnotation(Interceptor.class);
Expand All @@ -84,25 +78,34 @@ public JavaClassSource decorateSource(UIExecutionContext context, Project projec
" } finally {\n" +
" }")
.addAnnotation(AroundInvoke.class);
if (enabled.getValue())
return interceptor;
}

@Override
public void validate(UIValidationContext validator)
{
super.validate(validator);
checkEnablementConflict(validator,
"Interceptor enabled for both the application and the bean archive (beans.xml) will only be invoked in the @Priority part of the chain");
}

@Override
protected void enable(CDIFacet<?> facet, JavaClassSource source)
{
if (facet instanceof CDIFacet_1_0)
{
CDIFacet<?> facet = project.getFacet(CDIFacet.class);
// TODO: Create a parent BeansDescriptor
if (facet instanceof CDIFacet_1_0)
{
CDIFacet_1_0 cdiFacet_1_0 = (CDIFacet_1_0) facet;
BeansDescriptor bd = cdiFacet_1_0.getConfig();
bd.getOrCreateInterceptors().clazz(interceptor.getQualifiedName());
cdiFacet_1_0.saveConfig(bd);
}
else if (facet instanceof CDIFacet_1_1)
{
CDIFacet_1_1 cdiFacet_1_1 = (CDIFacet_1_1) facet;
org.jboss.shrinkwrap.descriptor.api.beans11.BeansDescriptor bd = cdiFacet_1_1.getConfig();
bd.getOrCreateInterceptors().clazz(interceptor.getQualifiedName());
cdiFacet_1_1.saveConfig(bd);
}
CDIFacet_1_0 cdiFacet_1_0 = (CDIFacet_1_0) facet;
BeansDescriptor bd = cdiFacet_1_0.getConfig();
bd.getOrCreateInterceptors().clazz(source.getQualifiedName());
cdiFacet_1_0.saveConfig(bd);
}
else if (facet instanceof CDIFacet_1_1)
{
CDIFacet_1_1 cdiFacet_1_1 = (CDIFacet_1_1) facet;
org.jboss.shrinkwrap.descriptor.api.beans11.BeansDescriptor bd = cdiFacet_1_1.getConfig();
bd.getOrCreateInterceptors().clazz(source.getQualifiedName());
cdiFacet_1_1.saveConfig(bd);
}
return interceptor;
}

}

0 comments on commit 0517bb7

Please sign in to comment.