public synchronized TopLevelItem createProjectFromXML(String name, InputStream xml) throws IOException { acl.checkPermission(Job.CREATE); Jenkins.getInstance().getProjectNamingStrategy().checkName(name); // place it as config.xml File configXml = Items.getConfigFile(getRootDirFor(name)).getFile(); configXml.getParentFile().mkdirs(); try { IOUtils.copy(xml, configXml); // load it TopLevelItem result; Items.updatingByXml.set(true); try { result = (TopLevelItem) Items.load(parent, configXml.getParentFile()); } finally { Items.updatingByXml.set(false); } add(result); ItemListener.fireOnCreated(result); Jenkins.getInstance().rebuildDependencyGraph(); return result; } catch (IOException e) { // if anything fails, delete the config file to avoid further confusion Util.deleteRecursive(configXml.getParentFile()); throw e; } }
/** * Loads all the child {@link Item}s. * * @param modulesDir Directory that contains sub-directories for each child item. */ public static <K, V extends Item> Map<K, V> loadChildren( ItemGroup parent, File modulesDir, Function1<? extends K, ? super V> key) { modulesDir.mkdirs(); // make sure it exists File[] subdirs = modulesDir.listFiles( new FileFilter() { public boolean accept(File child) { return child.isDirectory(); } }); CopyOnWriteMap.Tree<K, V> configurations = new CopyOnWriteMap.Tree<K, V>(); for (File subdir : subdirs) { try { V item = (V) Items.load(parent, subdir); configurations.put(key.call(item), item); } catch (IOException e) { e.printStackTrace(); // TODO: logging } } return configurations; }
/** * Copies an existing {@link TopLevelItem} to a new name. * * <p>The caller is responsible for calling {@link ItemListener#fireOnCopied(Item, Item)}. This * method cannot do that because it doesn't know how to make the newly added item reachable from * the parent. */ @SuppressWarnings({"unchecked"}) public synchronized <T extends TopLevelItem> T copy(T src, String name) throws IOException { acl.checkPermission(Job.CREATE); T result = (T) createProject(src.getDescriptor(), name, false); // copy config Util.copyFile(Items.getConfigFile(src).getFile(), Items.getConfigFile(result).getFile()); // reload from the new config Items.updatingByXml.set(true); try { result = (T) Items.load(parent, result.getRootDir()); } finally { Items.updatingByXml.set(false); } result.onCopiedFrom(src); add(result); ItemListener.fireOnCopied(src, result); Hudson.getInstance().rebuildDependencyGraph(); return result; }