Search in sources :

Example 1 with ItemListener

use of hudson.model.listeners.ItemListener in project hudson-2.x by hudson.

the class AbstractItem method renameTo.

/**
     * Renames this item.
     * Not all the Items need to support this operation, but if you decide to do so,
     * you can use this method.
     */
protected void renameTo(String newName) throws IOException {
    // always synchronize from bigger objects first
    final ItemGroup parent = getParent();
    synchronized (parent) {
        synchronized (this) {
            // sanity check
            if (newName == null)
                throw new IllegalArgumentException("New name is not given");
            // noop?
            if (this.name.equals(newName))
                return;
            Item existing = parent.getItem(newName);
            if (existing != null && existing != this)
                // see http://www.nabble.com/error-on-renaming-project-tt18061629.html
                throw new IllegalArgumentException("Job " + newName + " already exists");
            String oldName = this.name;
            performBeforeItemRenaming(oldName, newName);
            File oldRoot = this.getRootDir();
            doSetName(newName);
            File newRoot = this.getRootDir();
            boolean success = false;
            try {
                // rename data files
                boolean interrupted = false;
                boolean renamed = false;
                // so retry few times before we fall back to copy.
                for (int retry = 0; retry < 5; retry++) {
                    if (oldRoot.renameTo(newRoot)) {
                        renamed = true;
                        // succeeded
                        break;
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // process the interruption later
                        interrupted = true;
                    }
                }
                if (interrupted)
                    Thread.currentThread().interrupt();
                if (!renamed) {
                    // failed to rename. it must be that some lengthy
                    // process is going on
                    // to prevent a rename operation. So do a copy. Ideally
                    // we'd like to
                    // later delete the old copy, but we can't reliably do
                    // so, as before the VM
                    // shuts down there might be a new job created under the
                    // old name.
                    Copy cp = new Copy();
                    cp.setProject(new org.apache.tools.ant.Project());
                    cp.setTodir(newRoot);
                    FileSet src = new FileSet();
                    src.setDir(getRootDir());
                    cp.addFileset(src);
                    cp.setOverwrite(true);
                    cp.setPreserveLastModified(true);
                    // keep going even if
                    cp.setFailOnError(false);
                    // there's an error
                    cp.execute();
                    // try to delete as much as possible
                    try {
                        Util.deleteRecursive(oldRoot);
                    } catch (IOException e) {
                        // but ignore the error, since we expect that
                        e.printStackTrace();
                    }
                }
                success = true;
            } finally {
                // if failed, back out the rename.
                if (!success)
                    doSetName(oldName);
            }
            callOnRenamed(newName, parent, oldName);
            for (ItemListener l : ItemListener.all()) l.onRenamed(this, oldName, newName);
        }
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) IOException(java.io.IOException) Copy(org.apache.tools.ant.taskdefs.Copy) ItemListener(hudson.model.listeners.ItemListener) XmlFile(hudson.XmlFile) File(java.io.File)

Example 2 with ItemListener

use of hudson.model.listeners.ItemListener in project hudson-2.x by hudson.

the class Hudson method onDeleted.

/**
     * Called in response to {@link Job#doDoDelete(StaplerRequest, StaplerResponse)}
     */
public void onDeleted(TopLevelItem item) throws IOException {
    for (ItemListener l : ItemListener.all()) {
        l.onDeleted(item);
    }
    items.remove(item.getName());
    for (View v : views) {
        v.onJobRenamed(item, item.getName(), null);
    }
    save();
}
Also used : ItemListener(hudson.model.listeners.ItemListener) ExtensionListView(hudson.ExtensionListView)

Aggregations

ItemListener (hudson.model.listeners.ItemListener)2 ExtensionListView (hudson.ExtensionListView)1 XmlFile (hudson.XmlFile)1 File (java.io.File)1 IOException (java.io.IOException)1 Copy (org.apache.tools.ant.taskdefs.Copy)1 FileSet (org.apache.tools.ant.types.FileSet)1