1 package org.apache.turbine.services.servlet; 2 3 18 19 import java.io.InputStream ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import javax.servlet.ServletConfig ; 23 import javax.servlet.ServletContext ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.turbine.Turbine; 28 import org.apache.turbine.services.TurbineBaseService; 29 import org.apache.turbine.util.ServletUtils; 30 31 45 public class TurbineServletService 46 extends TurbineBaseService implements ServletService 47 { 48 49 private static Log log = LogFactory.getLog(TurbineServletService.class); 50 51 52 private ServletContext servletContext = null; 53 54 55 private ServletConfig servletConfig = null; 56 57 62 public void init() 63 { 64 this.servletConfig = Turbine.getTurbineServletConfig(); 65 try 66 { 67 this.servletContext = servletConfig.getServletContext(); 68 69 log.debug("Initializing with ServletConfig"); 70 } 71 catch (Exception e) 72 { 73 log.error("Cannot initialize TurbineServletService.", e); 74 } 75 setInit(true); 76 } 77 78 85 public void init(ServletConfig servletConfig) 86 { 87 init(); 88 } 89 90 99 public URL getResource(String uri) 100 { 101 if (servletContext == null) 102 { 103 return null; 104 } 105 106 URL url = null; 107 108 try 109 { 110 url = getServletContext().getResource(uri); 111 if (url != null && url.toString().startsWith("classloader:")) 113 { 114 url = new URL ("file:" + url.toString().substring(12)); 115 } 116 else if (url == null) 117 { 118 url = new URL ("file:" + getServletContext().getRealPath(uri)); 119 } 120 } 121 catch (MalformedURLException e) 122 { 123 } 125 126 return url; 127 } 128 129 136 public InputStream getResourceAsStream(String uri) 137 { 138 if (servletContext == null) 139 { 140 return null; 141 } 142 143 InputStream is = null; 144 is = servletContext.getResourceAsStream(uri); 145 return is; 146 } 147 148 156 public String getRealPath(String uri) 157 { 158 if (getServletContext() == null || uri == null) 159 { 160 return null; 161 } 162 else 163 { 164 return getServletContext().getRealPath(uri); 165 } 166 } 167 168 174 public ServletConfig getServletConfig() 175 { 176 return servletConfig; 177 } 178 179 185 public ServletContext getServletContext() 186 { 187 return servletContext; 188 } 189 190 197 public String getServerScheme() 198 { 199 return Turbine.getServerScheme(); 200 } 201 202 209 public String getServerName() 210 { 211 return Turbine.getServerName(); 212 } 213 214 221 public String getServerPort() 222 { 223 return Turbine.getServerPort(); 224 } 225 226 232 public String getContextPath() 233 { 234 return Turbine.getContextPath(); 235 } 236 237 246 public String expandRelative(String path) 247 { 248 return ServletUtils.expandRelative(getServletConfig(), path); 249 } 250 } 251 | Popular Tags |