1 package ejava.examples.ejbwar.customer.bo;
2
3 import java.io.InputStream;
4 import java.io.Serializable;
5 import java.io.StringWriter;
6
7 import javax.persistence.Column;
8 import javax.persistence.MappedSuperclass;
9 import javax.persistence.Version;
10 import javax.xml.bind.JAXBContext;
11 import javax.xml.bind.JAXBException;
12 import javax.xml.bind.Marshaller;
13 import javax.xml.bind.Unmarshaller;
14 import javax.xml.bind.annotation.XmlAttribute;
15
16
17
18
19
20 @SuppressWarnings("serial")
21 @MappedSuperclass
22 public abstract class CustomerRepresentation implements Serializable {
23 public static final String NAMESPACE = "http://webejb.ejava.info/customer";
24
25 @Version
26 @Column(name="VERSION")
27 private int version;
28
29
30
31
32
33
34 @XmlAttribute(required=true)
35 public int getVersion() { return version; }
36 public void setVersion(int version) {
37 this.version = version;
38 }
39
40
41
42
43
44 @Override
45 public String toString() {
46 try {
47 JAXBContext jbx = JAXBContext.newInstance(getClass());
48 Marshaller marshaller = jbx.createMarshaller();
49 marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
50 StringWriter writer = new StringWriter();
51 marshaller.marshal(this, writer);
52 return writer.toString();
53 } catch (JAXBException ex) {
54 throw new RuntimeException("unexpected JAXB error marshalling object:", ex);
55 }
56 }
57
58
59
60
61
62
63
64
65 @SuppressWarnings("unchecked")
66 public static <T> T unmarshall(Class<T> clazz, InputStream is) {
67 try {
68 JAXBContext jbx = JAXBContext.newInstance(clazz);
69 Unmarshaller marshaller = jbx.createUnmarshaller();
70 return (T) marshaller.unmarshal(is);
71 } catch (JAXBException ex) {
72 throw new RuntimeException("unexpected JAXB error marshalling object:", ex);
73 }
74 }
75 }