1 package org.sapia.magnet.domain; 2 3 import java.util.ArrayList ; 6 import java.util.Collection ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.List ; 10 import java.util.Map ; 11 12 import org.sapia.magnet.render.AbstractRenderable; 15 import org.sapia.magnet.render.MagnetContext; 16 import org.sapia.magnet.render.RenderingException; 17 18 19 30 public class Parameters extends AbstractRenderable { 31 32 36 37 private String _theProfile; 38 39 40 private List _theParams; 41 42 43 private Map _theParamsByName; 44 45 49 52 public Parameters() { 53 _theParams = new ArrayList (); 54 _theParamsByName = new HashMap (); 55 } 56 57 62 public Parameters(String aProfile) { 63 _theParams = new ArrayList (); 64 _theParamsByName = new HashMap (); 65 _theProfile = aProfile; 66 } 67 68 72 77 public String getProfile() { 78 return _theProfile; 79 } 80 81 86 public Collection getParams() { 87 return _theParams; 88 } 89 90 98 public Param getParam(String aName) { 99 if (aName == null) { 100 throw new IllegalArgumentException ("The name passed in is null"); 101 } 102 103 return (Param) _theParamsByName.get(aName); 104 } 105 106 110 115 public void setProfile(String aProfile) { 116 _theProfile = aProfile; 117 } 118 119 126 public void addParam(Param aParam) { 127 if (aParam == null) { 128 throw new IllegalArgumentException ("The parameter passed in is null"); 129 } else if (aParam.getName() == null) { 130 throw new IllegalArgumentException ("The name of the parameter passed in is null"); 131 } 132 133 Param anOldParam = (Param) _theParamsByName.get(aParam.getName()); 134 if (anOldParam != null) { 135 _theParams.remove(anOldParam); 136 } 137 138 _theParams.add(aParam); 139 _theParamsByName.put(aParam.getName(), aParam); 140 } 141 142 149 public void removeParam(Param aParam) { 150 if (aParam == null) { 151 throw new IllegalArgumentException ("The parameter passed in is null"); 152 } else if (aParam.getName() == null) { 153 throw new IllegalArgumentException ("The name of the parameter passed in is null"); 154 } 155 156 _theParams.remove(aParam); 157 _theParamsByName.remove(aParam.getName()); 158 } 159 160 163 public void clearParams() { 164 _theParams.clear(); 165 _theParamsByName.clear(); 166 } 167 168 172 178 public void render(MagnetContext aContext) throws RenderingException { 179 if (_theProfile == null || 180 (aContext.getProfile() != null && _theProfile.equals(aContext.getProfile()))) { 181 182 for (Iterator it = _theParams.iterator(); it.hasNext(); ) { 183 Param aParam = (Param) it.next(); 184 185 if ((aParam.getIfDefine() == null || 187 aContext.getValue(aParam.getIfDefine()) != null) && 188 (aParam.getUnlessDefine() == null || 189 aContext.getValue(aParam.getUnlessDefine()) == null)) { 190 191 aParam.render(aContext); 193 194 if (aParam.getScope().equals(Param.SCOPE_MAGNET)) { 196 aContext.addParameter(aParam, (_theProfile != null)); 197 } else if (aParam.getScope().equals(Param.SCOPE_SYSTEM)) { 198 System.setProperty(aParam.getName(), aParam.getValue()); 199 } else { 200 StringBuffer aBuffer = new StringBuffer (); 201 aBuffer.append("The param ").append(aParam.getName()). 202 append(" has an invalid scope: ").append(aParam.getScope()). 203 append(". The scope must either be ").append(Param.SCOPE_MAGNET). 204 append(" or ").append(Param.SCOPE_SYSTEM); 205 throw new RenderingException(aBuffer.toString()); 206 } 207 } 208 } 209 } 210 } 211 212 217 public String toString() { 218 StringBuffer aBuffer = new StringBuffer (super.toString()); 219 aBuffer.append("[profile=").append(_theProfile). 220 append(" params=").append(_theParams). 221 append("]"); 222 223 return aBuffer.toString(); 224 } 225 } 226 | Popular Tags |