001 package net.sf.tacos.seam; 002 003 import static org.jboss.seam.ScopeType.STATELESS; 004 import static org.jboss.seam.ScopeType.UNSPECIFIED; 005 006 import org.apache.hivemind.ApplicationRuntimeException; 007 import org.jboss.seam.Component; 008 import org.jboss.seam.ScopeType; 009 import org.jboss.seam.contexts.Context; 010 import org.jboss.seam.core.Expressions; 011 012 /** 013 * Class responsible for bijection of Seam components. 014 * 015 * @author Igor Drobiazko 016 */ 017 public class Bijection { 018 019 public static void outject(Object obj, String name, String scopeName, String required){ 020 ScopeType scope = ScopeType.valueOf(scopeName); 021 if (obj==null && Boolean.valueOf(required)){ 022 throw new ApplicationRuntimeException("@Out requires non-null value: " +name); 023 } 024 if(scope==STATELESS){ 025 throw new ApplicationRuntimeException( 026 "cannot specify explicit scope=STATELESS on @Out: " +name); 027 } 028 if(!scope.isContextActive())return; 029 Context context = scope.getContext(); 030 if (obj==null){ 031 context.remove(name); 032 }else{ 033 context.set(name, obj); 034 } 035 } 036 037 public static Object inject(Class<?> clazz, String name, ScopeType scope, boolean required, boolean create){ 038 if(name.startsWith("#")){ 039 Expressions e = Expressions.instance(); 040 return e.createValueExpression(name).getValue(); 041 } 042 if(scope==STATELESS){ 043 throw new ApplicationRuntimeException( 044 "cannot specify explicit scope=STATELESS on @In: " +name); 045 } 046 if(scope==UNSPECIFIED){ 047 return Component.getInstance(clazz, create); 048 } 049 if(scope.isContextActive()){ 050 Object result = scope.getContext().get(name); 051 if(result==null && required){ 052 throw new ApplicationRuntimeException("@In requires non-null value: " +name); 053 } 054 return result; 055 } 056 return null; 057 } 058 }