1 package com.sslexplorer.core; 2 3 import java.util.Locale ; 4 5 import javax.servlet.http.HttpSession ; 6 7 import org.apache.struts.Globals; 8 import org.apache.struts.util.MessageResources; 9 10 import com.sslexplorer.boot.CodedException; 11 12 13 17 public class CoreException extends CodedException { 18 19 22 public static final String DEFAULT_BUNDLE = "errors"; 23 24 26 private BundleActionMessage bundleActionMessage; 27 private String category; 28 29 30 36 public CoreException(int code, String category) { 37 this(code, category, (Throwable )null); 38 } 39 40 47 public CoreException(int code, String category, String arg0) { 48 this(code, category, DEFAULT_BUNDLE, (Throwable )null, arg0); 49 } 50 51 58 public CoreException(int code, String category, Throwable cause) { 59 this(code, category, DEFAULT_BUNDLE, cause); 60 } 61 62 70 public CoreException(int code, String category, String bundle, Throwable cause) { 71 this(code, category, bundle, cause, cause == null ? null : cause.getMessage()); 72 } 73 74 83 public CoreException(int code, String category, String bundle, Throwable cause, String arg0) { 84 this(code, category, bundle, cause, arg0, null, null, null); 85 86 } 87 88 100 public CoreException(int code, String category, String bundle, Throwable cause, String arg0, String arg1, String arg2, String arg3) { 101 super(code, "Error code " + category + "/" + code, cause); 102 this.bundleActionMessage = new BundleActionMessage(bundle, "error." + category + "." + code, arg0, arg1, arg2, arg3); 103 this.category = category; 104 } 105 106 111 public BundleActionMessage getBundleActionMessage(){ 112 return bundleActionMessage; 113 } 114 115 120 public String getBundle() { 121 return bundleActionMessage.getBundle(); 122 } 123 124 130 public String getLocalizedMessage(HttpSession session) { 131 MessageResources mr = getMessageResources(session, bundleActionMessage.getBundle()); 132 if(mr == null) { 133 return "[Could not locate message resources for bundle "+ bundleActionMessage.getBundle() +"]"; 134 } 135 Locale l = (Locale )session.getAttribute(Globals.LOCALE_KEY); 136 l = l == null ? Locale.getDefault() : l; 137 String message = mr.getMessage(l, bundleActionMessage.getKey(), bundleActionMessage.getArg0(), bundleActionMessage.getArg1(), bundleActionMessage.getArg2(), bundleActionMessage.getArg3()); 138 if(message == null) { 139 message = super.getMessage(); 140 } 141 return message; 142 } 143 144 protected MessageResources getMessageResources(HttpSession session, String bundle) { 145 return CoreUtil.getMessageResources(session, bundle); 146 } 147 148 protected MessageResources getMessageResources(String bundle) { 149 return (MessageResources)CoreServlet.getServlet().getServletContext().getAttribute( 150 bundle); 151 } 152 153 156 public String getMessage() { 157 MessageResources cmr = getMessageResources(getBundle()); 158 String message = null; 159 if(cmr != null) { 160 message = cmr.getMessage(bundleActionMessage.getKey(), bundleActionMessage.getArg0(), bundleActionMessage.getArg1(), bundleActionMessage.getArg2(), bundleActionMessage.getArg3()); 161 } 162 if(message == null) { 163 message = super.getMessage(); 164 } 165 return message; 166 } 167 168 173 public String getCategory() { 174 return category; 175 } 176 177 } 178 | Popular Tags |