All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.devsurf.injection.guice.integrations.guicyfruit.jndi.GuicyInitialContextFactory Maven / Gradle / Ivy

/**
 * Copyright (C) 2010 Daniel Manzke 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.devsurf.injection.guice.integrations.guicyfruit.jndi;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Map.Entry;

import javax.naming.Context;
import javax.naming.NamingException;

import org.guiceyfruit.Injectors;
import org.guiceyfruit.jndi.GuiceInitialContextFactory;
import org.guiceyfruit.jndi.JndiBindings;
import org.guiceyfruit.jndi.internal.JndiContext;

import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
import com.google.inject.ProvisionException;
import com.google.inject.Scopes;

import de.devsurf.injection.guice.scanner.ClasspathScanner;
import de.devsurf.injection.guice.scanner.PackageFilter;
import de.devsurf.injection.guice.scanner.StartupModule;

/**
 * ContextFactory-Implementation which provides JNDI with a Guice-Injector.
 * 
 * @author Daniel Manzke
 * 
 */
public class GuicyInitialContextFactory extends GuiceInitialContextFactory {
	public GuicyInitialContextFactory() {
		super();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Context getInitialContext(final Hashtable environment) throws NamingException {
		try {
			String classpathScannerClass = (String) environment.get("guice.classpath.scanner");
			if (classpathScannerClass == null || classpathScannerClass.length() == 0) {
				classpathScannerClass = "de.devsurf.injection.guice.scanner.asm.ASMClasspathScanner";
			}
			Class scannerClass = (Class) Class
				.forName(classpathScannerClass.trim());

			String classpathPackages = (String) environment.get("guice.classpath.packages");
			if (classpathPackages == null || classpathPackages.length() == 0) {
				classpathPackages = "com;de;org;net";
			}
			List packages = new ArrayList();

			StringTokenizer tok = new StringTokenizer(classpathPackages.trim(), ";");
			while (tok.hasMoreElements()) {
				packages.add(PackageFilter.create(tok.nextToken().trim()));
			}

			StartupModule startupModule = StartupModule.create(scannerClass, packages
				.toArray(new PackageFilter[packages.size()]));

			Injector injector = Injectors.createInjector(environment, startupModule,
				new AbstractModule() {
					protected void configure() {
						bind(Context.class).toProvider(new Provider() {
							@Inject
							Injector injector;

							public Context get() {
								JndiContext context = new JndiContext(environment);
								Properties jndiNames = createJndiNamesProperties(environment);
								try {
									JndiBindings.bindInjectorAndBindings(context, injector,
										jndiNames);
									return context;
								} catch (NamingException e) {
									throw new ProvisionException(
										"Failed to create JNDI bindings. Reason: " + e, e);
								}
							}
						}).in(Scopes.SINGLETON);
					}
				});
			return injector.getInstance(Context.class);
		} catch (Exception e) {
			NamingException exception = new NamingException(e.getMessage());
			exception.initCause(e);
			throw exception;
		}
	}

	@SuppressWarnings("unchecked")
	private Properties createJndiNamesProperties(Hashtable environment) {
		Set set = environment.entrySet();
		Properties answer = new Properties();
		for (Entry entry : set) {
			String key = entry.getKey().toString();
			if (key.startsWith(NAME_PREFIX)) {
				String name = key.substring(NAME_PREFIX.length());
				Object value = entry.getValue();
				answer.put(name, value);
			}
		}
		return answer;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy