1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.mailrepository.javamail;
21
22 import javax.mail.Folder;
23 import javax.mail.MessagingException;
24
25
26
27
28
29
30
31
32
33 public class FolderGateKeeperImpl implements FolderGateKeeper {
34
35 private final FolderInterface folder;
36
37 private int inUse = 0;
38
39 boolean open = false;
40
41 private final StoreGateKeeper storeGateKeeper;
42
43 public FolderGateKeeperImpl(FolderInterface folder,StoreGateKeeper storeGateKeeper) {
44 if (folder.isOpen()) {
45 throw new IllegalStateException(
46 "FolderGateKeeper: initial state must be closed");
47 }
48 this.folder = folder;
49 this.storeGateKeeper=storeGateKeeper;
50 }
51
52
53
54
55 public synchronized void use() {
56 inUse++;
57 }
58
59
60
61
62 public synchronized void free() throws MessagingException {
63 if (inUse < 1) {
64 throw new IllegalStateException(
65 "called free() but FolderGateKeeper is not in use");
66 } else {
67 inUse--;
68 if (inUse == 0) {
69 if (open != folder.isOpen()) {
70 throw new IllegalStateException(
71 "free(): folder state not equals last known state: open="
72 + open);
73 }
74 if (open) {
75
76 folder.close(true);
77 open = false;
78 }
79 }
80 }
81
82 }
83
84
85
86
87 public synchronized FolderInterface getOpenFolder() throws MessagingException {
88 if ((folder.getType() & Folder.HOLDS_MESSAGES)==0) {
89 throw new RuntimeException("cannot open a Folder that does not hold messages");
90 }
91 if (inUse < 1) {
92 throw new IllegalStateException(
93 "called getFolder() but folder is not in use");
94 }
95 if (open != folder.isOpen()) {
96 throw new IllegalStateException(
97 "getFolder() folder state not equals last known state: open="
98 + open);
99 }
100 if (!open) {
101 folder.open(Folder.READ_WRITE);
102 open = true;
103 }
104 return folder;
105
106 }
107
108
109
110
111 public synchronized int getUseCount() {
112 return inUse;
113 }
114
115
116
117
118 public synchronized FolderInterface getFolder() {
119 if (inUse < 1) {
120 throw new IllegalStateException(
121 "called getFolder() but folder is not in use");
122 }
123 if (open != folder.isOpen()) {
124 throw new IllegalStateException(
125 "getFolder() folder state not equals last known state: open="
126 + open);
127 }
128 return folder;
129 }
130
131
132
133
134 public String getFullName() throws MessagingException {
135 use();
136 String fn=getFolder().getFullName();
137 free();
138 return fn;
139 }
140
141
142
143
144 public void create(int holds_folders) throws MessagingException {
145 use();
146 getFolder().create(holds_folders);
147 free();
148 }
149
150
151
152
153 public boolean exists() throws MessagingException {
154 use();
155 boolean e=getFolder().exists();
156 free();
157 return e;
158 }
159
160
161
162
163
164 public synchronized void renameTo(Folder destination) throws MessagingException {
165 if (inUse!=0) {
166 throw new IllegalStateException("cannot operate of folder that is in use");
167 } else {
168 folder.renameTo(destination);
169 }
170
171 }
172
173
174
175
176 public synchronized void renameTo(String newName) throws MessagingException {
177 storeGateKeeper.renameTo(this,newName);
178 }
179
180 }