File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\RobustCollectionConverter.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import com.thoughtworks.xstream.mapper.CannotResolveClassException;
27 import com.thoughtworks.xstream.converters.UnmarshallingContext;
28 import com.thoughtworks.xstream.converters.collections.CollectionConverter;
29 import com.thoughtworks.xstream.converters.reflection.ReflectionProvider;
30 import com.thoughtworks.xstream.converters.reflection.SerializableConverter;
31 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
32 import com.thoughtworks.xstream.mapper.Mapper;
33 import com.thoughtworks.xstream.XStream;
34
35 import java.util.Collection;
36 import java.util.concurrent.CopyOnWriteArrayList;
37 import java.util.concurrent.CopyOnWriteArraySet;
38 import java.util.logging.Logger;
39
40 import static java.util.logging.Level.WARNING;
41
42 /**
43 * {@link CollectionConverter} that ignores {@link CannotResolveClassException}.
44 *
45 * <p>
46 * This allows Hudson to load XML files that contain non-existent classes
47 * (the expected scenario is that those classes belong to plugins that were unloaded.)
48 *
49 * @author Kohsuke Kawaguchi
50 */
51 public class RobustCollectionConverter extends CollectionConverter {
52 private final SerializableConverter sc;
53
54 public RobustCollectionConverter(XStream xs) {
55 this(xs.getMapper(),xs.getReflectionProvider());
56 }
57
58 public RobustCollectionConverter(Mapper mapper, ReflectionProvider reflectionProvider) {
59 super(mapper);
60 sc = new SerializableConverter(mapper,reflectionProvider);
61 }
62
63 @Override
64 public boolean canConvert(Class type) {
65 return super.canConvert(type) || type==CopyOnWriteArrayList.class || type==CopyOnWriteArraySet.class;
66 }
67
68 @Override
69 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
70 // CopyOnWriteArrayList used to serialize as custom serialization,
71 // so read it in a compatible fashion.
72 String s = reader.getAttribute("serialization");
73 if(s!=null && s.equals("custom")) {
74 return sc.unmarshal(reader,context);
75 } else {
76 return super.unmarshal(reader, context);
77 }
78 }
79
80 @Override
81 protected void populateCollection(HierarchicalStreamReader reader, UnmarshallingContext context, Collection collection) {
82 while (reader.hasMoreChildren()) {
83 reader.moveDown();
84 try {
85 Object item = readItem(reader, context, collection);
86 collection.add(item);
87 } catch (CannotResolveClassException e) {
88 LOGGER.log(WARNING,"Failed to resolve class",e);
89 RobustReflectionConverter.addErrorInContext(context, e);
90 } catch (LinkageError e) {
91 LOGGER.log(WARNING,"Failed to resolve class",e);
92 RobustReflectionConverter.addErrorInContext(context, e);
93 }
94 reader.moveUp();
95 }
96 }
97
98 private static final Logger LOGGER = Logger.getLogger(RobustCollectionConverter.class.getName());
99 }
100