Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seamfaces 147 2 - second approach #66

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
<groupId>org.jboss.solder</groupId>
<artifactId>solder-api</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.solder</groupId>
<artifactId>solder-impl</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.seam.security</groupId>
<artifactId>seam-security-api</artifactId>
Expand Down
48 changes: 48 additions & 0 deletions api/src/main/java/org/jboss/seam/faces/event/PhaseIdType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,59 @@
*/
package org.jboss.seam.faces.event;

import javax.faces.event.PhaseId;

/**
* Enum values corresponding to the phases of the JSF life-cycle.
*
* @author <a href="mailto:[email protected]">Brian Leathem</a>
*/
public enum PhaseIdType {
ANY_PHASE, RESTORE_VIEW, APPLY_REQUEST_VALUES, PROCESS_VALIDATIONS, UPDATE_MODEL_VALUES, INVOKE_APPLICATION, RENDER_RESPONSE;

public static PhaseId convert(PhaseIdType phaseIdType) {
switch (phaseIdType) {
case RESTORE_VIEW :
return PhaseId.RESTORE_VIEW;
case APPLY_REQUEST_VALUES :
return PhaseId.APPLY_REQUEST_VALUES;
case PROCESS_VALIDATIONS :
return PhaseId.PROCESS_VALIDATIONS;
case UPDATE_MODEL_VALUES :
return PhaseId.UPDATE_MODEL_VALUES;
case INVOKE_APPLICATION :
return PhaseId.INVOKE_APPLICATION;
case RENDER_RESPONSE :
return PhaseId.RENDER_RESPONSE;
case ANY_PHASE :
return PhaseId.ANY_PHASE;
default :
throw new IllegalArgumentException ("Couldn't convert "+phaseIdType+" to JSF phase Id");
}
}

public static PhaseIdType convert(PhaseId phaseId) {
if (PhaseId.RESTORE_VIEW.equals(phaseId)) {
return RESTORE_VIEW;
}
if (PhaseId.APPLY_REQUEST_VALUES.equals(phaseId)) {
return APPLY_REQUEST_VALUES;
}
if (PhaseId.PROCESS_VALIDATIONS.equals(phaseId)) {
return PROCESS_VALIDATIONS;
}
if (PhaseId.UPDATE_MODEL_VALUES.equals(phaseId)) {
return UPDATE_MODEL_VALUES;
}
if (PhaseId.INVOKE_APPLICATION.equals(phaseId)) {
return INVOKE_APPLICATION;
}
if (PhaseId.RENDER_RESPONSE.equals(phaseId)) {
return RENDER_RESPONSE;
}
if (PhaseId.ANY_PHASE.equals(phaseId)) {
return ANY_PHASE;
}
throw new IllegalArgumentException ("Couldn't convert "+phaseId+" to JSF phase Id");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@
*/
package org.jboss.seam.faces.event.qualifier;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.inject.Qualifier;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Qualifies observer method parameters to select events that occur in a "after" phase in the JSF lifecycle
*
* @author Nicklas Karlsson
*/
@Qualifier
@Target({FIELD, PARAMETER})
@Target({TYPE, FIELD, METHOD, PARAMETER})
@Retention(RUNTIME)
public @interface After {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import javax.inject.Qualifier;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
Expand All @@ -36,7 +38,7 @@
* @see javax.faces.event.PhaseEvent
*/
@Qualifier
@Target({FIELD, PARAMETER})
@Target({TYPE, FIELD, METHOD, PARAMETER})
@Retention(RUNTIME)
public @interface ApplyRequestValues {
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,24 @@
*/
package org.jboss.seam.faces.event.qualifier;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.inject.Qualifier;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Qualifies observer method parameters to select events that occur in a "before" phase in the JSF lifecycle
*
* @author Nicklas Karlsson
*/
@Qualifier
@Target({FIELD, PARAMETER})
@Target({TYPE, FIELD, METHOD, PARAMETER})
@Retention(RUNTIME)
public @interface Before {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import javax.inject.Qualifier;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
Expand All @@ -34,7 +36,7 @@
* @author Nicklas Karlsson
*/
@Qualifier
@Target({FIELD, PARAMETER})
@Target({TYPE, FIELD, METHOD, PARAMETER})
@Retention(RUNTIME)
public @interface InvokeApplication {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import javax.inject.Qualifier;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
Expand All @@ -35,7 +37,7 @@
* @After}. The event parameter is a {@link PhaseEvent}.
*/
@Qualifier
@Target({FIELD, PARAMETER})
@Target({TYPE, FIELD, METHOD, PARAMETER})
@Retention(RUNTIME)
public @interface ProcessValidations {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import javax.inject.Qualifier;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
Expand All @@ -34,7 +36,7 @@
* @author Nicklas Karlsson
*/
@Qualifier
@Target({FIELD, PARAMETER})
@Target({TYPE, FIELD, METHOD, PARAMETER})
@Retention(RUNTIME)
public @interface RenderResponse {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import javax.inject.Qualifier;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
Expand All @@ -34,7 +36,7 @@
* @author Nicklas Karlsson
*/
@Qualifier
@Target({FIELD, PARAMETER})
@Target({TYPE, FIELD, METHOD, PARAMETER})
@Retention(RUNTIME)
public @interface RestoreView {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import javax.inject.Qualifier;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
Expand All @@ -35,7 +37,7 @@
* @After}. The event parameter is a {@link PhaseEvent}.
*/
@Qualifier
@Target({FIELD, PARAMETER})
@Target({TYPE, FIELD, METHOD, PARAMETER})
@Retention(RUNTIME)
public @interface UpdateModelValues {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.jboss.seam.faces.view.action;

import java.lang.reflect.Method;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedMethod;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;

import org.jboss.solder.reflection.annotated.InjectableMethod;

/**
* Invokes method on a CDI bean.
*
* Note : copy from Seam Security's SecurityExtension class. Should be extracted into common utility.
*/
public abstract class AnnotatedMethodViewActionHandler implements ViewActionHandler {
private Bean<?> targetBean;
private BeanManager beanManager;
private InjectableMethod<?> injectableMethod;
private AnnotatedMethod<?> annotatedMethod;

public AnnotatedMethodViewActionHandler(AnnotatedMethod<?> annotatedMethod, BeanManager beanManager) {
this.beanManager = beanManager;
this.annotatedMethod = annotatedMethod;
}

public AnnotatedMethod<?> getAnnotatedMethod() {
return annotatedMethod;
}

public void setAnnotatedMethod(AnnotatedMethod<?> annotatedMethod) {
this.annotatedMethod = annotatedMethod;
}

public BeanManager getBeanManager() {
return beanManager;
}

public Object execute() {
if (targetBean == null) {
lookupTargetBean();
}
CreationalContext<?> cc = beanManager.createCreationalContext(targetBean);
Object reference = beanManager.getReference(targetBean, getAnnotatedMethod().getJavaMember().getDeclaringClass(),
cc);
return injectableMethod.invoke(reference, cc, null);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private synchronized void lookupTargetBean() {
if (targetBean == null) {
AnnotatedMethod annotatedMethod = getAnnotatedMethod();
Method method = annotatedMethod.getJavaMember();
Set<Bean<?>> beans = beanManager.getBeans(method.getDeclaringClass());
if (beans.size() == 1) {
targetBean = beans.iterator().next();
} else if (beans.isEmpty()) {
throw new IllegalStateException("Exception looking up method bean - " + "no beans found for method ["
+ method.getDeclaringClass() + "." + method.getName() + "]");
} else if (beans.size() > 1) {
throw new IllegalStateException("Exception looking up method bean - " + "multiple beans found for method ["
+ method.getDeclaringClass().getName() + "." + method.getName() + "]");
}
injectableMethod = new InjectableMethod(annotatedMethod, targetBean, beanManager);
}
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder(this.getClass().getSimpleName());
builder.append("{method: ").append(getAnnotatedMethod()).append("}");
return builder.toString();
}
}
22 changes: 22 additions & 0 deletions api/src/main/java/org/jboss/seam/faces/view/action/Order.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jboss.seam.faces.view.action;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* This optionnal annotation can be used to modify the execution order of viewActions.
*
* <p>viewActions will be executed from lowest to highest order. If order is not specified,
* The default value {@link OrderDefault#DEFAULT} is assumed.</p>
*
* @author Adriàn Gonzalez
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Order {
int value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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 org.jboss.seam.faces.view.action;

/**
* Default value for viewAction order.
*/
public class OrderDefault {
public static final int DEFAULT = 1000;
}
Loading