Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9478ae57af2a9135ac8dbbeb99b3d1b25cdae4d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/****************************************************************************
 * Copyright (c) 2006, 2007 Composent, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/
package org.eclipse.ecf.internal.provider.filetransfer;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import org.eclipse.core.net.proxy.IProxyService;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionDelta;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IRegistryChangeEvent;
import org.eclipse.core.runtime.IRegistryChangeListener;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.util.LogHelper;
import org.eclipse.ecf.core.util.PlatformHelper;
import org.eclipse.ecf.filetransfer.service.IRemoteFileSystemBrowser;
import org.eclipse.ecf.filetransfer.service.IRemoteFileSystemBrowserFactory;
import org.eclipse.ecf.filetransfer.service.IRetrieveFileTransfer;
import org.eclipse.ecf.filetransfer.service.IRetrieveFileTransferFactory;
import org.eclipse.ecf.filetransfer.service.ISendFileTransfer;
import org.eclipse.ecf.filetransfer.service.ISendFileTransferFactory;
import org.eclipse.ecf.provider.filetransfer.IFileTransferProtocolToFactoryMapper;
import org.eclipse.ecf.provider.filetransfer.retrieve.MultiProtocolRetrieveAdapter;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.log.LogService;
import org.osgi.service.url.AbstractURLStreamHandlerService;
import org.osgi.service.url.URLConstants;
import org.osgi.service.url.URLStreamHandlerService;
import org.osgi.util.tracker.ServiceTracker;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator implements BundleActivator, IFileTransferProtocolToFactoryMapper {

	// This is the name of a system property 'org.eclipse.ecf.provider.filetransfer.excludeContributors' 
	// that allows people to defeat the priority system for the browse, send, and retrieve factory contributions.
	// If a plugin (symbolic id) is given in this property (multiples separated by comma), then
	// that plugin's contributions will *not* be added to the browse, send, and retrieve protocol factories
	public static final String PLUGIN_EXCLUDED_SYS_PROP_NAME = Activator.PLUGIN_ID + ".excludeContributors"; //$NON-NLS-1$

	private static final String CLASS_ATTR = "class"; //$NON-NLS-1$
	private static final String PRIORITY_ATTR = "priority"; //$NON-NLS-1$
	private static final int DEFAULT_PRIORITY = 100;
	private static final String PROTOCOL_ATTR = "protocol"; //$NON-NLS-1$
	private static final String[] jvmSchemes = new String[] {Messages.FileTransferNamespace_Http_Protocol, Messages.FileTransferNamespace_Ftp_Protocol, Messages.FileTransferNamespace_File_Protocol, Messages.FileTransferNamespace_Jar_Protocol, Messages.FileTransferNamespace_Https_Protocol, Messages.FileTransferNamespace_Mailto_Protocol, Messages.FileTransferNamespace_Gopher_Protocol};

	private static final String URL_HANDLER_PROTOCOL_NAME = "url.handler.protocol"; //$NON-NLS-1$

	private static final String URLSTREAM_HANDLER_SERVICE_NAME = "org.osgi.service.url.URLStreamHandlerService"; //$NON-NLS-1$

	// The plug-in ID
	public static final String PLUGIN_ID = "org.eclipse.ecf.provider.filetransfer"; //$NON-NLS-1$

	private static final String RETRIEVE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT_NAME = "retrieveFileTransferProtocolFactory"; //$NON-NLS-1$

	private static final String RETRIEVE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT = PLUGIN_ID + "." //$NON-NLS-1$
			+ RETRIEVE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT_NAME;

	private static final String SEND_FILETRANSFER_PROTOCOL_FACTORY_EPOINT_NAME = "sendFileTransferProtocolFactory"; //$NON-NLS-1$

	private static final String SEND_FILETRANSFER_PROTOCOL_FACTORY_EPOINT = PLUGIN_ID + "." //$NON-NLS-1$
			+ SEND_FILETRANSFER_PROTOCOL_FACTORY_EPOINT_NAME;

	private static final String BROWSE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT_NAME = "browseFileTransferProtocolFactory"; //$NON-NLS-1$

	private static final String BROWSE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT = PLUGIN_ID + "." //$NON-NLS-1$
			+ BROWSE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT_NAME;

	// The shared instance
	private static Activator plugin;

	private BundleContext context = null;

	private ServiceRegistration fileTransferServiceRegistration;

	private ServiceTracker logServiceTracker = null;
	private ServiceTracker extensionRegistryTracker = null;

	private Map retrieveFileTransferProtocolMap;

	private Map sendFileTransferProtocolMap;

	private Map browseFileTransferProtocolMap;

	private ServiceTracker adapterManagerTracker = null;

	private ServiceTracker proxyServiceTracker = null;

	private IURLConnectionModifier urlConnectionModifier = null;

	private String[] excludedPlugins = null;

	private ServiceRegistration protocolMapperRegistration;

	private IRegistryChangeListener registryChangeListener = new IRegistryChangeListener() {

		public void registryChanged(IRegistryChangeEvent event) {
			final IExtensionDelta retrieveDelta[] = event.getExtensionDeltas(PLUGIN_ID, RETRIEVE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT_NAME);
			for (int i = 0; i < retrieveDelta.length; i++) {
				switch (retrieveDelta[i].getKind()) {
					case IExtensionDelta.ADDED :
						addRetrieveExtensions(retrieveDelta[i].getExtension().getConfigurationElements());
						break;
					case IExtensionDelta.REMOVED :
						removeRetrieveExtensions(retrieveDelta[i].getExtension().getConfigurationElements());
						break;
				}
			}
			final IExtensionDelta sendDelta[] = event.getExtensionDeltas(PLUGIN_ID, SEND_FILETRANSFER_PROTOCOL_FACTORY_EPOINT_NAME);
			for (int i = 0; i < sendDelta.length; i++) {
				switch (sendDelta[i].getKind()) {
					case IExtensionDelta.ADDED :
						addSendExtensions(sendDelta[i].getExtension().getConfigurationElements());
						break;
					case IExtensionDelta.REMOVED :
						removeSendExtensions(sendDelta[i].getExtension().getConfigurationElements());
						break;
				}
			}
			final IExtensionDelta browseDelta[] = event.getExtensionDeltas(PLUGIN_ID, BROWSE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT_NAME);
			for (int i = 0; i < browseDelta.length; i++) {
				switch (browseDelta[i].getKind()) {
					case IExtensionDelta.ADDED :
						addBrowseExtensions(browseDelta[i].getExtension().getConfigurationElements());
						break;
					case IExtensionDelta.REMOVED :
						removeBrowseExtensions(browseDelta[i].getExtension().getConfigurationElements());
						break;
				}
			}
		}

	};

	private String[] parseExcludedPlugins() {
		String prop = System.getProperty(PLUGIN_EXCLUDED_SYS_PROP_NAME);
		if (prop == null)
			return new String[0];
		StringTokenizer tok = new StringTokenizer(prop, ","); //$NON-NLS-1$
		int count = tok.countTokens();
		String[] results = new String[count];
		for (int i = 0; i < count; i++) {
			results[i] = tok.nextToken();
		}
		return results;
	}

	/**
	 * The constructor
	 */
	public Activator() {
		//
	}

	protected LogService getLogService() {
		synchronized (this) {
			if (this.context == null)
				return null;
			if (logServiceTracker == null) {
				logServiceTracker = new ServiceTracker(this.context, LogService.class.getName(), null);
				logServiceTracker.open();
			}
			return (LogService) logServiceTracker.getService();
		}
	}

	public IProxyService getProxyService() {
		try {
			if (proxyServiceTracker == null) {
				proxyServiceTracker = new ServiceTracker(this.context, IProxyService.class.getName(), null);
				proxyServiceTracker.open();
			}
			return (IProxyService) proxyServiceTracker.getService();
		} catch (Exception e) {
			logNoProxyWarning(e);
		} catch (NoClassDefFoundError e) {
			logNoProxyWarning(e);
		}
		return null;
	}

	public static void logNoProxyWarning(Throwable e) {
		getDefault().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, IStatus.ERROR, "Warning: Platform proxy API not available", e)); //$NON-NLS-1$
	}

	public void log(IStatus status) {
		if (this.context == null)
			return;
		final LogService logService = getLogService();
		if (logService != null) {
			logService.log(LogHelper.getLogCode(status), LogHelper.getLogMessage(status), status.getException());
		}
	}

	public Bundle getBundle() {
		if (context == null)
			return null;
		return context.getBundle();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext ctxt) throws Exception {
		plugin = this;
		this.context = ctxt;
		this.retrieveFileTransferProtocolMap = new HashMap(3);
		this.sendFileTransferProtocolMap = new HashMap(3);
		this.browseFileTransferProtocolMap = new HashMap(3);

		// initialize the default url connection modifier for ssl
		try {
			Class urlConnectionModifierClass = Class.forName("org.eclipse.ecf.internal.provider.filetransfer.ssl.ECFURLConnectionModifier"); //$NON-NLS-1$
			urlConnectionModifier = (IURLConnectionModifier) urlConnectionModifierClass.newInstance();
			urlConnectionModifier.init(ctxt);
		} catch (ClassNotFoundException e) {
			// will occur if fragment is not installed or not on proper execution environment
		} catch (Throwable t) {
			log(new Status(IStatus.ERROR, getDefault().getBundle().getSymbolicName(), "Unexpected Error in Activator.start", t)); //$NON-NLS-1$
		}

		fileTransferServiceRegistration = ctxt.registerService(IRetrieveFileTransferFactory.class.getName(), new IRetrieveFileTransferFactory() {
			public IRetrieveFileTransfer newInstance() {
				return new MultiProtocolRetrieveAdapter();
			}
		}, null);
		this.extensionRegistryTracker = new ServiceTracker(ctxt, IExtensionRegistry.class.getName(), null);
		this.extensionRegistryTracker.open();
		final IExtensionRegistry registry = getExtensionRegistry();
		if (registry != null) {
			registry.addRegistryChangeListener(registryChangeListener);
		}
		// Can't be lazy about this, as schemes need to be registered with
		// platform
		loadProtocolHandlers();
		// Finally, register this object as a IFileTransferProtocolToFactoryMapper service
		protocolMapperRegistration = context.registerService(IFileTransferProtocolToFactoryMapper.class.getName(), this, null);
	}

	public boolean reinitialize() {
		try {
			loadProtocolHandlers();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext ctxt) throws Exception {
		final IExtensionRegistry registry = getExtensionRegistry();
		if (registry != null) {
			registry.removeRegistryChangeListener(registryChangeListener);
		}

		if (urlConnectionModifier != null) {
			urlConnectionModifier.dispose();
			urlConnectionModifier = null;
		}
		if (extensionRegistryTracker != null) {
			extensionRegistryTracker.close();
			extensionRegistryTracker = null;
		}
		if (fileTransferServiceRegistration != null) {
			fileTransferServiceRegistration.unregister();
			fileTransferServiceRegistration = null;
		}
		if (adapterManagerTracker != null) {
			adapterManagerTracker.close();
			adapterManagerTracker = null;
		}
		if (proxyServiceTracker != null) {
			proxyServiceTracker.close();
			proxyServiceTracker = null;
		}
		if (this.retrieveFileTransferProtocolMap != null) {
			this.retrieveFileTransferProtocolMap.clear();
			this.retrieveFileTransferProtocolMap = null;
		}
		if (this.sendFileTransferProtocolMap != null) {
			this.sendFileTransferProtocolMap.clear();
			this.sendFileTransferProtocolMap = null;
		}
		if (this.browseFileTransferProtocolMap != null) {
			this.browseFileTransferProtocolMap.clear();
			this.browseFileTransferProtocolMap = null;
		}
		if (this.protocolMapperRegistration != null) {
			this.protocolMapperRegistration.unregister();
			this.protocolMapperRegistration = null;
		}

		synchronized (this) {
			this.context = null;
		}
		plugin = null;
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public synchronized static Activator getDefault() {
		if (plugin == null) {
			plugin = new Activator();
		}
		return plugin;
	}

	public String[] getPlatformSupportedSchemes() {
		final ServiceTracker handlers = new ServiceTracker(context, URLSTREAM_HANDLER_SERVICE_NAME, null);
		handlers.open();
		final ServiceReference[] refs = handlers.getServiceReferences();
		final Set protocols = new HashSet();
		if (refs != null)
			for (int i = 0; i < refs.length; i++) {
				final Object protocol = refs[i].getProperty(URL_HANDLER_PROTOCOL_NAME);
				if (protocol instanceof String)
					protocols.add(protocol);
				else if (protocol instanceof String[]) {
					final String[] ps = (String[]) protocol;
					for (int j = 0; j < ps.length; j++)
						protocols.add(ps[j]);
				}
			}
		handlers.close();
		for (int i = 0; i < jvmSchemes.length; i++)
			protocols.add(jvmSchemes[i]);
		return (String[]) protocols.toArray(new String[] {});
	}

	public IExtensionRegistry getExtensionRegistry() {
		if (extensionRegistryTracker == null) {
			this.extensionRegistryTracker = new ServiceTracker(context, IExtensionRegistry.class.getName(), null);
			this.extensionRegistryTracker.open();
		}
		return (IExtensionRegistry) extensionRegistryTracker.getService();
	}

	static class ProtocolFactory implements Comparable {
		Object factory;
		int priority = 0;
		String id;

		public ProtocolFactory(Object factory, int priority, String id) {
			this.factory = factory;
			this.priority = priority;
			this.id = id;
		}

		public Object getFactory() {
			return factory;
		}

		public String getID() {
			return id;
		}

		public int getPriority() {
			return priority;
		}

		/* (non-Javadoc)
		 * @see java.lang.Comparable#compareTo(java.lang.Object)
		 */
		public int compareTo(Object another) {
			if (!(another instanceof ProtocolFactory))
				return -1;
			ProtocolFactory other = (ProtocolFactory) another;
			if (this.priority == other.priority)
				return 0;
			return (this.priority < other.priority) ? -1 : 1;
		}
	}

	private int getPriority(IConfigurationElement configElement, String warning, String protocol) {
		// Get priority for new entry, if optional priority attribute specified
		final String priorityString = configElement.getAttribute(PRIORITY_ATTR);
		int priority = DEFAULT_PRIORITY;
		if (priorityString != null) {
			try {
				priority = new Integer(priorityString).intValue();
				// Make sure that any negative values are reset to 0 (highest priority)
				priority = (priority < 0) ? 0 : priority;
			} catch (NumberFormatException e) {
				// Give warning
				Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, NLS.bind("{0} for {1} from {2} has invalid priority {3}. Priority will be set to {4}", new Object[] {warning, protocol, configElement.getDeclaringExtension().getContributor().getName(), priorityString, String.valueOf(DEFAULT_PRIORITY)}), null)); //$NON-NLS-1$
			}
		}
		return priority;
	}

	boolean pluginExcluded(String pluginId) {
		if (excludedPlugins == null) {
			excludedPlugins = parseExcludedPlugins();
		}
		List l = Arrays.asList(excludedPlugins);
		return l.contains(pluginId);
	}

	void addRetrieveExtensions(IConfigurationElement[] configElements) {
		for (int i = 0; i < configElements.length; i++) {
			final String protocol = configElements[i].getAttribute(PROTOCOL_ATTR);
			if (protocol == null || "".equals(protocol)) //$NON-NLS-1$
				return;
			String CONTRIBUTION_WARNING = "File retrieve contribution"; //$NON-NLS-1$
			try {
				String pluginId = configElements[i].getDeclaringExtension().getContributor().getName();
				// Only add the factories if the contributor plugin has not been excluded
				if (!pluginExcluded(pluginId)) {
					// First create factory clazz 
					final IRetrieveFileTransferFactory retrieveFactory = (IRetrieveFileTransferFactory) configElements[i].createExecutableExtension(CLASS_ATTR);
					// Get priority for new entry, if optional priority attribute specified
					int priority = getPriority(configElements[i], CONTRIBUTION_WARNING, protocol);
					String contributorName = configElements[i].getDeclaringExtension().getContributor().getName();
					// Now add new ProtocolFactory
					setRetrieveFileTransferFactory(protocol, contributorName, retrieveFactory, priority);
				} else {
					Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, "Plugin " + pluginId + " excluded from contributing retrieve factory", null)); //$NON-NLS-1$ //$NON-NLS-2$
				}
			} catch (final CoreException e) {
				Activator.getDefault().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, NLS.bind("Error loading from {0} extension point", RETRIEVE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT), e)); //$NON-NLS-1$
			}
		}
	}

	void removeRetrieveExtensions(IConfigurationElement[] configElements) {
		for (int i = 0; i < configElements.length; i++) {
			final String protocol = configElements[i].getAttribute(PROTOCOL_ATTR);
			if (protocol == null || "".equals(protocol)) //$NON-NLS-1$
				return;
			String id = getRetrieveFileTransferFactoryId(protocol);
			if (id != null)
				removeRetrieveFileTransferFactory(id);
		}
	}

	void addSendExtensions(IConfigurationElement[] configElements) {
		for (int i = 0; i < configElements.length; i++) {
			final String protocol = configElements[i].getAttribute(PROTOCOL_ATTR);
			if (protocol == null || "".equals(protocol)) //$NON-NLS-1$
				return;
			String CONTRIBUTION_WARNING = "File send contribution"; //$NON-NLS-1$
			try {
				String pluginId = configElements[i].getDeclaringExtension().getContributor().getName();
				// Only add the factories if the contributor plugin has not been excluded
				if (!pluginExcluded(pluginId)) {
					// First create factory clazz 
					final ISendFileTransferFactory clazz = (ISendFileTransferFactory) configElements[i].createExecutableExtension(CLASS_ATTR);
					// Get priority for new entry, if optional priority attribute specified
					int priority = getPriority(configElements[i], CONTRIBUTION_WARNING, protocol);
					setSendFileTransferFactory(protocol, pluginId, clazz, priority);
				} else {
					Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, "Plugin " + pluginId + " excluded from contributing send factory", null)); //$NON-NLS-1$ //$NON-NLS-2$
				}
			} catch (final CoreException e) {
				Activator.getDefault().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, NLS.bind("Error loading from {0} extension point", SEND_FILETRANSFER_PROTOCOL_FACTORY_EPOINT), e)); //$NON-NLS-1$
			}
		}
	}

	void removeSendExtensions(IConfigurationElement[] configElements) {
		for (int i = 0; i < configElements.length; i++) {
			final String protocol = configElements[i].getAttribute(PROTOCOL_ATTR);
			if (protocol == null || "".equals(protocol)) //$NON-NLS-1$
				return;
			String id = getSendFileTransferFactoryId(protocol);
			if (id != null)
				removeSendFileTransferFactory(id);
		}
	}

	void addBrowseExtensions(IConfigurationElement[] configElements) {
		for (int i = 0; i < configElements.length; i++) {
			final String protocol = configElements[i].getAttribute(PROTOCOL_ATTR);
			if (protocol == null || "".equals(protocol)) //$NON-NLS-1$
				return;
			String CONTRIBUTION_WARNING = "File browse contribution"; //$NON-NLS-1$
			try {
				String pluginId = configElements[i].getDeclaringExtension().getContributor().getName();
				// Only add the factories if the contributor plugin has not been excluded
				if (!pluginExcluded(pluginId)) {
					// First create factory clazz 
					final IRemoteFileSystemBrowserFactory clazz = (IRemoteFileSystemBrowserFactory) configElements[i].createExecutableExtension(CLASS_ATTR);
					// Get priority for new entry, if optional priority attribute specified
					int priority = getPriority(configElements[i], CONTRIBUTION_WARNING, protocol);
					setBrowseFileTransferFactory(protocol, pluginId, clazz, priority);
				} else {
					Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, "Plugin " + pluginId + " excluded from contributing browse factory", null)); //$NON-NLS-1$ //$NON-NLS-2$
				}
			} catch (final CoreException e) {
				Activator.getDefault().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, NLS.bind("Error loading from {0} extension point", BROWSE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT), e)); //$NON-NLS-1$
			}
		}
	}

	void removeBrowseExtensions(IConfigurationElement[] configElements) {
		for (int i = 0; i < configElements.length; i++) {
			final String protocol = configElements[i].getAttribute(PROTOCOL_ATTR);
			if (protocol == null || "".equals(protocol)) //$NON-NLS-1$
				return;
			String id = getBrowseFileTransferFactoryId(protocol);
			if (id != null)
				removeBrowseFileTransferFactory(id);
		}
	}

	private void loadProtocolHandlers() {
		final IExtensionRegistry reg = getExtensionRegistry();
		if (reg != null) {
			final IExtensionPoint retrieveExtensionPoint = reg.getExtensionPoint(RETRIEVE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT);
			if (retrieveExtensionPoint != null)
				addRetrieveExtensions(retrieveExtensionPoint.getConfigurationElements());
			// Now do it with send
			final IExtensionPoint sendExtensionPoint = reg.getExtensionPoint(SEND_FILETRANSFER_PROTOCOL_FACTORY_EPOINT);
			if (sendExtensionPoint != null)
				addSendExtensions(sendExtensionPoint.getConfigurationElements());
			// Now for browse
			final IExtensionPoint browseExtensionPoint = reg.getExtensionPoint(BROWSE_FILETRANSFER_PROTOCOL_FACTORY_EPOINT);
			if (browseExtensionPoint != null)
				addBrowseExtensions(browseExtensionPoint.getConfigurationElements());
		}
	}

	private boolean isSchemeRegistered(String protocol, String[] schemes) {
		for (int i = 0; i < schemes.length; i++) {
			if (protocol.equals(schemes[i]))
				return true;
		}
		return false;
	}

	class DummyURLStreamHandlerService extends AbstractURLStreamHandlerService {

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.osgi.service.url.AbstractURLStreamHandlerService#openConnection(java.net.URL)
		 */
		public URLConnection openConnection(URL u) throws IOException {
			throw new IOException(NLS.bind("URLConnection cannot be created for {0}", u.toExternalForm())); //$NON-NLS-1$
		}

	}

	private final DummyURLStreamHandlerService dummyService = new DummyURLStreamHandlerService();

	private void registerScheme(String protocol) {
		final Hashtable properties = new Hashtable();
		properties.put(URLConstants.URL_HANDLER_PROTOCOL, new String[] {protocol});
		context.registerService(URLStreamHandlerService.class.getName(), dummyService, properties);
	}

	public IRetrieveFileTransfer getFileTransfer(String protocol) {
		ProtocolFactory protocolFactory = null;
		synchronized (retrieveFileTransferProtocolMap) {
			protocolFactory = (ProtocolFactory) retrieveFileTransferProtocolMap.get(protocol);
		}
		if (protocolFactory == null)
			return null;
		final IRetrieveFileTransferFactory factory = (IRetrieveFileTransferFactory) protocolFactory.getFactory();
		if (factory != null)
			return factory.newInstance();
		return null;
	}

	public ISendFileTransfer getSendFileTransfer(String protocol) {
		ProtocolFactory protocolFactory = null;
		synchronized (sendFileTransferProtocolMap) {
			protocolFactory = (ProtocolFactory) sendFileTransferProtocolMap.get(protocol);
		}
		if (protocolFactory == null)
			return null;
		final ISendFileTransferFactory factory = (ISendFileTransferFactory) protocolFactory.getFactory();
		if (factory != null)
			return factory.newInstance();
		return null;
	}

	public IRemoteFileSystemBrowser getBrowseFileTransfer(String protocol) {
		ProtocolFactory protocolFactory = null;
		synchronized (browseFileTransferProtocolMap) {
			protocolFactory = (ProtocolFactory) browseFileTransferProtocolMap.get(protocol);
		}
		if (protocolFactory == null)
			return null;
		final IRemoteFileSystemBrowserFactory factory = (IRemoteFileSystemBrowserFactory) protocolFactory.getFactory();
		if (factory != null)
			return factory.newInstance();
		return null;
	}

	public IAdapterManager getAdapterManager() {
		// First, try to get the adapter manager via
		if (adapterManagerTracker == null) {
			adapterManagerTracker = new ServiceTracker(this.context, IAdapterManager.class.getName(), null);
			adapterManagerTracker.open();
		}
		IAdapterManager adapterManager = (IAdapterManager) adapterManagerTracker.getService();
		// Then, if the service isn't there, try to get from Platform class via
		// PlatformHelper class
		if (adapterManager == null)
			adapterManager = PlatformHelper.getPlatformAdapterManager();
		return adapterManager;
	}

	public IURLConnectionModifier getURLConnectionModifier() {
		return urlConnectionModifier;
	}

	public boolean setRetrieveFileTransferFactory(String protocol, String id, IRetrieveFileTransferFactory factory, int priority) {
		if (protocol == null || "".equals(protocol)) //$NON-NLS-1$
			return false;
		if (id == null)
			return false;
		if (factory == null)
			return false;
		if (!pluginExcluded(id)) {
			// Now create new ProtocolFactory
			ProtocolFactory newProtocolFactory = new ProtocolFactory(factory, priority, id);
			synchronized (retrieveFileTransferProtocolMap) {
				ProtocolFactory oldProtocolFactory = (ProtocolFactory) retrieveFileTransferProtocolMap.get(protocol);
				// If found, choose between them based upon comparing their priority
				if (oldProtocolFactory != null) {
					// Now, compare priorities and pick winner
					String CONTRIBUTION_WARNING = "File retrieve contribution"; //$NON-NLS-1$
					int result = oldProtocolFactory.compareTo(newProtocolFactory);
					if (result < 0) {
						// Existing one has higher priority, so we provide warning and return (leaving existing one as the handler)
						Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, NLS.bind("{0} for protocol {1} from {2} will be ignored.  Existing protocol factory has higher priority.", new Object[] {CONTRIBUTION_WARNING, protocol, id}), null)); //$NON-NLS-1$
						return false;
					} else if (result == 0) {
						// Warn that we are using new one because they have the same priority.
						Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, NLS.bind("{0} for protocol {1} from {2} will be used in preference to existing handler.  Both have same priority={3}.", new Object[] {CONTRIBUTION_WARNING, protocol, id, new Integer(priority)}), null)); //$NON-NLS-1$
					} else if (result > 0) {
						// Warn that we are using new one because it has higher priority.
						Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, NLS.bind("{0} for protocol {1} from {2} will be used in preference to existing handler.  New handler has higher priority={3}<{4}.", new Object[] {CONTRIBUTION_WARNING, protocol, id, new Integer(priority), new Integer(oldProtocolFactory.priority)}), null)); //$NON-NLS-1$
					}
				}
				String[] existingSchemes = getPlatformSupportedSchemes();
				if (!isSchemeRegistered(protocol, existingSchemes))
					registerScheme(protocol);
				// Finally, put clazz in map with protocol as key
				retrieveFileTransferProtocolMap.put(protocol, newProtocolFactory);
				return true;
			}
		}
		return false;
	}

	public String getRetrieveFileTransferFactoryId(String protocol) {
		if (protocol == null)
			return null;
		synchronized (retrieveFileTransferProtocolMap) {
			ProtocolFactory oldProtocolFactory = (ProtocolFactory) retrieveFileTransferProtocolMap.get(protocol);
			if (oldProtocolFactory == null)
				return null;
			return oldProtocolFactory.getID();
		}
	}

	public int getRetrieveFileTransferPriority(String protocol) {
		if (protocol == null)
			return -1;
		synchronized (retrieveFileTransferProtocolMap) {
			ProtocolFactory oldProtocolFactory = (ProtocolFactory) retrieveFileTransferProtocolMap.get(protocol);
			if (oldProtocolFactory == null)
				return -1;
			return oldProtocolFactory.getPriority();
		}
	}

	public boolean removeRetrieveFileTransferFactory(String id) {
		if (id == null)
			return false;
		boolean removed = false;
		synchronized (retrieveFileTransferProtocolMap) {
			for (Iterator i = retrieveFileTransferProtocolMap.keySet().iterator(); i.hasNext();) {
				ProtocolFactory oldProtocolFactory = (ProtocolFactory) retrieveFileTransferProtocolMap.get(i.next());
				if (oldProtocolFactory == null)
					continue;
				if (id.equals(oldProtocolFactory.getID())) {
					i.remove();
					removed = true;
				}
			}
			return removed;
		}
	}

	public boolean setBrowseFileTransferFactory(String protocol, String id, IRemoteFileSystemBrowserFactory factory, int priority) {
		if (protocol == null || "".equals(protocol)) //$NON-NLS-1$
			return false;
		if (id == null)
			return false;
		if (factory == null)
			return false;
		if (!pluginExcluded(id)) {
			// Now create new ProtocolFactory
			ProtocolFactory newProtocolFactory = new ProtocolFactory(factory, priority, id);
			synchronized (browseFileTransferProtocolMap) {
				ProtocolFactory oldProtocolFactory = (ProtocolFactory) browseFileTransferProtocolMap.get(protocol);
				// If found, choose between them based upon comparing their priority
				if (oldProtocolFactory != null) {
					// Now, compare priorities and pick winner
					String CONTRIBUTION_WARNING = "File browse contribution"; //$NON-NLS-1$
					int result = oldProtocolFactory.compareTo(newProtocolFactory);
					if (result < 0) {
						// Existing one has higher priority, so we provide warning and return (leaving existing one as the handler)
						Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, NLS.bind("{0} for protocol {1} from {2} will be ignored.  Existing protocol factory has higher priority.", new Object[] {CONTRIBUTION_WARNING, protocol, id}), null)); //$NON-NLS-1$
						return false;
					} else if (result == 0) {
						// Warn that we are using new one because they have the same priority.
						Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, NLS.bind("{0} for protocol {1} from {2} will be used in preference to existing handler.  Both have same priority={3}.", new Object[] {CONTRIBUTION_WARNING, protocol, id, new Integer(priority)}), null)); //$NON-NLS-1$
					} else if (result > 0) {
						// Warn that we are using new one because it has higher priority.
						Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, NLS.bind("{0} for protocol {1} from {2} will be used in preference to existing handler.  New handler has higher priority={3}<{4}.", new Object[] {CONTRIBUTION_WARNING, protocol, id, new Integer(priority), new Integer(oldProtocolFactory.priority)}), null)); //$NON-NLS-1$
					}
				}
				String[] existingSchemes = getPlatformSupportedSchemes();
				if (!isSchemeRegistered(protocol, existingSchemes))
					registerScheme(protocol);
				// Finally, put clazz in map with protocol as key
				browseFileTransferProtocolMap.put(protocol, newProtocolFactory);
				return true;
			}
		}
		return false;
	}

	public String getBrowseFileTransferFactoryId(String protocol) {
		if (protocol == null)
			return null;
		synchronized (browseFileTransferProtocolMap) {
			ProtocolFactory oldProtocolFactory = (ProtocolFactory) browseFileTransferProtocolMap.get(protocol);
			if (oldProtocolFactory == null)
				return null;
			return oldProtocolFactory.getID();
		}
	}

	public int getBrowseFileTransferPriority(String protocol) {
		if (protocol == null)
			return -1;
		synchronized (browseFileTransferProtocolMap) {
			ProtocolFactory oldProtocolFactory = (ProtocolFactory) browseFileTransferProtocolMap.get(protocol);
			if (oldProtocolFactory == null)
				return -1;
			return oldProtocolFactory.getPriority();
		}
	}

	public boolean removeBrowseFileTransferFactory(String id) {
		if (id == null)
			return false;
		boolean removed = false;
		synchronized (browseFileTransferProtocolMap) {
			for (Iterator i = browseFileTransferProtocolMap.keySet().iterator(); i.hasNext();) {
				ProtocolFactory oldProtocolFactory = (ProtocolFactory) browseFileTransferProtocolMap.get(i.next());
				if (oldProtocolFactory == null)
					continue;
				if (id.equals(oldProtocolFactory.getID())) {
					i.remove();
					removed = true;
				}
			}
			return removed;
		}
	}

	public boolean setSendFileTransferFactory(String protocol, String id, ISendFileTransferFactory factory, int priority) {
		if (protocol == null || "".equals(protocol)) //$NON-NLS-1$
			return false;
		if (id == null)
			return false;
		if (factory == null)
			return false;
		if (!pluginExcluded(id)) {
			// Now create new ProtocolFactory
			ProtocolFactory newProtocolFactory = new ProtocolFactory(factory, priority, id);
			synchronized (sendFileTransferProtocolMap) {
				ProtocolFactory oldProtocolFactory = (ProtocolFactory) sendFileTransferProtocolMap.get(protocol);
				// If found, choose between them based upon comparing their priority
				if (oldProtocolFactory != null) {
					// Now, compare priorities and pick winner
					String CONTRIBUTION_WARNING = "File send contribution"; //$NON-NLS-1$
					int result = oldProtocolFactory.compareTo(newProtocolFactory);
					if (result < 0) {
						// Existing one has higher priority, so we provide warning and return (leaving existing one as the handler)
						Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, NLS.bind("{0} for protocol {1} from {2} will be ignored.  Existing protocol factory has higher priority.", new Object[] {CONTRIBUTION_WARNING, protocol, id}), null)); //$NON-NLS-1$
						return false;
					} else if (result == 0) {
						// Warn that we are using new one because they have the same priority.
						Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, NLS.bind("{0} for protocol {1} from {2} will be used in preference to existing handler.  Both have same priority={3}.", new Object[] {CONTRIBUTION_WARNING, protocol, id, new Integer(priority)}), null)); //$NON-NLS-1$
					} else if (result > 0) {
						// Warn that we are using new one because it has higher priority.
						Activator.getDefault().log(new Status(IStatus.WARNING, PLUGIN_ID, IStatus.WARNING, NLS.bind("{0} for protocol {1} from {2} will be used in preference to existing handler.  New handler has higher priority={3}<{4}.", new Object[] {CONTRIBUTION_WARNING, protocol, id, new Integer(priority), new Integer(oldProtocolFactory.priority)}), null)); //$NON-NLS-1$
					}
				}
				String[] existingSchemes = getPlatformSupportedSchemes();
				if (!isSchemeRegistered(protocol, existingSchemes))
					registerScheme(protocol);
				// Finally, put clazz in map with protocol as key
				sendFileTransferProtocolMap.put(protocol, newProtocolFactory);
				return true;
			}
		}
		return false;
	}

	public String getSendFileTransferFactoryId(String protocol) {
		if (protocol == null)
			return null;
		synchronized (sendFileTransferProtocolMap) {
			ProtocolFactory oldProtocolFactory = (ProtocolFactory) sendFileTransferProtocolMap.get(protocol);
			if (oldProtocolFactory == null)
				return null;
			return oldProtocolFactory.getID();
		}
	}

	public int getSendFileTransferPriority(String protocol) {
		if (protocol == null)
			return -1;
		synchronized (sendFileTransferProtocolMap) {
			ProtocolFactory oldProtocolFactory = (ProtocolFactory) sendFileTransferProtocolMap.get(protocol);
			if (oldProtocolFactory == null)
				return -1;
			return oldProtocolFactory.getPriority();
		}
	}

	public boolean removeSendFileTransferFactory(String id) {
		if (id == null)
			return false;
		boolean removed = false;
		synchronized (sendFileTransferProtocolMap) {
			for (Iterator i = sendFileTransferProtocolMap.keySet().iterator(); i.hasNext();) {
				ProtocolFactory oldProtocolFactory = (ProtocolFactory) sendFileTransferProtocolMap.get(i.next());
				if (oldProtocolFactory == null)
					continue;
				if (id.equals(oldProtocolFactory.getID())) {
					i.remove();
					removed = true;
				}
			}
			return removed;
		}
	}

}

Back to the top