KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > ant > IvyConfigure


1 /*
2  * This file is subject to the license found in LICENCE.TXT in the root directory of the project.
3  *
4  * #SNAPSHOT#
5  */

6 package fr.jayasoft.ivy.ant;
7
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.net.MalformedURLException JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.util.Properties JavaDoc;
14
15 import org.apache.tools.ant.BuildException;
16 import org.apache.tools.ant.Project;
17 import org.apache.tools.ant.taskdefs.Property;
18
19 import fr.jayasoft.ivy.Ivy;
20 import fr.jayasoft.ivy.url.CredentialsStore;
21 import fr.jayasoft.ivy.url.URLHandler;
22 import fr.jayasoft.ivy.url.URLHandlerDispatcher;
23 import fr.jayasoft.ivy.url.URLHandlerRegistry;
24 import fr.jayasoft.ivy.util.Message;
25
26 /**
27  * Configure Ivy with an ivyconf.xml file
28  *
29  * @author Xavier Hanin
30  *
31  */

32 public class IvyConfigure extends IvyTask {
33     public static class Credentials {
34         private String JavaDoc _realm;
35         private String JavaDoc _host;
36         private String JavaDoc _username;
37         private String JavaDoc _passwd;
38         
39         public String JavaDoc getPasswd() {
40             return _passwd;
41         }
42         public void setPasswd(String JavaDoc passwd) {
43             _passwd = passwd;
44         }
45         public String JavaDoc getRealm() {
46             return _realm;
47         }
48         public void setRealm(String JavaDoc realm) {
49             _realm = format(realm);
50         }
51         public String JavaDoc getHost() {
52             return _host;
53         }
54         public void setHost(String JavaDoc host) {
55             _host = format(host);
56         }
57         public String JavaDoc getUsername() {
58             return _username;
59         }
60         public void setUsername(String JavaDoc userName) {
61             _username = format(userName);
62         }
63     }
64
65     private File JavaDoc _file = null;
66     private URL JavaDoc _url = null;
67     private String JavaDoc _realm = null;
68     private String JavaDoc _host = null;
69     private String JavaDoc _userName = null;
70     private String JavaDoc _passwd = null;
71
72     public File JavaDoc getFile() {
73         return _file;
74     }
75     public void setFile(File JavaDoc conf) {
76         _file = conf;
77     }
78     public URL JavaDoc getUrl() {
79         return _url;
80     }
81     public void setUrl(String JavaDoc url) throws MalformedURLException JavaDoc {
82         _url = new URL JavaDoc(url);
83     }
84     public String JavaDoc getPasswd() {
85         return _passwd;
86     }
87     public void setPasswd(String JavaDoc passwd) {
88         _passwd = passwd;
89     }
90     public String JavaDoc getRealm() {
91         return _realm;
92     }
93     public void setRealm(String JavaDoc realm) {
94         _realm = format(realm);
95     }
96     public String JavaDoc getHost() {
97         return _host;
98     }
99     public void setHost(String JavaDoc host) {
100         _host = format(host);
101     }
102     public String JavaDoc getUsername() {
103         return _userName;
104     }
105     public void setUsername(String JavaDoc userName) {
106         _userName = format(userName);
107     }
108     private static String JavaDoc format(String JavaDoc str) {
109         return str == null ? str : (str.trim().length() == 0 ? null : str.trim());
110     }
111     
112     public void addConfiguredCredentials(Credentials c) {
113         CredentialsStore.INSTANCE.addCredentials(c.getRealm(), c.getHost(), c.getUsername(), c.getPasswd());
114     }
115
116     public void execute() throws BuildException {
117         try {
118             loadDefaultProperties();
119         } catch (Exception JavaDoc ex) {
120             throw new BuildException("impossible to load ivy default properties file: "+ex, ex);
121         }
122         ensureMessageInitialised();
123         Ivy ivy = new Ivy();
124         try {
125             configureURLHandler();
126             ivy.addAllVariables(getProject().getProperties());
127             if (_file == null && _url == null) {
128                 _file = new File JavaDoc(getProject().getBaseDir(), getProject().getProperty("ivy.conf.file"));
129                 Message.verbose("searching ivyconf file: trying "+_file);
130                 if (!_file.exists()) {
131                     _file = new File JavaDoc(getProject().getProperty("ivy.conf.file"));
132                     Message.verbose("searching ivyconf file: trying "+_file);
133                     if (!_file.exists()) {
134                         Message.info("no configuration file found, using default...");
135                         _file = null;
136                         _url = Ivy.getDefaultConfigurationURL();
137                     }
138                 }
139             }
140             if (_file != null) {
141                 if (!_file.exists()) {
142                     throw new BuildException("configuration file does not exist: "+_file);
143                 } else {
144                     ivy.configure(_file);
145                 }
146             } else {
147                 ivy.configure(_url);
148             }
149             setIvyInstance(ivy);
150         } catch (Exception JavaDoc ex) {
151             throw new BuildException("impossible to configure ivy with given "+(_file != null ? "file: "+_file : "url :"+_url)+" :"+ex, ex);
152         }
153     }
154
155     private void loadDefaultProperties() {
156         Property prop = new Property() {
157             public void execute() throws BuildException {
158                 URL JavaDoc url = Ivy.class.getResource("ivy.properties");
159                 // this is copy of loadURL code from ant Property task (not available in 1.5.1)
160
Properties JavaDoc props = new Properties JavaDoc();
161                 Message.verbose("Loading " + url);
162                 try {
163                     InputStream JavaDoc is = url.openStream();
164                     try {
165                         props.load(is);
166                     } finally {
167                         if (is != null) {
168                             is.close();
169                         }
170                     }
171                     addProperties(props);
172                 } catch (IOException JavaDoc ex) {
173                     throw new BuildException(ex, getLocation());
174                 }
175             }
176         };
177         prop.setProject(getProject());
178         prop.execute();
179     }
180     
181     private void configureURLHandler() {
182         CredentialsStore.INSTANCE.addCredentials(getRealm(), getHost(), getUsername(), getPasswd());
183
184         URLHandlerDispatcher dispatcher = new URLHandlerDispatcher();
185         URLHandler httpHandler = URLHandlerRegistry.getHttp();
186         dispatcher.setDownloader("http", httpHandler);
187         dispatcher.setDownloader("https", httpHandler);
188         URLHandlerRegistry.setDefault(dispatcher);
189     }
190 }
191
Popular Tags