1 3 package com.sslexplorer.properties; 4 5 import java.util.ArrayList ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Locale ; 9 import java.util.StringTokenizer ; 10 11 import javax.servlet.ServletContext ; 12 import javax.servlet.http.HttpServletRequest ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.apache.struts.Globals; 17 import org.apache.struts.action.Action; 18 import org.apache.struts.config.ModuleConfig; 19 import org.apache.struts.util.MessageResources; 20 import org.apache.struts.util.ModuleUtils; 21 22 import com.sslexplorer.boot.PropertyDefinition; 23 import com.sslexplorer.boot.PropertyList; 24 import com.sslexplorer.boot.TypeMetaListItem; 25 import com.sslexplorer.core.CoreServlet; 26 import com.sslexplorer.input.MultiSelectDataSource; 27 import com.sslexplorer.input.MultiSelectSelectionModel; 28 import com.sslexplorer.security.LogonControllerFactory; 29 import com.sslexplorer.security.SessionInfo; 30 31 37 public class PropertyItem implements Comparable <PropertyItem> { 38 39 final static Log log = LogFactory.getLog(PropertyItem.class); 40 41 43 private PropertyDefinition definition; 44 private Object value; 45 private Pair[] listItems; 46 private int rows, columns; 47 private MultiSelectSelectionModel listDataSourceModel; 48 private HttpServletRequest request; 49 50 59 public PropertyItem(Action action, HttpServletRequest request, 60 PropertyDefinition definition, String value) { 61 if(value == null) { 62 throw new IllegalArgumentException ("Value may not be null."); 63 } 64 65 this.request = request; 66 this.definition = definition; 67 68 rows = 0; 69 columns = 0; 70 71 if (definition.getType() == PropertyDefinition.TYPE_LIST) { 72 List listItemsList = new ArrayList (); 73 if (!definition.getTypeMeta().startsWith("!")) { 74 for (Iterator i = ((List ) definition.getTypeMetaObject()).iterator(); i.hasNext();) { 75 TypeMetaListItem item = (TypeMetaListItem) i.next(); 76 ServletContext context = CoreServlet.getServlet().getServletContext(); 77 ModuleConfig moduleConfig = ModuleUtils.getInstance().getModuleConfig(request, context); 78 String mrKey = (item.getMessageResourcesKey() == null ? "properties" : item.getMessageResourcesKey()) 79 + moduleConfig.getPrefix(); 80 MessageResources res = (MessageResources) context.getAttribute(mrKey); 81 String k = definition.getName() + ".value." + item.getValue(); 82 String v = ""; 83 if (res != null) { 84 v = res.getMessage((Locale )request.getSession().getAttribute(Globals.LOCALE_KEY), k); 85 if (v == null) { 86 v = item.getValue(); 87 } 88 } 89 Pair pair = new Pair(item.getValue(), v); 90 if (item.getValue().equals(value)) { 91 this.value = pair.getValue(); 92 } 93 listItemsList.add(pair); 94 } 95 } 96 else { 97 String className = definition.getTypeMeta().substring(1); 98 try { 99 Class clazz = Class.forName(className); 100 Object obj = clazz.newInstance(); 101 if(obj instanceof PairListDataSource) 102 listItemsList.addAll(((PairListDataSource)obj).getValues(request)); 103 else 104 throw new Exception ("Not a PairListDataSource."); 105 } 106 catch(Exception e) { 107 log.error("Failed to create list data source.", e); 108 } 109 this.value = value; 110 } 111 listItems = new Pair[listItemsList.size()]; 112 listItemsList.toArray(listItems); 113 } else if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST) { 114 this.value = new PropertyList(value).getAsTextFieldText(); 115 StringTokenizer t = new StringTokenizer (definition.getTypeMeta(), 116 "x"); 117 try { 118 if(t.hasMoreTokens()) { 119 columns = Integer.parseInt(t.nextToken()); 120 rows = Integer.parseInt(t.nextToken()); 121 } 122 } catch (NumberFormatException nfe) { 123 124 } 125 } else if (definition.getType() == PropertyDefinition.TYPE_TEXT_AREA) { 126 this.value = value; 127 StringTokenizer t = new StringTokenizer (definition.getTypeMeta(), 128 "x"); 129 try { 130 columns = Integer.parseInt(t.nextToken()); 131 rows = Integer.parseInt(t.nextToken()); 132 } catch (NumberFormatException nfe) { 133 134 } 135 } else if (definition.getType() == PropertyDefinition.TYPE_BOOLEAN) { 136 if(definition.getTypeMetaObject() != null) { 137 String trueVal = (String )(((List )definition.getTypeMetaObject()).get(0)); 138 this.value = value.equals(trueVal) ? Boolean.TRUE : Boolean.FALSE; 139 } 140 else { 141 this.value = Boolean.valueOf(value); 142 } 143 } else if (definition.getType() == PropertyDefinition.TYPE_STRING) { 144 try { 145 columns = Integer.parseInt(definition.getTypeMeta()); 146 } catch (NumberFormatException nfe) { 147 } 148 this.value = value; 149 } else if (definition.getType() == PropertyDefinition.TYPE_INTEGER) { 150 try { 151 columns = Integer.parseInt(definition.getTypeMeta()); 152 } catch (NumberFormatException nfe) { 153 } 154 this.value = value; 155 } else if (definition.getType() == PropertyDefinition.TYPE_PASSWORD) { 156 try { 157 columns = Integer.parseInt(definition.getTypeMeta()); 158 } catch (NumberFormatException nfe) { 159 } 160 this.value = value; 161 } else if (definition.getType() == PropertyDefinition.TYPE_TIME_IN_MS) { 162 try { 163 int val = Integer.parseInt(value); 164 if (definition.getTypeMeta().equalsIgnoreCase("s")) { 165 this.value = String.valueOf(val / 1000); 166 } else if (definition.getTypeMeta().equalsIgnoreCase("m")) { 167 this.value = String.valueOf(val / 1000 / 60); 168 } else if (definition.getTypeMeta().equalsIgnoreCase("h")) { 169 this.value = String.valueOf(val / 1000 / 60 / 60); 170 } else if (definition.getTypeMeta().equalsIgnoreCase("d")) { 171 this.value = String.valueOf(val / 1000 / 60 / 60 / 24); 172 } else { 173 this.value = String.valueOf(val); 174 } 175 } catch (Exception e) { 176 this.value = value; 177 } 178 } else if (definition.getType() == PropertyDefinition.TYPE_MULTI_SELECT_LIST) { 179 PropertyList pList = new PropertyList(value); 180 this.value = pList.getAsTextFieldText(); 181 String clazz = definition.getTypeMeta(); 182 SessionInfo session = LogonControllerFactory.getInstance().getSessionInfo(request); 183 try { 184 listDataSourceModel = new MultiSelectSelectionModel(session, ((MultiSelectDataSource)Class.forName(clazz).newInstance()), pList); 185 } 186 catch(Throwable t) { 187 log.error("Failed to list of available of values. " , t); 188 } 189 190 } else { 191 this.value = value; 192 } 193 194 } 196 197 203 public String getMessageResourcesKey() { 204 return definition.getMessageResourcesKey(); 205 } 206 207 217 public int getColumns() { 218 return columns; 219 } 220 221 228 public int getRows() { 229 return rows; 230 } 231 232 237 public PropertyDefinition getDefinition() { 238 return definition; 239 } 240 241 246 public void setDefinition(PropertyDefinition definition) { 247 this.definition = definition; 248 } 249 250 255 public String getName() { 256 return definition.getName(); 257 } 258 259 265 public int getCategory() { 266 return definition.getCategory(); 267 } 268 269 275 public String getDefaultValue() { 276 return definition.getDefaultValue(); 277 } 278 279 285 public String getDefaultText() { 286 String val = getDefaultValue(); 287 try { 288 if (definition.getType() == PropertyDefinition.TYPE_PASSWORD) { 289 val = ""; 290 } else if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST) { 291 PropertyList list = new PropertyList(definition.getDefaultValue()); 292 val = list.size() > 0 ? list.getPropertyItem(0) : ""; 293 } else if (definition.getType() == PropertyDefinition.TYPE_LIST) { 294 for(int i = 0 ; i < listItems.length; i++) { 295 if(definition.getDefaultValue().equals(listItems[i].getValue())) { 296 val = listItems[i].getLabel(); 297 break; 298 } 299 } 300 } else if (definition.getType() == PropertyDefinition.TYPE_TIME_IN_MS) { 301 try { 302 int defaultItem = Integer 303 .parseInt(definition.getDefaultValue()); 304 if (definition.getTypeMeta().equalsIgnoreCase("s")) { 305 val = String.valueOf(defaultItem / 1000); 306 } else if (definition.getTypeMeta().equalsIgnoreCase("m")) { 307 val = String.valueOf(defaultItem / 1000 / 60); 308 } else if (definition.getTypeMeta().equalsIgnoreCase("h")) { 309 val = String.valueOf(defaultItem / 1000 / 60 / 60); 310 } else if (definition.getTypeMeta().equalsIgnoreCase("d")) { 311 val = String.valueOf(defaultItem / 1000 / 60 / 60 / 24); 312 } else { 313 val = String.valueOf(val); 314 } 315 } catch (Exception e) { 316 val = String.valueOf(val); 317 } 318 } 319 if (val.length() > 15) { 320 val = val.substring(0, 15); 321 } 322 } 323 catch(Throwable t) { 324 } 325 return val; 326 } 327 328 337 public String getTypeMeta() { 338 return definition.getTypeMeta(); 339 } 340 341 344 public Pair[] getListItems() { 345 return listItems; 346 } 347 348 353 public Object getValue() { 354 return value; 355 } 356 357 363 public boolean getSelected() { 364 return value.equals(Boolean.TRUE); 365 } 366 367 373 public void setSelected(boolean selected) { 374 this.value = Boolean.valueOf(selected); 375 } 376 377 383 public void setValue(Object value) { 384 this.value = value; 385 if(getType() == PropertyDefinition.TYPE_MULTI_SELECT_LIST) { 386 PropertyList l = new PropertyList(); 387 l.setAsTextFieldText(getValue().toString()); 388 String clazz = definition.getTypeMeta(); 389 try { 390 SessionInfo session = LogonControllerFactory.getInstance().getSessionInfo(request); 391 listDataSourceModel = new MultiSelectSelectionModel(session, ((MultiSelectDataSource)Class.forName(clazz).newInstance()), l); 392 } 393 catch(Throwable t) { 394 log.error("Failed to list of available of values. " , t); 395 } 396 } 397 } 398 399 405 public int getType() { 406 return definition.getType(); 407 } 408 409 417 public Object getPropertyValue() { 418 if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST || 419 definition.getType() == PropertyDefinition.TYPE_MULTI_SELECT_LIST) { 420 PropertyList l = new PropertyList(); 421 l.setAsTextFieldText(getValue().toString()); 422 return l.getAsPropertyText(); 423 } 424 else if (getDefinition().getType() == PropertyDefinition.TYPE_TIME_IN_MS) { 425 try { 426 int v = Integer.parseInt(getValue().toString()); 427 if (getDefinition().getTypeMeta().equalsIgnoreCase("s")) { 428 v = v * 1000; 429 } else if (getDefinition().getTypeMeta().equalsIgnoreCase("m")) { 430 v = v * 1000 * 60; 431 } else if (getDefinition().getTypeMeta().equalsIgnoreCase("h")) { 432 v = v * 1000 * 60 * 60; 433 } else if (getDefinition().getTypeMeta().equalsIgnoreCase("d")) { 434 v = v * 1000 * 60 * 60 * 24; 435 } 436 return String.valueOf(v); 437 } catch (Exception e) { 438 439 } 440 } 441 return getValue(); 442 } 443 444 450 451 public MultiSelectSelectionModel getListDataSourceModel() { 452 return listDataSourceModel; 453 } 454 455 public int compareTo(PropertyItem o) { 456 return getDefinition().compareTo(o.getDefinition()); 457 } 458 } | Popular Tags |