01 /*
02 * Copyright 2008-2014 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.ApplicationClassLoader;
19 import griffon.core.GriffonApplication;
20 import griffon.core.LifecycleHandler;
21 import griffon.exceptions.InstanceNotFoundException;
22 import griffon.exceptions.TypeNotFoundException;
23
24 import javax.annotation.Nonnull;
25 import javax.inject.Inject;
26 import javax.inject.Provider;
27 import java.lang.reflect.Constructor;
28 import java.lang.reflect.InvocationTargetException;
29
30 import static griffon.util.AnnotationUtils.named;
31 import static griffon.util.GriffonNameUtils.requireNonBlank;
32 import static java.util.Objects.requireNonNull;
33
34 /**
35 * @author Andres Almiray
36 * @since 2.0.0
37 */
38 public class LifecycleHandlerProvider implements Provider<LifecycleHandler> {
39 private final String basename;
40
41 private GriffonApplication application;
42 private ApplicationClassLoader applicationClassLoader;
43
44 public LifecycleHandlerProvider(@Nonnull String basename) {
45 this.basename = requireNonBlank(basename, "Argument 'basename' must not be blank");
46 }
47
48 @Inject
49 public void setGriffonApplication(@Nonnull GriffonApplication application) {
50 this.application = requireNonNull(application, "Argument 'application' must not be null");
51 }
52
53 @Inject
54 public void setApplicationClassLoader(ApplicationClassLoader applicationClassLoader) {
55 this.applicationClassLoader = requireNonNull(applicationClassLoader, "Argument 'applicationClassLoader' must not be null");
56 }
57
58 @Override
59 @SuppressWarnings("unchecked")
60 public LifecycleHandler get() {
61 Class<LifecycleHandler> handlerClass;
62 try {
63 handlerClass = (Class<LifecycleHandler>) applicationClassLoader.get().loadClass(basename);
64 } catch (ClassNotFoundException e) {
65 throw new TypeNotFoundException(LifecycleHandler.class.getName(), named(basename), e);
66 }
67
68 try {
69 Constructor<LifecycleHandler> ctor = handlerClass.getDeclaredConstructor(GriffonApplication.class);
70 return ctor.newInstance(application);
71 } catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
72 throw new InstanceNotFoundException(handlerClass, named(basename), e);
73 } catch (InvocationTargetException e) {
74 throw new InstanceNotFoundException(handlerClass, named(basename), e.getTargetException());
75 }
76 }
77 }
|