View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstart.cdi.extension;
18  
19  import java.lang.reflect.Field;
20  import java.util.Set;
21  
22  import javax.enterprise.context.spi.CreationalContext;
23  import javax.enterprise.inject.InjectionException;
24  import javax.enterprise.inject.spi.InjectionPoint;
25  import javax.enterprise.inject.spi.InjectionTarget;
26  
27  import org.jboss.as.quickstart.cdi.extension.model.Creature;
28  import org.w3c.dom.Element;
29  
30  /**
31   * Wrapper for the standard {@link InjectionTarget} to add the field values from the XML file.
32   */
33  public class XmlBackedWrappedInjectionTarget<X extends Creature> implements InjectionTarget<X> {
34      private final InjectionTarget<X> wrapped;
35      private final Element xmlBacking;
36  
37      public XmlBackedWrappedInjectionTarget(InjectionTarget<X> it, Element xmlElement) {
38          wrapped = it;
39          xmlBacking = xmlElement;
40      }
41  
42      @Override
43      public void inject(X instance, CreationalContext<X> ctx) {
44          wrapped.inject(instance, ctx);
45  
46          final Class<? extends Creature> klass = instance.getClass();
47          for (Field field : klass.getDeclaredFields()) {
48              field.setAccessible(true);
49              final String fieldValueFromXml = xmlBacking.getAttribute(field.getName());
50              try {
51                  if (field.getType().isAssignableFrom(Integer.TYPE)) {
52                      field.set(instance, Integer.parseInt(fieldValueFromXml));
53                  } else if (field.getType().isAssignableFrom(String.class)) {
54                      field.set(instance, fieldValueFromXml);
55                  } else {
56                      // TODO: left up for the reader
57                      throw new InjectionException("Cannot convert to type " + field.getType());
58                  }
59              } catch (IllegalAccessException e) {
60                  throw new InjectionException("Cannot access field " + field);
61              }
62          }
63      }
64  
65      @Override
66      public void postConstruct(X instance) {
67          wrapped.postConstruct(instance);
68      }
69  
70      @Override
71      public void preDestroy(X instance) {
72          wrapped.preDestroy(instance);
73      }
74  
75      @Override
76      public X produce(CreationalContext<X> ctx) {
77          return wrapped.produce(ctx);
78      }
79  
80      @Override
81      public void dispose(X instance) {
82          wrapped.dispose(instance);
83      }
84  
85      @Override
86      public Set<InjectionPoint> getInjectionPoints() {
87          return wrapped.getInjectionPoints();
88      }
89  }