package com.newatlanta.cfc; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.naryx.tagfusion.cfm.engine.cfComponentData; import com.naryx.tagfusion.cfm.engine.cfData; import com.naryx.tagfusion.cfm.engine.cfEngine; import com.naryx.tagfusion.cfm.engine.cfSession; import com.naryx.tagfusion.cfm.engine.cfcMethodData; import com.naryx.tagfusion.cfm.engine.cfmRunTimeException; import com.naryx.tagfusion.cfm.tag.tagUtils; public class CFCProxy { private cfSession session; private cfComponentData compData; private Writer writer; /* * CFCProxy * * Creates a wrapper around a CFC so it can be called from a JSP or servlet. * This version doesn't write the output generated by the CFC methods. */ public CFCProxy(String compName, HttpServletRequest req, HttpServletResponse res) throws cfmRunTimeException { this(compName, req, res, null); } /* * CFCProxy * * Creates a wrapper around a CFC so it can be called from a JSP or servlet. * This version writes the output generated by the CFC methods to the passed in Writer. */ public CFCProxy(String compName, HttpServletRequest req, HttpServletResponse res, Writer writer) throws cfmRunTimeException { // If BlueDragon hasn't been initialized yet then throw an exception // NOTE: we can't initialize BlueDragon here because we don't have access to its // ServletConfig data. // NOTE: if the BlueDragon cfServlet is configured to load on startup in web.xml // then this should never happen. if ( cfEngine.thisInstance == null ) throw new IllegalStateException("BlueDragon must be initialized before creating this object."); // Create the cfSession instance session = new cfSession(req, res, cfEngine.thisServletContext); // Create the cfComponentData instance compData = new cfComponentData(session, compName); this.writer = writer; } /* * invoke * * Invokes the specified CFC method and writes the output to the passed in Writer. */ public Object invoke(String methodName, Object... args) throws cfmRunTimeException, IOException { try { // Convert the arguments to CF data types and place them in a vector array list List<cfData> arguments = new ArrayList<cfData>(args.length); for ( int i = 0; i < args.length; i++ ) arguments.add( tagUtils.convertToCfData(args[i]) ); // Create the invocation data object cfcMethodData invocationData = new cfcMethodData(session, methodName, arguments); // Invoke the CFC metbod cfData retval = compData.invokeComponentFunction( session, invocationData ); // Write the output if ( writer != null ) writer.write( session.RES.getOutputAsString() ); // Convert the returned CF data object to its java equivalent return tagUtils.getNatural( retval ); } finally { // Make sure per request connections are closed (bug NA#3174) session.sessionEnd(); session.resetBuffer(); } } /* * getThisScope * * Returns the component data as a Map object. */ public Map getThisScope() { return compData; } }