KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.FileOutputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.io.OutputStreamWriter JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.NullProgressMonitor;
29 import org.eclipse.update.core.ContentReference;
30 import org.eclipse.update.core.ICategory;
31 import org.eclipse.update.core.IFeature;
32 import org.eclipse.update.core.IFeatureContentProvider;
33 import org.eclipse.update.core.IFeatureReference;
34 import org.eclipse.update.core.INonPluginEntry;
35 import org.eclipse.update.core.IPluginEntry;
36 import org.eclipse.update.core.ISite;
37 import org.eclipse.update.core.ISiteFeatureReference;
38 import org.eclipse.update.core.IURLEntry;
39 import org.eclipse.update.core.IVerificationListener;
40 import org.eclipse.update.core.Site;
41 import org.eclipse.update.core.SiteFeatureReferenceModel;
42 import org.eclipse.update.core.Utilities;
43 import org.eclipse.update.core.VersionedIdentifier;
44 import org.eclipse.update.core.model.CategoryModel;
45 import org.eclipse.update.core.model.SiteModelFactory;
46 import org.eclipse.update.core.model.URLEntryModel;
47 import org.eclipse.update.internal.core.CoreExceptionWithRootCause;
48 import org.eclipse.update.internal.core.FatalIOException;
49 import org.eclipse.update.internal.core.FeaturePackagedContentProvider;
50 import org.eclipse.update.internal.core.ISiteContentConsumer;
51 import org.eclipse.update.internal.core.UpdateCore;
52 import org.eclipse.update.internal.core.UpdateManagerUtils;
53 import org.eclipse.update.standalone.StandaloneUpdateApplication;
54
55 /**
56  * Local mirror site. Read/Write
57  */

58 public class MirrorSite extends Site {
59     private final static String JavaDoc INDENT = " "; //$NON-NLS-1$
60
private SiteModelFactory factory;
61     /**
62      * plugin entries
63      */

64     private Collection JavaDoc downloadedPluginEntries = new ArrayList JavaDoc();
65     private Collection JavaDoc downloadedFeatureReferenceModels = new ArrayList JavaDoc();
66     private boolean ignoreNonPresentPlugins;
67     public MirrorSite(SiteModelFactory factory) {
68         this.factory = factory;
69     }
70
71     /**
72      * Mirrors the specified features and listed optional features on this site.
73      * @see ISite#install(IFeature, IVerificationListener, IProgressMonitor)
74      * @param mirrorSiteUrl external URL of the mirror site or null;
75      * if parameter is provided policy fragment will be generated
76      * @exception CoreException
77      */

78     public void mirrorAndExpose(
79         ISite remoteSite,
80         ISiteFeatureReference[] sourceFeatureRefs,
81         IFeatureReference[] optionalfeatures,
82         String JavaDoc mirrorSiteUrl)
83         throws CoreException {
84
85         mirrorAndExposeFeatures(
86             remoteSite,
87             sourceFeatureRefs,
88             optionalfeatures);
89
90         System.out.println(
91             "Installing features finished. Updating categories ..."); //$NON-NLS-1$
92
updateCategories(remoteSite);
93         System.out.println(
94             "Updating categories finished. Updating site description ..."); //$NON-NLS-1$
95
updateDescription(remoteSite);
96         System.out.println(
97             "Updating site description finished. Saving site.xml ..."); //$NON-NLS-1$
98
save();
99         if (mirrorSiteUrl != null) {
100             generateUpdatePolicy(mirrorSiteUrl);
101         }
102     }
103     private void mirrorAndExposeFeatures(
104         ISite remoteSite,
105         ISiteFeatureReference[] sourceFeatureRefs,
106         IFeatureReference[] optionalfeatures)
107         throws CoreException {
108
109         // Features that failed will be retried once again
110
Collection JavaDoc failedFeatures = new ArrayList JavaDoc();
111         for (int i = 0; i < sourceFeatureRefs.length; i++) {
112             try {
113                 IFeature sourceFeature =
114                     sourceFeatureRefs[i].getFeature(new NullProgressMonitor());
115                 SiteFeatureReferenceModel featureRef =
116                     mirrorFeature(
117                         remoteSite,
118                         sourceFeature,
119                         optionalfeatures,
120                         1);
121                 // Set categories of the new feature
122
ICategory remoteCategories[] =
123                     sourceFeatureRefs[i].getCategories();
124                 for (int j = 0; j < remoteCategories.length; j++) {
125                     featureRef.addCategoryName(remoteCategories[j].getName());
126                 }
127
128                 addFeatureReferenceModel(remoteSite, featureRef);
129             } catch (CoreException ce) {
130                 failedFeatures.add(sourceFeatureRefs[i]);
131             }
132         }
133
134         // do we need to retry?
135
if (failedFeatures.size() > 0) {
136             sourceFeatureRefs =
137                 (ISiteFeatureReference[]) failedFeatures.toArray(
138                     new ISiteFeatureReference[failedFeatures.size()]);
139         } else {
140             return;
141         }
142
143         for (int i = 0; i < sourceFeatureRefs.length; i++) {
144             IFeature sourceFeature =
145                 sourceFeatureRefs[i].getFeature(new NullProgressMonitor());
146             SiteFeatureReferenceModel featureRef =
147                 mirrorFeature(remoteSite, sourceFeature, optionalfeatures, 1);
148             // Set categories of the new feature
149
ICategory remoteCategories[] = sourceFeatureRefs[i].getCategories();
150             for (int j = 0; j < remoteCategories.length; j++) {
151                 featureRef.addCategoryName(remoteCategories[j].getName());
152             }
153
154             addFeatureReferenceModel(remoteSite, featureRef);
155         }
156     }
157
158     /**
159      * Install the specified feature and listed optional features on this site.
160      * @see ISite#install(IFeature, IVerificationListener, IProgressMonitor)
161      * @exception CoreException
162      */

163     private SiteFeatureReferenceModel mirrorFeature(
164         ISite remoteSite,
165         IFeature sourceFeature,
166         IFeatureReference[] optionalfeatures,
167         int indent)
168         throws CoreException {
169         String JavaDoc tab = ""; //$NON-NLS-1$
170
for (int i = 0; i < indent; i++)
171             tab += " "; //$NON-NLS-1$
172
System.out.println(
173             tab
174                 + "Mirroring feature " //$NON-NLS-1$
175
+ sourceFeature.getVersionedIdentifier()
176                 + " ..."); //$NON-NLS-1$
177
SiteFeatureReferenceModel existingFeatures[] =
178             getDownloadedFeatureReferenceModels();
179         for (int e = 0; e < existingFeatures.length; e++) {
180             if (existingFeatures[e]
181                 .getVersionedIdentifier()
182                 .equals(sourceFeature.getVersionedIdentifier())) {
183                 System.out.println(
184                     tab
185                         + "Feature " //$NON-NLS-1$
186
+ sourceFeature.getVersionedIdentifier()
187                         + " already exists. Skipping downloading."); //$NON-NLS-1$
188
return existingFeatures[e];
189             }
190         }
191
192         final IFeatureContentProvider provider =
193             sourceFeature.getFeatureContentProvider();
194         
195         // TODO: passing command options could be made more general in future, so this
196
// cast is not needed.
197
if (provider instanceof FeaturePackagedContentProvider) {
198             ((FeaturePackagedContentProvider) provider).setContinueOnError(ignoreNonPresentPlugins);
199         }
200         
201         System.out.println(
202             tab
203                 + "Getting plugin entries for " //$NON-NLS-1$
204
+ sourceFeature.getVersionedIdentifier()
205                 + " ..."); //$NON-NLS-1$
206
final IPluginEntry[] sourceFeaturePluginEntries =
207             sourceFeature.getRawPluginEntries();
208
209         // determine list of plugins to install
210
// find the intersection between the plugin entries already contained
211
// on the target site, and plugin entries packaged in source feature
212

213         IPluginEntry[] pluginsToInstall =
214             UpdateManagerUtils.diff(
215                 sourceFeaturePluginEntries,
216                 getDownloadedPluginEntries());
217
218         System.out.println(
219             tab
220                 + "Getting non plugin entries for " //$NON-NLS-1$
221
+ sourceFeature.getVersionedIdentifier()
222                 + " ..."); //$NON-NLS-1$
223
final INonPluginEntry[] nonPluginsToInstall =
224             sourceFeature.getRawNonPluginEntries();
225
226         System.out.println(
227             tab
228                 + "Getting included features for " //$NON-NLS-1$
229
+ sourceFeature.getVersionedIdentifier()
230                 + " ..."); //$NON-NLS-1$
231
IFeatureReference[] children =
232             sourceFeature.getRawIncludedFeatureReferences();
233         if (optionalfeatures != null) {
234             children =
235                 UpdateManagerUtils.optionalChildrenToInstall(
236                     children,
237                     optionalfeatures);
238         }
239
240         System.out.println(
241             tab
242                 + "Downloading feature archives for " //$NON-NLS-1$
243
+ sourceFeature.getVersionedIdentifier()
244                 + " ..."); //$NON-NLS-1$
245
// download feature archives
246
provider.getFeatureEntryArchiveReferences(null);
247
248         System.out.println(
249             tab
250                 + "Downloading plug-in archives for " //$NON-NLS-1$
251
+ sourceFeature.getVersionedIdentifier()
252                 + " ..."); //$NON-NLS-1$
253
// download plugin archives
254
for (int i = 0; i < pluginsToInstall.length; i++) {
255             try {
256                 provider.getPluginEntryArchiveReferences(pluginsToInstall[i], null);
257             } catch (CoreException ce) {
258                 if ( ignoreNonPresentPlugins &&
259                         (ce instanceof CoreExceptionWithRootCause) &&
260                         (((CoreExceptionWithRootCause)ce).getRootException() != null) &&
261                         (((CoreExceptionWithRootCause)ce).getRootException() instanceof FatalIOException) ) {
262                     System.out.println("Could not mirror plug-in " + pluginsToInstall[i].getVersionedIdentifier().toString() + ". It does not exist on the given site"); //$NON-NLS-1$//$NON-NLS-2$
263
} else {
264                     throw ce;
265                 }
266             }
267         }
268
269         System.out.println(
270             tab
271                 + "Downloading non plug-in archives for " //$NON-NLS-1$
272
+ sourceFeature.getVersionedIdentifier()
273                 + " ..."); //$NON-NLS-1$
274
// download non-plugin archives
275
for (int i = 0; i < nonPluginsToInstall.length; i++) {
276             provider.getNonPluginEntryArchiveReferences(
277                 nonPluginsToInstall[i],
278                 null);
279         }
280
281         System.out.println(
282             tab
283                 + "Installing child features for " //$NON-NLS-1$
284
+ sourceFeature.getVersionedIdentifier()
285                 + " ..."); //$NON-NLS-1$
286
// install child features first
287
for (int i = 0; i < children.length; i++) {
288             IFeature childFeature = children[i].getFeature(null);
289             mirrorFeature(
290                 remoteSite,
291                 childFeature,
292                 optionalfeatures,
293                 indent + 1);
294         }
295
296         System.out.println(
297             tab
298                 + "Storing plug-in archives for " //$NON-NLS-1$
299
+ sourceFeature.getVersionedIdentifier()
300                 + " ..."); //$NON-NLS-1$
301
// store plugins' archives
302
for (int i = 0; i < pluginsToInstall.length; i++) {
303             try {
304                 ContentReference[] references = provider.getPluginEntryArchiveReferences( pluginsToInstall[i], null);
305                 storePluginArchive(references[0]);
306                 addDownloadedPluginEntry(pluginsToInstall[i]);
307             } catch (CoreException ce) {
308                 if ( ignoreNonPresentPlugins &&
309                         (ce instanceof CoreExceptionWithRootCause) &&
310                         (((CoreExceptionWithRootCause)ce).getRootException() != null) &&
311                         (((CoreExceptionWithRootCause)ce).getRootException() instanceof FatalIOException) ) {
312                     System.out.println("Could not write plug-in " + pluginsToInstall[i].getVersionedIdentifier().toString() + ". It does not exist on the given site"); //$NON-NLS-1$ //$NON-NLS-2$
313
} else {
314                     //System.out.println("ignoreNonPresentPlugins:"+ignoreNonPresentPlugins); //$NON-NLS-1$
315
throw ce;
316                 }
317             }
318         }
319
320         System.out.println(
321             tab
322                 + "Storing non plug-in archives for " //$NON-NLS-1$
323
+ sourceFeature.getVersionedIdentifier()
324                 + " ..."); //$NON-NLS-1$
325
// store non plugins' archives
326
for (int i = 0; i < nonPluginsToInstall.length; i++) {
327             ContentReference[] references =
328                 provider.getNonPluginEntryArchiveReferences(
329                     nonPluginsToInstall[i],
330                     null);
331             for (int r = 0; r < references.length; r++) {
332                 storeNonPluginArchive(
333                     sourceFeature.getVersionedIdentifier(),
334                     references[r]);
335             }
336         }
337
338         System.out.println(
339             tab
340                 + "Storing feature archives for " //$NON-NLS-1$
341
+ sourceFeature.getVersionedIdentifier()
342                 + " ..."); //$NON-NLS-1$
343
// store feature archive
344
ContentReference[] references =
345             provider.getFeatureEntryArchiveReferences(null);
346         storeFeatureArchive(references[0]);
347
348         System.out.println(
349             tab
350                 + "Adding feature " //$NON-NLS-1$
351
+ sourceFeature.getVersionedIdentifier()
352                 + " to model ..."); //$NON-NLS-1$
353

354         // add feature model to site model
355
SiteFeatureReferenceModel featureRef =
356             factory.createFeatureReferenceModel();
357         featureRef.setSiteModel(this);
358         //featureRef.setURLString(featureURL.toExternalForm());
359
featureRef.setType(ISite.DEFAULT_PACKAGED_FEATURE_TYPE);
360         featureRef.setFeatureIdentifier(
361             sourceFeature.getVersionedIdentifier().getIdentifier());
362         featureRef.setFeatureVersion(
363             sourceFeature.getVersionedIdentifier().getVersion().toString());
364         addDownloadedFeatureReferenceModel(featureRef);
365
366         System.out.println(
367             tab
368                 + "Mirroring feature " //$NON-NLS-1$
369
+ sourceFeature.getVersionedIdentifier()
370                 + " finished."); //$NON-NLS-1$
371
return featureRef;
372
373     }
374     /**
375      * Adds a feature reference model to this site,
376      * and exposes in site.xml if remote site exposes given feature.
377      */

378     public void addFeatureReferenceModel(
379         ISite remoteSite,
380         SiteFeatureReferenceModel featureReference) {
381         // check if remote site exposes this feature
382
ISiteFeatureReference remoteFeatures[] =
383             remoteSite.getRawFeatureReferences();
384         for (int i = 0; i < remoteFeatures.length; i++) {
385             ISiteFeatureReference remoteFeatureRef = remoteFeatures[i];
386             try {
387                 if (remoteFeatureRef
388                     .getVersionedIdentifier()
389                     .equals(featureReference.getVersionedIdentifier())) {
390                     addFeatureReferenceModel(featureReference);
391                 }
392             } catch (CoreException ce) {
393                 StandaloneUpdateApplication.exceptionLogged();
394                 UpdateCore.log(ce);
395             }
396         }
397         save();
398         System.out.println(
399             "Feature " //$NON-NLS-1$
400
+ featureReference.getVersionedIdentifier()
401                 + " added to site.xml."); //$NON-NLS-1$
402
}
403     /**
404      * Adds feature model to site model, removing old feature
405      */

406     public void addFeatureReferenceModel(SiteFeatureReferenceModel featureReference) {
407         SiteFeatureReferenceModel[] existingModels =
408             getFeatureReferenceModels();
409         for (int j = 0; j < existingModels.length; j++) {
410             if (existingModels[j]
411                 .getVersionedIdentifier()
412                 .equals(featureReference.getVersionedIdentifier())) {
413                 super.removeFeatureReferenceModel(existingModels[j]);
414             }
415         }
416         super.addFeatureReferenceModel(featureReference);
417     }
418
419     /**
420      * @see ISiteContentConsumer#store(ContentReference, IProgressMonitor)
421      */

422     private void storeFeatureArchive(ContentReference contentReference)
423         throws CoreException {
424         InputStream JavaDoc inStream = null;
425         String JavaDoc featurePath = null;
426
427         try {
428             URL JavaDoc newURL =
429                 new URL JavaDoc(
430                     this.getURL(),
431                     Site.DEFAULT_INSTALLED_FEATURE_PATH
432                         + contentReference.getIdentifier()
433                         + ".jar"); //$NON-NLS-1$
434
featurePath = newURL.getFile();
435             inStream = contentReference.getInputStream();
436             UpdateManagerUtils.copyToLocal(inStream, featurePath, null);
437         } catch (IOException JavaDoc e) {
438             throw Utilities.newCoreException(
439                 "Error occurred while creating "+ featurePath+" file.", //$NON-NLS-1$ //$NON-NLS-2$
440
e);
441         } finally {
442             if (inStream != null) {
443                 try {
444                     inStream.close();
445                 } catch (IOException JavaDoc e) {
446                 }
447             }
448         }
449
450     }
451     /**
452     * @see ISiteContentConsumer#store(ContentReference, IProgressMonitor)
453     */

454     private void storePluginArchive(ContentReference contentReference)
455         throws CoreException {
456
457         InputStream JavaDoc inStream = null;
458         String JavaDoc pluginPath = null;
459         try {
460             URL JavaDoc newURL = new URL JavaDoc(getURL(), contentReference.getIdentifier());
461             pluginPath = newURL.getFile();
462             inStream = contentReference.getInputStream();
463             // added null check here, since contentReference can, in theory, return null for input stream.
464
if (inStream != null) {
465                 UpdateManagerUtils.copyToLocal(inStream, pluginPath, null);
466             }
467         } catch (IOException JavaDoc e) {
468             throw Utilities.newCoreException(
469             "Error occurred while creating "+ pluginPath+" file.", //$NON-NLS-1$ //$NON-NLS-2$
470
e);
471         } finally {
472             if (inStream != null) {
473                 try {
474                     inStream.close();
475                 } catch (IOException JavaDoc e) {
476                 }
477             }
478         }
479     }
480
481     private void storeNonPluginArchive(
482         VersionedIdentifier featureVersionedIdentifier,
483         ContentReference contentReference)
484         throws CoreException {
485
486         InputStream JavaDoc inStream = null;
487         File JavaDoc nonPluginArchivePath = null;
488         try {
489             URL JavaDoc newDirURL =
490                 new URL JavaDoc(
491                     getURL(),
492                     Site.DEFAULT_INSTALLED_FEATURE_PATH
493                         + "/" //$NON-NLS-1$
494
+ featureVersionedIdentifier);
495             File JavaDoc dir = new File JavaDoc(newDirURL.getFile());
496             dir.mkdirs();
497             inStream = contentReference.getInputStream();
498             nonPluginArchivePath =
499                 new File JavaDoc(dir, contentReference.getIdentifier());
500             UpdateManagerUtils.copyToLocal(
501                 inStream,
502                 nonPluginArchivePath.getAbsolutePath(),
503                 null);
504         } catch (IOException JavaDoc e) {
505             throw Utilities.newCoreException(
506             "Error occurred while creating "+ nonPluginArchivePath.getAbsolutePath()+" file." //$NON-NLS-1$ //$NON-NLS-2$
507
,e);
508         } finally {
509             if (inStream != null) {
510                 try {
511                     inStream.close();
512                 } catch (IOException JavaDoc e) {
513                 }
514             }
515         }
516     }
517
518     private void save() {
519         FileOutputStream JavaDoc fos = null;
520         try {
521             URL JavaDoc siteURL = new URL JavaDoc(this.getURL(), "site.xml"); //$NON-NLS-1$
522
fos = new FileOutputStream JavaDoc(new File JavaDoc(siteURL.getFile()));
523             OutputStreamWriter JavaDoc outWriter = new OutputStreamWriter JavaDoc(fos, "UTF-8"); //$NON-NLS-1$
524
PrintWriter JavaDoc writer = new PrintWriter JavaDoc(outWriter);
525             save(writer);
526             writer.flush();
527         } catch (IOException JavaDoc ioe) {
528             StandaloneUpdateApplication.exceptionLogged();
529             UpdateCore.log(
530                 Utilities.newCoreException(
531                     "Site XML could not be saved.", //$NON-NLS-1$
532
ioe));
533         } finally {
534             if (fos != null) {
535                 try {
536                     fos.close();
537                 } catch (IOException JavaDoc ioe2) {
538                 }
539             }
540         }
541     }
542     private void save(PrintWriter JavaDoc writer) {
543         writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
544
//writer.println("<!DOCTYPE site SYSTEM \"dtd/site.dtd\">");
545
writeSite("", writer); //$NON-NLS-1$
546
}
547
548     private void writeSite(String JavaDoc indent, PrintWriter JavaDoc writer) {
549         writer.print(indent + "<site"); //$NON-NLS-1$
550
String JavaDoc indent2 = indent + INDENT;
551         // default type
552
//writeIfDefined(indenta, writer, "type", getType());
553
// stored relative to site.xml
554
//writeIfDefined(indenta, writer, "url", getURL());
555
writer.println(">"); //$NON-NLS-1$
556
URLEntryModel description = getDescriptionModel();
557         if (description != null) {
558             writer.println();
559             writeDescription(indent2, writer, description);
560             writer.println();
561         }
562         writeFeatures(indent2, writer);
563         writeCategories(indent2, writer);
564         writer.println(indent + "</site>"); //$NON-NLS-1$
565
}
566     private void writeFeatures(String JavaDoc indent, PrintWriter JavaDoc writer) {
567         SiteFeatureReferenceModel[] featureReferenceModels =
568             getFeatureReferenceModels();
569         for (int i = 0; i < featureReferenceModels.length; i++) {
570             writer.print(indent);
571             writer.print("<feature"); //$NON-NLS-1$
572
writer.print(
573                 " url=\"features/" //$NON-NLS-1$
574
+ featureReferenceModels[i].getFeatureIdentifier()
575                     + "_" //$NON-NLS-1$
576
+ featureReferenceModels[i].getFeatureVersion()
577                     + ".jar\""); //$NON-NLS-1$
578
writer.print(
579                 " id=\"" //$NON-NLS-1$
580
+ featureReferenceModels[i].getFeatureIdentifier()
581                     + "\""); //$NON-NLS-1$
582
writer.print(
583                 " version=\"" //$NON-NLS-1$
584
+ featureReferenceModels[i].getFeatureVersion()
585                     + "\""); //$NON-NLS-1$
586
writer.println(">"); //$NON-NLS-1$
587

588             String JavaDoc[] categoryNames =
589                 featureReferenceModels[i].getCategoryNames();
590             for (int cn = 0; cn < categoryNames.length; cn++) {
591                 writer.print(indent + INDENT);
592                 writer.println(
593                     "<category name=\"" + categoryNames[cn] + "\" />"); //$NON-NLS-1$ //$NON-NLS-2$
594

595             }
596
597             writer.print(indent);
598             writer.println("</feature>"); //$NON-NLS-1$
599
writer.println();
600         }
601     }
602     private void writeCategories(String JavaDoc indent, PrintWriter JavaDoc writer) {
603         CategoryModel[] categoryModels = getCategoryModels();
604         if (categoryModels.length <= 0) {
605             return;
606         }
607         for (int i = 0; i < categoryModels.length; i++) {
608             writer.print(indent);
609             writer.print("<category-def"); //$NON-NLS-1$
610
writer.print(
611                 " name=\"" //$NON-NLS-1$
612
+ categoryModels[i].getName()
613                     + "\" label=\"" //$NON-NLS-1$
614
+ categoryModels[i].getLabel()
615                     + "\""); //$NON-NLS-1$
616
writer.println(">"); //$NON-NLS-1$
617
if (categoryModels[i].getDescriptionModel() != null) {
618                 writeDescription(
619                         indent + INDENT,
620                         writer,
621                         categoryModels[i].getDescriptionModel());
622             }
623             writer.print(indent);
624             writer.println("</category-def>"); //$NON-NLS-1$
625
writer.println();
626         }
627     }
628     private void writeDescription(
629         String JavaDoc indent,
630         PrintWriter JavaDoc writer,
631         URLEntryModel urlEntryModel) {
632         String JavaDoc url = urlEntryModel.getURLString();
633         String JavaDoc text = urlEntryModel.getAnnotationNonLocalized();
634         if (url == null && text == null && text.length() <= 0) {
635             return;
636         }
637         writer.print(indent);
638         writer.print("<description"); //$NON-NLS-1$
639
if (url != null)
640             writer.print(" url=\"" + url + "\""); //$NON-NLS-1$ //$NON-NLS-2$
641
if (text == null || text.length() <= 0) {
642             writer.println(" />"); //$NON-NLS-1$
643
} else {
644             writer.println(">"); //$NON-NLS-1$
645
if (text != null) {
646                 writer.println(
647                     indent + INDENT + UpdateManagerUtils.Writer.xmlSafe(text));
648             }
649             writer.println(indent + "</description>"); //$NON-NLS-1$
650
}
651     }
652     /**
653      * Adds a plugin entry
654      * Either from parsing the file system or
655      * installing a feature
656      *
657      * We cannot figure out the list of plugins by reading the Site.xml as
658      * the archives tag are optionals
659      */

660     public void addDownloadedPluginEntry(IPluginEntry pluginEntry) {
661         downloadedPluginEntries.add(pluginEntry);
662     }
663
664     private IPluginEntry[] getDownloadedPluginEntries() {
665         return (IPluginEntry[]) downloadedPluginEntries.toArray(
666             new IPluginEntry[downloadedPluginEntries.size()]);
667     }
668     /**
669      * Adds a plugin entry
670      * Either from parsing the file system or
671      * installing a feature
672      *
673      * We cannot figure out the list of plugins by reading the Site.xml as
674      * the archives tag are optionals
675      */

676     public void addDownloadedFeatureReferenceModel(SiteFeatureReferenceModel featureModel) {
677         downloadedFeatureReferenceModels.add(featureModel);
678     }
679
680     private SiteFeatureReferenceModel[] getDownloadedFeatureReferenceModels() {
681         return (
682             SiteFeatureReferenceModel[]) downloadedFeatureReferenceModels
683                 .toArray(
684             new SiteFeatureReferenceModel[downloadedFeatureReferenceModels
685                 .size()]);
686     }
687     /**
688      * Checks if mirror site contains a feature with given ID and version
689      * @param featureRefModel
690      * @return true if such feature exists
691      */

692     /*private boolean contains(SiteFeatureReferenceModel featureRefModel) {
693         ISiteFeatureReference featureRefs[] = getRawFeatureReferences();
694         for (int i = 0; i < featureRefs.length; i++) {
695             try {
696                 if (featureRefs[i]
697                     .getVersionedIdentifier()
698                     .equals(featureRefModel.getVersionedIdentifier())) {
699                     return true;
700                 }
701             } catch (CoreException ce) {
702                 ce.printStackTrace();
703             }
704         }
705         return false;
706     }*/

707
708     /**
709      * Updates description of this site
710      * from description of the remote site.
711      */

712     private void updateDescription(ISite remoteSite) {
713         IURLEntry urlEntry = remoteSite.getDescription();
714         if (urlEntry != null) {
715             URLEntryModel newUrlEntryModel = new URLEntryModel();
716             URL JavaDoc url = urlEntry.getURL();
717             newUrlEntryModel.setAnnotation(urlEntry.getAnnotation());
718             // https://bugs.eclipse.org/bugs/show_bug.cgi?id=136249
719
// URL is not required, so might be null
720
// The null case is (already) handled correctly in
721
// writeDescription
722
if (url != null) {
723                 newUrlEntryModel.setURLString(url.toExternalForm());
724             }
725             this.setDescriptionModel(newUrlEntryModel);
726         }
727     }
728     /**
729      * Updates all categories used by features on this site
730      * from categories defined on remote site.
731      * Categories not defined on remote site are unchanged.
732      */

733     private void updateCategories(ISite remoteSite) {
734         // collect name of categories used on this site
735
Set JavaDoc usedCategoryNames = new HashSet JavaDoc();
736         SiteFeatureReferenceModel featureRefModels[] =
737             getFeatureReferenceModels();
738         for (int f = 0; f < featureRefModels.length; f++) {
739             String JavaDoc[] featureCategoryNames =
740                 featureRefModels[f].getCategoryNames();
741
742             for (int c = 0; c < featureCategoryNames.length; c++) {
743                 usedCategoryNames.add(featureCategoryNames[c]);
744             }
745         }
746
747         Collection JavaDoc newCategoryModels = new ArrayList JavaDoc();
748         for (Iterator JavaDoc it = usedCategoryNames.iterator(); it.hasNext();) {
749             String JavaDoc name = (String JavaDoc) it.next();
750             ICategory remoteCategory = remoteSite.getCategory(name);
751             if (remoteCategory == null) {
752                 // remote site does not define this category
753
CategoryModel oldCategory = null;
754                 try {
755                     oldCategory = (CategoryModel) getCategory(name);
756                 } catch (NullPointerException JavaDoc npe) {
757                     // cannot reproduce npe anymore
758
}
759                 if (oldCategory != null) {
760                     newCategoryModels.add(oldCategory);
761                 }
762             } else {
763                 newCategoryModels.add(remoteCategory);
764             }
765
766         }
767         setCategoryModels(
768             (CategoryModel[]) newCategoryModels.toArray(
769                 new CategoryModel[newCategoryModels.size()]));
770
771     }
772     private void generateUpdatePolicy(String JavaDoc url) {
773         FileOutputStream JavaDoc fos = null;
774         try {
775             URL JavaDoc siteURL = new URL JavaDoc(this.getURL(), "policy.xml"); //$NON-NLS-1$
776
fos = new FileOutputStream JavaDoc(new File JavaDoc(siteURL.getFile()));
777             OutputStreamWriter JavaDoc outWriter = new OutputStreamWriter JavaDoc(fos, "UTF-8"); //$NON-NLS-1$
778
PrintWriter JavaDoc writer = new PrintWriter JavaDoc(outWriter);
779
780             writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
781
writer.println("<update-policy>"); //$NON-NLS-1$
782

783             writer.println(
784                 "<!-- You can paste the following fragment, containing url-map elements, into another policy file. -->"); //$NON-NLS-1$
785
writeUrlMaps(writer, url);
786             writer.println("<!-- End of fragment with url-map elements. -->"); //$NON-NLS-1$
787

788             writer.println("</update-policy>"); //$NON-NLS-1$
789

790             writer.flush();
791         } catch (IOException JavaDoc ioe) {
792             StandaloneUpdateApplication.exceptionLogged();
793             UpdateCore.log(
794                 Utilities.newCoreException(
795                     "policy.xml could not be saved", //$NON-NLS-1$
796
ioe));
797         } finally {
798             if (fos != null) {
799                 try {
800                     fos.close();
801                 } catch (IOException JavaDoc ioe2) {
802                 }
803             }
804         }
805     }
806     private void writeUrlMaps(PrintWriter JavaDoc writer, String JavaDoc url) {
807         SiteFeatureReferenceModel[] featureReferenceModels =
808             getFeatureReferenceModels();
809         for (int i = 0; i < featureReferenceModels.length; i++) {
810             writer.print("\t"); //$NON-NLS-1$
811
writer.print("<url-map"); //$NON-NLS-1$
812
writer.print(
813                 " pattern=\"" //$NON-NLS-1$
814
+ featureReferenceModels[i].getFeatureIdentifier()
815                     + "\""); //$NON-NLS-1$
816
writer.print(" url=\"" + url + "\""); //$NON-NLS-1$ //$NON-NLS-2$
817
writer.println(" />"); //$NON-NLS-1$
818
}
819     }
820
821     public void setIgnoreNonPresentPlugins(boolean ignoreNonPresentPlugins) {
822         this.ignoreNonPresentPlugins = ignoreNonPresentPlugins;
823         
824     }
825 }
826
Popular Tags