1 6 package fr.jayasoft.ivy.resolver; 7 8 import java.io.File ; 9 import java.net.MalformedURLException ; 10 import java.net.URL ; 11 import java.text.ParseException ; 12 import java.util.ArrayList ; 13 import java.util.Collection ; 14 import java.util.Collections ; 15 import java.util.Date ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.xml.sax.SAXException ; 22 import org.xml.sax.helpers.DefaultHandler ; 23 24 import fr.jayasoft.ivy.Artifact; 25 import fr.jayasoft.ivy.DependencyDescriptor; 26 import fr.jayasoft.ivy.Ivy; 27 import fr.jayasoft.ivy.ResolveData; 28 import fr.jayasoft.ivy.ResolvedModuleRevision; 29 import fr.jayasoft.ivy.report.DownloadReport; 30 import fr.jayasoft.ivy.util.IvyPatternHelper; 31 import fr.jayasoft.ivy.util.Message; 32 import fr.jayasoft.ivy.util.XMLHelper; 33 34 40 public class IvyRepResolver extends URLResolver { 41 public static final String DEFAULT_IVYPATTERN = "[organisation]/[module]/ivy-[revision].xml"; 42 public static final String DEFAULT_IVYROOT = "http://ivyrep.jayasoft.org/"; 43 private String _ivyroot = null; 44 private String _ivypattern = null; 45 46 private String _artroot = null; 47 private String _artpattern = null; 48 49 public IvyRepResolver() { 50 } 51 52 private void ensureArtifactConfigured(Ivy ivy) { 53 if (ivy != null && (_artroot == null || _artpattern == null)) { 54 if (_artroot == null) { 55 String root = ivy.getVariable("ivy.ivyrep.default.artifact.root"); 56 if (root != null) { 57 _artroot = root; 58 } else { 59 ivy.configureRepositories(true); 60 _artroot = ivy.getVariable("ivy.ivyrep.default.artifact.root"); 61 } 62 } 63 if (_artpattern == null) { 64 String pattern = ivy.getVariable("ivy.ivyrep.default.artifact.pattern"); 65 if (pattern != null) { 66 _artpattern = pattern; 67 } else { 68 ivy.configureRepositories(false); 69 _artpattern = ivy.getVariable("ivy.ivyrep.default.artifact.pattern"); 70 } 71 } 72 updateWholeArtPattern(); 73 } 74 } 75 76 private void ensureIvyConfigured(Ivy ivy) { 77 if (ivy != null && (_ivyroot == null || _ivypattern == null)) { 78 if (_ivyroot == null) { 79 String root = ivy.getVariable("ivy.ivyrep.default.ivy.root"); 80 if (root != null) { 81 _ivyroot = root; 82 } else { 83 ivy.configureRepositories(true); 84 _ivyroot = ivy.getVariable("ivy.ivyrep.default.ivy.root"); 85 } 86 } 87 if (_ivypattern == null) { 88 String pattern = ivy.getVariable("ivy.ivyrep.default.ivy.pattern"); 89 if (pattern != null) { 90 _ivypattern = pattern; 91 } else { 92 ivy.configureRepositories(false); 93 _ivypattern = ivy.getVariable("ivy.ivyrep.default.ivy.pattern"); 94 } 95 } 96 updateWholeIvyPattern(); 97 } 98 } 99 100 private String getWholeIvyPattern() { 101 if (_ivyroot == null || _ivypattern == null) { 102 return null; 103 } 104 return _ivyroot + _ivypattern; 105 } 106 private String getWholeArtPattern() { 107 return _artroot + _artpattern; 108 } 109 public String getIvypattern() { 110 return _ivypattern; 111 } 112 public void setIvypattern(String pattern) { 113 if (pattern == null) { 114 throw new NullPointerException ("pattern must not be null"); 115 } 116 _ivypattern = pattern; 117 ensureIvyConfigured(getIvy()); 118 updateWholeIvyPattern(); 119 } 120 public String getIvyroot() { 121 return _ivyroot; 122 } 123 129 public void setIvyroot(String root) { 130 if (root == null) { 131 throw new NullPointerException ("root must not be null"); 132 } 133 if (!root.endsWith("/")) { 134 _ivyroot = root + "/"; 135 } else { 136 _ivyroot = root; 137 } 138 ensureIvyConfigured(getIvy()); 139 updateWholeIvyPattern(); 140 } 141 142 public void setM2compatible(boolean m2compatible) { 143 if (m2compatible) { 144 throw new IllegalArgumentException ("ivyrep does not support maven2 compatibility. Please use ibiblio resolver instead, or even url or filesystem resolvers for more specific needs."); 145 } 146 } 147 148 private void updateWholeIvyPattern() { 149 setIvyPatterns(Collections.singletonList(getWholeIvyPattern())); 150 } 151 private void updateWholeArtPattern() { 152 setArtifactPatterns(Collections.singletonList(getWholeArtPattern())); 153 } 154 public void publish(Artifact artifact, File src) { 155 throw new UnsupportedOperationException ("publish not supported by IBiblioResolver"); 156 } 157 158 public String getArtroot() { 159 return _artroot; 160 } 161 162 163 public String getArtpattern() { 164 return _artpattern; 165 } 166 167 168 public void setArtpattern(String pattern) { 169 if (pattern == null) { 170 throw new NullPointerException ("pattern must not be null"); 171 } 172 _artpattern = pattern; 173 ensureArtifactConfigured(getIvy()); 174 updateWholeArtPattern(); 175 } 176 177 public void setArtroot(String root) { 178 if (root == null) { 179 throw new NullPointerException ("root must not be null"); 180 } 181 if (!root.endsWith("/")) { 182 _artroot = root + "/"; 183 } else { 184 _artroot = root; 185 } 186 ensureArtifactConfigured(getIvy()); 187 updateWholeArtPattern(); 188 } 189 190 public OrganisationEntry[] listOrganisations() { 191 ensureIvyConfigured(getIvy()); 192 try { 193 URL content = new URL (_ivyroot + "content.xml"); 194 final List ret = new ArrayList (); 195 XMLHelper.parse(content, null, new DefaultHandler () { 196 public void startElement(String uri,String localName,String qName,org.xml.sax.Attributes attributes) throws SAXException { 197 if ("organisation".equals(qName)) { 198 String org = attributes.getValue("name"); 199 if (org != null) { 200 ret.add(new OrganisationEntry(IvyRepResolver.this, org)); 201 } 202 } 203 } 204 }); 205 return (OrganisationEntry[])ret.toArray(new OrganisationEntry[ret.size()]); 206 } catch (MalformedURLException e) { 207 } catch (Exception e) { 208 Message.warn("unable to parse content.xml file on ivyrep: "+e.getMessage()); 209 } 210 return super.listOrganisations(); 211 } 212 213 public ModuleEntry[] listModules(OrganisationEntry org) { 215 ensureIvyConfigured(getIvy()); 216 Map tokenValues = new HashMap (); 217 tokenValues.put(IvyPatternHelper.ORGANISATION_KEY, org.getOrganisation()); 218 Collection names = findIvyNames(tokenValues, IvyPatternHelper.MODULE_KEY); 219 ModuleEntry[] ret = new ModuleEntry[names.size()]; 220 int i =0; 221 for (Iterator iter = names.iterator(); iter.hasNext(); i++) { 222 String name = (String )iter.next(); 223 ret[i] = new ModuleEntry(org, name); 224 } 225 return ret; 226 } 227 228 public RevisionEntry[] listRevisions(ModuleEntry mod) { 229 ensureIvyConfigured(getIvy()); 230 ensureArtifactConfigured(getIvy()); 231 return super.listRevisions(mod); 232 } 233 234 public String getTypeName() { 235 return "ivyrep"; 236 } 237 238 public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data) throws ParseException { 240 ensureIvyConfigured(data.getIvy()); 241 return super.getDependency(dd, data); 242 } 243 244 protected ResolvedResource findArtifactRef(Artifact artifact, Date date) { 245 ensureArtifactConfigured(getIvy()); 246 return super.findArtifactRef(artifact, date); 247 } 248 249 public DownloadReport download(Artifact[] artifacts, Ivy ivy, File cache, boolean useOrigin) { 250 ensureArtifactConfigured(ivy); 251 return super.download(artifacts, ivy, cache, useOrigin); 252 } 253 public boolean exists(Artifact artifact) { 254 ensureArtifactConfigured(getIvy()); 255 return super.exists(artifact); 256 } 257 public List getIvyPatterns() { 258 ensureIvyConfigured(getIvy()); 259 return super.getIvyPatterns(); 260 } 261 public List getArtifactPatterns() { 262 ensureArtifactConfigured(getIvy()); 263 return super.getArtifactPatterns(); 264 } 265 } 266 | Popular Tags |