Skip to content

Commit

Permalink
It WORKS, UIInput is injected
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Dec 20, 2012
1 parent ad3ab62 commit 07509d1
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 7 deletions.
2 changes: 2 additions & 0 deletions ui/api/src/main/java/org/jboss/forge/ui/UIInput.java
Expand Up @@ -26,4 +26,6 @@ public interface UIInput<T>
UIInput<T> setRequired(boolean required);

UIInput<T> setRequired(Callable<Boolean> required);

UIInput<T> setValue(T value);
}
2 changes: 0 additions & 2 deletions ui/bom/pom.xml
Expand Up @@ -15,13 +15,11 @@
<artifactId>ui-api</artifactId>
<scope>compile</scope>
</dependency>
<!--
<dependency>
<groupId>org.jboss.forge</groupId>
<artifactId>ui-impl</artifactId>
<scope>runtime</scope>
</dependency>
-->
</dependencies>
<build>
<plugins>
Expand Down
32 changes: 32 additions & 0 deletions ui/impl/pom.xml
@@ -0,0 +1,32 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jboss.forge</groupId>
<artifactId>ui-parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<artifactId>ui-impl</artifactId>
<name>Forge - UI Impl</name>
<dependencies>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.forge</groupId>
<artifactId>ui-api</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.forge</groupId>
<artifactId>forge-test-harness</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.forge</groupId>
<artifactId>arquillian-forge-classpath</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
85 changes: 85 additions & 0 deletions ui/impl/src/main/java/org/jboss/forge/ui/impl/Callables.java
@@ -0,0 +1,85 @@
/*
* Copyright 2012 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.ui.impl;

import java.util.concurrent.Callable;

/**
* Utilities to handle {@link Callable} objects
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/

public final class Callables
{
private Callables()
{
}

/**
* Wrap a constant value into a Callable Object
*
* @param value
* @return
*/
public static <T> Callable<T> constant(T value)
{
return new ConstantCallable<T>(value);
}

/**
* Calls the {@link Callable} avoiding the checked exception
*
* @param c
* @return
*/
public static <T> T call(Callable<T> c)
{
if (c == null)
{
return null;
}
try
{
return c.call();
}
catch (RuntimeException re)
{
throw re;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}

/**
* Simple callable class that returns the same value
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
* @param <V>
*/
static class ConstantCallable<V> implements Callable<V>
{
private final V value;

public ConstantCallable(V value)
{
this.value = value;
}

@Override
public V call()
{
return value;
}
}

}
95 changes: 95 additions & 0 deletions ui/impl/src/main/java/org/jboss/forge/ui/impl/UIInputImpl.java
@@ -0,0 +1,95 @@
/*
* Copyright 2012 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.ui.impl;

import java.util.concurrent.Callable;

import org.jboss.forge.ui.UIInput;

/**
* Implementation of a {@link UIInput} object
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
* @param <T>
*/
public class UIInputImpl<T> implements UIInput<T>
{
private final String name;
private final Class<T> type;

private T value;
private Callable<Boolean> required;
private Callable<T> defaultValue;

public UIInputImpl(String name, Class<T> type)
{
this.name = name;
this.type = type;
}

@Override
public String getName()
{
return name;
}

@Override
public Class<T> getType()
{
return type;
}

@Override
public T getValue()
{
return (value == null) ? Callables.call(defaultValue) : value;
}

@Override
public boolean isRequired()
{
return Callables.call(required);
}

@Override
public UIInput<T> setDefaultValue(T value)
{
this.defaultValue = Callables.constant(value);
return this;
}

@Override
public UIInput<T> setDefaultValue(Callable<T> callback)
{
this.defaultValue = callback;
return this;
}

@Override
public UIInput<T> setRequired(boolean required)
{
this.required = Callables.constant(required);
return this;
}

@Override
public UIInput<T> setRequired(Callable<Boolean> required)
{
this.required = required;
return this;
}

@Override
public UIInput<T> setValue(T value)
{
this.value = value;
return this;
}

}
34 changes: 34 additions & 0 deletions ui/impl/src/main/java/org/jboss/forge/ui/impl/UIInputProducer.java
@@ -0,0 +1,34 @@
/*
* Copyright 2012 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.ui.impl;

import java.lang.reflect.ParameterizedType;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.forge.ui.UIInput;

/**
* Produces UIInput objects
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*
*/
public class UIInputProducer
{
@SuppressWarnings("unchecked")
@Produces
public <T> UIInput<T> produceInput(InjectionPoint injectionPoint)
{
String name = injectionPoint.getMember().getName();
ParameterizedType ptype = (ParameterizedType) injectionPoint.getType();
Class<T> c = (Class<T>) ptype.getActualTypeArguments()[0];
return new UIInputImpl<T>(name, c);
}
}
3 changes: 3 additions & 0 deletions ui/impl/src/main/resources/META-INF/beans.xml
@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://jboss.org/schema/cdi/beans_1_0.xsd" />
@@ -0,0 +1,57 @@
/*
* Copyright 2012 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.ui.impl;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.arquillian.Addon;
import org.jboss.forge.arquillian.Dependencies;
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.ui.UI;
import org.jboss.forge.ui.UIInput;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class UIInputInjectionTest
{
@Deployment
@Dependencies(@Addon(name = "org.jboss.forge:facets", version = "2.0.0-SNAPSHOT"))
public static ForgeArchive getDeployment()
{
ForgeArchive archive = ShrinkWrap
.create(ForgeArchive.class)
.addPackages(true, UI.class.getPackage())
.addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));

System.out.println(archive);
return archive;
}

@Inject
UIInput<String> firstName;

@Test
public void testInjectionNotNull()
{
Assert.assertNotNull(firstName);
}

@Test
public void testInputValues()
{
Assert.assertEquals("firstName", firstName.getName());
Assert.assertEquals(String.class, firstName.getType());
}
}
6 changes: 1 addition & 5 deletions ui/pom.xml
Expand Up @@ -12,9 +12,7 @@
<name>Forge - UI AddOn Parent</name>
<modules>
<module>api</module>
<!--
<module>impl</module>
-->
<module>bom</module>
</modules>
<dependencyManagement>
Expand All @@ -24,13 +22,11 @@
<artifactId>ui-api</artifactId>
<version>${project.version}</version>
</dependency>
<!--
<dependency>
<groupId>org.jboss.forge</groupId>
<artifactId>resource-impl</artifactId>
<artifactId>ui-impl</artifactId>
<version>${project.version}</version>
</dependency>
-->
</dependencies>
</dependencyManagement>
</project>

0 comments on commit 07509d1

Please sign in to comment.