View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * TheseFoolishThings: Miscellaneous utilities
5    * http://tidalwave.it/projects/thesefoolishthings
6    *
7    * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8    *
9    * *************************************************************************************************************************************************************
10   *
11   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *     http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17   * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
18   *
19   * *************************************************************************************************************************************************************
20   *
21   * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22   * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23   *
24   * *************************************************************************************************************************************************************
25   */
26  package it.tidalwave.thesefoolishthings.examples.dci.marshal.xstream;
27  
28  import javax.annotation.Nonnull;
29  import javax.inject.Inject;
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  import java.nio.file.Path;
34  import it.tidalwave.util.AsExtensions;
35  import it.tidalwave.util.ContextManager;
36  import it.tidalwave.util.Id;
37  import it.tidalwave.thesefoolishthings.examples.dci.marshal.role.XStreamContext1;
38  import it.tidalwave.thesefoolishthings.examples.dci.marshal.role.XStreamContext2;
39  import it.tidalwave.thesefoolishthings.examples.person.ListOfPersons;
40  import it.tidalwave.thesefoolishthings.examples.person.Person;
41  import lombok.experimental.ExtensionMethod;
42  import lombok.extern.slf4j.Slf4j;
43  import static it.tidalwave.thesefoolishthings.examples.dci.marshal.role.Loadable._Loadable_;
44  import static it.tidalwave.thesefoolishthings.examples.dci.marshal.role.Savable._Savable_;
45  import static java.nio.charset.StandardCharsets.UTF_8;
46  import static it.tidalwave.role.io.Marshallable._Marshallable_;
47  import static it.tidalwave.role.io.Unmarshallable._Unmarshallable_;
48  
49  /***************************************************************************************************************************************************************
50   *
51   * Note: this code uses {@code @ExtensionMethod} from Lombok to enhance datum classes with the
52   * {@link it.tidalwave.util.As#as(Class)}, but it is not strictly required.
53   *
54   * @author  Fabrizio Giudici
55   *
56   **************************************************************************************************************************************************************/
57  @ExtensionMethod(AsExtensions.class) @Slf4j
58  public class DciMarshalXStreamExample
59    {
60      @Inject @Nonnull
61      private ContextManager contextManager;
62  
63      public void run()
64              throws IOException
65        {
66          runWithXStreamContext1();
67          runWithXStreamContext2();
68        }
69  
70      private void runWithXStreamContext1()
71              throws IOException
72        {
73          // START SNIPPET: xstreamcontext-contextmanager
74          final var xStreamContext1 = new XStreamContext1();
75  
76          try
77            {
78              contextManager.addLocalContext(xStreamContext1);
79              codeThatUsesMarshalling();
80            }
81          finally
82            {
83              contextManager.removeLocalContext(xStreamContext1);
84            }
85          // END SNIPPET: xstreamcontext-contextmanager
86        }
87  
88      @SuppressWarnings("unused")
89      private void runWithXStreamContext2()
90              throws IOException
91        {
92          // START SNIPPET: xstreamcontext-contextmanager2
93          try (final var binder = contextManager.binder(new XStreamContext2()))
94            {
95              codeThatUsesMarshalling();
96            }
97          // END SNIPPET: xstreamcontext-contextmanager2
98        }
99  
100     @SuppressWarnings({"unused", "UPM_UNCALLED_PRIVATE_METHOD"})
101     private void alternateSyntax()
102             throws IOException
103       {
104         contextManager.runEWithContexts(this::codeThatUsesMarshalling, new XStreamContext2());
105         final var s = contextManager.runEWithContexts(this::codeThatUsesMarshalling2, new XStreamContext2());
106         log.info("{}", s);
107       }
108 
109     private void codeThatUsesMarshalling()
110             throws IOException
111       {
112         final var path1 = Path.of("target/Person.xml");
113         final var path2 = Path.of("target/People.xml");
114         // START SNIPPET: xstreamcontext-example1
115         final var joe = new Person(new Id("1"), "Joe", "Smith");
116         final var luke = new Person(new Id("2"), "Luke", "Skywalker");
117 
118         var marshalledPersons = "";
119         var marshalledPerson = "";
120 
121         try (final var os = new ByteArrayOutputStream())
122           {
123             joe.as(_Marshallable_).marshal(os);
124             log.info("******** (joe as Marshallable) marshalled: {}\n", marshalledPerson = os.toString(UTF_8));
125           }
126 
127         try (final var os = new ByteArrayOutputStream())
128           {
129             ListOfPersons.of(joe, luke).as(_Marshallable_).marshal(os);
130             log.info("******** (listOfPersons as Marshallable) marshalled: {}\n", marshalledPersons = os.toString(UTF_8));
131           }
132         // END SNIPPET: xstreamcontext-example1
133 
134         // START SNIPPET: xstreamcontext-example2
135         try (final var is = new ByteArrayInputStream(marshalledPerson.getBytes(UTF_8)))
136           {
137             final var person = Person.prototype().as(_Unmarshallable_).unmarshal(is);
138             log.info("******** Unmarshalled person: {}\n", person);
139           }
140 
141         try (final var is = new ByteArrayInputStream(marshalledPersons.getBytes(UTF_8)))
142           {
143             final var listOfPersons = ListOfPersons.empty().as(_Unmarshallable_).unmarshal(is);
144             log.info("******** Unmarshalled persons: {}\n", listOfPersons);
145           }
146         // END SNIPPET: xstreamcontext-example2
147 
148         // START SNIPPET: xstreamcontext-savable-loadable
149         joe.as(_Savable_).saveTo(path1);
150         ListOfPersons.of(joe, luke).as(_Savable_).saveTo(path2);
151         final var p = Person.prototype().as(_Loadable_).loadFrom(path1);
152         final var lp = ListOfPersons.empty().as(_Loadable_).loadFrom(path2);
153         // END SNIPPET: xstreamcontext-savable-loadable
154         log.info("******** Loaded person: {}\n", p);
155         log.info("******** Loaded persons: {}\n", lp);
156       }
157 
158     @Nonnull
159     private String codeThatUsesMarshalling2()
160             throws IOException
161       {
162         codeThatUsesMarshalling();
163         return "foo bar";
164       }
165   }