KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > mirror > MirrorCommand


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.mirror;
12
13 import java.io.File JavaDoc;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
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 /**
37  * Mirrors a remote site locally.
38  */

39 public class MirrorCommand extends ScriptedCommand {
40
41     private String JavaDoc featureId;
42     private String JavaDoc featureVersion;
43     private String JavaDoc fromSiteUrl;
44     private String JavaDoc toSiteDir;
45     private String JavaDoc mirrorURL;
46     private boolean ignoreNonPresentPlugins = false;
47     private MirrorSite mirrorSite;
48
49     public MirrorCommand(
50         String JavaDoc featureId,
51         String JavaDoc featureVersion,
52         String JavaDoc fromSiteUrl,
53         String JavaDoc toSiteDir,
54         String JavaDoc mirrorUrl,
55         String JavaDoc 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")); //$NON-NLS-1$
62
}
63
64     /**
65      * true if success
66      */

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 JavaDoc remoteSiteUrl = new URL JavaDoc(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 + ".", //$NON-NLS-1$ //$NON-NLS-2$
87
null));
88                 return false;
89             }
90
91             mirrorSite.mirrorAndExpose(
92                 remoteSite,
93                 featureReferencesToMirror,
94                 null,
95                 mirrorURL);
96             return true;
97         } catch (MalformedURLException JavaDoc 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)); //$NON-NLS-1$
114
return false;
115         }
116         try {
117             new URL JavaDoc(fromSiteUrl);
118         } catch (MalformedURLException JavaDoc mue) {
119             StandaloneUpdateApplication.exceptionLogged();
120             UpdateCore.log(
121                 Utilities.newCoreException("from must be a valid URL", null)); //$NON-NLS-1$
122
return false;
123         }
124         if (toSiteDir == null || toSiteDir.length() <= 0) {
125             StandaloneUpdateApplication.exceptionLogged();
126             UpdateCore.log(
127                 Utilities.newCoreException("to parameter is missing.", null)); //$NON-NLS-1$
128
return false;
129         }
130         return true;
131     }
132     private MirrorSite getMirrorSite()
133         throws MalformedURLException JavaDoc, CoreException {
134         // Create mirror site
135
if (mirrorSite == null) {
136             if (toSiteDir != null) {
137                 MirrorSiteFactory factory = new MirrorSiteFactory();
138                 System.out.print("Analyzing features already mirrored ..."); //$NON-NLS-1$
139
try {
140                     mirrorSite =
141                         (MirrorSite) factory.createSite(new File JavaDoc(toSiteDir));
142                     mirrorSite.setIgnoreNonPresentPlugins(ignoreNonPresentPlugins);
143                 } catch (InvalidSiteTypeException iste) {
144                 }
145                 System.out.println(" Done."); //$NON-NLS-1$
146
}
147             if (mirrorSite == null) {
148                 StandaloneUpdateApplication.exceptionLogged();
149                 UpdateCore.log(
150                     Utilities.newCoreException(
151                         "Mirror site at " + toSiteDir + " cannot be accessed.", //$NON-NLS-1$ //$NON-NLS-2$
152
null));
153                 return null;
154             }
155         }
156         return mirrorSite;
157
158     }
159     /**
160      * Returns subset of feature references on remote site
161      * as specified by optional featureId and featureVersion
162      * parameters
163      * @param remoteSite
164      * @return ISiteFeatureReference[]
165      * @throws CoreException
166      */

167     private ISiteFeatureReference[] findFeaturesToMirror(ISite remoteSite)
168         throws CoreException {
169         ISiteFeatureReference remoteSiteFeatureReferences[] =
170             remoteSite.getRawFeatureReferences();
171         SiteFeatureReferenceModel existingFeatureModels[] =
172             mirrorSite.getFeatureReferenceModels();
173         Collection JavaDoc featureReferencesToMirror = new ArrayList JavaDoc();
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."); //$NON-NLS-1$
180
}
181         if (featureVersion == null) {
182             System.out.println(
183                 "Parameter version not specified. All versions of features on the remote site will be mirrored."); //$NON-NLS-1$
184
} 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                 // id does not match
196
continue;
197             }
198             if (featureVersionIdentifier != null
199                 && !featureVersionIdentifier.isPerfect(
200                     remoteFeatureVersionedIdentifier.getVersion())) {
201                 // version does not match
202
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 " //$NON-NLS-1$
211
+ remoteFeatureVersionedIdentifier
212                             + " already mirrored and exposed."); //$NON-NLS-1$
213
// feature already mirrored and exposed in site.xml
214
continue;
215                 }
216             }
217
218             // Check feature type
219
String JavaDoc type =
220                 ((SiteFeatureReference) remoteSiteFeatureReferences[i])
221                     .getType();
222             if (type != null
223                 && !ISite.DEFAULT_PACKAGED_FEATURE_TYPE.equals(type)) {
224                 // unsupported
225
throw Utilities.newCoreException(
226                     "Feature " //$NON-NLS-1$
227
+ remoteFeatureVersionedIdentifier
228                         + " is of type " //$NON-NLS-1$
229
+ type
230                         + ". Only features of type " //$NON-NLS-1$
231
+ ISite.DEFAULT_PACKAGED_FEATURE_TYPE
232                         + " are supported.", //$NON-NLS-1$
233
null);
234             }
235
236             featureReferencesToMirror.add(remoteSiteFeatureReferences[i]);
237             System.out.println(
238                 "Feature " //$NON-NLS-1$
239
+ remoteSiteFeatureReferences[i].getVersionedIdentifier()
240                     + " will be mirrored."); //$NON-NLS-1$
241
}
242         return (ISiteFeatureReference[]) featureReferencesToMirror.toArray(
243             new ISiteFeatureReference[featureReferencesToMirror.size()]);
244     }
245
246 }
247
Popular Tags