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.PlatformHandler;
20 import griffon.exceptions.InstanceNotFoundException;
21 import griffon.exceptions.TypeNotFoundException;
22 import griffon.util.CollectionUtils;
23 import griffon.util.GriffonApplicationUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import javax.inject.Inject;
28 import javax.inject.Provider;
29 import java.util.Map;
30
31 import static griffon.util.GriffonNameUtils.isBlank;
32
33 /**
34 * @author Andres Almiray
35 * @since 2.0.0
36 */
37 public class PlatformHandlerProvider implements Provider<PlatformHandler> {
38 private static final Logger LOG = LoggerFactory.getLogger(PlatformHandlerProvider.class);
39
40 private static final String DEFAULT_PLATFORM_HANDLER = "org.codehaus.griffon.runtime.core.DefaultPlatformHandler";
41
42 private static final Map<String, String> DEFAULT_PLATFORM_HANDLERS = CollectionUtils.<String, String>map()
43 .e("linux", DEFAULT_PLATFORM_HANDLER)
44 .e("linux64", DEFAULT_PLATFORM_HANDLER)
45 .e("macosx", "org.codehaus.griffon.runtime.core.DefaultMacOSXPlatformHandler")
46 .e("macosx64", "org.codehaus.griffon.runtime.core.DefaultMacOSXPlatformHandler")
47 .e("solaris", DEFAULT_PLATFORM_HANDLER)
48 .e("windows", DEFAULT_PLATFORM_HANDLER)
49 .e("windows64", DEFAULT_PLATFORM_HANDLER);
50
51 @Inject
52 private Configuration configuration;
53
54 @Override
55 @SuppressWarnings("unchecked")
56 public PlatformHandler get() {
57 String platform = GriffonApplicationUtils.platform;
58
59 String handlerClassName = configuration.getAsString("platform.handler." + platform, DEFAULT_PLATFORM_HANDLERS.get(platform));
60 if (isBlank(handlerClassName)) {
61 handlerClassName = DEFAULT_PLATFORM_HANDLER;
62 }
63
64 if (LOG.isDebugEnabled()) {
65 LOG.debug("Using " + handlerClassName + " as PlatformHandler");
66 }
67
68 Class<PlatformHandler> handlerClass;
69 try {
70 handlerClass = (Class<PlatformHandler>) Class.forName(handlerClassName, true, getClass().getClassLoader());
71 } catch (ClassNotFoundException e) {
72 throw new TypeNotFoundException(PlatformHandler.class.getName(), e);
73 } catch (NoClassDefFoundError e) {
74 // could be caused by a missing class in the classpath (such as com.apple.mrj.MRJAboutHandler)
75 // use a dummy handler if and only if +JDK9
76 if (GriffonApplicationUtils.isJdk9()) {
77 return new DefaultPlatformHandler();
78 }
79 throw new TypeNotFoundException(PlatformHandler.class.getName(), e);
80 }
81
82 try {
83 return handlerClass.newInstance();
84 } catch (IllegalAccessException | InstantiationException e) {
85 throw new InstanceNotFoundException(handlerClass, e);
86 }
87 }
88 }
|