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  
20  package org.apache.myfaces.cdi.behavior;
21  
22  import java.util.logging.Level;
23  import java.util.logging.Logger;
24  import javax.enterprise.context.Dependent;
25  import javax.enterprise.context.spi.CreationalContext;
26  import javax.enterprise.inject.Typed;
27  import javax.enterprise.inject.spi.BeanManager;
28  import javax.faces.FacesException;
29  import javax.faces.component.behavior.Behavior;
30  import org.apache.myfaces.cdi.util.AbstractDynamicProducer;
31  import org.apache.myfaces.shared.util.ClassUtils;
32  
33  /**
34   *
35   */
36  @Typed
37  public class FacesBehaviorProducer extends AbstractDynamicProducer<Behavior>
38  {
39      public FacesBehaviorProducer(BeanManager beanManager, FacesBehaviorInfo typeInfo)
40      {
41          super(beanManager);
42          
43          FacesBehaviorAnnotationLiteral literal = new FacesBehaviorAnnotationLiteral(
44                          typeInfo.getBehaviorId() == null ? "" : typeInfo.getBehaviorId(), true);
45          
46          String behaviorId = typeInfo.getBehaviorId() == null ? "" : typeInfo.getBehaviorId();
47          String id = "" + typeInfo.getType() + "_" + behaviorId;
48  
49          super.id(id)
50                  .scope(Dependent.class)
51                  .qualifiers(literal)
52                  .types(typeInfo.getType(), Object.class)
53                  .beanClass(ClassUtils.simpleClassForName(typeInfo.getType().getTypeName()))
54                  .create(e -> createBehavior(e));
55      }
56  
57      protected Behavior createBehavior(CreationalContext<Behavior> cc)
58      {
59          Class<? extends Behavior> behaviorClass = (Class<? extends Behavior>) getBeanClass();        
60          Behavior converter = null;
61          try
62          {
63              converter = behaviorClass.newInstance();
64          }
65          catch (Exception ex)
66          {
67              Logger.getLogger(FacesBehaviorProducer.class.getName()).log(
68                      Level.SEVERE, "Could not instantiate behavior " + behaviorClass.getName(), ex);
69              throw new FacesException("Could not instantiate behavior: " + behaviorClass.getName(), ex);
70              
71          }
72          return converter;
73      }
74  
75  }