| 1 6 package fr.jayasoft.ivy.ant; 7 8 import java.io.File ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.net.MalformedURLException ; 12 import java.net.URL ; 13 import java.util.Properties ; 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 32 public class IvyConfigure extends IvyTask { 33 public static class Credentials { 34 private String _realm; 35 private String _host; 36 private String _username; 37 private String _passwd; 38 39 public String getPasswd() { 40 return _passwd; 41 } 42 public void setPasswd(String passwd) { 43 _passwd = passwd; 44 } 45 public String getRealm() { 46 return _realm; 47 } 48 public void setRealm(String realm) { 49 _realm = format(realm); 50 } 51 public String getHost() { 52 return _host; 53 } 54 public void setHost(String host) { 55 _host = format(host); 56 } 57 public String getUsername() { 58 return _username; 59 } 60 public void setUsername(String userName) { 61 _username = format(userName); 62 } 63 } 64 65 private File _file = null; 66 private URL _url = null; 67 private String _realm = null; 68 private String _host = null; 69 private String _userName = null; 70 private String _passwd = null; 71 72 public File getFile() { 73 return _file; 74 } 75 public void setFile(File conf) { 76 _file = conf; 77 } 78 public URL getUrl() { 79 return _url; 80 } 81 public void setUrl(String url) throws MalformedURLException { 82 _url = new URL (url); 83 } 84 public String getPasswd() { 85 return _passwd; 86 } 87 public void setPasswd(String passwd) { 88 _passwd = passwd; 89 } 90 public String getRealm() { 91 return _realm; 92 } 93 public void setRealm(String realm) { 94 _realm = format(realm); 95 } 96 public String getHost() { 97 return _host; 98 } 99 public void setHost(String host) { 100 _host = format(host); 101 } 102 public String getUsername() { 103 return _userName; 104 } 105 public void setUsername(String userName) { 106 _userName = format(userName); 107 } 108 private static String format(String 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 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 (getProject().getBaseDir(), getProject().getProperty("ivy.conf.file")); 129 Message.verbose("searching ivyconf file: trying "+_file); 130 if (!_file.exists()) { 131 _file = new File (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 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 url = Ivy.class.getResource("ivy.properties"); 159 Properties props = new Properties (); 161 Message.verbose("Loading " + url); 162 try { 163 InputStream 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 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 |