1 14 package org.wings.plaf.css; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.wings.session.SessionManager; 19 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.util.Properties ; 23 24 public class CSSLookAndFeel 25 extends org.wings.plaf.LookAndFeel { 26 private static final String WEB_INF = "WEB-INF/"; 27 private final transient static Log log = LogFactory.getLog(CSSLookAndFeel.class); 28 private static final String PROPERTIES_FILENAME_START = CSSLookAndFeel.class.getPackage().getName(); 29 private static final String PROPERTIES_FILENAME_END = ".properties"; 30 private static final String PROPERTIES_CLASSPATH = PROPERTIES_FILENAME_START.replace('.','/').concat("/"); 31 32 public CSSLookAndFeel() throws IOException { 33 super(loadProperties()); 34 } 35 36 private static Properties loadProperties() throws IOException { 37 StringBuffer propertiesFilename = new StringBuffer (PROPERTIES_FILENAME_START); 39 propertiesFilename.append(PROPERTIES_FILENAME_END); 40 String browserType = SessionManager.getSession().getUserAgent().getBrowserType().getShortName(); 42 StringBuffer browserPropertiesFilename = new StringBuffer (PROPERTIES_FILENAME_START); 43 browserPropertiesFilename.append("."); 44 browserPropertiesFilename.append(browserType); 45 browserPropertiesFilename.append(PROPERTIES_FILENAME_END); 46 47 Properties properties = new Properties (); 48 properties = loadProperties(propertiesFilename); 49 try { 50 properties.putAll(loadProperties(browserPropertiesFilename)); 51 } catch (IOException e) { 52 log.info("An Exception occured while loading browser specific properties. This is OK."); 53 } 54 return properties; 55 } 56 57 67 private static Properties loadProperties(StringBuffer propertiesFilename) throws IOException { 68 Properties properties; 69 InputStream in; 70 IOException finalException = null; 71 final String classPath = PROPERTIES_CLASSPATH + propertiesFilename.toString(); 73 properties = loadPropertiesFromClasspath(classPath); 74 String webappUrl = WEB_INF + propertiesFilename.toString(); 76 properties.putAll(loadPropertiesFromContainer(webappUrl)); 77 return properties; 78 } 79 80 85 private static Properties loadPropertiesFromContainer(String webappUrl) { 86 Properties properties = new Properties (); 87 InputStream in; 88 try { 89 in = SessionManager.getSession().getServletContext().getResourceAsStream(webappUrl); 90 properties.load(in); 91 in.close(); 92 } catch (Exception e) { 93 final String error = "Unable to open " + webappUrl + " due to "+e+".\nIt seems you didn't provide a custom config file."; 94 log.warn(error); 95 } 96 return properties; 97 } 98 99 105 private static Properties loadPropertiesFromClasspath(final String classPath) throws IOException { 106 Properties properties = new Properties (); 107 InputStream in; 108 try { 109 in = CSSLookAndFeel.class.getClassLoader().getResourceAsStream(classPath); 110 properties.load(in); 111 in.close(); 112 } catch (Exception e) { 113 final String error = "Unable to open " + classPath + " from classPath due to "+e+".\nPlease check deployment!"; 114 log.warn(error); 115 throw new IOException (error); 116 } 117 return properties; 118 } 119 } 120 121 122 | Popular Tags |