View Javadoc
1 package com.fc.taglibs.castor; 2 3 import java.io.*; 4 import java.util.ArrayList; 5 import javax.naming.*; 6 import javax.servlet.jsp.*; 7 import javax.servlet.jsp.tagext.*; 8 import org.apache.taglibs.standard.lang.support.*; 9 10 import org.exolab.castor.jdo.DataObjects; 11 import org.exolab.castor.jdo.Database; 12 import org.exolab.castor.jdo.ClassNotPersistenceCapableException; 13 import org.exolab.castor.jdo.TransactionNotInProgressException; 14 import org.exolab.castor.jdo.PersistenceException; 15 16 public class CastorLoadTag extends BodyTagSupport 17 { 18 19 /*** 20 * The name of this tag 21 */ 22 private static String _tagName = "CastorLoadTag"; 23 24 /*** 25 * The object to load 26 */ 27 private Object data = null; 28 29 /*** 30 * The Class of the object to load 31 */ 32 private Class c = null; 33 34 /*** 35 * The database to perform the update 36 */ 37 private Database db = null; 38 39 /*** 40 * The id by which we will refer to the data object in the JSP page. 41 */ 42 private String id; 43 44 /*** 45 * The fully qualified class name of the bean returned 46 */ 47 private String className; 48 49 /*** 50 * The primary key of the bean in the database 51 */ 52 private String key; 53 54 55 /*** 56 * The scope the bean will be placed in. 57 * Default to page scope 58 */ 59 private String scope = "page"; 60 61 /*** 62 * For determining access mode 63 */ 64 private boolean accessModeSet = false; 65 private short _accessMode = Database.Shared; 66 private String accessMode = null; 67 68 /*** 69 * Sets the name of the page scoped attribute 70 * that is the bean we are deleting/updating 71 */ 72 public void setId(String _id) 73 { 74 this.id = _id; 75 } 76 77 /*** 78 * Sets the classname 79 */ 80 public void setClassName(String _class) 81 { 82 this.className = _class; 83 } 84 85 86 /*** 87 * Sets the primary key 88 */ 89 public void setKey(String _key) 90 { 91 this.key = _key; 92 } 93 94 /*** 95 * Sets the scope of the bean 96 */ 97 public void setScope(String _scope) 98 { 99 this.scope = _scope; 100 } 101 102 /*** 103 * Sets the access mode string 104 */ 105 public void setAccessMode(String _accessModeStr) 106 { 107 this.accessModeSet = true; 108 this.accessMode = _accessModeStr; 109 } 110 111 /*** 112 * Do the start tag bit 113 */ 114 public int doStartTag() throws JspException 115 { 116 117 try 118 { 119 /*** 120 * Determine the accessmode 121 */ 122 if (this.accessModeSet) 123 { 124 if (this.accessMode.equals("DbLocked")) 125 this._accessMode = Database.DbLocked; 126 else if (this.accessMode.equals("Exclusive")) 127 this._accessMode = Database.Exclusive; 128 else if (this.accessMode.equals("Shared")) 129 this._accessMode = Database.Shared; 130 else if (this.accessMode.equals("ReadOnly")) 131 this._accessMode = Database.ReadOnly; 132 else 133 { 134 String msg = "Invalid Access Mode set."; 135 log(msg , null); 136 throw new JspTagException(msg); 137 } 138 } 139 140 141 /* 142 * Check to see if this tag is nested within a transactional tag 143 * Get the transaction. Exception if no Transaction parent tag found. 144 */ 145 CastorTransactionInitiator t = (CastorTransactionInitiator)findAncestorWithClass(this, CastorTransactionInitiator.class); 146 if (t!=null) 147 { 148 db = t.getDatabase(); 149 log ("Got transaction from parent tag", null); 150 151 /* 152 * Evaluate the primary key 153 */ 154 Object pk = null; 155 if (this.key != null) 156 { 157 pk = (Object)ExpressionEvaluatorManager.evaluate("dataSource",this.key, Object.class, this, pageContext); 158 } 159 160 /* 161 * Create an instance of the Class we are retrieving 162 */ 163 c = Class.forName(this.className); 164 165 /* 166 * Attempt to load the object only if the key is not null 167 */ 168 log("Loading object of type " + c + " with key " + pk, null); 169 170 if (pk!=null) 171 data = db.load(c,pk,_accessMode); 172 else 173 data = c.newInstance(); 174 /* 175 * Put the returned object in the 176 * specified scope. 177 */ 178 179 if (this.scope.equals("request")) 180 pageContext.setAttribute(this.id, data, PageContext.REQUEST_SCOPE); 181 else if (this.scope.equals("page")) 182 pageContext.setAttribute(this.id, data, PageContext.PAGE_SCOPE); 183 else if (this.scope.equals("session")) 184 pageContext.setAttribute(this.id, data, PageContext.SESSION_SCOPE); 185 else if (this.scope.equals("application")) 186 pageContext.setAttribute(this.id, data, PageContext.APPLICATION_SCOPE); 187 188 } 189 else 190 { 191 String msg = "Tag not nested within a CastorTransactionTag."; 192 log(msg,null); 193 throw new Exception(msg); 194 } 195 } 196 catch(TransactionNotInProgressException e) 197 { 198 log(e.getMessage() , e); 199 throw new JspTagException(e.getMessage()); 200 } 201 catch(PersistenceException e) 202 { 203 log(e.getMessage() , e); 204 throw new JspTagException(e.getMessage()); 205 } 206 catch(Exception e) 207 { 208 log(e.getMessage() , e); 209 throw new JspTagException(e.getMessage()); 210 } 211 212 return EVAL_PAGE; 213 } 214 215 private void log(String msg,Exception e) 216 { 217 if (e!=null) 218 { 219 System.err.println(_tagName + ": " + msg); 220 e.printStackTrace(); 221 } 222 else 223 { 224 System.out.println(_tagName + ": " + msg); 225 } 226 } 227 228 }

This page was automatically generated by Maven