Skip to content

Commit 9b139a0

Browse files
committed
Remove ChildNameGenerator.CHILD_NAME_FILE
1 parent 5ed27a3 commit 9b139a0

File tree

7 files changed

+14
-123
lines changed

7 files changed

+14
-123
lines changed

src/main/java/com/cloudbees/hudson/plugins/folder/AbstractFolder.java

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,10 @@ public static <K, V extends TopLevelItem> Map<K, V> loadChildren(AbstractFolder<
360360
}
361361
for (File subdir : subdirs) {
362362
File effectiveSubdir = subdir;
363-
String childName = childNameGenerator.readItemName(subdir);
364363
// Try to retain the identity of an existing child object if we can.
365364
V itemFromDir;
366365
V item;
367-
String name;
368-
var legacyName = subdir.getName();
369-
item = itemFromDir = byDirName.get(legacyName);
366+
item = itemFromDir = byDirName.get(subdir.getName());
370367
try {
371368
if (item == null) {
372369
XmlFile xmlFile = Items.getConfigFile(subdir);
@@ -377,19 +374,15 @@ public static <K, V extends TopLevelItem> Map<K, V> loadChildren(AbstractFolder<
377374
throw new FileNotFoundException("Could not find configuration file " + xmlFile.getFile());
378375
}
379376
}
380-
name = childNameGenerator.writeItemName(parent, item, effectiveSubdir, childName);
381-
String computedName = childNameGenerator.itemNameFromItem(parent, item);
382-
if (computedName == null) {
383-
computedName = subdir.getName();
384-
}
385-
if (!computedName.equals(name)) {
386-
throw new IllegalStateException("Computed name '" + computedName + "' does not match name written to file '" + name + "'");
377+
String name = childNameGenerator.itemNameFromItem(parent, item);
378+
if (name == null) {
379+
name = subdir.getName();
387380
}
388381
if (item instanceof AbstractItem) {
389382
var abstractItem = (AbstractItem) item;
390-
if (itemFromDir != null && !legacyName.equals(name) && abstractItem.getDisplayNameOrNull() == null) {
383+
if (itemFromDir != null && !subdir.getName().equals(name) && abstractItem.getDisplayNameOrNull() == null) {
391384
try (BulkChange ignored = new BulkChange(item)) {
392-
abstractItem.setDisplayName(childName);
385+
abstractItem.setDisplayName(name);
393386
}
394387
}
395388
}
@@ -407,22 +400,13 @@ public static <K, V extends TopLevelItem> Map<K, V> loadChildren(AbstractFolder<
407400

408401
@Override
409402
public String getItemName(File dir, I item) {
410-
String name = childNameGenerator().readItemName(dir);
411-
String computedName = childNameGenerator().itemNameFromItem(this, item);
412-
if (computedName == null) {
413-
computedName = dir.getName();
414-
}
415-
if (!computedName.equals(name)) {
416-
throw new IllegalStateException("Computed name '" + computedName + "' does not match name read from file '" + name + "'");
403+
String name = childNameGenerator().itemNameFromItem(this, item);
404+
if (name == null) {
405+
name = dir.getName();
417406
}
418407
return name;
419408
}
420409

421-
protected final I itemsPut(String name, I item) {
422-
childNameGenerator().writeItemName(this, item, getRootDirFor(item), name);
423-
return items.put(name, item);
424-
}
425-
426410
/**
427411
* Reloads this folder itself.
428412
* Compared to {@link #load}, this method skips parts of {@link #onLoad} such as the call to {@link #loadChildren}.
@@ -453,7 +437,7 @@ public void addLoadedChild(I item, String name) throws IOException, IllegalArgum
453437
if (items.containsKey(name)) {
454438
throw new IllegalArgumentException("already an item '" + name + "'");
455439
}
456-
itemsPut(item.getName(), item);
440+
items.put(item.getName(), item);
457441
}
458442

459443
/**
@@ -1032,7 +1016,7 @@ public I getItem(String name) throws AccessDeniedException {
10321016
@Override
10331017
public void onRenamed(I item, String oldName, String newName) throws IOException {
10341018
items.remove(oldName);
1035-
itemsPut(newName, item);
1019+
items.put(newName, item);
10361020
// For compatibility with old views:
10371021
for (View v : folderViews.getViews()) {
10381022
v.onJobRenamed(item, oldName, newName);

src/main/java/com/cloudbees/hudson/plugins/folder/ChildNameGenerator.java

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@
3535
import java.io.Closeable;
3636
import java.io.File;
3737
import java.io.IOException;
38-
import java.io.UncheckedIOException;
39-
import java.nio.charset.StandardCharsets;
40-
import java.nio.file.Files;
4138
import java.util.Map;
4239
import java.util.WeakHashMap;
4340
import edu.umd.cs.findbugs.annotations.CheckForNull;
4441
import edu.umd.cs.findbugs.annotations.NonNull;
4542
import java.util.logging.Level;
4643
import java.util.logging.Logger;
4744
import jenkins.model.TransientActionFactory;
48-
import org.apache.commons.lang.StringUtils;
4945

5046
/**
5147
* Provides a way for a {@link ComputedFolder} to break the association between the directory names on disk
@@ -87,17 +83,6 @@
8783
*/
8884
public abstract class ChildNameGenerator<P extends AbstractFolder<I>, I extends TopLevelItem> {
8985
private static final Logger LOGGER = Logger.getLogger(ChildNameGenerator.class.getName());
90-
/**
91-
* The name of the file that contains the actual name of the child item. This file is to allow a Jenkins
92-
* Administrator to determine which child is which when dealing with a folder containing child names that have
93-
* been mangled.
94-
* <p>
95-
* If there is nothing else to go on, this file will be used in preference to the child directory name, but as it
96-
* is too easy for users to mistakenly think changing the contents of the file will rename the child (which could
97-
* cause data loss for the computed folder's child) it is better for implementations to store the definitive
98-
* ideal name in a {@link JobProperty}, {@link Action} or equivalent that is attached directly to the {@link Item}.
99-
*/
100-
public static final String CHILD_NAME_FILE = "name-utf8.txt";
10186

10287
private static final Map<Trace,String> idealNames = new WeakHashMap<>();
10388

@@ -289,61 +274,6 @@ final File ensureItemDirectory(@NonNull P parent, @NonNull I item, @NonNull File
289274
*/
290275
public abstract void recordLegacyName(P parent, I item, String legacyDirName) throws IOException;
291276

292-
/**
293-
* Reads an item name from the given directory.
294-
* @param directory the directory containing the item.
295-
* @return The item name obtained from the directory, or the directory name if the name file is missing or empty.
296-
*/
297-
@NonNull
298-
public final String readItemName(@NonNull File directory) {
299-
String childName = directory.getName();
300-
File nameFile = new File(directory, CHILD_NAME_FILE);
301-
if (nameFile.isFile()) {
302-
try {
303-
childName = StringUtils.defaultString(StringUtils.trimToNull(Files.readString(nameFile.toPath(), StandardCharsets.UTF_8)), directory.getName());
304-
} catch (IOException e) {
305-
LOGGER.log(Level.WARNING, () -> "Could not read "+ nameFile + ", assuming child name is " + directory.getName());
306-
}
307-
}
308-
return childName;
309-
}
310-
311-
/**
312-
* Writes the item name to the given directory.
313-
* @param parent The parent folder of the item.
314-
* @param item The item we want to write the name for.
315-
* @param itemDirectory The directory where the item is stored.
316-
* @param childName The desired name for the item.
317-
* @return The name that was written to the directory, and whether the item needs to be saved.
318-
*/
319-
@NonNull
320-
public final String writeItemName(@NonNull P parent, @NonNull I item, @NonNull File itemDirectory, @NonNull String childName) {
321-
String name = itemNameFromItem(parent, item);
322-
if (name == null) {
323-
name = itemNameFromLegacy(parent, childName);
324-
}
325-
File nameFile = new File(itemDirectory, CHILD_NAME_FILE);
326-
try {
327-
var itemPath = itemDirectory.toPath();
328-
if (Files.notExists(itemPath)) {
329-
Files.createDirectories(itemPath);
330-
}
331-
String existingName;
332-
if (Files.exists(nameFile.toPath())) {
333-
existingName = Files.readString(nameFile.toPath(), StandardCharsets.UTF_8);
334-
} else {
335-
existingName = null;
336-
}
337-
if (existingName == null || !existingName.equals(name)) {
338-
Files.writeString(nameFile.toPath(), name, StandardCharsets.UTF_8);
339-
}
340-
} catch (IOException e) {
341-
// Unfortunately not all callers of this method throw IOException, so we need to go unchecked
342-
throw new UncheckedIOException("Failed to load " + name + " as could not write " + nameFile, e);
343-
}
344-
return name;
345-
}
346-
347277
/**
348278
* Traces the creation of a new {@link Item} in a folder. Use
349279
* {@link ChildNameGenerator#beforeCreateItem(AbstractFolder, String, String)} to get the instance.

src/main/java/com/cloudbees/hudson/plugins/folder/Folder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ private MixInImpl(Folder parent) {
457457

458458
@Override
459459
protected void add(TopLevelItem item) {
460-
itemsPut(item.getName(), item);
460+
items.put(item.getName(), item);
461461
}
462462

463463
@Override

src/main/java/com/cloudbees/hudson/plugins/folder/computed/ComputedFolder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ public void created(I child) {
870870
} catch (IOException x) {
871871
LOGGER.log(Level.WARNING, "Failed to save " + child, x);
872872
}
873-
itemsPut(name, child);
873+
items.put(name, child);
874874
Jenkins j = Jenkins.getInstanceOrNull();
875875
if (j != null) {
876876
j.rebuildDependencyGraphAsync();
@@ -991,7 +991,7 @@ public void created(I child) {
991991
} catch (IOException x) {
992992
LOGGER.log(Level.WARNING, "Failed to save " + child, x);
993993
}
994-
itemsPut(name, child);
994+
items.put(name, child);
995995
Jenkins j = Jenkins.getInstanceOrNull();
996996
if (j != null) {
997997
j.rebuildDependencyGraphAsync();

src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorAltTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@
4040
import hudson.model.TaskListener;
4141
import hudson.model.TopLevelItem;
4242
import java.io.ByteArrayOutputStream;
43-
import java.io.File;
4443
import java.io.IOException;
4544
import java.io.UnsupportedEncodingException;
4645
import java.nio.charset.StandardCharsets;
47-
import java.nio.file.Files;
4846
import java.text.Normalizer;
4947
import java.util.ArrayList;
5048
import java.util.Arrays;
@@ -246,12 +244,6 @@ private void checkChild(ComputedFolderImpl instance, String idealName) throws IO
246244
assertThat("We have an item for name " + idealName, item, notNullValue());
247245
assertThat("The root directory if the item for name " + idealName + " is mangled",
248246
item.getRootDir().getName(), is(mangle(idealName)));
249-
File nameFile = new File(item.getRootDir(), ChildNameGenerator.CHILD_NAME_FILE);
250-
assertThat("We have the " + ChildNameGenerator.CHILD_NAME_FILE + " for the item for name " + idealName,
251-
nameFile.isFile(), is(true));
252-
String name = Files.readString(nameFile.toPath(), StandardCharsets.UTF_8);
253-
assertThat("The " + ChildNameGenerator.CHILD_NAME_FILE + " for the item for name " + idealName
254-
+ " contains the encoded name", name, is(encodedName));
255247
}
256248

257249
public static String encode(String s) {

src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorRecTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@
4040
import hudson.model.TaskListener;
4141
import hudson.model.TopLevelItem;
4242
import java.io.ByteArrayOutputStream;
43-
import java.io.File;
4443
import java.io.IOException;
4544
import java.nio.charset.StandardCharsets;
46-
import java.nio.file.Files;
4745
import java.text.Normalizer;
4846
import java.util.ArrayList;
4947
import java.util.Arrays;
@@ -177,12 +175,6 @@ private void checkChild(ComputedFolderImpl instance, String idealName) throws IO
177175
assertThat("We have an item for name " + idealName, item, notNullValue());
178176
assertThat("The root directory of the item for name " + idealName + " is mangled",
179177
item.getRootDir().getName(), is(mangle(idealName)));
180-
File nameFile = new File(item.getRootDir(), ChildNameGenerator.CHILD_NAME_FILE);
181-
assertThat("We have the " + ChildNameGenerator.CHILD_NAME_FILE + " for the item for name " + idealName,
182-
nameFile.isFile(), is(true));
183-
String name = Files.readString(nameFile.toPath(), StandardCharsets.UTF_8);
184-
assertThat("The " + ChildNameGenerator.CHILD_NAME_FILE + " for the item for name " + idealName
185-
+ " contains the encoded name", name, is(encodedName));
186178
}
187179

188180
public static String encode(String s) {

src/test/java/com/cloudbees/hudson/plugins/folder/ChildNameGeneratorTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.io.File;
4444
import java.io.IOException;
4545
import java.nio.charset.StandardCharsets;
46-
import java.nio.file.Files;
4746
import java.text.Normalizer;
4847
import java.util.ArrayList;
4948
import java.util.Arrays;
@@ -314,12 +313,6 @@ private void checkChild(ComputedFolderImpl instance, String idealName) throws IO
314313
assertThat("Alternative normalized form: " + altRootDir + " does not exist",
315314
altRootDir.isDirectory(), is(false));
316315
}
317-
File nameFile = new File(item.getRootDir(), ChildNameGenerator.CHILD_NAME_FILE);
318-
assertThat("We have the " + ChildNameGenerator.CHILD_NAME_FILE + " for the item for name " + idealName,
319-
nameFile.isFile(), is(true));
320-
String name = Files.readString(nameFile.toPath(), StandardCharsets.UTF_8);
321-
assertThat("The " + ChildNameGenerator.CHILD_NAME_FILE + " for the item for name " + idealName
322-
+ " contains the encoded name", name, is(encodedName));
323316
}
324317

325318
public static String encode(String s) {

0 commit comments

Comments
 (0)