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.io.IOException;
20  import java.io.InputStream;
21  import java.util.logging.Logger;
22  
23  import javax.enterprise.event.Observes;
24  import javax.enterprise.inject.spi.Extension;
25  import javax.enterprise.inject.spi.ProcessInjectionTarget;
26  import javax.xml.parsers.DocumentBuilder;
27  import javax.xml.parsers.DocumentBuilderFactory;
28  import javax.xml.parsers.ParserConfigurationException;
29  
30  import org.jboss.as.quickstart.cdi.extension.model.Creature;
31  import org.w3c.dom.Document;
32  import org.w3c.dom.Element;
33  import org.xml.sax.SAXException;
34  
35  /**
36   * A simple CDI Portable Extension to "inject" values from XML into the instances of a bean.
37   */
38  public class CreatureExtension implements Extension {
39      private final Document document;
40      private final Logger log = Logger.getLogger(CreatureExtension.class.getName());
41  
42      public CreatureExtension() {
43          try {
44              InputStream creatureDefs = CreatureExtension.class.getClassLoader().getResourceAsStream("creatures.xml");
45              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
46              DocumentBuilder builder = factory.newDocumentBuilder();
47              document = builder.parse(creatureDefs);
48          } catch (ParserConfigurationException e) {
49              throw new RuntimeException("Error building xml parser, aborting", e);
50          } catch (SAXException e) {
51              throw new RuntimeException("SAX exception while parsing xml file", e);
52          } catch (IOException e) {
53              throw new RuntimeException("Error reading or parsing xml file", e);
54          }
55      }
56  
57      /**
58       * Observer to a CDI lifecycle event to correctly setup the XML backed "injection".
59       * @param pit CDI lifecycle callback payload
60       * @param <X> Type of the Injection to observe
61       */
62      <X extends Creature> void processInjectionTarget(@Observes ProcessInjectionTarget<X> pit) {
63          Class<? extends Creature> klass = pit.getAnnotatedType().getJavaClass();
64          log.info("Setting up injection target for " + klass);
65          final Element entry = (Element) document.getElementsByTagName(klass.getSimpleName().toLowerCase()).item(0);
66          pit.setInjectionTarget(new XmlBackedWrappedInjectionTarget<X>(pit.getInjectionTarget(), entry));
67      }
68  }