001    package railo.runtime.net.rpc.server;
002    
003    import javax.xml.rpc.encoding.TypeMapping;
004    
005    import org.apache.axis.AxisFault;
006    
007    import railo.runtime.Component;
008    import railo.runtime.PageContext;
009    import railo.runtime.exp.ApplicationException;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.net.rpc.AxisCaster;
012    import railo.runtime.op.Caster;
013    import railo.runtime.type.Collection.Key;
014    import railo.runtime.type.UDF;
015    
016    /**
017     * 
018     */
019    public final class ComponentController {
020    
021            private static ThreadLocal<Component> component=new ThreadLocal<Component>();
022            private static ThreadLocal<PageContext> pagecontext=new ThreadLocal<PageContext>();
023    
024            /**
025             * invokes thread local component
026             * @param name
027             * @param args
028             * @return
029             * @throws AxisFault 
030             * @throws PageException
031             */
032            public static Object invoke(String name, Object[] args) throws AxisFault {
033                    try {
034                            return _invoke(name, args);
035                    } 
036                    catch (Throwable t) {
037                            throw AxisFault.makeFault((Caster.toPageException(t)));
038                    }
039            }
040            public static Object _invoke(String name, Object[] args) throws PageException {
041                    Key key = Caster.toKey(name);
042                    Component c=component.get();
043                    PageContext p=pagecontext.get();
044                    if(c==null) throw new ApplicationException("missing component");
045                    if(p==null) throw new ApplicationException("missing pagecontext");
046                    
047                    for(int i=0;i<args.length;i++) {
048                            args[i]=AxisCaster.toRailoType(p,args[i]);
049                    }
050                    
051                    Object udf = c.get(p,key,null);
052                    String rt="any";
053                    if(udf instanceof UDF) {
054                            rt=((UDF)udf).getReturnTypeAsString();
055                    }
056                    Object rv = c.call(p, key, args);
057                    
058                    try {
059                            RPCServer server = RPCServer.getInstance(p.getId(),p.getServletContext());
060                            TypeMapping tm = server.getEngine().getTypeMappingRegistry().getDefaultTypeMapping();
061                            rv=Caster.castTo(p, rt, rv, false);
062                            Class clazz = Caster.cfTypeToClass(rt);
063                            return AxisCaster.toAxisType(tm,rv,clazz.getComponentType()!=null?clazz:null);
064                    } 
065                    catch (Throwable t) {
066                            throw Caster.toPageException(t);
067                    }
068            }
069    
070            /**
071             * removes PageContext and Component
072             * sets component and pageContext to invoke
073             * @param p
074             * @param c
075             */
076            public static void set(PageContext p,Component c) {
077                    pagecontext.set(p);
078                    component.set(c);
079            }
080            
081            /**
082             * 
083             */
084            public static void release() {
085                    pagecontext.set(null);
086                    component.set(null);
087            }
088    }