001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.thread; 020 021import java.io.OutputStream; 022 023import javax.servlet.http.Cookie; 024import javax.servlet.http.HttpServletRequest; 025import javax.servlet.http.HttpServletResponse; 026 027import lucee.commons.io.DevNullOutputStream; 028import lucee.commons.lang.Pair; 029import lucee.runtime.CFMLFactory; 030import lucee.runtime.CFMLFactoryImpl; 031import lucee.runtime.PageContext; 032import lucee.runtime.PageContextImpl; 033import lucee.runtime.config.Config; 034import lucee.runtime.config.ConfigImpl; 035import lucee.runtime.config.ConfigWeb; 036import lucee.runtime.net.http.HTTPServletRequestWrap; 037import lucee.runtime.net.http.HttpServletRequestDummy; 038import lucee.runtime.net.http.HttpServletResponseDummy; 039import lucee.runtime.type.Struct; 040 041public class ThreadUtil { 042 043 044 public static PageContextImpl clonePageContext(PageContext pc, OutputStream os, boolean stateless,boolean registerPC,boolean isChild) { 045 // TODO stateless 046 CFMLFactoryImpl factory = (CFMLFactoryImpl) ((ConfigImpl)pc.getConfig()).getFactory(); 047 HttpServletRequest req=new HTTPServletRequestWrap(cloneHttpServletRequest(pc)); 048 HttpServletResponse rsp=createHttpServletResponse(os); 049 050 // copy state 051 PageContextImpl pci = (PageContextImpl) pc; 052 PageContextImpl dest = factory.getPageContextImpl(factory.getServlet(), req, rsp, null, false, -1, false,registerPC, isChild); 053 pci.copyStateTo(dest); 054 return dest; 055 } 056 057 public static PageContextImpl createPageContext(ConfigWeb config,OutputStream os,String serverName,String requestURI,String queryString,Cookie[] cookies,Pair[] headers,Pair[] parameters,Struct attributes) { 058 CFMLFactory factory = config.getFactory(); 059 HttpServletRequest req = new HttpServletRequestDummy( 060 config.getRootDirectory(), 061 serverName, 062 requestURI, 063 queryString, 064 cookies, 065 headers, 066 parameters, 067 attributes, 068 null 069 ); 070 071 072 req=new HTTPServletRequestWrap(req); 073 HttpServletResponse rsp=createHttpServletResponse(os); 074 075 return (PageContextImpl) factory.getLuceePageContext( 076 factory.getServlet(), 077 req, rsp, null, false, -1, false); 078 079 } 080 081 082 public static HttpServletRequest cloneHttpServletRequest(PageContext pc) { 083 Config config = pc.getConfig(); 084 HttpServletRequest req = pc.getHttpServletRequest(); 085 HttpServletRequestDummy dest = HttpServletRequestDummy.clone(config,config.getRootDirectory(),req); 086 return dest; 087 } 088 089 public static HttpServletResponse createHttpServletResponse(OutputStream os) { 090 if(os==null) os = DevNullOutputStream.DEV_NULL_OUTPUT_STREAM; 091 092 HttpServletResponseDummy dest = new HttpServletResponseDummy(os); 093 return dest; 094 } 095 096 /** 097 * return priority as a String representation 098 * @param priority Thread priority 099 * @return String defintion of priority (null when input is invalid) 100 */ 101 public static String toStringPriority(int priority) { 102 if(priority==Thread.NORM_PRIORITY) return "NORMAL"; 103 if(priority==Thread.MAX_PRIORITY) return "HIGH"; 104 if(priority==Thread.MIN_PRIORITY) return "LOW"; 105 return null; 106 } 107 108 /** 109 * return priority as a int representation 110 * @param priority Thread priority as String definition 111 * @return int defintion of priority (-1 when input is invalid) 112 */ 113 public static int toIntPriority(String strPriority) { 114 strPriority=strPriority.trim().toLowerCase(); 115 116 if("low".equals(strPriority)) return Thread.MIN_PRIORITY; 117 if("min".equals(strPriority)) return Thread.MIN_PRIORITY; 118 if("high".equals(strPriority)) return Thread.MAX_PRIORITY; 119 if("max".equals(strPriority)) return Thread.MAX_PRIORITY; 120 if("normal".equals(strPriority)) return Thread.NORM_PRIORITY; 121 if("norm".equals(strPriority)) return Thread.NORM_PRIORITY; 122 return -1; 123 } 124}