Skip to content

Commit

Permalink
[FIXED JENKINS-28440] Raises a critical exception for an error in a c…
Browse files Browse the repository at this point in the history
…ritical field. This allows plugins to reject unacceptable configurations via REST / CLI.
  • Loading branch information
ikedam committed May 20, 2015
1 parent be67b45 commit 2082b08
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/src/main/java/hudson/util/CopyOnWriteList.java
Expand Up @@ -39,6 +39,8 @@
import java.util.List;
import java.util.Arrays;

import jenkins.util.xstream.CriticalXStreamException;


/**
* {@link List}-like implementation that has copy-on-write semantics.
Expand Down Expand Up @@ -194,6 +196,8 @@ public CopyOnWriteList unmarshal(HierarchicalStreamReader reader, UnmarshallingC
try {
Object item = readItem(reader, context, items);
items.add(item);
} catch (CriticalXStreamException e) {
throw e;
} catch (XStreamException e) {
RobustReflectionConverter.addErrorInContext(context, e);
} catch (LinkageError e) {
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/hudson/util/RobustCollectionConverter.java
Expand Up @@ -36,6 +36,8 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

import jenkins.util.xstream.CriticalXStreamException;


/**
* {@link CollectionConverter} that ignores {@link XStreamException}.
Expand Down Expand Up @@ -82,6 +84,8 @@ protected void populateCollection(HierarchicalStreamReader reader, Unmarshalling
try {
Object item = readItem(reader, context, collection);
collection.add(item);
} catch (CriticalXStreamException e) {
throw e;
} catch (XStreamException e) {
RobustReflectionConverter.addErrorInContext(context, e);
} catch (LinkageError e) {
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/java/hudson/util/RobustMapConverter.java
Expand Up @@ -30,6 +30,7 @@
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.mapper.Mapper;
import java.util.Map;
import jenkins.util.xstream.CriticalXStreamException;

/**
* Loads a {@link Map} while tolerating read errors on its keys and values.
Expand All @@ -55,6 +56,8 @@ private Object read(HierarchicalStreamReader reader, UnmarshallingContext contex
reader.moveDown();
try {
return readItem(reader, context, map);
} catch (CriticalXStreamException x) {
throw x;
} catch (XStreamException x) {
RobustReflectionConverter.addErrorInContext(context, x);
return ERROR;
Expand Down
Expand Up @@ -54,6 +54,7 @@
import static java.util.logging.Level.FINE;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import jenkins.util.xstream.CriticalXStreamException;

/**
* Custom {@link ReflectionConverter} that handle errors more gracefully.
Expand Down Expand Up @@ -307,9 +308,11 @@ public Object doUnmarshal(final Object result, final HierarchicalStreamReader re
implicitCollectionsForCurrentObject = writeValueToImplicitCollection(context, value, implicitCollectionsForCurrentObject, result, fieldName);
}
}
} catch (CriticalXStreamException e) {
throw e;
} catch (XStreamException e) {
if (critical) {
throw e;
throw new CriticalXStreamException(e);
}
addErrorInContext(context, e);
} catch (LinkageError e) {
Expand Down
Expand Up @@ -39,6 +39,8 @@
import java.util.ArrayList;
import java.util.List;

import jenkins.util.xstream.CriticalXStreamException;

/**
* {@link ImmutableList} should convert like a list, instead of via serialization.
*
Expand Down Expand Up @@ -76,6 +78,8 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
try {
Object item = readItem(reader, context, items);
items.add(item);
} catch (CriticalXStreamException e) {
throw e;
} catch (XStreamException e) {
RobustReflectionConverter.addErrorInContext(context, e);
} catch (LinkageError e) {
Expand Down
@@ -0,0 +1,39 @@
/*
* The MIT License
*
* Copyright (c) 2015 IKEDA Yasuyuki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package jenkins.util.xstream;

import com.thoughtworks.xstream.XStreamException;
import com.thoughtworks.xstream.converters.ConversionException;

/**
* Wraps {@link XStreamException} to indicate it is critical for Jenkins.
*/
public class CriticalXStreamException extends ConversionException {
private static final long serialVersionUID = 1L;

public CriticalXStreamException(XStreamException cause) {
super(cause);
}
}

0 comments on commit 2082b08

Please sign in to comment.