Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2177c21c53d0fcdf59b8e674317f35c15a2b67fd (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
/*******************************************************************************
 * 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.wireadmin;

import org.eclipse.equinox.internal.util.ref.Log;
import org.osgi.framework.*;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.wireadmin.*;

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

public class Activator implements BundleActivator, ServiceListener, ServiceFactory {

	static final String PREFIX = "[WireAdmin]: ";

	public static boolean LOG_DEBUG;
	static Log log;

	private WireAdminImpl wireAdmin;
	private ServiceReference cmRef;

	public static BundleContext bc;

	WireReDispatcher wireReDispatcher;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext bc) throws Exception {
		Activator.bc = bc;
		log = new Log(bc, false);
		log.setPrintOnConsole(getBoolean("equinox.services.wireadmin.console"));

		LOG_DEBUG = getBoolean("equinox.services.wireadmin.debug");
		log.setDebug(LOG_DEBUG);

		cmRef = bc.getServiceReference(ConfigurationAdmin.class.getName());

		ConfigurationAdmin cm = null;
		if (cmRef != null) {
			cm = (ConfigurationAdmin) bc.getService(cmRef);
		}

		try {
			bc.addServiceListener(this, '(' + Constants.OBJECTCLASS + '=' + ConfigurationAdmin.class.getName() + ')');
		} catch (InvalidSyntaxException ise) {
			/* syntax is valid */
		}

		wireAdmin = new WireAdminImpl(bc, cm);

		try {
			bc.addServiceListener(wireAdmin, "(|(" + Constants.OBJECTCLASS + '=' + WireAdminListener.class.getName() + ')' + '(' + Constants.OBJECTCLASS + '=' + Consumer.class.getName() + ')' + '(' + Constants.OBJECTCLASS + '=' + Producer.class.getName() + "))");
		} catch (InvalidSyntaxException ise) {
			/* syntax is valid */
			// ise.printStackTrace();
		}
		wireReDispatcher = new WireReDispatcher();
		wireReDispatcher.start(bc);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext bc) throws Exception {
		if (wireReDispatcher != null) {
			wireReDispatcher.stop();
			wireReDispatcher = null;
		}

		wireAdmin.unregister();

		bc.removeServiceListener(wireAdmin);
		bc.removeServiceListener(this);

		if (cmRef != null) {
			bc.ungetService(cmRef);
			cmRef = null;
		}

		log.close();
		log = null;

		wireAdmin = null;

		bc = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.osgi.framework.ServiceListener#serviceChanged(org.osgi.framework.ServiceEvent)
	 */
	public void serviceChanged(ServiceEvent e) {
		switch (e.getType()) {
			case ServiceEvent.MODIFIED :
			case ServiceEvent.REGISTERED :
				this.cmRef = e.getServiceReference();
				wireAdmin.cm = (ConfigurationAdmin) bc.getService(cmRef);
				break;
			case ServiceEvent.UNREGISTERING :
				this.cmRef = null;
				wireAdmin.cm = null;
				break;
		}
	}

	public Object getService(Bundle bundle, ServiceRegistration registration) {
		return null;
	}

	public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
	}

	public static boolean getBoolean(String property) {
		String prop = (bc != null) ? bc.getProperty(property) : System.getProperty(property);
		return ((prop != null) && prop.equalsIgnoreCase("true"));
	}

	public static int getInteger(String property, int defaultValue) {
		String prop = (bc != null) ? bc.getProperty(property) : System.getProperty(property);
		if (prop != null) {
			try {
				return Integer.decode(prop).intValue();
			} catch (NumberFormatException e) {
				//do nothing
			}
		}
		return defaultValue;
	}
}

Back to the top