Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1cce91819a34bd827f531f7a8daba2c024b57bd6 (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
/*******************************************************************************
 * Copyright (c) 1997, 2008 by ProSyst Software GmbH
 * http://www.prosyst.com
 * 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:
 *    ProSyst Software GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.io.impl;

import java.io.IOException;
import java.util.*;
import javax.microedition.io.Connection;
import org.eclipse.equinox.internal.io.impl.PrivilegedRunner.PrivilegedDispatcher;
import org.osgi.framework.*;
import org.osgi.service.io.ConnectionFactory;

/**
 * @author Pavlin Dobrev
 * @version 1.0
 */

public class ConnectionFactoryListener implements ServiceListener, PrivilegedDispatcher {
	private static Hashtable urlToConN = new Hashtable(5);
	private BundleContext bc;

	public ConnectionFactoryListener(BundleContext bc) {
		this.bc = bc;

		try {
			bc.addServiceListener(this, '(' + Constants.OBJECTCLASS + '=' + ConnectionFactory.class.getName() + ')');
		} catch (InvalidSyntaxException ex) {
			// Ignored - syntax is right!
		}
	}

	public void close() {
		bc.removeServiceListener(this);

		if (!urlToConN.isEmpty()) {
			Vector copyV = new Vector(urlToConN.size());

			for (Enumeration en = urlToConN.elements(); en.hasMoreElements();) {
				copyV.addElement(en.nextElement());
			}

			for (Enumeration en = copyV.elements(); en.hasMoreElements();) {
				ConnectionNotifierImpl cn = (ConnectionNotifierImpl) en.nextElement();
				try {
					cn.close();
				} catch (IOException ex) {
				}
			}
		}
	}

	static void removeConnectionNotifier(String url) {
		urlToConN.remove(url);
	}

	static int count = 0;

	static Connection getConnectionNotifier(String scheme, String url, int mode, boolean timeouts, String filter, int count) throws IOException {
		ConnectionNotifierImpl ret = null;
		synchronized (urlToConN) {
			if (urlToConN.containsKey(url)) {
				return (Connection) urlToConN.get(url);
			}

			ret = new ConnectionNotifierImpl(scheme, url, mode, timeouts, filter);
			urlToConN.put(url, ret);
			if (count == ConnectionFactoryListener.count)
				return ret;
		}

		Connection c = ConnectorServiceImpl.getConnection(filter, url, mode, timeouts, false);
		if (c != null) {
			if (ret.hasListeners())
				ret.notifyCreated(c);
			else
				ret.close();
		}
		return c;
	}

	public void serviceChanged(ServiceEvent event) {
		if (event.getType() == ServiceEvent.REGISTERED && !urlToConN.isEmpty()) {
			ServiceReference ref = event.getServiceReference();
			ConnectionFactory factory = (ConnectionFactory) bc.getService(ref);
			String[] schemes = (String[]) ref.getProperty(ConnectionFactory.IO_SCHEME);

			Vector toNotify = new Vector(urlToConN.size());
			synchronized (urlToConN) {
				for (Enumeration en = urlToConN.elements(); en.hasMoreElements();) {
					ConnectionNotifierImpl cn = (ConnectionNotifierImpl) en.nextElement();
					if (match(schemes, cn.scheme))
						toNotify.addElement(cn);
				}
			}
			for (int i = 0; i < toNotify.size(); i++) {
				ConnectionNotifierImpl cn = (ConnectionNotifierImpl) toNotify.elementAt(i);
				try {
					if (cn.context != null) {
						PrivilegedRunner.doPrivileged(cn.context, this, 0, cn, factory, null, null);
						return;
					}
					Connection connection = factory.createConnection(cn.url, cn.mode, cn.timeouts);
					if (cn.hasListeners())
						cn.notifyCreated(connection);
					else
						cn.close();
				} catch (Throwable ex) {
					ex.printStackTrace();
				}
			}

			bc.ungetService(ref);
		}
	}

	static boolean match(String[] schemes, String scheme) {
		if (schemes != null && scheme != null) {
			for (int i = 0; i < schemes.length; i++) {
				if (scheme.equals(schemes[i])) {
					return true;
				}
			}
		}

		return false;
	}

	/**
	 * @see org.eclipse.equinox.internal.io.impl.PrivilegedRunner.PrivilegedDispatcher#dispatchPrivileged(int,
	 *      java.lang.Object, java.lang.Object, java.lang.Object,
	 *      java.lang.Object)
	 */
	public Object dispatchPrivileged(int type, Object arg1, Object arg2, Object arg3, Object arg4) throws Exception {
		ConnectionNotifierImpl cn = (ConnectionNotifierImpl) arg1;
		ConnectionFactory factory = (ConnectionFactory) arg2;
		Connection connection = factory.createConnection(cn.url, cn.mode, cn.timeouts);
		if (cn.hasListeners()) {
			cn.notifyCreated(connection);
		} else {
			cn.close();
		}
		return null;
	}
}

Back to the top