Skip to content

Commit

Permalink
[FIXED JENKINS-20304] - Non-serialize classes
Browse files Browse the repository at this point in the history
Data classes are serializable now.
Resolves https://issues.jenkins-ci.org/browse/JENKINS-20304

Signed-off-by: Oleg Nenashev <nenashev@synopsys.com>
  • Loading branch information
oleg-nenashev committed Nov 2, 2013
1 parent dec5139 commit 987e633
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -33,7 +33,7 @@ THE SOFTWARE.
</parent>

<artifactId>collapsing-console-sections</artifactId>
<version>1.5-SNAPSHOT</version>
<version>1.4.1-SNAPSHOT</version>
<packaging>hpi</packaging>

<name>Hudson Collapsing Console Sections Plugin</name>
Expand Down
Expand Up @@ -27,6 +27,7 @@
import hudson.Util;
import hudson.console.ConsoleAnnotator;
import hudson.model.Run;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;
Expand All @@ -36,9 +37,9 @@ public class CollapsingSectionAnnotator extends ConsoleAnnotator<Object> {
private List<SectionDefinition> sections;
private Stack<SectionDefinition> currentSections;
private Stack<StackLevel> numberingStack;
private CollapsingSectionNote.DescriptorImpl configs;
private CollapsingSectionsConfiguration configs;

public CollapsingSectionAnnotator(CollapsingSectionNote.DescriptorImpl configs) {
public CollapsingSectionAnnotator(CollapsingSectionsConfiguration configs) {
this.configs = configs;
this.sections = Arrays.asList(configs.getSectionDefinitions());
this.currentSections = new Stack<SectionDefinition>();
Expand Down Expand Up @@ -103,7 +104,7 @@ private void popSection(MarkupText text) {
}

/**Enumerates stack levels for the numbering*/
private static class StackLevel {
private static class StackLevel implements Serializable {
int counter = 0;

public void increment() {
Expand Down
Expand Up @@ -41,6 +41,6 @@ public ConsoleAnnotator newInstance(Object context) {
return null;
}

return new CollapsingSectionAnnotator(descr);
return new CollapsingSectionAnnotator(descr.getConfiguration());
}
}
Expand Up @@ -29,7 +29,6 @@
import hudson.console.ConsoleAnnotator;
import hudson.console.ConsoleNote;
import java.lang.reflect.Array;
import java.util.ArrayList;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -82,6 +81,7 @@ public ConsoleAnnotator annotate(Object context, MarkupText text, int charPos) {
public static final class DescriptorImpl extends ConsoleAnnotationDescriptor {
private CollapsingSectionNote[] sections;
private boolean numberingEnabled;
private transient CollapsingSectionsConfiguration configuration;

public DescriptorImpl() {
load();
Expand All @@ -100,14 +100,7 @@ public CollapsingSectionNote[] getSections() {
}

public SectionDefinition[] getSectionDefinitions() {
CollapsingSectionNote[] configs = getSections();
ArrayList<SectionDefinition> defs = new ArrayList<SectionDefinition>();

for (CollapsingSectionNote config : configs) {
defs.add(config.getDefinition());
}

return defs.toArray((SectionDefinition[]) Array.newInstance(SectionDefinition.class, 0));
return configuration.getSectionDefinitions();
}

public void setSections(CollapsingSectionNote... sections) {
Expand All @@ -118,11 +111,23 @@ public boolean isNumberingEnabled() {
return numberingEnabled;
}

public CollapsingSectionsConfiguration getConfiguration() {
return configuration;
}

@Override
public synchronized void load() {
super.load();
// Enable configuration cache
configuration = new CollapsingSectionsConfiguration(sections, numberingEnabled);
}

@Override
@SuppressWarnings("unchecked") // cast to T[]
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
setSections(req.bindJSONToList(clazz, json.get("consolesection")).toArray((CollapsingSectionNote[]) Array.newInstance(clazz, 0)));
numberingEnabled = json.getBoolean("numberingEnabled");
configuration = new CollapsingSectionsConfiguration(sections, numberingEnabled);
save();

return true;
Expand Down
@@ -0,0 +1,61 @@
/*
* The MIT License
*
* Copyright 2013 Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
*
* 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 org.jvnet.hudson.plugins.collapsingconsolesections;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;

/**
* Provides a serializable instance of collapsing sections global configs.
* @author Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
*/
public class CollapsingSectionsConfiguration implements Serializable {
private final CollapsingSectionNote[] sections;
private final boolean numberingEnabled;

public CollapsingSectionsConfiguration(CollapsingSectionNote[] sections, boolean numberingEnabled) {
this.sections = sections;
this.numberingEnabled = numberingEnabled;
}

public boolean isNumberingEnabled() {
return numberingEnabled;
}

public CollapsingSectionNote[] getSections() {
return sections;
}

public SectionDefinition[] getSectionDefinitions() {
CollapsingSectionNote[] configs = getSections();
ArrayList<SectionDefinition> defs = new ArrayList<SectionDefinition>();

for (CollapsingSectionNote config : configs) {
defs.add(config.getDefinition());
}

return defs.toArray((SectionDefinition[]) Array.newInstance(SectionDefinition.class, 0));
}
}
@@ -0,0 +1,48 @@
/*
* The MIT License
*
* Copyright 2013 Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
*
* 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 org.jvnet.hudson.plugins.collapsingconsolesections;

import org.apache.commons.lang.SerializationUtils;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;

/**
* Tests {@linl CollapsingSectionAnnotator}
* @author Oleg Nenashev <nenashev@synopsys.com>, Synopsys Inc.
*/
public class CollapsingSectionAnnotatorTest {
@Test
@Bug(20304)
public void testSerialization() {
// Prepare data
CollapsingSectionsConfiguration config = new CollapsingSectionsConfiguration(
new CollapsingSectionNote[] {
new CollapsingSectionNote("test", "test", "test", true)}
,true);
CollapsingSectionAnnotator annotator = new CollapsingSectionAnnotator(config);

// Try to serialize
SerializationUtils.serialize(annotator);
}
}

0 comments on commit 987e633

Please sign in to comment.