Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.task.exec

File SpringElementContext.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart6.png
31% of files have more coverage

Code metrics

8
27
7
1
81
68
12
0.44
3.86
7
1.71

Classes

Class Line # Actions
SpringElementContext 15 27 12
0.5476190454.8%
 

Contributing tests

This file is covered by 14 tests. .

Source view

1    package io.sunshower.service.task.exec;
2   
3    import io.sunshower.service.task.*;
4    import java.util.HashMap;
5    import java.util.Map;
6    import java.util.Properties;
7    import java.util.logging.Logger;
8    import javax.xml.bind.JAXBContext;
9    import javax.xml.bind.JAXBElement;
10    import javax.xml.bind.Unmarshaller;
11    import org.eclipse.persistence.jaxb.JAXBContextFactory;
12    import org.w3c.dom.Element;
13   
14    /** Created by haswell on 3/27/17. */
 
15    public class SpringElementContext implements ElementContext {
16   
17    static final Logger log = Logger.getLogger(SpringElementContext.class.getName());
18   
19    private final Map<String, Class<?>> types;
20   
 
21  2 toggle public SpringElementContext() {
22  2 types = new HashMap<>();
23    }
24   
 
25  46 toggle @Override
26    public <T> void transform(T graph, Class<T> type) {}
27   
 
28  50 toggle @Override
29    public void register(String key, Class<?> type) {
30  50 this.types.put(key, type);
31    }
32   
 
33  161 toggle @Override
34    @SuppressWarnings("unchecked")
35    public <T> ElementDescriptor<T> resolve(Node v, TaskGraph taskGraph, ContextResolver resolver) {
36  161 final Class<?> elementType = resolveType(v);
37  161 final Object unprocessedElement = create(elementType, v, taskGraph);
38  161 return (ElementDescriptor<T>)
39    new ElementDescriptor(unprocessedElement, elementType, v.getName(), v.getId());
40    }
41   
 
42  165 toggle @Override
43    public Relationship resolveRelationship(Edge edge, TaskGraph result) {
44  165 return new Relationship("run-before");
45    }
46   
 
47  161 toggle private Object create(Class<?> elementType, Node v, TaskGraph taskGraph) {
48  161 if (!v.isValueRaw()) {
49  161 return v.getValue();
50    }
51  0 try {
52   
53  0 JAXBContext jaxbContext =
54    JAXBContextFactory.createContext(new Class[] {elementType}, new Properties());
55  0 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
56  0 unmarshaller.setProperty("eclipselink.json.include-root", false);
57  0 String format = taskGraph.getFormat();
58  0 if (format == null) {
59  0 format = "application/json";
60    }
61  0 unmarshaller.setProperty("eclipselink.media-type", format);
62  0 JAXBElement<?> unmarshal = unmarshaller.unmarshal((Element) v.getValue(), elementType);
63  0 return unmarshal.getName();
64    } catch (Exception ex) {
65  0 log.warning(ex.getMessage());
66  0 if (ex instanceof RuntimeException) {
67  0 throw (RuntimeException) ex;
68    }
69  0 throw new RuntimeException(ex);
70    }
71    }
72   
 
73  161 toggle private Class<?> resolveType(Node v) {
74  161 final String typeKey = v.getKey();
75  161 final Class<?> elementType = types.get(typeKey);
76  161 if (elementType == null) {
77  8 return NullType.class;
78    }
79  153 return elementType;
80    }
81    }