Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-20008] Added DirectlyModifiableTopLevelItemGroup.
  • Loading branch information
jglick committed Dec 18, 2013
1 parent a6e01f7 commit 1d21017
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
@@ -0,0 +1,59 @@
/*
* The MIT License
*
* Copyright 2013 Jesse Glick.
*
* 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.model;

import hudson.model.Item;
import hudson.model.TopLevelItem;
import hudson.model.listeners.ItemListener;
import java.io.IOException;

/**
* Item group which supports items being directly moved in or out of it.
* @since TODO
*/
public interface DirectlyModifiableTopLevelItemGroup extends ModifiableTopLevelItemGroup {

/**
* Returns true if a particular item may be added to this group.
* @param item an item currently elsewhere
* @return true if {@link #add} may be called with this item
*/
boolean canAdd(TopLevelItem item);

/**
* Adds an item to this group.
* Unlike {@link Jenkins#putItem} this does not try to call {@link Item#delete} on an existing item, nor does it fire {@link ItemListener#onCreated}, nor check permissions.
* Normally you would call {@link Item#onLoad} after calling this method (the implementation is not expected to do so).
* To remove an item, use {@link #onDeleted}.
* @param <I> the kind of item
* @param item an item to add which is currently elsewhere
* @param name the desired item name in this group (might simply be the original {@link Item#getName})
* @return normally the same {@code item}, but might be a new cppy if necessary
* @throws IOException if adding fails
* @throws IllegalArgumentException if {@link #canAdd} is false, or an item with this name already exists, or this item is as yet unnamed
*/
<I extends TopLevelItem> I add(I item, String name) throws IOException, IllegalArgumentException;

}
16 changes: 14 additions & 2 deletions core/src/main/java/jenkins/model/Jenkins.java
Expand Up @@ -304,7 +304,7 @@
* @author Kohsuke Kawaguchi
*/
@ExportedBean
public class Jenkins extends AbstractCIBase implements ModifiableTopLevelItemGroup, StaplerProxy, StaplerFallback,
public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLevelItemGroup, StaplerProxy, StaplerFallback,
ModifiableViewGroup, AccessControlled, DescriptorByNameOwner,
ModelObjectWithContextMenu, ModelObjectWithChildren {
private transient final Queue queue;
Expand Down Expand Up @@ -472,7 +472,7 @@ public DescriptorExtensionList compute(Class key) {
* Active {@link Cloud}s.
*/
public final Hudson.CloudList clouds = new Hudson.CloudList(this);

public static class CloudList extends DescribableList<Cloud,Descriptor<Cloud>> {
public CloudList(Jenkins h) {
super(h);
Expand Down Expand Up @@ -2452,6 +2452,18 @@ public void onDeleted(TopLevelItem item) throws IOException {
save();
}

@Override public boolean canAdd(TopLevelItem item) {
return true;
}

@Override synchronized public <I extends TopLevelItem> I add(I item, String name) throws IOException, IllegalArgumentException {
if (items.containsKey(name)) {
throw new IllegalArgumentException("already an item '" + name + "'");
}
items.put(name, item);
return item;
}

public FingerprintMap getFingerprintMap() {
return fingerprintMap;
}
Expand Down
16 changes: 14 additions & 2 deletions test/src/main/java/org/jvnet/hudson/test/MockFolder.java
Expand Up @@ -54,8 +54,8 @@
import java.util.Set;
import java.util.TreeMap;
import javax.servlet.ServletException;
import jenkins.model.DirectlyModifiableTopLevelItemGroup;
import jenkins.model.Jenkins;
import jenkins.model.ModifiableTopLevelItemGroup;
import org.kohsuke.stapler.StaplerFallback;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
Expand All @@ -69,7 +69,7 @@
* @since 1.494
*/
@SuppressWarnings({"unchecked", "rawtypes"}) // the usual API mistakes
public class MockFolder extends AbstractItem implements ModifiableTopLevelItemGroup, TopLevelItem, ModifiableViewGroup, StaplerFallback {
public class MockFolder extends AbstractItem implements DirectlyModifiableTopLevelItemGroup, TopLevelItem, ModifiableViewGroup, StaplerFallback {

private transient Map<String,TopLevelItem> items = new TreeMap<String,TopLevelItem>();
private final List<View> views = new ArrayList<View>(Collections.singleton(new AllView("All", this)));
Expand Down Expand Up @@ -187,6 +187,18 @@ public <T extends TopLevelItem> T createProject(Class<T> type, String name) thro
items.remove(item.getName());
}

@Override public boolean canAdd(TopLevelItem item) {
return true;
}

@Override synchronized public <I extends TopLevelItem> I add(I item, String name) throws IOException, IllegalArgumentException {
if (items.containsKey(name)) {
throw new IllegalArgumentException("already an item '" + name + "'");
}
items.put(name, item);
return item;
}

@Override public TopLevelItemDescriptor getDescriptor() {
return Jenkins.getInstance().getDescriptorByType(DescriptorImpl.class);
}
Expand Down

0 comments on commit 1d21017

Please sign in to comment.