| 
01 /*02  * Copyright 2008-2015 the original author or authors.
 03  *
 04  * Licensed under the Apache License, Version 2.0 (the "License");
 05  * you may not use this file except in compliance with the License.
 06  * You may obtain a copy of the License at
 07  *
 08  *     http://www.apache.org/licenses/LICENSE-2.0
 09  *
 10  * Unless required by applicable law or agreed to in writing, software
 11  * distributed under the License is distributed on an "AS IS" BASIS,
 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  * See the License for the specific language governing permissions and
 14  * limitations under the License.
 15  */
 16 package org.codehaus.griffon.runtime.core;
 17
 18 import griffon.core.Configuration;
 19 import griffon.core.LifecycleHandler;
 20 import griffon.core.PlatformHandler;
 21 import griffon.exceptions.InstanceNotFoundException;
 22 import griffon.exceptions.TypeNotFoundException;
 23 import griffon.util.CollectionUtils;
 24 import griffon.util.GriffonApplicationUtils;
 25 import org.slf4j.Logger;
 26 import org.slf4j.LoggerFactory;
 27
 28 import javax.inject.Inject;
 29 import javax.inject.Provider;
 30 import java.util.Map;
 31
 32 import static griffon.util.GriffonNameUtils.isBlank;
 33
 34 /**
 35  * @author Andres Almiray
 36  * @since 2.0.0
 37  */
 38 public class PlatformHandlerProvider implements Provider<PlatformHandler> {
 39     private static final Logger LOG = LoggerFactory.getLogger(PlatformHandlerProvider.class);
 40
 41     private static final String DEFAULT_PLATFORM_HANDLER = "org.codehaus.griffon.runtime.core.DefaultPlatformHandler";
 42
 43     private static final Map<String, String> DEFAULT_PLATFORM_HANDLERS = CollectionUtils.<String, String>map()
 44         .e("linux", DEFAULT_PLATFORM_HANDLER)
 45         .e("linux64", DEFAULT_PLATFORM_HANDLER)
 46         .e("macosx", "org.codehaus.griffon.runtime.core.DefaultMacOSXPlatformHandler")
 47         .e("macosx64", "org.codehaus.griffon.runtime.core.DefaultMacOSXPlatformHandler")
 48         .e("solaris", DEFAULT_PLATFORM_HANDLER)
 49         .e("windows", DEFAULT_PLATFORM_HANDLER)
 50         .e("windows64", DEFAULT_PLATFORM_HANDLER);
 51
 52     @Inject
 53     private Configuration configuration;
 54
 55     @Override
 56     @SuppressWarnings("unchecked")
 57     public PlatformHandler get() {
 58         String platform = GriffonApplicationUtils.platform;
 59
 60         String handlerClassName = configuration.getAsString("platform.handler." + platform, DEFAULT_PLATFORM_HANDLERS.get(platform));
 61         if (isBlank(handlerClassName)) {
 62             handlerClassName = DEFAULT_PLATFORM_HANDLER;
 63         }
 64
 65         if (LOG.isDebugEnabled()) {
 66             LOG.debug("Using " + handlerClassName + " as PlatformHandler");
 67         }
 68
 69         Class<PlatformHandler> handlerClass;
 70         try {
 71             handlerClass = (Class<PlatformHandler>) Class.forName(handlerClassName, true, getClass().getClassLoader());
 72         } catch (ClassNotFoundException e) {
 73             throw new TypeNotFoundException(LifecycleHandler.class.getName(), e);
 74         }
 75
 76         try {
 77             return handlerClass.newInstance();
 78         } catch (IllegalAccessException | InstantiationException e) {
 79             throw new InstanceNotFoundException(handlerClass, e);
 80         }
 81     }
 82 }
 |