Add session to cache with both hostname and address.
[cyberduck.git] / source / ch / cyberduck / core / AbstractFolderHostCollection.java
blobd6f502c3f48a4093018aef0460fc48a58d15c521
1 package ch.cyberduck.core;
3 /*
4 * Copyright (c) 2002-2010 David Kocher. All rights reserved.
6 * http://cyberduck.ch/
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;
28 /**
29 * @version $Id$
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;
42 /**
43 * Reading bookmarks from this folder
45 * @param f Parent directory to look for bookmarks
47 public AbstractFolderHostCollection(final Local f) {
48 this.folder = f;
51 @Override
52 public String getName() {
53 return LocaleFactory.localizedString(folder.getName());
56 /**
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() {
65 return folder;
68 @Override
69 public void collectionItemAdded(final Host bookmark) {
70 this.save(bookmark);
71 super.collectionItemAdded(bookmark);
74 @Override
75 public void collectionItemChanged(final Host bookmark) {
76 this.save(bookmark);
77 super.collectionItemChanged(bookmark);
80 @Override
81 public void collectionItemRemoved(final Host bookmark) {
82 try {
83 this.lock();
84 final Local file = this.getFile(bookmark);
85 file.delete();
87 catch(AccessDeniedException e) {
88 log.error(String.format("Failure removing bookmark %s", e.getMessage()));
90 finally {
91 this.unlock();
92 super.collectionItemRemoved(bookmark);
96 protected void save(final Host bookmark) {
97 if(this.isLocked()) {
98 log.debug(String.format("Skip saving bookmark %s while loading", bookmark));
100 else {
101 this.lock();
102 try {
103 folder.mkdir();
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()));
113 finally {
114 this.unlock();
119 @Override
120 public void load() throws AccessDeniedException {
121 if(log.isInfoEnabled()) {
122 log.info(String.format("Reloading %s", folder.getAbsolute()));
124 this.lock();
125 try {
126 folder.mkdir();
127 final AttributedList<Local> bookmarks = folder.list().filter(
128 new Filter<Local>() {
129 @Override
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) {
138 continue;
140 // Legacy support.
141 if(!this.getFile(bookmark).equals(next)) {
142 this.rename(next, bookmark);
145 this.add(bookmark);
147 // Sort using previously built index
148 this.sort();
150 finally {
151 this.unlock();
153 super.load();
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));
161 @Override
162 public void save() {
163 // Save individual bookmarks upon add but not collection itself.