1 package ch
.cyberduck
.core
;
4 * Copyright (c) 2002-2010 David Kocher. All rights reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * Bug fixes, suggestions and comments should be sent to:
19 * dkocher@cyberduck.ch
22 import ch
.cyberduck
.core
.exception
.AccessDeniedException
;
23 import ch
.cyberduck
.core
.serializer
.Reader
;
24 import ch
.cyberduck
.core
.serializer
.Writer
;
26 import org
.apache
.log4j
.Logger
;
31 public abstract class AbstractFolderHostCollection
extends AbstractHostCollection
{
32 private static final Logger log
= Logger
.getLogger(AbstractFolderHostCollection
.class);
34 private static final long serialVersionUID
= 6598370606581477494L;
36 private Writer
<Host
> writer
= HostWriterFactory
.get();
38 private Reader
<Host
> reader
= HostReaderFactory
.get();
40 protected Local folder
;
43 * Reading bookmarks from this folder
45 * @param f Parent directory to look for bookmarks
47 public AbstractFolderHostCollection(final Local f
) {
52 public String
getName() {
53 return LocaleFactory
.localizedString(folder
.getName());
57 * @param bookmark Bookmark
58 * @return File for bookmark
60 public Local
getFile(final Host bookmark
) {
61 return LocalFactory
.get(folder
, String
.format("%s.duck", bookmark
.getUuid()));
64 public Local
getFolder() {
69 public void collectionItemAdded(final Host bookmark
) {
71 super.collectionItemAdded(bookmark
);
75 public void collectionItemChanged(final Host bookmark
) {
77 super.collectionItemChanged(bookmark
);
81 public void collectionItemRemoved(final Host bookmark
) {
84 final Local file
= this.getFile(bookmark
);
87 catch(AccessDeniedException e
) {
88 log
.error(String
.format("Failure removing bookmark %s", e
.getMessage()));
92 super.collectionItemRemoved(bookmark
);
96 protected void save(final Host bookmark
) {
98 log
.debug(String
.format("Skip saving bookmark %s while loading", bookmark
));
104 final Local f
= this.getFile(bookmark
);
105 if(log
.isInfoEnabled()) {
106 log
.info(String
.format("Save bookmark %s", f
));
108 writer
.write(bookmark
, f
);
110 catch(AccessDeniedException e
) {
111 log
.warn(String
.format("Failure saving item in collection %s", e
.getMessage()));
120 public void load() throws AccessDeniedException
{
121 if(log
.isInfoEnabled()) {
122 log
.info(String
.format("Reloading %s", folder
.getAbsolute()));
127 final AttributedList
<Local
> bookmarks
= folder
.list().filter(
128 new Filter
<Local
>() {
130 public boolean accept(final Local file
) {
131 return file
.getName().endsWith(".duck");
135 for(Local next
: bookmarks
) {
136 final Host bookmark
= reader
.read(next
);
137 if(null == bookmark
) {
141 if(!this.getFile(bookmark
).equals(next
)) {
142 this.rename(next
, bookmark
);
147 // Sort using previously built index
156 protected void rename(final Local next
, final Host bookmark
) throws AccessDeniedException
{
157 // Rename all files previously saved with nickname to UUID.
158 next
.rename(this.getFile(bookmark
));
163 // Save individual bookmarks upon add but not collection itself.