1 11 package org.eclipse.update.internal.mirror; 12 13 import java.io.File ; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IProgressMonitor; 21 import org.eclipse.core.runtime.NullProgressMonitor; 22 import org.eclipse.core.runtime.PluginVersionIdentifier; 23 import org.eclipse.update.core.ISite; 24 import org.eclipse.update.core.ISiteFeatureReference; 25 import org.eclipse.update.core.JarContentReference; 26 import org.eclipse.update.core.SiteFeatureReference; 27 import org.eclipse.update.core.SiteFeatureReferenceModel; 28 import org.eclipse.update.core.SiteManager; 29 import org.eclipse.update.core.Utilities; 30 import org.eclipse.update.core.VersionedIdentifier; 31 import org.eclipse.update.core.model.InvalidSiteTypeException; 32 import org.eclipse.update.internal.core.UpdateCore; 33 import org.eclipse.update.standalone.ScriptedCommand; 34 import org.eclipse.update.standalone.StandaloneUpdateApplication; 35 36 39 public class MirrorCommand extends ScriptedCommand { 40 41 private String featureId; 42 private String featureVersion; 43 private String fromSiteUrl; 44 private String toSiteDir; 45 private String mirrorURL; 46 private boolean ignoreNonPresentPlugins = false; 47 private MirrorSite mirrorSite; 48 49 public MirrorCommand( 50 String featureId, 51 String featureVersion, 52 String fromSiteUrl, 53 String toSiteDir, 54 String mirrorUrl, 55 String ignoreNonPresentPlugins) { 56 this.featureId = featureId; 57 this.featureVersion = featureVersion; 58 this.fromSiteUrl = fromSiteUrl; 59 this.toSiteDir = toSiteDir; 60 this.mirrorURL = mirrorUrl; 61 this.ignoreNonPresentPlugins = (ignoreNonPresentPlugins != null) && (ignoreNonPresentPlugins.equals("true")); } 63 64 67 public boolean run(IProgressMonitor monitor) { 68 if (!validateParameters()) { 69 return false; 70 } 71 72 try { 73 if (getMirrorSite() == null) 74 return false; 75 76 URL remoteSiteUrl = new URL (fromSiteUrl); 77 ISite remoteSite = 78 SiteManager.getSite(remoteSiteUrl, new NullProgressMonitor()); 79 80 ISiteFeatureReference featureReferencesToMirror[] = 81 findFeaturesToMirror(remoteSite); 82 if (featureReferencesToMirror.length == 0) { 83 StandaloneUpdateApplication.exceptionLogged(); 84 UpdateCore.log( 85 Utilities.newCoreException( 86 "No matching features found on " + remoteSiteUrl + ".", null)); 88 return false; 89 } 90 91 mirrorSite.mirrorAndExpose( 92 remoteSite, 93 featureReferencesToMirror, 94 null, 95 mirrorURL); 96 return true; 97 } catch (MalformedURLException e) { 98 StandaloneUpdateApplication.exceptionLogged(); 99 UpdateCore.log(e); 100 return false; 101 } catch (CoreException ce) { 102 StandaloneUpdateApplication.exceptionLogged(); 103 UpdateCore.log(ce); 104 return false; 105 } finally { 106 JarContentReference.shutdown(); 107 } 108 } 109 private boolean validateParameters() { 110 if (fromSiteUrl == null || fromSiteUrl.length() <= 0) { 111 StandaloneUpdateApplication.exceptionLogged(); 112 UpdateCore.log( 113 Utilities.newCoreException("from parameter is missing.", null)); return false; 115 } 116 try { 117 new URL (fromSiteUrl); 118 } catch (MalformedURLException mue) { 119 StandaloneUpdateApplication.exceptionLogged(); 120 UpdateCore.log( 121 Utilities.newCoreException("from must be a valid URL", null)); return false; 123 } 124 if (toSiteDir == null || toSiteDir.length() <= 0) { 125 StandaloneUpdateApplication.exceptionLogged(); 126 UpdateCore.log( 127 Utilities.newCoreException("to parameter is missing.", null)); return false; 129 } 130 return true; 131 } 132 private MirrorSite getMirrorSite() 133 throws MalformedURLException , CoreException { 134 if (mirrorSite == null) { 136 if (toSiteDir != null) { 137 MirrorSiteFactory factory = new MirrorSiteFactory(); 138 System.out.print("Analyzing features already mirrored ..."); try { 140 mirrorSite = 141 (MirrorSite) factory.createSite(new File (toSiteDir)); 142 mirrorSite.setIgnoreNonPresentPlugins(ignoreNonPresentPlugins); 143 } catch (InvalidSiteTypeException iste) { 144 } 145 System.out.println(" Done."); } 147 if (mirrorSite == null) { 148 StandaloneUpdateApplication.exceptionLogged(); 149 UpdateCore.log( 150 Utilities.newCoreException( 151 "Mirror site at " + toSiteDir + " cannot be accessed.", null)); 153 return null; 154 } 155 } 156 return mirrorSite; 157 158 } 159 167 private ISiteFeatureReference[] findFeaturesToMirror(ISite remoteSite) 168 throws CoreException { 169 ISiteFeatureReference remoteSiteFeatureReferences[] = 170 remoteSite.getRawFeatureReferences(); 171 SiteFeatureReferenceModel existingFeatureModels[] = 172 mirrorSite.getFeatureReferenceModels(); 173 Collection featureReferencesToMirror = new ArrayList (); 174 175 PluginVersionIdentifier featureVersionIdentifier = null; 176 177 if (featureId == null) { 178 System.out.println( 179 "Parameter feature not specified. All features on the remote site will be mirrored."); } 181 if (featureVersion == null) { 182 System.out.println( 183 "Parameter version not specified. All versions of features on the remote site will be mirrored."); } else { 185 featureVersionIdentifier = 186 new PluginVersionIdentifier(featureVersion); 187 } 188 for (int i = 0; i < remoteSiteFeatureReferences.length; i++) { 189 VersionedIdentifier remoteFeatureVersionedIdentifier = 190 remoteSiteFeatureReferences[i].getVersionedIdentifier(); 191 192 if (featureId != null 193 && !featureId.equals( 194 remoteFeatureVersionedIdentifier.getIdentifier())) { 195 continue; 197 } 198 if (featureVersionIdentifier != null 199 && !featureVersionIdentifier.isPerfect( 200 remoteFeatureVersionedIdentifier.getVersion())) { 201 continue; 203 } 204 205 for (int e = 0; e < existingFeatureModels.length; e++) { 206 if (existingFeatureModels[e] 207 .getVersionedIdentifier() 208 .equals(remoteFeatureVersionedIdentifier)) { 209 System.out.println( 210 "Feature " + remoteFeatureVersionedIdentifier 212 + " already mirrored and exposed."); continue; 215 } 216 } 217 218 String type = 220 ((SiteFeatureReference) remoteSiteFeatureReferences[i]) 221 .getType(); 222 if (type != null 223 && !ISite.DEFAULT_PACKAGED_FEATURE_TYPE.equals(type)) { 224 throw Utilities.newCoreException( 226 "Feature " + remoteFeatureVersionedIdentifier 228 + " is of type " + type 230 + ". Only features of type " + ISite.DEFAULT_PACKAGED_FEATURE_TYPE 232 + " are supported.", null); 234 } 235 236 featureReferencesToMirror.add(remoteSiteFeatureReferences[i]); 237 System.out.println( 238 "Feature " + remoteSiteFeatureReferences[i].getVersionedIdentifier() 240 + " will be mirrored."); } 242 return (ISiteFeatureReference[]) featureReferencesToMirror.toArray( 243 new ISiteFeatureReference[featureReferencesToMirror.size()]); 244 } 245 246 } 247 | Popular Tags |