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.artifact;
17
18 import griffon.core.GriffonApplication;
19 import griffon.core.artifact.GriffonClass;
20 import griffon.core.artifact.GriffonService;
21 import griffon.core.artifact.GriffonServiceClass;
22 import griffon.core.injection.Binding;
23 import griffon.inject.Typed;
24 import org.codehaus.griffon.runtime.core.injection.AnnotatedBindingBuilder;
25 import org.codehaus.griffon.runtime.core.injection.Bindings;
26 import org.codehaus.griffon.runtime.core.injection.LinkedBindingBuilder;
27
28 import javax.annotation.Nonnull;
29 import javax.inject.Inject;
30 import java.util.List;
31
32 import static griffon.util.AnnotationUtils.typed;
33 import static java.util.Objects.requireNonNull;
34
35 /**
36 * Handler for 'Service' artifacts.
37 *
38 * @author Andres Almiray
39 */
40 @Typed(GriffonService.class)
41 public class ServiceArtifactHandler extends AbstractArtifactHandler<GriffonService> {
42 @Inject
43 public ServiceArtifactHandler(@Nonnull GriffonApplication application) {
44 super(application, GriffonService.class, GriffonServiceClass.TYPE, GriffonServiceClass.TRAILING);
45 }
46
47 @Nonnull
48 public GriffonClass newGriffonClassInstance(@Nonnull Class<GriffonService> clazz) {
49 requireNonNull(clazz, ERROR_CLASS_NULL);
50 return new DefaultGriffonServiceClass(getApplication(), clazz);
51 }
52
53 @Override
54 protected void createBindings(@Nonnull List<Binding<?>> bindings, @Nonnull Class<GriffonService> clazz, @Nonnull GriffonClass griffonClass) {
55 LinkedBindingBuilder<GriffonClass> builder1 = Bindings.bind(GriffonClass.class)
56 .withClassifier(typed(clazz));
57 builder1.toInstance(griffonClass);
58 bindings.add(builder1.getBinding());
59 AnnotatedBindingBuilder<GriffonService> builder2 = Bindings.bind(clazz);
60 builder2.asSingleton();
61 bindings.add(builder2.getBinding());
62 }
63 }
|