Skip to content

Commit c1616c8

Browse files
committed
Modeler prefs - removing unused nodes
1 parent ea47c9b commit c1616c8

44 files changed

Lines changed: 272 additions & 125 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/Application.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.apache.cayenne.modeler.pref.DBConnectorPrefs;
3636
import org.apache.cayenne.modeler.pref.GeneralPrefs;
3737
import org.apache.cayenne.modeler.pref.PrefsLocator;
38-
import org.apache.cayenne.modeler.pref.PrefsRepository;
38+
import org.apache.cayenne.modeler.pref.PrefsManager;
3939
import org.apache.cayenne.modeler.pref.RecentProjectsPrefs;
4040
import org.apache.cayenne.modeler.service.action.GlobalActions;
4141
import org.apache.cayenne.modeler.service.classloader.ModelerClassLoader;
@@ -90,7 +90,7 @@ public static void launch(String[] args, UIInitializer platformInit) {
9090
private final UIInitializer platformInit;
9191
private final ModelerClassLoader classLoader;
9292
private final PrefsLocator prefsLocator;
93-
private final PrefsRepository prefsRepository;
93+
private final PrefsManager prefsManager;
9494
private final ProjectValidator projectValidator;
9595
private final CliArgs cli;
9696
private GlobalActions actionManager;
@@ -106,7 +106,7 @@ public Application(Injector injector, UIInitializer platformInit, CliArgs cli) {
106106

107107
this.classLoader = new ModelerClassLoader();
108108
this.prefsLocator = new PrefsLocator();
109-
this.prefsRepository = new PrefsRepository(injector.getInstance(ConfigurationNameMapper.class), prefsLocator);
109+
this.prefsManager = new PrefsManager(injector.getInstance(ConfigurationNameMapper.class), prefsLocator);
110110
this.projectValidator = new ConfigurableProjectValidator(this);
111111
}
112112

@@ -185,7 +185,7 @@ public void launch(File initialProject) {
185185
this.logConsole = new LogConsole(this);
186186
ModelerLogFactory.setAppender(logConsole);
187187

188-
getPrefsRepository().runMigrations();
188+
getPrefsManager().runMigrations();
189189

190190
this.dbConnectors = new DBConnectorPrefs(prefsLocator).getConnectors();
191191

@@ -226,8 +226,8 @@ public DBConnectors getDbConnectors() {
226226
return dbConnectors;
227227
}
228228

229-
public PrefsRepository getPrefsRepository() {
230-
return prefsRepository;
229+
public PrefsManager getPrefsManager() {
230+
return prefsManager;
231231
}
232232

233233
public PrefsLocator getPrefsLocator() {

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/NameGeneratorPreferences.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static NameGeneratorPreferences getInstance() {
4848
}
4949

5050
Preferences getPreference(Application application) {
51-
return application.getPrefsRepository().projectPref(application.getFrame().getProjectSession().project(), null);
51+
return application.getPrefsManager().projectPref(application.getFrame().getProjectSession().project(), null);
5252
}
5353

5454
/**

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/pref/DataMapPrefs.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public final class DataMapPrefs extends PreferenceAdapter {
2626
public static final String SUPERCLASS_PACKAGE_PROPERTY = "superclassPackage";
2727
public static final String DEFAULT_SUPERCLASS_PACKAGE_SUFFIX = "auto";
2828

29-
public DataMapPrefs(PrefsRepository repository, DataMap dataMap) {
29+
public DataMapPrefs(PrefsManager repository, DataMap dataMap) {
3030
super(repository.dataMapPref(dataMap, null));
3131
}
3232

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/pref/DataNodePrefs.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ public final class DataNodePrefs extends PreferenceAdapter {
2626

2727
static final String NODE = "dataNode";
2828

29-
private final PrefsRepository repository;
29+
private final PrefsManager repository;
3030
private final Project project;
3131

3232
private String localDataSource;
3333

34-
public DataNodePrefs(PrefsRepository repository, Project project, String dataNodeName) {
34+
public DataNodePrefs(PrefsManager repository, Project project, String dataNodeName) {
3535
super(repository.projectPref(project, NODE + "/" + dataNodeName));
3636

3737
// capture repo and project for the sake of "rename"

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/pref/PreferenceMigration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
package org.apache.cayenne.modeler.pref;
2020

2121
/**
22-
* One-time data migration applied by {@link PrefsRepository#runMigrations()}.
22+
* One-time data migration applied by {@link PrefsManager#runMigrations()}.
2323
*/
2424
public interface PreferenceMigration {
2525

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/pref/PrefsLocator.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@
2121
import java.util.prefs.Preferences;
2222

2323
/**
24-
* Maps logical Modeler preference locations to {@link Preferences} nodes. Pure
25-
* JDK; holds the layout constants for {@code org/apache/cayenne/modeler/v5/...}
26-
* (the regular preferences tree) and {@code org/apache/cayenne/modeler/mcp-handshake/...}
27-
* (the sibling MCP launch-handshake namespace). All Modeler code that addresses
28-
* a preferences node should go through this class.
24+
* Resolves preference nodes against the common root.
2925
*/
3026
public final class PrefsLocator {
3127

@@ -43,7 +39,15 @@ public final class PrefsLocator {
4339
private final Preferences root;
4440

4541
public PrefsLocator() {
46-
this.root = Preferences.userRoot();
42+
this(Preferences.userRoot());
43+
}
44+
45+
/**
46+
* Test-friendly constructor letting callers point the locator at an isolated
47+
* preferences subtree instead of {@link Preferences#userRoot()}.
48+
*/
49+
public PrefsLocator(Preferences root) {
50+
this.root = root;
4751
}
4852

4953
/**

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/pref/PrefsRepository.java renamed to modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/pref/PrefsManager.java

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.cayenne.modeler.pref.migration.toV5._5_FrameGeometryMigration;
2828
import org.apache.cayenne.modeler.pref.migration.toV5._6_ProjectSplitPaneMigration;
2929
import org.apache.cayenne.modeler.pref.migration.toV5._7_EntityTablePrefsMigration;
30+
import org.apache.cayenne.modeler.pref.migration.toV5._8_RemoveRedundantPathIndexMigration;
3031
import org.apache.cayenne.configuration.ConfigurationNameMapper;
3132
import org.apache.cayenne.project.Project;
3233
import org.apache.cayenne.resource.Resource;
@@ -47,25 +48,19 @@
4748

4849

4950
/**
50-
* App-wide preferences service for the Modeler. Delegates preferences-node
51-
* location to {@link PrefsLocator} and adds Modeler-specific lifecycle
52-
* on top: tracking unsaved projects/DataMaps with in-memory ids, staging
53-
* renames/moves so they can be replayed on save, running version-gated
54-
* preference migrations, and resetting the subtree.
51+
* A stateful preferences manager for the Application. Handles preference versioning, DataMap and project renaming,
52+
* locating preference nodes under the common root, etc.
5553
*/
56-
public class PrefsRepository {
54+
public class PrefsManager {
5755

58-
private static final Logger LOGGER = LoggerFactory.getLogger(PrefsRepository.class);
56+
private static final Logger LOGGER = LoggerFactory.getLogger(PrefsManager.class);
5957

60-
private static final String PROJECT_INDEX_NODE = "projectIndex";
61-
private static final String DATAMAP_INDEX_NODE = "dataMapIndex";
6258
private static final String UNSAVED_PREFIX = "unsaved-";
6359

6460
private static final String UI_NODE = "ui";
6561
private static final String META_NODE = "_meta";
6662

6763
private static final String MIGRATIONS_VERSION_KEY = "migrationsAppliedVersion";
68-
private static final String PATH_KEY = "path";
6964

7065
private final ConfigurationNameMapper nameMapper;
7166
private final List<PreferenceMigration> migrations;
@@ -84,14 +79,15 @@ private static List<PreferenceMigration> toV5Migrations() {
8479
new _4_RecentProjectsMigration(),
8580
new _5_FrameGeometryMigration(),
8681
new _6_ProjectSplitPaneMigration(),
87-
new _7_EntityTablePrefsMigration())
82+
new _7_EntityTablePrefsMigration(),
83+
new _8_RemoveRedundantPathIndexMigration())
8884

8985
// just in case, sort to prevent any ordering issues with manual insertion
9086
.sorted(Comparator.comparingInt(PreferenceMigration::version))
9187
.collect(Collectors.toList());
9288
}
9389

94-
public PrefsRepository(ConfigurationNameMapper nameMapper, PrefsLocator locator) {
90+
public PrefsManager(ConfigurationNameMapper nameMapper, PrefsLocator locator) {
9591
this.nameMapper = nameMapper;
9692
this.locator = locator;
9793
this.migrations = toV5Migrations();
@@ -105,16 +101,11 @@ public PrefsRepository(ConfigurationNameMapper nameMapper, PrefsLocator locator)
105101
* Returns the preferences node for the given {@link Project}, optionally descending into a subtree within it.
106102
*/
107103
public Preferences projectPref(Project project, String relativePath) {
108-
Preferences node;
109104
String path = projectPath(project);
110-
if (path != null) {
111-
String id = PreferenceNodeIds.idForPath(path);
112-
node = locator.projectNode(id);
113-
recordPath(node, id, path, PROJECT_INDEX_NODE);
114-
} else {
115-
String id = newProjectIds.computeIfAbsent(project, p -> newUnsavedId());
116-
node = locator.projectNode(id);
117-
}
105+
String id = path != null
106+
? PreferenceNodeIds.idForPath(path)
107+
: newProjectIds.computeIfAbsent(project, p -> newUnsavedId());
108+
Preferences node = locator.projectNode(id);
118109
return relativePath == null || relativePath.isEmpty() ? node : node.node(relativePath);
119110
}
120111

@@ -133,16 +124,11 @@ public Preferences uiNode(String relativePath) {
133124
* Returns the preferences node for the given {@link DataMap}, optionally descending into a subtree within it.
134125
*/
135126
public Preferences dataMapPref(DataMap map, String relativePath) {
136-
Preferences node;
137127
String path = dataMapPath(map);
138-
if (path != null) {
139-
String id = PreferenceNodeIds.idForPath(path);
140-
node = locator.dataMapNode(id);
141-
recordPath(node, id, path, DATAMAP_INDEX_NODE);
142-
} else {
143-
String id = newDataMapIds.computeIfAbsent(map, m -> newUnsavedId());
144-
node = locator.dataMapNode(id);
145-
}
128+
String id = path != null
129+
? PreferenceNodeIds.idForPath(path)
130+
: newDataMapIds.computeIfAbsent(map, m -> newUnsavedId());
131+
Preferences node = locator.dataMapNode(id);
146132
return relativePath == null || relativePath.isEmpty() ? node : node.node(relativePath);
147133
}
148134

@@ -310,18 +296,14 @@ private void reconcileProject(Project project) {
310296
if (stagedOldPath != null) {
311297
String oldId = PreferenceNodeIds.idForPath(stagedOldPath);
312298
if (!oldId.equals(savedId)) {
313-
relocate(locator::projectNode, oldId, savedId, PROJECT_INDEX_NODE, currentPath);
314-
} else {
315-
recordPath(locator.projectNode(savedId), savedId, currentPath, PROJECT_INDEX_NODE);
299+
relocate(locator::projectNode, oldId, savedId);
316300
}
317301
return;
318302
}
319303

320304
String oldId = newProjectIds.remove(project);
321305
if (oldId != null && !oldId.equals(savedId)) {
322-
relocate(locator::projectNode, oldId, savedId, PROJECT_INDEX_NODE, currentPath);
323-
} else {
324-
recordPath(locator.projectNode(savedId), savedId, currentPath, PROJECT_INDEX_NODE);
306+
relocate(locator::projectNode, oldId, savedId);
325307
}
326308
}
327309

@@ -336,35 +318,19 @@ private void reconcileDataMap(DataMap map) {
336318
if (stagedOldPath != null) {
337319
String oldId = PreferenceNodeIds.idForPath(stagedOldPath);
338320
if (!oldId.equals(savedId)) {
339-
relocate(locator::dataMapNode, oldId, savedId, DATAMAP_INDEX_NODE, currentPath);
340-
} else {
341-
recordPath(locator.dataMapNode(savedId), savedId, currentPath, DATAMAP_INDEX_NODE);
321+
relocate(locator::dataMapNode, oldId, savedId);
342322
}
343323
return;
344324
}
345325

346326
String oldId = newDataMapIds.remove(map);
347327
if (oldId != null && !oldId.equals(savedId)) {
348-
relocate(locator::dataMapNode, oldId, savedId, DATAMAP_INDEX_NODE, currentPath);
349-
} else {
350-
recordPath(locator.dataMapNode(savedId), savedId, currentPath, DATAMAP_INDEX_NODE);
328+
relocate(locator::dataMapNode, oldId, savedId);
351329
}
352330
}
353331

354-
private void relocate(Function<String, Preferences> nodeForId, String oldId, String newId, String indexNode, String path) {
355-
Preferences src = nodeForId.apply(oldId);
356-
Preferences dst = nodeForId.apply(newId);
357-
PreferencesCopier.move(src, dst);
358-
locator.appNode(indexNode).remove(oldId);
359-
recordPath(dst, newId, path, indexNode);
360-
}
361-
362-
private void recordPath(Preferences node, String id, String path, String indexNode) {
363-
if (path == null) {
364-
return;
365-
}
366-
node.put(PATH_KEY, path);
367-
locator.appNode(indexNode).put(id, path);
332+
private static void relocate(Function<String, Preferences> nodeForId, String oldId, String newId) {
333+
PreferencesCopier.move(nodeForId.apply(oldId), nodeForId.apply(newId));
368334
}
369335

370336
private static String projectPath(Project project) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*****************************************************************
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
****************************************************************/
19+
package org.apache.cayenne.modeler.pref.migration.toV5;
20+
21+
import org.apache.cayenne.modeler.pref.PreferenceMigration;
22+
import org.apache.cayenne.modeler.pref.PrefsLocator;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
26+
import java.util.prefs.BackingStoreException;
27+
import java.util.prefs.Preferences;
28+
29+
/**
30+
* Removes the write-only {@code app/projectIndex} and {@code app/dataMapIndex}
31+
* subtrees and the per-node {@code path} key from {@code project/&lt;id&gt;} /
32+
* {@code datamap/&lt;id&gt;} entries. All three were introduced during the v5
33+
* Modeler preferences refactor but never read by any code path; their keys
34+
* fully duplicate the addressing already used by the per-project and
35+
* per-DataMap subtrees.
36+
*/
37+
public class _8_RemoveRedundantPathIndexMigration implements PreferenceMigration {
38+
39+
private static final Logger LOGGER = LoggerFactory.getLogger(_8_RemoveRedundantPathIndexMigration.class);
40+
41+
private static final String PATH_KEY = "path";
42+
private static final String PROJECT_INDEX_NODE = "projectIndex";
43+
private static final String DATAMAP_INDEX_NODE = "dataMapIndex";
44+
private static final String PROJECT_ROOT_NODE = "project";
45+
private static final String DATAMAP_ROOT_NODE = "datamap";
46+
47+
@Override
48+
public int version() {
49+
return 8;
50+
}
51+
52+
@Override
53+
public void apply(PrefsLocator locator) {
54+
removeIndexNode(locator.appNode(null), PROJECT_INDEX_NODE);
55+
removeIndexNode(locator.appNode(null), DATAMAP_INDEX_NODE);
56+
stripPathKey(locator.modelerRoot(), PROJECT_ROOT_NODE);
57+
stripPathKey(locator.modelerRoot(), DATAMAP_ROOT_NODE);
58+
}
59+
60+
private static void removeIndexNode(Preferences appNode, String name) {
61+
try {
62+
if (appNode.nodeExists(name)) {
63+
appNode.node(name).removeNode();
64+
}
65+
} catch (BackingStoreException e) {
66+
LOGGER.warn("Error removing redundant prefs index '{}'", name, e);
67+
}
68+
}
69+
70+
private static void stripPathKey(Preferences modelerRoot, String rootNodeName) {
71+
try {
72+
if (!modelerRoot.nodeExists(rootNodeName)) {
73+
return;
74+
}
75+
Preferences root = modelerRoot.node(rootNodeName);
76+
for (String childName : root.childrenNames()) {
77+
root.node(childName).remove(PATH_KEY);
78+
}
79+
} catch (BackingStoreException e) {
80+
LOGGER.warn("Error stripping '{}' from prefs under '{}'", PATH_KEY, rootNodeName, e);
81+
}
82+
}
83+
}

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/project/ProjectPrefs.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import org.apache.cayenne.modeler.event.display.ProcedureParameterDisplayEvent;
5454
import org.apache.cayenne.modeler.event.display.QueryDisplayEvent;
5555
import org.apache.cayenne.modeler.pref.PreferenceAdapter;
56-
import org.apache.cayenne.modeler.pref.PrefsRepository;
56+
import org.apache.cayenne.modeler.pref.PrefsManager;
5757
import org.apache.cayenne.project.Project;
5858
import org.apache.cayenne.util.CayenneMapEntry;
5959

@@ -96,7 +96,7 @@ private enum Kind {
9696
query, multipleObjects
9797
}
9898

99-
public ProjectPrefs(PrefsRepository repository, Project project) {
99+
public ProjectPrefs(PrefsManager repository, Project project) {
100100
super(repository.projectPref(project, null));
101101
}
102102

0 commit comments

Comments
 (0)