1 28 29 package com.idaremedia.antx.valueuri.info; 30 31 import java.io.File ; 32 import java.util.Properties ; 33 34 import org.apache.tools.ant.BuildException; 35 import org.apache.tools.ant.Project; 36 37 import com.idaremedia.antx.AntX; 38 import com.idaremedia.antx.AntXFixture; 39 import com.idaremedia.antx.FixtureExaminer; 40 import com.idaremedia.antx.Iteration; 41 import com.idaremedia.antx.ValueURIHandler; 42 import com.idaremedia.antx.apis.Requester; 43 import com.idaremedia.antx.helpers.InputFileLoader; 44 import com.idaremedia.antx.helpers.Tk; 45 import com.idaremedia.antx.parameters.TransformHelper; 46 import com.idaremedia.antx.parameters.ValueTransform; 47 import com.idaremedia.antx.starters.ValueURIHandlerSkeleton; 48 49 94 95 public final class PasswordValueURIHandler extends ValueURIHandlerSkeleton 96 { 97 private static final boolean OPTIONAL= true; 98 99 110 private Properties readFile(File f, boolean optional, Requester clnt) 111 { 112 try { 113 return InputFileLoader.loadProperties 114 (AntXFixture.fileUtils().getFileURL(f), null); 115 } catch(Exception anyX) { 116 clnt.problem(anyX.getMessage(), 117 optional? Project.MSG_WARN : Project.MSG_ERR); 118 if (!optional) { 119 throw new BuildException(anyX,clnt.getLocation()); 120 } 121 } 122 return null; 123 } 124 125 126 127 139 private Properties getPWFile(Requester clnt) 140 { 141 Project project = clnt.getProject(); 142 AntX.verify_(project!=null, "PasswordValueURIHandler", 143 "valueFrom- called from project"); 144 145 String generated = Iteration.valueURIHandler("artifact").valueFrom 146 ("passwordfile","$artifact:passwordfile",clnt); 147 if (generated!=null) { 148 File f = new File (TransformHelper.apply 149 (ValueTransform.OSPATH,generated, project)); 150 return readFile(f,!OPTIONAL,clnt); 151 } 152 153 Properties passwords = FixtureExaminer.getReferencedProperties 154 (project, "build.passwords", null); 155 if (passwords!=null) { 156 return passwords; 157 } 158 Object o = project.getReference("build.passwords"); 159 if (o!=null) { 160 clnt.problem("Unrecognized data under 'build.passwords': "+ 161 o.getClass().getName(), Project.MSG_WARN); 162 } 163 164 String filename = Iteration.defaultdefaults().passwordFileLocation(project); 165 if (filename!=null) { 166 File f = project.resolveFile(filename); 167 return readFile(f,!OPTIONAL,clnt); 168 } 169 170 filename = Tk.getTheProperty(project,"user.home"); 171 File f = new File (filename); 172 f = new File (f,".ant"+File.separator+"build.passwords"); 173 return readFile(f,OPTIONAL,clnt); 174 } 175 176 177 178 183 public String valueFrom(String uriFragment, String fullUri, Requester clnt) 184 { 185 String value = null; 186 Properties pwfile = getPWFile(clnt); 187 if (pwfile!=null) { 188 String owner = Tk.resolveString(clnt.getProject(),uriFragment,true); 189 value = pwfile.getProperty(owner); 190 pwfile.clear(); 191 } 192 if (value==null) { 193 ValueURIHandler helper = Iteration.valueURIHandler("default"); 194 value = helper.valueFrom("password","$default:password",clnt); 195 } 196 return value; 197 } 198 } 199 200 | Popular Tags |