Ticket #2228: closeMasterAccesses.patch
File closeMasterAccesses.patch, 22.0 KB (added by mira, 15 years ago) |
---|
-
src/main/java/org/sophie2/main/app/commons/app/DocumentsLogic.java
### Eclipse Workspace Patch 1.0 #P org.sophie2.main.app.commons
114 114 mainWindow.currentDocument().set(null); 115 115 116 116 } 117 118 117 docView.getAccess().close(); 119 118 } 120 119 -
src/main/java/org/sophie2/main/app/commons/util/SaveDocUtil.java
9 9 import org.sophie2.base.model.resources.r4.access.AccessOptions; 10 10 import org.sophie2.base.model.resources.r4.access.BaseResourceAccess; 11 11 import org.sophie2.base.model.resources.r4.access.ResourceAccess; 12 import org.sophie2.base.model.resources.r4.access.ResourceLocator; 12 13 import org.sophie2.base.model.resources.r4.access.StoredResourceAccess; 13 14 import org.sophie2.main.app.commons.app.AppMainWindow; 14 15 import org.sophie2.main.app.commons.book.BookDocView; … … 90 91 file = FileDialogInput.appendFileExtension(file, FileDialogInput.BOOK_FILTER); 91 92 92 93 final ResourceRefR4 fileRef = ResourceRefR4.make(file); 94 AppMainWindow mainWindow = AppViewUtil.findMainWindow(bookDocView); 95 ResourceLocator locator = mainWindow.locator().get(); 93 96 try { 94 if (!access.getRef().equals(fileRef)) { 95 // if (ResourceFilesUtil.hasFileDirectory(bookDocView.model().get())) { 96 // ResourceFilesUtil.copyResourceDir( 97 // access.getRef().getLocation(), fileRef.getLocation()); 98 // } 99 100 AppMainWindow mainWindow = AppViewUtil.findMainWindow(bookDocView); 101 StoredResourceAccess fileAccess = (StoredResourceAccess) mainWindow.locator().get().create( 102 fileRef, AccessOptions.DEFAULT_ACCESS_OPTIONS, 97 StoredResourceAccess fileAccess; 98 if (access.getRef().equals(fileRef)) { 99 fileAccess = ((StoredResourceAccess) access); 100 } else { 101 fileAccess = (StoredResourceAccess) locator.create( 102 fileRef, AccessOptions.DEFAULT_ACCESS_OPTIONS.modifyViewId(), 103 103 ((BaseResourceAccess) bookDocView.getAccess()).cloneHeadRevision()); 104 fileAccess.save(null); 105 bookDocView.reopen(fileAccess); 106 } else { 107 ((StoredResourceAccess) access).save(null); 104 if (fileAccess == null) { 105 DialogUtils.showErrorDialog(bookDocView.swingComponent().get(), 106 "The resource at "+ fileRef + " is currently in use.", 107 "Could not save book"); 108 return false; 109 } 108 110 } 111 fileAccess.save(null); 112 bookDocView.reopen(fileAccess); 113 109 114 } catch (IOException e) { 110 115 DialogUtils.showExceptionDialog(bookDocView.swingComponent().get(), 111 e, "Couldn't Save Book ");116 e, "Couldn't Save Book At The Given Location."); 112 117 return false; 113 } 118 } 114 119 return true; 115 120 } 121 116 122 } -
src/main/java/org/sophie2/main/func/servers/logic/ServersTabLogic.java
#P org.sophie2.main.func.servers
3 3 import java.awt.Component; 4 4 import java.io.File; 5 5 import java.io.IOException; 6 import java.util.ArrayList; 7 import java.util.List; 6 8 7 9 import org.sophie2.base.commons.util.Mapper; 8 10 import org.sophie2.base.connectivity.resources.DelegatingServerAccess; 9 11 import org.sophie2.base.connectivity.resources.ResourceDirectoryH; 10 12 import org.sophie2.base.dialogs.DialogInput; 11 13 import org.sophie2.base.dialogs.DialogManager; 14 import org.sophie2.base.layout.model.DocView; 12 15 import org.sophie2.base.menus.MenuItem; 13 16 import org.sophie2.base.model.resources.r4.LocationPrefix; 14 17 import org.sophie2.base.model.resources.r4.ResourceRefList; … … 27 30 import org.sophie2.core.mvc.SortKey; 28 31 import org.sophie2.core.mvc.events.EventR3; 29 32 import org.sophie2.main.app.commons.app.AppMainWindow; 33 import org.sophie2.main.app.commons.app.ResourceDocView; 30 34 import org.sophie2.main.app.commons.book.BookDocView; 31 35 import org.sophie2.main.app.commons.util.AppViewUtil; 32 36 import org.sophie2.main.dialogs.input.DialogUtils; … … 79 83 AccountH account = palette.selectedConnection().get().account().get(); 80 84 if (palette.isConnected(account)) { 81 85 if (palette.disconnect(account)) { 86 //Close all views to the server accesses that we just closed. 87 AppMainWindow mainWindow = AppViewUtil.findMainWindow(palette); 88 List<DocView> docViews = mainWindow.documents().get(); 89 List<DocView> toRemove = new ArrayList<DocView>(); 90 for (DocView docView : docViews) { 91 if (docView instanceof ResourceDocView) { 92 ResourceAccess access = ((ResourceDocView)docView).getAccess(); 93 if (access.isClosed()) { 94 if (mainWindow.currentDocument().get() == docView) { 95 mainWindow.currentDocument().set(null); 96 } 97 toRemove.add(docView); 98 } 99 } 100 } 101 mainWindow.documents().get().removeAll(toRemove); 82 102 DialogManager.get().showDialog( 83 103 new MessageDialogInput(null, 84 104 "Succesfully disconected.")); … … 203 223 int accountIndex = connectionsPalette.accounts().get().indexOf( 204 224 account); 205 225 connectionsPalette.accounts().remove(accountIndex); 226 AccountUtil.deleteAccount(account); 206 227 account.getAccess().close(); 207 AccountUtil.deleteAccount(account);208 228 } 209 229 } 210 230 return true; -
src/main/java/org/sophie2/main/app/menus/file/FileMenuLogic.java
#P org.sophie2.main.app.menus
194 194 195 195 AppMainWindow mainWindow = AppViewUtil.findMainWindow(bookDocView); 196 196 StoredResourceAccess fileAccess = (StoredResourceAccess) mainWindow.locator().get().create( 197 ResourceRefR4.make(file), AccessOptions.DEFAULT_ACCESS_OPTIONS ,197 ResourceRefR4.make(file), AccessOptions.DEFAULT_ACCESS_OPTIONS.modifyViewId(), 198 198 bookDocView.getAccess().getHead()); 199 199 200 200 try { -
src/main/java/org/sophie2/main/func/text/model/TailTextFrameH.java
#P org.sophie2.main.func.text
3 3 import org.sophie2.base.commons.util.ImmMap; 4 4 import org.sophie2.base.commons.util.ImmMap.ImmEntry; 5 5 import org.sophie2.base.model.resources.r4.ResourceRefR4; 6 import org.sophie2.base.model.resources.r4.access.AccessOptions;7 6 import org.sophie2.base.model.resources.r4.access.ResourceAccess; 8 7 import org.sophie2.base.model.resources.r4.resources.ResourceH; 9 8 import org.sophie2.main.app.commons.book.BookView; … … 44 43 assert bookView != null : "Cannot get the head helper in a null bookView."; 45 44 46 45 ResourceAccess headAccess = 47 bookView.getAccess().open(getHeadReference(bookView.getAccess().getRef()), AccessOptions.DEFAULT_ACCESS_OPTIONS);46 bookView.getAccess().open(getHeadReference(bookView.getAccess().getRef()), null); 48 47 49 48 return ResourceH.getHelper(headAccess, HeadTextFrameH.class); 50 49 } -
src/main/java/org/sophie2/main/func/text/model/HeadTextFrameH.java
72 72 * @return The {@link HotTextResourceH}. 73 73 */ 74 74 public HotTextResourceH getTextModel() { 75 ResourceAccess textAccess = 76 getAccess().open(getMainResource(), getAccess().getAccessOptions()); 75 ResourceAccess textAccess = getAccess().open(getMainResource(), null); 77 76 78 77 return ResourceH.getHelper(textAccess, HotTextResourceH.class); 79 78 } -
src/main/java/org/sophie2/base/model/resources/r4/access/MasterTopAccess.java
#P org.sophie2.base.model.resources.r4
6 6 7 7 import org.sophie2.base.model.resources.r4.LocationPrefix; 8 8 import org.sophie2.base.model.resources.r4.ResourceRefR4; 9 import org.sophie2.base.model.resources.r4.access.cacheKey.CacheKey; 9 10 import org.sophie2.base.model.resources.r4.access.cacheKey.DelegatingAccessCacheKey; 10 11 import org.sophie2.base.model.resources.r4.changes.Change; 11 12 import org.sophie2.base.model.resources.r4.changes.ChangeEffect; … … 32 33 */ 33 34 public abstract class MasterTopAccess extends BaseResourceAccess { 34 35 35 36 36 37 // Aspects used for tracking: 37 38 private Map<Key<?>, Aspect> readAspects = new HashMap<Key<?>, Aspect>(); 38 39 private Aspect headAspect = new Aspect(null); … … 41 42 * The head revision of this <code>ResourceAccess</code>. 42 43 */ 43 44 protected ResourceRevision headRevision; 44 45 45 46 /** 46 47 * A ResourceRefR4 used as a user for master accesses. The master user 47 48 * should not register changes since the {@link MasterTopAccess}es are not … … 49 50 */ 50 51 private static ResourceRefR4 masterUser = ResourceRefR4.make( 51 52 LocationPrefix.MEMORY + "master-user"); 52 53 53 54 /** 54 55 * The {@link AccessOptions} used by the {@link MasterTopAccess}es. 55 56 */ … … 60 61 * They are memorized by their relative ref to the master and accessOptions. 61 62 */ 62 63 protected final Map<DelegatingAccessCacheKey, DelegatingAccess> delegatingAccessesCache = 63 64 64 new HashMap<DelegatingAccessCacheKey, DelegatingAccess>(); 65 65 66 /** 66 67 * The root access that this master delegates operations for outer references. 67 68 */ … … 83 84 super(masterOptions, ref); 84 85 this.rootAccess = parent; 85 86 this.headRevision = (headRevision==null)? 86 87 LocalResourceRevision.getInitialRevision():headRevision; 87 88 } 88 89 89 90 public ResourceAccess open(ResourceRefR4 ref, AccessOptions options) { 90 91 91 92 ResourceAccess res; 92 93 assert (options != null): 93 94 "You can not use the master access options for delegating accesses"; … … 109 110 refToOpen = ResourceRefR4.getRelativeRef(getRef(), 110 111 access.getRef().append(ref.getThisChildRef())); 111 112 } 112 113 DelegatingAccessCacheKey key = new DelegatingAccessCacheKey(refToOpen, options); 113 114 DelegatingAccessCacheKey key = 115 new DelegatingAccessCacheKey(this.location.append(refToOpen), options); 114 116 res = this.delegatingAccessesCache.get(key); 115 117 if (res == null) { 116 118 DelegatingAccess innerAccess = createInner(refToOpen, options); … … 126 128 127 129 //gets the resource access that does not point to a redirect 128 130 private ResourceAccess getActualAccess(ResourceAccess access) { 129 131 130 132 if (RedirectR4.KIND.equals(ResourceR4.KEY_KIND.get(access))) { 131 133 ResourceRefR4 redirect = RedirectR4.KEY_TARGET.get(access); 132 134 133 135 if (!redirect.equals(ResourceRefR4.NONE_REF)) { 134 136 ResourceAccess res = access.open(redirect, null); 135 137 return res; … … 139 141 } 140 142 141 143 public void close() { 144 closeAccess(); 142 145 // FIXME This should not be done this way. 143 146 BaseLocator locator = (BaseLocator)this.rootAccess; 144 locator.topAccessesCache.remove(locator.getCacheKey(getRef(), getAccessOptions())); 147 CacheKey key = locator.getCacheKey(getRef(), getAccessOptions()); 148 assert (locator.topAccessesCache.remove(key) != null) : 149 "The master access should be in the accessesCache."; 145 150 } 146 151 147 152 public <T> T getRaw(Key<T> key) { 148 153 if (!this.readAspects.containsKey(key)) { 149 154 this.readAspects.put(key, new Aspect(null)); … … 164 169 } 165 170 166 171 public void registerChange(Change change) { 172 checkState(); 167 173 registerChange(change, null); 168 174 } 169 175 … … 219 225 assert oldRevision.getId().equals(revision.getId()); 220 226 221 227 } 228 222 229 public List<Change> syncFromSlave(RevisionId lastSync, 223 230 List<Change> slaveChanges) { 224 231 throw new UnsupportedOperationException("This is not implemented yet."); 225 232 } 226 233 227 234 /** 228 235 * Factory method for creation of delegating accesses. 229 236 * … … 235 242 * Inner access to the pointed sub resource. 236 243 */ 237 244 protected abstract DelegatingAccess createInner(ResourceRefR4 innerRef, 238 AccessOptions options); 245 AccessOptions options); 246 247 public boolean isClosed() { 248 return this.closed; 249 } 239 250 } -
src/main/java/org/sophie2/base/model/resources/r4/access/DelegatingAccess.java
16 16 * their own revisions and model and simply delegate all operations to the 17 17 * {@link MasterTopAccess} that created them. 18 18 * 19 * @author m eddle, mira19 * @author mira 20 20 */ 21 21 public abstract class DelegatingAccess extends BaseResourceAccess { 22 22 … … 24 24 * The parent access that this inner delegates to. 25 25 */ 26 26 protected final MasterTopAccess masterAccess; 27 27 28 28 /** 29 29 * Default constructor with parameters. 30 30 * … … 40 40 super(options, location); 41 41 this.masterAccess = parentAccess; 42 42 } 43 43 44 44 public ResourceAccess open(ResourceRefR4 ref, AccessOptions options) { 45 45 AccessOptions optionsToSet = (options == null) ? getAccessOptions() : options; 46 46 ResourceRefR4 toOpen = ref; … … 48 48 toOpen = this.location.append(ref); 49 49 } 50 50 SophieLog.trace("Reference to open : " + toOpen.getLocation()); 51 51 52 52 return this.masterAccess.open(toOpen, optionsToSet); 53 53 } 54 54 55 55 public void close() { 56 DelegatingAccessCacheKey key = 57 new DelegatingAccessCacheKey(this.location, this.getAccessOptions()); 58 this.masterAccess.delegatingAccessesCache.remove(key); 59 if (this.masterAccess.delegatingAccessesCache.isEmpty()) { 60 this.masterAccess.close(); 56 ResourceRefR4 ref = this.masterAccess.getRef().append(this.location); 57 DelegatingAccessCacheKey key = new DelegatingAccessCacheKey(ref, this.getAccessOptions()); 58 this.masterAccess.delegatingAccessesCache.get(key).closeAccess(); 59 60 for (DelegatingAccess access : this.masterAccess.delegatingAccessesCache.values()) { 61 if (!access.isClosed()) { 62 //There are unclosed delegating accesses 63 return; 64 } 61 65 } 66 this.masterAccess.close(); 67 } 68 69 public boolean isClosed() { 70 if (this.closed) { 71 return true; 72 } 62 73 74 if (ResourceRefR4.CURRENT_REF.equals(this.location)) { 75 return this.masterAccess.isClosed(); 76 } 77 BaseResourceAccess parent = (BaseResourceAccess) open(ResourceRefR4.PARENT_REF, null); 78 return parent.isClosed(); 63 79 } 64 80 65 81 public List<Change> syncFromSlave(RevisionId lastSync, … … 67 83 // TODO Auto-generated method stub 68 84 return null; 69 85 } 70 86 71 87 public <T> T getRaw(Key<T> key) { 72 88 // security 73 89 return this.masterAccess.getRaw(ResourceR4.KEY_CHILDREN.sub(this.location).sub(key)); 74 90 } 75 91 76 92 public void registerChange(Change change) { 77 93 // security 94 checkState(); 78 95 this.masterAccess.registerChange(change); 79 96 } 80 97 81 98 public ResourceRevision getHead() { 82 99 return this.masterAccess.getHead(); 83 100 } 84 101 85 102 @Override 86 103 public final ResourceRefR4 getRef() { 87 104 return this.masterAccess.getRef().sub(this.location); -
src/main/java/org/sophie2/base/model/resources/r4/access/ResourceAccess.java
58 58 */ 59 59 public List<Change> syncFromSlave(RevisionId lastSync, 60 60 List<Change> slaveChanges); 61 62 /** 63 * Checks the state of the {@link ResourceAccess}. If the access if closed 64 * the resource it holds can not be modified in any way through this access. 65 * 66 * @return 67 * true if the access if closed, false if it is open. 68 */ 69 public boolean isClosed(); 61 70 } -
src/main/java/org/sophie2/base/model/resources/r4/access/cacheKey/DelegatingAccessCacheKey.java
70 70 return true; 71 71 } 72 72 73 /** 74 * Checks is this key is a sub key of the given one. Sub key is key with the same 75 * {@link AccessOptions} and inner ref of the given one. 76 * 77 * @param key 78 * The parent key. 79 * @return 80 * True if the given key is parent to this one, false otherwise. 81 */ 82 public boolean isSubKey(DelegatingAccessCacheKey key) { 83 if (this.accessOptions.equals(key.accessOptions) 84 && this.ref.getLocation().startsWith(key.ref.getLocation())) { 85 return true; 86 } 87 return false; 88 89 } 73 90 } -
src/main/java/org/sophie2/base/model/resources/r4/access/BaseResourceAccess.java
16 16 * @author meddle, mira 17 17 */ 18 18 public abstract class BaseResourceAccess implements ResourceAccess { 19 19 20 /** 21 * Flag indicating if this access is closed. 22 */ 23 protected boolean closed; 24 20 25 private final AccessOptions accessOptions; 21 26 /** 22 27 * The {@link ResourceRefR4} pointing to the resource kept by this access. … … 35 40 * The {@link ResourceRefR4} for this access. 36 41 */ 37 42 public BaseResourceAccess(AccessOptions options, ResourceRefR4 location) { 43 this.closed = false; 38 44 this.location = location; 39 45 this.accessOptions = options; 40 46 } … … 55 61 * @return A {@link LocalResourceRevision} which is cloned by the current one. 56 62 */ 57 63 public ResourceRevision cloneHeadRevision() { 64 checkState(); 58 65 ResourceRevision headRevision = getHead(); 59 66 60 67 final ResourceModel copyModel; … … 70 77 } 71 78 return LocalResourceRevision.getInitialRevision(copyModel); 72 79 } 80 81 /** 82 * Closes this access. 83 */ 84 public void closeAccess() { 85 this.closed = true; 86 } 87 88 /** 89 * Checks if this access is closed. This would mean that its resource is locked for changes. 90 */ 91 protected void checkState(){ 92 if (isClosed()) { 93 throw new IllegalStateException("This access is already closed. You should " + 94 "not change resource through it."); 95 } 96 } 97 73 98 } -
src/main/java/org/sophie2/base/model/resources/r4/access/BaseLocator.java
7 7 import org.sophie2.base.model.resources.r4.ResourceRefR4; 8 8 import org.sophie2.base.model.resources.r4.access.cacheKey.CacheKey; 9 9 import org.sophie2.base.model.resources.r4.model.ResourceRevision; 10 import org.sophie2.core.logging.SophieLog; 10 11 11 12 /** 12 13 * Base implementation of the {@link ResourceLocator} interface. … … 44 45 public ResourceAccess create(ResourceRefR4 target, 45 46 AccessOptions options, ResourceRevision head) { 46 47 CacheKey cacheKey = getCacheKey(target, options); 47 assert (!this.topAccessesCache.containsKey(cacheKey)) : 48 "This resource is already created - " + target; 48 if (this.topAccessesCache.containsKey(cacheKey)) { 49 SophieLog.debug("The resource " + target + " is already created"); 50 return null; 51 } 49 52 MasterTopAccess masterAccess = createTop(target, options, head); 50 53 this.topAccessesCache.put(cacheKey, masterAccess); 51 54 return masterAccess.open(ResourceRefR4.CURRENT_REF, options); 52 55 } 53 56 54 57 public void close() { 55 throw new RuntimeException("You should not close the RootAccess!"); 58 for (MasterTopAccess topAccess : this.topAccessesCache.values()) { 59 topAccess.close(); 60 } 56 61 } 57 62 58 63 public AccessOptions getAccessOptions() { -
src/main/java/org/sophie2/dev/author/FakeAuthorMain.java
#P org.sophie2.dev
101 101 //SophieLog.setMinLevel("org.sophie2.base.layout", LogLevel.DEBUG); 102 102 //SophieLog.setMinLevel("org.sophie2.base.model.resources.r4", LogLevel.DEBUG); 103 103 //SophieLog.setMinLevel("org.sophie2.base.model.resources.r4.access", LogLevel.INFO); 104 SophieLog.setMinLevel("org.sophie2.main.media", LogLevel.ALL);105 SophieLog.setMinLevel(" org.sophie2.main.media.natlib.output", LogLevel.NONE);104 //SophieLog.setMinLevel("org.sophie2.main.media", LogLevel.ALL); 105 SophieLog.setMinLevel("", LogLevel.NONE); 106 106 //SophieLog.setMinLevel("org.sophie2.main.func.timelines", LogLevel.DEBUG); 107 107 //SophieLog.setMinLevel("org.sophie2.main.media.natlib.output.JavaSoundOutputBridge", LogLevel.TRACE); 108 108 //SophieLog.setMinLevel("org.sophie2.main.media.natlib.input.JavaSoundInputBridge", LogLevel.TRACE);