1 45 package org.openejb.util; 46 47 import java.util.Properties ; 48 49 import org.openejb.OpenEJBException; 50 51 public class SafeProperties{ 52 53 private String systemLocation; 54 private Properties props; 55 56 61 public SafeProperties(Properties props, String systemLocation) throws OpenEJBException{ 62 if (props == null) OpenEJBErrorHandler.propertiesObjectIsNull(systemLocation); 63 this.props = props; 64 this.systemLocation = systemLocation; 65 } 66 67 72 public String getProperty(String key) throws OpenEJBException{ 73 String value = props.getProperty(key); 74 if (value == null)OpenEJBErrorHandler.propertyNotFound(key, systemLocation+ " properties object"); 75 return value; 76 } 77 82 public String getProperty(String key, String defaultValue) throws OpenEJBException{ 83 String value = props.getProperty(key); 84 if (value == null) 85 return defaultValue; 86 else 87 return value; 88 } 89 90 95 public int getPropertyAsInt(String key) throws OpenEJBException{ 96 int integer = 0; 97 String value = getProperty(key); 98 try{ 99 integer = Integer.parseInt(value); 100 } 101 catch(NumberFormatException nfe){ 102 OpenEJBErrorHandler.propertyValueIsIllegal(key, value); 103 } 104 return integer; 105 } 106 111 public int getPropertyAsInt(String key, int defaultValue) throws OpenEJBException{ 112 int integer = defaultValue; 113 String value = getProperty(key,String.valueOf(defaultValue)); 114 try{ 115 integer = Integer.parseInt(value); 116 } 117 catch(NumberFormatException nfe){ 118 OpenEJBErrorHandler.propertyValueIsIllegal(key, value); 119 } 120 return integer; 121 } 122 127 public Integer getPropertyAsInteger(String key, Integer defaultValue) throws OpenEJBException{ 128 Integer integer = null; 129 String value = getProperty(key,defaultValue.toString()); 130 try{ 131 integer = new Integer (value); 132 } 133 catch(NumberFormatException nfe){ 134 OpenEJBErrorHandler.propertyValueIsIllegal(key, value); 135 } 136 return integer; 137 } 138 143 public Integer getPropertyAsInteger(String key) throws OpenEJBException{ 144 Integer integer = null; 145 String value = getProperty(key); 146 try{ 147 integer = new Integer (value); 148 } 149 catch(NumberFormatException nfe){ 150 OpenEJBErrorHandler.propertyValueIsIllegal(key, value); 151 } 152 return integer; 153 } 154 158 public boolean getPropertyAsBoolean(String key) throws OpenEJBException{ 159 Integer integer = null; 160 String value = getProperty(key); 161 return new Boolean (value).booleanValue(); 162 } 163 167 public Boolean getPropertyAsBoolean(String key, Boolean defaultValue) throws OpenEJBException{ 168 Integer integer = null; 169 String value = getProperty(key, defaultValue.toString()); 170 return new Boolean (value); 171 } 172 173 }
| Popular Tags
|