/*******************************************************************************
 * Copyright (c) 2011, 2016 Eurotech and/or its affiliates 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:
 *     Eurotech - initial API and implementation
 *
 *******************************************************************************/
package org.eclipse.kapua.locator.guice;

import com.google.inject.ConfigurationException;
import org.eclipse.kapua.KapuaRuntimeException;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.locator.KapuaLocatorErrorCodes;
import org.eclipse.kapua.model.KapuaObjectFactory;
import org.eclipse.kapua.service.KapuaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Guice;
import com.google.inject.Injector;

/**
 * Kapua locator implementation bases on Guice framework
 * 
 * @since 1.0
 * 
 */
public class GuiceLocatorImpl extends KapuaLocator
{
	
	private static final Logger logger = LoggerFactory.getLogger(GuiceLocatorImpl.class);
	
	private static Injector s_injector = null;

	static {
		try {
			s_injector = Guice.createInjector(new KapuaModule());
		}
		catch (Throwable e) {
			logger.error("Cannot instantiate injector {}", e.getMessage(), e);
			throw e;
		}
	}
	
    @Override
    public <S extends KapuaService> S getService(Class<S> serviceClass) {
        try {
            return s_injector.getInstance(serviceClass);
        } catch (ConfigurationException e) {
            throw new KapuaRuntimeException(KapuaLocatorErrorCodes.SERVICE_UNAVAILABLE, serviceClass);
        }
    }

    @Override
    public <F extends KapuaObjectFactory> F getFactory(Class<F> factoryClass)
    {
    	F kapuaEntityFactory = s_injector.getInstance(factoryClass);
        if (kapuaEntityFactory == null) {
            throw new KapuaRuntimeException(KapuaLocatorErrorCodes.SERVICE_UNAVAILABLE, factoryClass);
        }
    	
    	return kapuaEntityFactory;
    }
}

@@ -1155,14 +1155,16 @@
 ator
-%0A%7B%0A%09%0A%09
+ %7B%0A%0A    
 priv
@@ -1249,11 +1249,13 @@
 s);%0A
-%09%0A%09
+%0A    
 priv
@@ -1294,17 +1294,20 @@
  null;%0A%0A
-%09
+    
 static %7B
@@ -1311,19 +1311,34 @@
 c %7B%0A
-%09%09try %7B%0A%09%09%09
+        try %7B%0A            
 s_in
@@ -1391,14 +1391,18 @@
 ));%0A
-%09%09%7D%0A%09%09
+        %7D 
 catc
@@ -1419,19 +1419,28 @@
 le e) %7B%0A
-%09%09%09
+            
 logger.e
@@ -1502,28 +1502,45 @@
 e);%0A
-%09%09%09throw e;%0A%09%09%7D%0A%09
+            throw e;%0A        %7D%0A    
 %7D%0A
-%09
 %0A   
@@ -1955,20 +1955,19 @@
 ass)
-%0A   
  %7B%0A    
-%09
+    
 F ka
@@ -2182,19 +2182,17 @@
   %7D%0A
+%0A
     
-%09%0A
     
-%09
 retu

Apply code formatter