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