001 package railo.runtime.tag; 002 003 import railo.runtime.exp.ApplicationException; 004 import railo.runtime.exp.PageException; 005 import railo.runtime.exp.SecurityException; 006 import railo.runtime.ext.tag.TagImpl; 007 import railo.runtime.op.Caster; 008 import railo.runtime.registry.RegistryEntry; 009 import railo.runtime.registry.RegistryQuery; 010 import railo.runtime.security.SecurityManager; 011 import railo.runtime.type.KeyImpl; 012 import railo.runtime.type.QueryImpl; 013 014 /** 015 * Reads, writes, and deletes keys and values in the system registry. The cfregistry tag is supported 016 * on all platforms, including Linux, Solaris, and HP-UX. 017 * 018 * 019 * 020 **/ 021 public final class Registry extends TagImpl { 022 023 private static final short ACTION_GET_ALL=0; 024 private static final short ACTION_GET=1; 025 private static final short ACTION_SET=2; 026 private static final short ACTION_DELETE=3; 027 028 029 030 /** Value data to set. If you omit this attribute, cfregistry creates default value, as follows: 031 ** 032 ** string: creates an empty string: "" 033 ** dWord: creates a value of 0 (zero) */ 034 private String value; 035 036 /** action to the registry */ 037 private short action=-1; 038 039 /** Sorts query column data (case-insensitive). Sorts on Entry, Type, and Value columns as text. Specify a combination of columns from query output, in a comma-delimited list. For example: 040 ** sort = "value desc, entry asc" 041 ** 042 ** asc: ascending (a to z) sort order 043 ** desc: descending (z to a) sort order */ 044 private String sort; 045 046 /** string: return string values 047 ** dWord: return DWord values 048 ** key: return keys 049 ** any: return keys and values */ 050 private short type=RegistryEntry.TYPE_ANY; 051 052 /** Name of a registry branch. */ 053 private String branch; 054 055 /** Registry value to access. */ 056 private String entry; 057 058 /** Variable into which to put value. */ 059 private String variable; 060 061 /** Name of record set to contain returned keys and values. */ 062 private String name; 063 064 065 /** 066 * @see javax.servlet.jsp.tagext.Tag#release() 067 */ 068 public void release() { 069 super.release(); 070 value=null; 071 action=-1; 072 sort=null; 073 type=RegistryEntry.TYPE_ANY; 074 branch=null; 075 entry=null; 076 variable=null; 077 name=null; 078 } 079 080 /** set the value value 081 * Value data to set. If you omit this attribute, cfregistry creates default value, as follows: 082 * 083 * string: creates an empty string: "" 084 * dWord: creates a value of 0 (zero) 085 * @param value value to set 086 **/ 087 public void setValue(String value) { 088 this.value=value; 089 } 090 091 /** set the value action 092 * action to the registry 093 * @param action value to set 094 * @throws ApplicationException 095 **/ 096 public void setAction(String action) throws ApplicationException { 097 action=action.toLowerCase().trim(); 098 if(action.equals("getall")) this.action=ACTION_GET_ALL; 099 else if(action.equals("get")) this.action=ACTION_GET; 100 else if(action.equals("set")) this.action=ACTION_SET; 101 else if(action.equals("delete")) this.action=ACTION_DELETE; 102 else throw new ApplicationException("attribute action of the tag registry has a invalid value ["+action+"], valid values are [getAll, get, set, delete]"); 103 } 104 105 /** set the value sort 106 * Sorts query column data (case-insensitive). Sorts on Entry, Type, and Value columns as text. Specify a combination of columns from query output, in a comma-delimited list. For example: 107 * sort = "value desc, entry asc" 108 * 109 * asc: ascending (a to z) sort order 110 * desc: descending (z to a) sort order 111 * @param sort value to set 112 **/ 113 public void setSort(String sort) { 114 this.sort=sort; 115 } 116 117 /** set the value type 118 * string: return string values 119 * dWord: return DWord values 120 * key: return keys 121 * any: return keys and values 122 * @param type value to set 123 * @throws ApplicationException 124 **/ 125 public void setType(String type) throws ApplicationException { 126 type=type.toLowerCase().trim(); 127 if(type.equals("string")) this.type=RegistryEntry.TYPE_STRING; 128 else if(type.equals("dword")) this.type=RegistryEntry.TYPE_DWORD; 129 else if(type.equals("key")) this.type=RegistryEntry.TYPE_KEY; 130 else if(type.equals("any")) this.type=RegistryEntry.TYPE_ANY; 131 else throw new ApplicationException("attribute type of the tag registry has a invalid value ["+type+"], valid values are [string, dword]"); 132 } 133 134 /** set the value branch 135 * Name of a registry branch. 136 * @param branch value to set 137 **/ 138 public void setBranch(String branch) { 139 this.branch=branch; 140 } 141 142 /** set the value entry 143 * Registry value to access. 144 * @param entry value to set 145 **/ 146 public void setEntry(String entry) { 147 this.entry=entry; 148 } 149 150 /** set the value variable 151 * Variable into which to put value. 152 * @param variable value to set 153 **/ 154 public void setVariable(String variable) { 155 this.variable=variable; 156 } 157 158 /** set the value name 159 * Name of record set to contain returned keys and values. 160 * @param name value to set 161 **/ 162 public void setName(String name) { 163 this.name=name; 164 } 165 166 167 /** 168 * @see javax.servlet.jsp.tagext.Tag#doStartTag() 169 */ 170 public int doStartTag() throws PageException { 171 if(pageContext.getConfig().getSecurityManager().getAccess(SecurityManager.TYPE_TAG_REGISTRY)==SecurityManager.VALUE_NO) 172 throw new SecurityException("can't access tag [registry]","access is prohibited by security manager"); 173 174 175 if(action==ACTION_GET) doGet(); 176 else if(action==ACTION_GET_ALL) doGetAll(); 177 else if(action==ACTION_SET) doSet(); 178 else if(action==ACTION_DELETE) doDelete(); 179 180 181 182 return SKIP_BODY; 183 } 184 185 /** 186 * @throws PageException 187 * 188 */ 189 private void doDelete() throws PageException { 190 try { 191 RegistryQuery.deleteValue(branch,entry); 192 } 193 catch (Exception e) { 194 throw Caster.toPageException(e); 195 } 196 } 197 198 /** 199 * @throws PageException 200 * 201 */ 202 private void doSet() throws PageException { 203 if(entry==null)throw new ApplicationException("attribute entry is required for tag registry, when action is [set]"); 204 if(type==RegistryEntry.TYPE_ANY)type=RegistryEntry.TYPE_STRING; 205 if(value==null) { 206 if(type==RegistryEntry.TYPE_DWORD)value="0"; 207 else value=""; 208 } 209 210 try { 211 RegistryQuery.setValue(branch,entry,type,value); 212 } 213 catch (Exception e) { 214 throw Caster.toPageException(e); 215 } 216 217 } 218 219 /** 220 * @throws PageException 221 * 222 */ 223 private void doGetAll() throws PageException { 224 if(name==null)throw new ApplicationException("attribute name is required for tag registry, when action is [getAll]"); 225 226 227 try { 228 RegistryEntry[] entries = RegistryQuery.getValues(branch,type); 229 if(entries!=null) { 230 railo.runtime.type.Query qry=new QueryImpl( 231 new String[]{"entry","type","value"}, 232 new String[]{"VARCHAR","VARCHAR","OTHER"}, 233 entries.length,"query"); 234 for(int i=0;i<entries.length;i++) { 235 RegistryEntry e = entries[i]; 236 int row=i+1; 237 qry.setAt(KeyImpl.ENTRY,row,e.getKey()); 238 qry.setAt(KeyImpl.TYPE,row,RegistryEntry.toCFStringType(e.getType())); 239 qry.setAt(KeyImpl.VALUE,row,e.getValue()); 240 } 241 242 243 // sort 244 if(sort!=null) { 245 String[] arr=sort.toLowerCase().split(","); 246 for(int i=arr.length-1;i>=0;i--) { 247 String[] col=arr[i].trim().split("\\s+"); 248 if(col.length==1)qry.sort(KeyImpl.init(col[0].trim())); 249 else if(col.length==2) { 250 String order=col[1].toLowerCase().trim(); 251 if(order.equals("asc")) 252 qry.sort(KeyImpl.init(col[0]),railo.runtime.type.Query.ORDER_ASC); 253 else if(order.equals("desc")) 254 qry.sort(KeyImpl.init(col[0]),railo.runtime.type.Query.ORDER_DESC); 255 else 256 throw new ApplicationException("invalid order type ["+col[1]+"]"); 257 } 258 } 259 } 260 261 262 263 264 265 266 267 pageContext.setVariable(name,qry); 268 } 269 } 270 catch (Exception e) { 271 throw Caster.toPageException(e); 272 } 273 274 } 275 276 /** 277 * @throws PageException 278 * 279 */ 280 private void doGet() throws PageException { 281 // "HKEY_LOCAL_MACHINE\\SOFTWARE\\Google\\NavClient","installtime" 282 if(entry==null)throw new ApplicationException("attribute entry is required for tag registry, when action is [get]"); 283 if(variable==null)throw new ApplicationException("attribute variable is required for tag registry, when action is [get]"); 284 //if(type==-1)throw new ApplicationException("attribute type is required for tag registry, when action is [get]"); 285 286 287 try { 288 RegistryEntry re = RegistryQuery.getValue(branch,entry,type); 289 if(re!=null)pageContext.setVariable(variable,re.getValue()); 290 } 291 catch (Exception e) { 292 throw Caster.toPageException(e); 293 } 294 295 } 296 297 /** 298 * @see javax.servlet.jsp.tagext.Tag#doEndTag() 299 */ 300 public int doEndTag() { 301 return EVAL_PAGE; 302 } 303 304 }