Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-45892] IllegalStateException at hudson.XmlFile.replaceIfNotA… #257

Merged
merged 9 commits into from
Dec 1, 2017
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<!-- dependency versions -->
<httpclient.version>4.5.1</httpclient.version>
<jackson.version>2.5.0</jackson.version>
<jenkins.version>2.32.1</jenkins.version>
<jenkins.version>2.89</jenkins.version>

<kubernetes-client.version>2.6.1</kubernetes-client.version>

Expand Down Expand Up @@ -208,6 +208,11 @@
<artifactId>ssh-credentials</artifactId>
<version>1.12</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>1.6.0</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, 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.csanchez.jenkins.plugins.kubernetes.pipeline;

import java.io.IOException;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.BulkChange;
import hudson.model.InvisibleAction;
import hudson.model.Run;
import jenkins.model.RunAction2;

/**
* @author Carlos Sanchez
* @since 1.1.1
*
*/
public abstract class AbstractInvisibleRunAction2 extends InvisibleAction implements RunAction2 {

private static final Logger LOGGER = Logger.getLogger(AbstractInvisibleRunAction2.class.getName());

protected final Stack<String> stack = new Stack<>();

private transient Run<?, ?> run;

public AbstractInvisibleRunAction2(Run<?, ?> run) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand RunAction2, this is not necessary, as onAttached(Run) will be called as soon as it is attached to a Run instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setRun(run);
}

protected abstract AbstractInvisibleRunAction2 createAction(Run<?, ?> run);

public Run<?, ?> getRun() {
return run;
}

protected void setRun(Run<?, ?> run) {
this.run = run;
}

public void push(String item) throws IOException {
if (run == null) {
LOGGER.warning("run is null, cannot push");
return;
}
synchronized (run) {
BulkChange bc = new BulkChange(run);
try {
AbstractInvisibleRunAction2 action = run.getAction(this.getClass());
if (action == null) {
action = createAction(run);
run.addAction(action);
}
LOGGER.log(Level.FINEST, "Pushing item {0} to action {1} in run {2}",
new Object[] { item, action, run });
action.stack.push(item);
bc.commit();
} finally {
bc.abort();
}
}
}

public String pop() throws IOException {
if (run == null) {
LOGGER.warning("run is null, cannot pop");
return null;
}
synchronized (getRun()) {
BulkChange bc = new BulkChange(getRun());
try {
AbstractInvisibleRunAction2 action = getRun().getAction(this.getClass());
if (action == null) {
action = createAction(getRun());
getRun().addAction(action);
}
String item = action.stack.pop();
LOGGER.log(Level.FINEST, "Popped item {0} from action {1} in run {2}",
new Object[] { item, action, run });
bc.commit();
return item;
} finally {
bc.abort();
}
}
}

@Override
public void onAttached(Run<?, ?> r) {
setRun(r);
}

@Override
public void onLoad(Run<?, ?> r) {
setRun(r);
}

}
Original file line number Diff line number Diff line change
@@ -1,81 +1,30 @@
package org.csanchez.jenkins.plugins.kubernetes.pipeline;

import java.io.IOException;
import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;
import java.util.Stack;
import java.util.logging.Logger;

import hudson.BulkChange;
import hudson.model.InvisibleAction;
import hudson.model.Run;
import jenkins.model.RunAction2;

public class NamespaceAction extends InvisibleAction {
public class NamespaceAction extends AbstractInvisibleRunAction2 implements RunAction2 {

private static final Logger LOGGER = Logger.getLogger(NamespaceAction.class.getName());

private final Stack<String> namespaces = new Stack<>();
private final Run run;


public NamespaceAction(Run run) {
this.run = run;
}

public void push(String namespace) throws IOException {
if (run == null) {
LOGGER.warning("run is null, cannot push");
return;
}
synchronized (run) {
BulkChange bc = new BulkChange(run);
try {
NamespaceAction action = run.getAction(NamespaceAction.class);
if (action == null) {
action = new NamespaceAction(run);
run.addAction(action);
}
action.namespaces.push(namespace);
bc.commit();
} finally {
bc.abort();
}
}
public NamespaceAction(Run<?, ?> run) {
super(run);
}

public String pop() throws IOException {
if (run == null) {
LOGGER.warning("run is null, cannot pop");
return null;
}
synchronized (run) {
BulkChange bc = new BulkChange(run);
try {
NamespaceAction action = run.getAction(NamespaceAction.class);
if (action == null) {
action = new NamespaceAction(run);
run.addAction(action);
}
String namespace = action.namespaces.pop();
bc.commit();
return namespace;
} finally {
bc.abort();
return null;
}
}
@Override
protected AbstractInvisibleRunAction2 createAction(Run<?, ?> run) {
return new NamespaceAction(run);
}

public String getNamespace() {
synchronized (run) {
NamespaceAction action = run.getAction(NamespaceAction.class);
synchronized (getRun()) {
NamespaceAction action = getRun().getAction(NamespaceAction.class);
if (action == null) {
action = new NamespaceAction(run);
run.addAction(action);
action = new NamespaceAction(getRun());
getRun().addAction(action);
}
try {
return action.namespaces.peek();
return action.stack.peek();
} catch (EmptyStackException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,79 +1,30 @@
package org.csanchez.jenkins.plugins.kubernetes.pipeline;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.logging.Logger;

import hudson.BulkChange;
import hudson.model.InvisibleAction;
import hudson.model.Run;
import jenkins.model.RunAction2;

public class PodTemplateAction extends InvisibleAction {
public class PodTemplateAction extends AbstractInvisibleRunAction2 implements RunAction2 {

private static final Logger LOGGER = Logger.getLogger(PodTemplateAction.class.getName());

private final Stack<String> names = new Stack<>();
private final Run run;


PodTemplateAction(Run run) {
this.run = run;
PodTemplateAction(Run<?, ?> run) {
super(run);
}

public void push(String template) throws IOException {
if (run == null) {
LOGGER.warning("run is null, cannot push");
return;
}
synchronized (run) {
BulkChange bc = new BulkChange(run);
try {
PodTemplateAction action = run.getAction(PodTemplateAction.class);
if (action == null) {
action = new PodTemplateAction(run);
run.addAction(action);
}
action.names.push(template);
bc.commit();
} finally {
bc.abort();
}
}
}

public String pop() throws IOException {
if (run == null) {
LOGGER.warning("run is null, cannot pop");
return null;
}
synchronized (run) {
BulkChange bc = new BulkChange(run);
try {
PodTemplateAction action = run.getAction(PodTemplateAction.class);
if (action == null) {
action = new PodTemplateAction(run);
run.addAction(action);
}
String template = action.names.pop();
bc.commit();
return template;
} finally {
bc.abort();
return null;
}
}
@Override
protected AbstractInvisibleRunAction2 createAction(Run<?, ?> run) {
return new PodTemplateAction(run);
}

public List<String> getParentTemplateList() {
synchronized (run) {
PodTemplateAction action = run.getAction(PodTemplateAction.class);
synchronized (getRun()) {
PodTemplateAction action = getRun().getAction(PodTemplateAction.class);
if (action == null) {
action = new PodTemplateAction(run);
run.addAction(action);
action = new PodTemplateAction(getRun());
getRun().addAction(action);
}
return new ArrayList<>(action.names);
return new ArrayList<>(action.stack);
}
}

Expand Down