Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3f713e923c396d9618e983185eda3be6514dbc7d (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
/****************************************************************************
 * Copyright (c) 2007, 2011 IBM, 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:
 *    Chris Aniszczyk - initial API and implementation
 *    Henrich Kraemer - Bug 297742 - [transport] Investigate how to maintain HTTP session 
 *****************************************************************************/
package org.eclipse.ecf.internal.provider.filetransfer.httpclient;

import javax.net.ssl.SSLSocketFactory;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ecf.core.util.LogHelper;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;

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

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

	// The shared instance
	private static Activator plugin;
	private BundleContext context = null;

	private ServiceTracker logServiceTracker = null;

	private ServiceTracker sslSocketFactoryTracker;

	private ISSLSocketFactoryModifier sslSocketFactoryModifier;

	private ConnectionManagerHelper cmHelper;

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

	public BundleContext getContext() {
		return context;
	}

	public void start(BundleContext ctxt) throws Exception {
		plugin = this;
		this.context = ctxt;
		// initialize the default sslSocketFactoryModifier.  This instance is then used within HttpClientRetrieveFileTransfer.setupHostAndPort
		// to set the socket factory for the specific proxy and httpclient instance
		try {
			Class socketFactoryModifierClass = Class.forName("org.eclipse.ecf.internal.provider.filetransfer.httpclient.ssl.SSLSocketFactoryModifier"); //$NON-NLS-1$
			sslSocketFactoryModifier = (ISSLSocketFactoryModifier) socketFactoryModifierClass.newInstance();
		} catch (ClassNotFoundException e) {
			// will occur if fragment is not installed or not on proper execution environment
		} catch (Throwable t) {
			log(new Status(IStatus.ERROR, PLUGIN_ID, "Unexpected Error in Activator.start", t)); //$NON-NLS-1$
		}

	}

	public ConnectionManagerHelper getConnectionManagerHelper() {
		if (cmHelper == null) {
			cmHelper = new ConnectionManagerHelper();
		}
		return cmHelper;
	}

	public ISSLSocketFactoryModifier getSSLSocketFactoryModifier() {
		return sslSocketFactoryModifier;
	}

	public void stop(BundleContext ctxt) throws Exception {
		if (sslSocketFactoryModifier != null) {
			sslSocketFactoryModifier.dispose();
			sslSocketFactoryModifier = null;
		}

		if (sslSocketFactoryTracker != null) {
			sslSocketFactoryTracker.close();
		}

		if (logServiceTracker != null) {
			logServiceTracker.close();
		}
		if (cmHelper != null) {
			cmHelper.shutdown();
		}
		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;
	}

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

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

	public synchronized SSLSocketFactory getSSLSocketFactory() {
		if (sslSocketFactoryTracker == null) {
			sslSocketFactoryTracker = new ServiceTracker(this.context, SSLSocketFactory.class.getName(), null);
			sslSocketFactoryTracker.open();
		}
		return (SSLSocketFactory) sslSocketFactoryTracker.getService();
	}

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

}

Back to the top