⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertyitem.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 */
package com.sslexplorer.properties;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.ModuleUtils;

import com.sslexplorer.boot.PropertyDefinition;
import com.sslexplorer.boot.PropertyList;
import com.sslexplorer.boot.TypeMetaListItem;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.input.MultiSelectDataSource;
import com.sslexplorer.input.MultiSelectSelectionModel;

/**
 * Wrapper bean used for displaying {@link com.sslexplorer.boot.PropertyDefinition}s
 * and their values in the various configuration pages.
 * 
 * @author Brett Smith <a href="mailto:brett@3sp.com">&lt;brett@3sp.com&gt;</a>
 * @version $Revision: 1.5 $
 */
public class PropertyItem {
    
    final static Log log  = LogFactory.getLog(PropertyItem.class);
    
    // Private instance variables
    
	private PropertyDefinition definition;
	private Object value;
	private Pair[] listItems;
	private int rows, columns;
    private MultiSelectSelectionModel listDataSourceModel;
    
    /**
     * Constructor
     * 
     * @param action the struts action used to display this bean
     * @param request the request object used to display the bean
     * @param definition the property definition to display
     * @param value the value to display
     */
	public PropertyItem(Action action, HttpServletRequest request,
			PropertyDefinition definition, String value) {
		this.definition = definition;

		rows = 0;
		columns = 0;

		if (definition.getType() == PropertyDefinition.TYPE_LIST) {
			List listItemsList = new ArrayList();
			for(Iterator i = ((List)definition.getTypeMetaObject()).iterator(); i.hasNext(); ) {
			    TypeMetaListItem item = (TypeMetaListItem)i.next();
		        ServletContext context = CoreServlet.getServlet().getServletContext();
		        ModuleConfig moduleConfig =
		            ModuleUtils.getInstance().getModuleConfig(request, context);	
		        String mrKey = (item.getMessageResourcesKey() == null ? "properties" : item.getMessageResourcesKey())  + moduleConfig.getPrefix();
			     MessageResources res = (MessageResources) context.getAttribute(
			 		            mrKey);
				String k = definition.getName() + ".value." + item.getValue();
				String v = ""; 
				if(res != null) {
				    v = res.getMessage(k);
                    if(v == null) {
                        v = item.getValue();
                    }
				}
				Pair pair = new Pair(item.getValue(), v);
				if (item.getValue().equals(value)) {
					this.value = pair.getValue();
				}
				listItemsList.add(pair);
			}
			listItems = new Pair[listItemsList.size()];
			listItemsList.toArray(listItems);
		} else if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST) {
			this.value = new PropertyList(value).getAsTextFieldText();
			StringTokenizer t = new StringTokenizer(definition.getTypeMeta(),
					"x");
			try {
                if(t.hasMoreTokens()) {
    				columns = Integer.parseInt(t.nextToken());
    				rows = Integer.parseInt(t.nextToken());
                }
			} catch (NumberFormatException nfe) {

			}
		} else if (definition.getType() == PropertyDefinition.TYPE_TEXT_AREA) {
			this.value = value;
			StringTokenizer t = new StringTokenizer(definition.getTypeMeta(),
					"x");
			try {
				columns = Integer.parseInt(t.nextToken());
				rows = Integer.parseInt(t.nextToken());
			} catch (NumberFormatException nfe) {

			}
		} else if (definition.getType() == PropertyDefinition.TYPE_BOOLEAN) {
            if(definition.getTypeMetaObject() != null) {
                String trueVal = (String)(((List)definition.getTypeMetaObject()).get(0));
                this.value = value.equals(trueVal) ? Boolean.TRUE : Boolean.FALSE;                
            }
            else {
                this.value = Boolean.valueOf(value);           
            }
		} else if (definition.getType() == PropertyDefinition.TYPE_STRING) {
			try {
				columns = Integer.parseInt(definition.getTypeMeta());
			} catch (NumberFormatException nfe) {
			}
			this.value = value;
		} else if (definition.getType() == PropertyDefinition.TYPE_PASSWORD) {
			try {
				columns = Integer.parseInt(definition.getTypeMeta());
			} catch (NumberFormatException nfe) {
			}
			this.value = value;
		} else if (definition.getType() == PropertyDefinition.TYPE_TIME_IN_MS) {
			try {
				int val = Integer.parseInt(value);
				if (definition.getTypeMeta().equalsIgnoreCase("s")) {
					this.value = String.valueOf(val / 1000);
				} else if (definition.getTypeMeta().equalsIgnoreCase("m")) {
					this.value = String.valueOf(val / 1000 / 60);
				} else if (definition.getTypeMeta().equalsIgnoreCase("h")) {
					this.value = String.valueOf(val / 1000 / 60 / 60);
				} else if (definition.getTypeMeta().equalsIgnoreCase("d")) {
					this.value = String.valueOf(val / 1000 / 60 / 60 / 24);
				} else {
					this.value = String.valueOf(val);
				}
			} catch (Exception e) {
				this.value = value;
			}
        } else if (definition.getType() == PropertyDefinition.TYPE_MULTI_SELECT_LIST) {
            PropertyList pList = new PropertyList(value); 
            this.value = pList.getAsTextFieldText();
            String clazz = definition.getTypeMeta();
            try {
                listDataSourceModel = new MultiSelectSelectionModel(((MultiSelectDataSource)Class.forName(clazz).newInstance()), pList);
            }
            catch(Throwable t) {
                log.error("Failed to list of available of values. " , t);
            }
            
		} else {
			this.value = value;
		}

		// 
	}
	
    /**
     * Get the message resource key (i.e. bundle ID) of the bundle that
     * contains the message resources for this property definition.
     * 
     * @return message resources key (bundle)
     */
	public String getMessageResourcesKey() {
	    return definition.getMessageResourcesKey();
	}

    /**
     * Get the number of columns appropriate for this property definition. Only
     * applies to property definitions of type {@link PropertyDefinition#TYPE_PASSWORD},
     * {@link PropertyDefinition#TYPE_STRING}, {@link PropertyDefinition#TYPE_TEXT_AREA}
     * and {@link PropertyDefinition#TYPE_TIME_IN_MS}
     * 
     * @return number of columns
     */
	public int getColumns() {
		return columns;
	}

    /**
     * Get the number of row appropriate for this property definition. Only
     * applies to property definitions of type {@link PropertyDefinition#TYPE_TEXT_AREA}.
     * 
     * @return number of rows
     */
	public int getRows() {
		return rows;
	}

	/**
     * Get the property definition this object wraps
     * 
	 * @return property definition
	 */
	public PropertyDefinition getDefinition() {
		return definition;
	}

	/**
     * Set the property definition this object wraps
     * 
	 * @param definition property definition
	 */
	public void setDefinition(PropertyDefinition definition) {
		this.definition = definition;
	}

	/**
     * Convience method to get the name of the property definition this object wraps.
     * 
	 * @return the name.
	 */
	public String getName() {
		return definition.getName();
	}

	/**
     * Convience method to get the category ID this property definition this 
     * object wraps is in.
     * 
	 * @return the category ID.
	 */
	public int getCategory() {
		return definition.getCategory();
	}

	/**
     * Convience method to get the default value of the property defintiion 
     * this object wraps.
     * 
	 * @return Returns the value.
	 */
	public String getDefaultValue() {
		return definition.getDefaultValue();
	}

	/**
     * Get the default of the property definition this object wraps as 
     * text.
     * 
	 * @return Returns the value.
	 */
	public String getDefaultText() {
		String val = getDefaultValue();
		try {
		if (definition.getType() == PropertyDefinition.TYPE_PASSWORD) {
			val = "";
		} else if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST) {
			PropertyList list = new PropertyList(definition.getDefaultValue());
			val = list.size() > 0 ? list.getPropertyItem(0) : "";
		} else if (definition.getType() == PropertyDefinition.TYPE_LIST) {
		    for(int i = 0 ; i < listItems.length; i++) {
		        if(definition.getDefaultValue().equals(listItems[i].getValue())) {
		            val = listItems[i].getLabel();
		            break;
		        }
		    }
		} else if (definition.getType() == PropertyDefinition.TYPE_TIME_IN_MS) {
			try {
				int defaultItem = Integer
						.parseInt(definition.getDefaultValue());
				if (definition.getTypeMeta().equalsIgnoreCase("s")) {
					val = String.valueOf(defaultItem / 1000);
				} else if (definition.getTypeMeta().equalsIgnoreCase("m")) {
					val = String.valueOf(defaultItem / 1000 / 60);
				} else if (definition.getTypeMeta().equalsIgnoreCase("h")) {
					val = String.valueOf(defaultItem / 1000 / 60 / 60);
				} else if (definition.getTypeMeta().equalsIgnoreCase("d")) {
					val = String.valueOf(defaultItem / 1000 / 60 / 60 / 24);
				} else {
					val = String.valueOf(val);
				}
			} catch (Exception e) {
				val = String.valueOf(val);
			}
		}
		if (val.length() > 15) {
			val = val.substring(0, 15);
		}
		}
		catch(Throwable t) {
		}
		return val;
	}

	/**
     * Convience method to get the property definition 'type meta', the 
     * string that describes and constraints for the type of property definition. 
     * For example 40x5 may be the 'type meta' for a property definition of 
     * type {@link PropertyDefinition#TYPE_TEXT_AREA} means supply a text area
     * with 40 columns and 5 rows.
     * 
	 * @return get the type meta
	 */
	public String getTypeMeta() {
		return definition.getTypeMeta();
	}

	/**
	 * @return Returns the value.
	 */
	public Pair[] getListItems() {
		return listItems;
	}

	/**
     * Get the value as an object
     * 
	 * @return the value.
	 */
	public Object getValue() {
		return value;
	}

	/**
     * For boolean property definitions such as checkkbox 
     * that will return <code>true</code> if the value is true. 
     * 
	 * @return selected.
	 */
	public boolean getSelected() {
		return value.equals(Boolean.TRUE);
	}

	/**
     * Set the value as a boolean. For boolean property definitions such as 
     * checkbox.
     * 
     *  @param selected selected
	 */
	public void setSelected(boolean selected) {
		this.value = Boolean.valueOf(selected);
	}

	/**
     * Set the value from a generic object. The actual value will
     * be determined depending on the property definitions type.
     * 
	 * @param value the value to set.
	 */
	public void setValue(Object value) {
		this.value = value;
        if(getType() == PropertyDefinition.TYPE_MULTI_SELECT_LIST) {
            PropertyList l = new PropertyList();
            l.setAsTextFieldText(getValue().toString());
            String clazz = definition.getTypeMeta();
            try {
                listDataSourceModel = new MultiSelectSelectionModel(((MultiSelectDataSource)Class.forName(clazz).newInstance()), l);
            }
            catch(Throwable t) {
                log.error("Failed to list of available of values. " , t);
            }
        }
	}

    /**
     * Convience method to get the type of the property definition. See
     * {@link PropertyDefinition#getType()} for a more details description.
     * 
     * @return type
     */
	public int getType() {
		return definition.getType();
	}

    /**
     * Get the value as an object suitable for displaying with the appropriate
     * component. For most types a {@link String} is returned, but other
     * default struts supported type are also used e.g. {@link Boolean} or
     * {@link Integer}.
     * 
     * @return value
     */
    public Object getPropertyValue() {
        if (definition.getType() == PropertyDefinition.TYPE_MULTI_ENTRY_LIST ||
                        definition.getType() == PropertyDefinition.TYPE_MULTI_SELECT_LIST) {
            PropertyList l = new PropertyList();
            l.setAsTextFieldText(getValue().toString());
            return l.getAsPropertyText();
        }
        else if (getDefinition().getType() == PropertyDefinition.TYPE_TIME_IN_MS) {
            try {
                int v = Integer.parseInt(getValue().toString());
                if (getDefinition().getTypeMeta().equalsIgnoreCase("s")) {
                    v = v * 1000;
                } else if (getDefinition().getTypeMeta().equalsIgnoreCase("m")) {
                    v = v * 1000 * 60;
                } else if (getDefinition().getTypeMeta().equalsIgnoreCase("h")) {
                    v = v * 1000 * 60 * 60;
                } else if (getDefinition().getTypeMeta().equalsIgnoreCase("d")) {
                    v = v * 1000 * 60 * 60 * 24;
                }
                return String.valueOf(v);
            } catch (Exception e) {

            }
        }
        return getValue();
    }
    
    /**
     * Get the list data source model that may be used as the source list
     * for property definitions of type {@link PropertyDefinition#TYPE_MULTI_SELECT_LIST}
     * 
     * @return list data source model
     */
    
    public MultiSelectSelectionModel getListDataSourceModel() {
        return listDataSourceModel;
    }
    
    // Supporting classes

	public class Pair {
		Object value;

		String label;

		public Pair(Object value, String label) {
			this.value = value;
			this.label = label;
		}

		public Object getValue() {
			return value;
		}

		public String getLabel() {
			return label;
		}

		public void setValue(Object value) {
			this.value = value;
		}

		public String toString() {
			return "pair[label=" + label + ",value=" + value + "]";
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -