KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > valueuri > info > PasswordValueURIHandler


1 /**
2  * $Id: PasswordValueURIHandler.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004-2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.valueuri.info;
30
31 import java.io.File JavaDoc;
32 import java.util.Properties JavaDoc;
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 /**
50  * Example value uri handler that lets a script reference a password without embedding
51  * the password directly in script. Whether the referred to password is clear text
52  * or cipher text is application dependent.
53  * <p/>
54  * The handler looks for a set of properties of username=password settings. It
55  * looks for these properties in four places:<ol>
56  * <li>A <span class="src">passwordfile</span> artifact value URI. If available,
57  * the artifact must specify either a file path or a file path URL.</li>
58  * <li>A local &lt;propertyset&gt; or &lt;properties&gt; object under refid
59  * <span class="src">build&#46;passwords</span>. This option exists primarily
60  * to aid debugging and script development.</li>
61  * <li>The file named in the default iteration password file property. See
62  * {@linkplain com.idaremedia.antx.Defaults#passwordFileLocation
63  * Defaults.passwordFileLocation} for more information.</li>
64  * <li>A <span class="src">build&#46;passwords</span> file stored under the
65  * directory <span class="src">${user&#46;home}/&#46;ant</span>.</li>
66  * </ol>
67  * At least <em>one</em> of these places must contain a valid set of properties. If no
68  * properties are found, this value uri handler signals an error. (This not the same
69  * response the handler generates if it does not find a matching entry for the password.
70  * If the properties exist but there is no match, the handler returns <i>null</i> or
71  * the password stored as the default.)
72  * <p/>
73  * <b>Example Usage:</b><pre>
74  * &lt;baseurl name="data" uri="http://qalab.mysoft.com/myproject/data/&gt;
75  * &lt;parameter name="auth:username" value="nightlybuild"/&gt;
76  * &lt;parameter name="auth:password" value="${$password:nightlybuild}"/&gt;
77  * &lt;/baseurl&gt;
78  *
79  * -- To Install --
80  * &lt;valueuri-interpreter action="install"&gt;
81  * &lt;parameter name="password"
82  * value="com.idaremedia.antx.valueuri.info.PasswordValueURIHandler"/&gt;
83  * &lt;/valueuri-interpreter&gt;
84  * </pre>
85  *
86  * @since AntXtras/Oofs 1.0
87  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
88  * @version 0.5
89  * @.safety multiple
90  * @.group helper,example
91  * @.caveat The password file is loaded <em>everytime</em> a handler is
92  * asked for a value. Nothing is cached.
93  **/

94
95 public final class PasswordValueURIHandler extends ValueURIHandlerSkeleton
96 {
97     private static final boolean OPTIONAL= true;
98
99     /**
100      * Tries to load the passwords properties file to search for
101      * a single entry. (A bit excessive but we are just a sample.)
102      * @param f password file
103      * @param optional <i>true</i> if file does not have to exist.
104      * @param clnt problem handler (non-null)
105      * @return password file as Properties object (null if optional
106      * and cannot find/load file).
107      * @throws BuildException if unable to load file as properties
108      * file for any reason and is not optional
109      **/

110     private Properties JavaDoc readFile(File JavaDoc f, boolean optional, Requester clnt)
111     {
112         try {
113             return InputFileLoader.loadProperties
114                 (AntXFixture.fileUtils().getFileURL(f), null);
115         } catch(Exception JavaDoc 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     /**
128      * Get password file according to our search algorithm. First we
129      * look for an artifact "<span class="src">passwordfile</span>".
130      * Next we look for a local propertyset (or properties) called
131      * "<span class="src">build&#46;passwords</span>". Next we look
132      * for a client-supplied file using the iteration's passwordfile
133      * configuration setting. If no such setting, we look for a file
134      * <span class="src">build&#46;passwords</span> in the user's ant
135      * directory at <span class="src">${user&#46;home}/&#46;ant</span>.
136      * @param clnt error handler (non-null)
137      * @return username/password pairs as a properties object.
138      **/

139     private Properties JavaDoc getPWFile(Requester clnt)
140     {
141         Project project = clnt.getProject();
142         AntX.verify_(project!=null, "PasswordValueURIHandler",
143                      "valueFrom- called from project");
144
145         String JavaDoc generated = Iteration.valueURIHandler("artifact").valueFrom
146             ("passwordfile","$artifact:passwordfile",clnt);
147         if (generated!=null) {
148             File JavaDoc f = new File JavaDoc(TransformHelper.apply
149                 (ValueTransform.OSPATH,generated, project));
150             return readFile(f,!OPTIONAL,clnt);
151         }
152
153         Properties JavaDoc passwords = FixtureExaminer.getReferencedProperties
154             (project, "build.passwords", null);
155         if (passwords!=null) {
156             return passwords;
157         }
158         Object JavaDoc 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 JavaDoc filename = Iteration.defaultdefaults().passwordFileLocation(project);
165         if (filename!=null) {
166             File JavaDoc f = project.resolveFile(filename);
167             return readFile(f,!OPTIONAL,clnt);
168         }
169
170         filename = Tk.getTheProperty(project,"user.home");
171         File JavaDoc f = new File JavaDoc(filename);
172         f = new File JavaDoc(f,".ant"+File.separator+"build.passwords");
173         return readFile(f,OPTIONAL,clnt);
174     }
175
176
177
178     /**
179      * Tries to extract the named password from the current iteration's
180      * password file. The password file is loaded everytime a uri is
181      * evaluated (yick).
182      **/

183     public String JavaDoc valueFrom(String JavaDoc uriFragment, String JavaDoc fullUri, Requester clnt)
184     {
185         String JavaDoc value = null;
186         Properties JavaDoc pwfile = getPWFile(clnt);
187         if (pwfile!=null) {
188             String JavaDoc 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 /* end-of-PasswordValueURIHandler.java */
Popular Tags