Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1efdc77606e07e13912a5be7400188707cd5172b (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
/*******************************************************************************
 * Copyright (c) 2010, 2012 Oracle.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Apache License v2.0 which accompanies this distribution. 
 * The Eclipse Public License is available at
 *     http://www.eclipse.org/legal/epl-v10.html
 * and the Apache License v2.0 is available at 
 *     http://www.opensource.org/licenses/apache2.0.php.
 * You may elect to redistribute this code under either of these licenses.
 *
 * Contributors:
 *     Bob Nettleton (Oracle) - Initial Reference Implementation
 ******************************************************************************/

package org.eclipse.gemini.naming;

import java.lang.reflect.Field;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactoryBuilder;
import javax.naming.spi.NamingManager;
import javax.naming.spi.ObjectFactory;
import javax.naming.spi.ObjectFactoryBuilder;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.jndi.JNDIConstants;
import org.osgi.service.jndi.JNDIContextManager;
import org.osgi.service.jndi.JNDIProviderAdmin;

/**
 * Activator implementation for the Gemini Naming Bundle.
 * 
 * This activator's main purpose is to register the JNDI Builder singleton
 * implementations that allow the Factory Manager to override the default JNDI
 * framework.
 * 
 * 
 */
public class Activator implements BundleActivator {

	private static final String					OSGI_URL_SCHEME					= "osgi";
	private static final String					RMI_URL_SCHEME					= "rmi";
	private static final String					RMI_URL_CONTEXT_FACTORY			= "com.sun.jndi.url.rmi.rmiURLContextFactory";
	
	private static Logger logger = Logger.getLogger(Activator.class.getName());

	private BundleContext						m_bundleContext					= null;
	private final List<ServiceRegistration>        m_listOfServiceRegistrations = new LinkedList<ServiceRegistration>();

	private CloseableProviderAdmin	m_providerAdminService;
	private ContextManagerServiceFactoryImpl m_contextManagerServiceFactory;
	
	/*
	 * Create the Factory Manager's builder implementation, and register it with
	 * the JNDI NamingManager.
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext context) throws Exception {
		logger.info("Initializing Gemini Naming Factory Manager Bundle");
		
		m_bundleContext = context;

		// register static singletons with the JNDI framework
		logger.info("Installing Static Singletons");
		registerInitialContextFactoryBuilderSingleton();
		registerObjectFactoryBuilderSingleton();

		logger.info("Registering URL Context Factory for 'osgi' URL scheme");
		registerOSGiURLContextFactory();
		
		logger.info("Registering Default Runtime Builder for JRE-provided factories");
		registerDefaultRuntimeBuilder();
		
		logger.info("Registering ContextManager service");
		// register the JNDIContextManager service once all Factory
		// Manager initialization is complete
		registerContextManager();
		
		logger.info("Registering ProviderAdmin service");
		// register the JNDIProviderAdmin interface, used by OSGi-aware
		// context implementations to resolve JNDI references
		registerProviderAdmin();
	}
	

	/*
	 * Allow the Builder implementation to clean up any
	 * ServiceListener/ServiceTracker instances.
	 * 
	 * @see
	 * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		logger.info("Shutting down Gemini Naming Factory Manager Bundle");
		
		// close all known Contexts associated with the JNDIContextManager service
		m_contextManagerServiceFactory.closeAll();
		
		// close the JNDIProviderAdmin service
		m_providerAdminService.close();

		// unregister all the JNDI services registered by this Activator
		Iterator<ServiceRegistration> iterator = m_listOfServiceRegistrations.iterator();
		while(iterator.hasNext()) {
			ServiceRegistration serviceRegistration = 
				(ServiceRegistration)iterator.next();
			serviceRegistration.unregister();
		}
		
		unregisterSingletons();
	}


	/**
	 * Registers the InitialContextFactoryBuilder static singleton
	 * @throws NamingException on any error that occurs during the setting
	 *         of the builder.  
	 */
	private static void registerInitialContextFactoryBuilderSingleton() throws NamingException {
		try {
			NamingManager.setInitialContextFactoryBuilder(new TraditionalInitialContextFactoryBuilder());
		}
		catch (IllegalStateException illegalStateException) {
			logger.log(Level.SEVERE, 
			           "Gemini Naming Implementation cannot set the InitialContextFactoryBuilder - another builder was already installed",
			           illegalStateException);
			NamingException namingException = 
				new NamingException("Error occurred while attempting to set the IntialContextFactoryBuilder.");
			namingException.setRootCause(illegalStateException);
			throw namingException;
		} 
		catch(SecurityException securityException) {
			logger.log(Level.SEVERE, 
					   "Gemini Naming Implementation did not have the proper security permissions to install the InitialContextFactoryBuilder",
					   securityException);
			NamingException namingException = 
				new NamingException("Error occurred while attempting to set the IntialContextFactoryBuilder.");
			namingException.setRootCause(securityException);
			throw namingException;
		}
	}
	
	
	/**
	 * Registers the ObjectFactoryBuilder static singleton
	 * @throws NamingException on any error that occurs during the setting
	 *         of the builder.  
	 */
	private static void registerObjectFactoryBuilderSingleton() throws NamingException {
		try {
			NamingManager.setObjectFactoryBuilder(new TraditionalObjectFactoryBuilder());
		}
		catch (IllegalStateException illegalStateException) {
			logger.log(Level.SEVERE, 
			           "Gemini Naming Implementation cannot set the ObjectFactoryBuilder - another builder was already installed",
			           illegalStateException);
			NamingException namingException = 
				new NamingException("Error occurred while attempting to set the ObjectFactoryBuilder.");
			namingException.setRootCause(illegalStateException);
			throw namingException;
		} 
		catch(SecurityException securityException) {
			logger.log(Level.SEVERE, 
					   "Gemini Naming Implementation did not have the proper security permissions to install the ObjectFactoryBuilder",
					   securityException);
			NamingException namingException = 
				new NamingException("Error occurred while attempting to set the ObjectFactoryBuilder.");
			namingException.setRootCause(securityException);
			throw namingException;
		}
	}
		
	/**
	 * Unregisters the InitialContextFactoryBuilder static singleton
	 * and the ObjectFactoryBuilder static singleton.
	 */
	private static void unregisterSingletons() {
		Field[] fields = NamingManager.class.getDeclaredFields();
		if (fields != null && fields.length > 0) {
			for (Field field: fields) {
				if (InitialContextFactoryBuilder.class.equals(field.getType()) 
						|| ObjectFactoryBuilder.class.equals(field.getType())){
					field.setAccessible(true);
					try {
						field.set(null, null);
					} catch (IllegalArgumentException e) {
						logger.log(Level.SEVERE,
								   "Unable to reset NamingManager static singleton " + field.getType(),
								   e);
					} catch (IllegalAccessException e) {
						logger.log(Level.SEVERE,
								   "Unable to reset NamingManager static singleton " + field.getType(),
								   e);
					}
				}
			}
		}
	}
	
	
	
	
	/**
	 * Registers the OSGi URL Context Factory.
	 * 
	 */
	private void registerOSGiURLContextFactory() {
		Hashtable<Object, Object> serviceProperties = new Hashtable<Object, Object>();
		serviceProperties.put(JNDIConstants.JNDI_URLSCHEME, OSGI_URL_SCHEME);

		ServiceRegistration serviceRegistration = 
			m_bundleContext.registerService(ObjectFactory.class.getName(), 
										    new OSGiURLContextFactoryServiceFactory(), 
										    serviceProperties);
		m_listOfServiceRegistrations.add(serviceRegistration);
	}
	
	
	/**
	 * Registers the InitialContextFactoryBuilder implementation that 
	 * is responsible for loading the JDK-defined providers that must be
	 * loaded from the boot classpath.  
	 * 
	 */
	private void registerDefaultRuntimeBuilder() {
		ServiceRegistration serviceRegistration = 
			m_bundleContext.registerService(InitialContextFactoryBuilder.class.getName(), 
					                        new DefaultRuntimeInitialContextFactoryBuilder(), 
					                        null);
		m_listOfServiceRegistrations.add(serviceRegistration);
		
		Hashtable<Object, Object> props = new Hashtable<Object, Object>();
        props.put(JNDIConstants.JNDI_URLSCHEME, RMI_URL_SCHEME);
		try {
			ServiceRegistration rmiRegistration = 
				m_bundleContext.registerService(ObjectFactory.class.getName(),
												ClassLoader.getSystemClassLoader().loadClass(RMI_URL_CONTEXT_FACTORY).newInstance(),
												props);
			m_listOfServiceRegistrations.add(rmiRegistration);
		}
		catch(ClassNotFoundException e) {
			logger.log(Level.SEVERE, RMI_URL_CONTEXT_FACTORY + " cannot be found through the system classloader.", e);
		}
		catch(InstantiationException e) {
			logger.log(Level.SEVERE, "Exception occurred while instantiating " + RMI_URL_CONTEXT_FACTORY, e);
		}
		catch(IllegalAccessException e) {
			logger.log(Level.SEVERE, "Exception occured while instantiating " + RMI_URL_CONTEXT_FACTORY, e);
		}
	}
	
	
	private void registerContextManager() {
		m_contextManagerServiceFactory = 
			new ContextManagerServiceFactoryImpl(m_bundleContext);
		ServiceRegistration serviceRegistration = 
			m_bundleContext.registerService(JNDIContextManager.class.getName(),
											m_contextManagerServiceFactory,
					                        null);
		m_listOfServiceRegistrations.add(serviceRegistration);
	}
	

	private void registerProviderAdmin() {
		m_providerAdminService = 
			new SecurityAwareProviderAdminImpl(new ProviderAdminImpl(m_bundleContext));
		
		
		ServiceRegistration serviceRegistration =  
			m_bundleContext.registerService(JNDIProviderAdmin.class.getName(),
					                        m_providerAdminService,
					                        null);
		m_listOfServiceRegistrations.add(serviceRegistration);
	}

}

Back to the top