1 package org.sapia.magnet.domain; 2 3 import org.apache.log4j.Logger; 6 7 import org.sapia.magnet.MagnetException; 10 import org.sapia.magnet.Log; 11 import org.sapia.magnet.render.AbstractRenderable; 12 import org.sapia.magnet.render.MagnetContext; 13 import org.sapia.magnet.render.RenderingException; 14 15 16 27 public class Script extends AbstractRenderable { 28 29 33 34 private static final Logger _theLogger = Logger.getLogger(Script.class); 35 36 40 41 private String _theType; 42 43 44 private String _theProfile; 45 46 47 private String _theCode; 48 49 50 private String _isAbortingOnError; 51 52 56 59 public Script() { 60 _isAbortingOnError = "false"; 61 } 62 63 67 72 public String getType() { 73 return _theType; 74 } 75 76 81 public String getProfile() { 82 return _theProfile; 83 } 84 85 90 public String getCode() { 91 return _theCode; 92 } 93 94 99 public boolean isAbortingOnError() { 100 return _isAbortingOnError.equals("true"); 101 } 102 103 107 112 public void setType(String aType) { 113 _theType = aType; 114 } 115 116 121 public void setProfile(String aProfile) { 122 _theProfile = aProfile; 123 } 124 125 130 public void setCode(String aCode) { 131 _theCode = aCode; 132 } 133 134 139 public void setText(String aContent) { 140 _theCode = aContent; 141 } 142 143 148 public void setIsAbortingOnError(String isAbortingOnError) { 149 _isAbortingOnError = isAbortingOnError; 150 } 151 152 156 162 public void render(MagnetContext aContext) throws RenderingException { 163 try { 165 _isAbortingOnError = resolveValue(aContext, _isAbortingOnError); 166 _theCode = resolveValue(aContext, _theCode); 167 } catch (RenderingException re) { 168 StringBuffer aBuffer = new StringBuffer (); 169 aBuffer.append("Unable to resolve an attribute of the script of type "). 170 append(_theType); 171 if (_theProfile != null) { 172 aBuffer.append(" for the profile ").append(_theProfile); 173 } else { 174 aBuffer.append(" for the default profile"); 175 } 176 177 throw new RenderingException(aBuffer.toString(), re); 178 } 179 180 try { 181 ScriptHandlerIF aHandler = HandlerFactory.getInstance().createScriptHandler(_theType); 183 aHandler.execute(_theCode); 184 } catch (ObjectCreationException oce) { 185 StringBuffer aBuffer = new StringBuffer (); 186 aBuffer.append("Unable to create a script handler of type "). 187 append(_theType); 188 if (_theProfile != null) { 189 aBuffer.append(" for the profile ").append(_theProfile); 190 } else { 191 aBuffer.append(" for the default profile"); 192 } 193 194 if (isAbortingOnError()) { 195 throw new RenderingException(aBuffer.toString(), oce); 196 } else { 197 aBuffer.append(": ").append(Log.extactMessage(oce)); 198 Log.warn(aBuffer.toString()); 199 } 200 } catch (MagnetException de) { 201 StringBuffer aBuffer = new StringBuffer (); 202 aBuffer.append("Unable to execute the script of type "). 203 append(_theType); 204 if (_theProfile != null) { 205 aBuffer.append(" for the profile ").append(_theProfile); 206 } else { 207 aBuffer.append(" for the default profile"); 208 } 209 210 if (isAbortingOnError()) { 211 throw new RenderingException(aBuffer.toString(), de); 212 } else { 213 aBuffer.append(": ").append(Log.extactMessage(de)); 214 Log.warn(aBuffer.toString()); 215 } 216 } catch (RuntimeException re) { 217 StringBuffer aBuffer = new StringBuffer (); 218 aBuffer.append("System error executing the script of type "). 219 append(_theType); 220 if (_theProfile != null) { 221 aBuffer.append(" for the profile ").append(_theProfile); 222 } else { 223 aBuffer.append(" for the default profile"); 224 } 225 226 if (isAbortingOnError()) { 227 throw new RenderingException(aBuffer.toString(), re); 228 } else { 229 aBuffer.append(": ").append(Log.extactMessage(re)); 230 Log.warn(aBuffer.toString()); 231 } 232 } 233 } 234 235 240 public String toString() { 241 StringBuffer aBuffer = new StringBuffer (super.toString()); 242 aBuffer.append("[type=").append(_theType). 243 append(" profile=").append(_theProfile). 244 append(" abortOnError=").append(_isAbortingOnError). 245 append(" code=").append(_theCode). 246 append("]"); 247 248 return aBuffer.toString(); 249 } 250 } 251 | Popular Tags |