View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.cdi.util;
20  
21  import java.io.Serializable;
22  import java.lang.annotation.Annotation;
23  import java.lang.reflect.Type;
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.HashSet;
27  import java.util.Set;
28  import java.util.function.Function;
29  import javax.enterprise.context.Dependent;
30  import javax.enterprise.context.spi.CreationalContext;
31  import javax.enterprise.inject.spi.Bean;
32  import javax.enterprise.inject.spi.BeanManager;
33  import javax.enterprise.inject.spi.InjectionPoint;
34  import javax.enterprise.inject.spi.PassivationCapable;
35  
36  // Inspired by Mojarra's CdiProducer
37  public abstract class AbstractDynamicProducer<T> implements Bean<T>, PassivationCapable, Serializable
38  {
39      private BeanManager beanManager;
40      private String id;
41      private String name;
42      private Class<?> beanClass;
43      private Set<Type> types;
44      private Set<Annotation> qualifiers;
45      private Class<? extends Annotation> scope;
46      private Function<CreationalContext<T>, T> create;
47  
48      public AbstractDynamicProducer(BeanManager beanManager)
49      {
50          this.beanManager = beanManager;
51          this.id = this.getClass().getName();
52          this.beanClass = Object.class;
53          this.types = Collections.singleton(Object.class);
54          this.qualifiers = Collections.unmodifiableSet(asSet(new DefaultLiteral(), new AnyLiteral()));
55          this.scope = Dependent.class;
56      }
57      
58      @Override
59      public String getId()
60      {
61          return id;
62      }
63  
64      @Override
65      public String getName()
66      {
67          return name;
68      }
69  
70      @Override
71      public Class<?> getBeanClass()
72      {
73          return beanClass;
74      }
75  
76      @Override
77      public Set<Type> getTypes()
78      {
79          return types;
80      }
81  
82      @Override
83      public Set<Annotation> getQualifiers()
84      {
85          return qualifiers;
86      }
87  
88      @Override
89      public Class<? extends Annotation> getScope()
90      {
91          return scope;
92      }
93  
94      @Override
95      public T create(CreationalContext<T> creationalContext)
96      {
97          return create.apply(creationalContext);
98      }
99  
100     @Override
101     public void destroy(T instance, CreationalContext<T> creationalContext)
102     {
103         // not required - we just push a JSF artifact into CDI
104     }
105 
106     @Override
107     public Set<InjectionPoint> getInjectionPoints()
108     {
109         return Collections.emptySet();
110     }
111 
112     @Override
113     public Set<Class<? extends Annotation>> getStereotypes()
114     {
115         return Collections.emptySet();
116     }
117 
118     @Override
119     public boolean isAlternative()
120     {
121         return false;
122     }
123 
124     @Override
125     public boolean isNullable()
126     {
127         return false;
128     }
129 
130     public BeanManager getBeanManager()
131     {
132         return beanManager;
133     }
134 
135     
136     public AbstractDynamicProducer<T> name(String name)
137     {
138         this.name = name;
139         return this;
140     }
141 
142     public AbstractDynamicProducer<T> create(Function<CreationalContext<T>, T> create)
143     {
144         this.create = create;
145         return this;
146     }
147 
148     public AbstractDynamicProducer<T> beanClass(Class<?> beanClass)
149     {
150         this.beanClass = beanClass;
151         return this;
152     }
153 
154     public AbstractDynamicProducer<T> types(Type... types)
155     {
156         this.types = asSet(types);
157         return this;
158     }
159 
160     public AbstractDynamicProducer<T> beanClassAndType(Class<?> beanClass)
161     {
162         beanClass(beanClass);
163         types(beanClass);
164         return this;
165     }
166 
167     public AbstractDynamicProducer<T> qualifiers(Annotation... qualifiers)
168     {
169         this.qualifiers = asSet(qualifiers);
170         return this;
171     }
172 
173     public AbstractDynamicProducer<T> scope(Class<? extends Annotation> scope)
174     {
175         this.scope = scope;
176         return this;
177     }
178 
179     public AbstractDynamicProducer<T> id(String id)
180     {
181         this.id = id;
182         return this;
183     }
184     
185     public AbstractDynamicProducer<T> addToId(Object object)
186     {
187         id = id + " " + object.toString();
188         return this;
189     }
190 
191     @SafeVarargs
192     public static <T> Set<T> asSet(T... a)
193     {
194         return new HashSet<>(Arrays.asList(a));
195     }
196 
197 }