Skip to contentPackage: DefaultMailFile
DefaultMailFile
Coverage
1: /*
2: * Copyright (c) 1997, 2023 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package org.eclipse.angus.mail.mbox;
18:
19: import java.io.*;
20:
21: public class DefaultMailbox extends Mailbox {
22: private final String home;
23:
24: private static final boolean homeRelative =
25:                                 Boolean.getBoolean("mail.mbox.homerelative");
26:
27: public DefaultMailbox() {
28:         home = System.getProperty("user.home");
29: }
30:
31: public MailFile getMailFile(String user, String folder) {
32:         return new DefaultMailFile(filename(user, folder));
33: }
34:
35: public String filename(String user, String folder) {
36:         try {
37:          char c = folder.charAt(0);
38:          if (c == File.separatorChar) {
39:                 return folder;
40:          } else if (c == '~') {
41:                 int i = folder.indexOf(File.separatorChar);
42:                 String tail = "";
43:                 if (i > 0) {
44:                  tail = folder.substring(i);
45:                  folder = folder.substring(0, i);
46:                 }
47:                 return home + tail;
48:          } else {
49:                 if (folder.equalsIgnoreCase("INBOX"))
50:                  folder = "INBOX";
51:                 if (homeRelative)
52:                  return home + File.separator + folder;
53:                 else
54:                  return folder;
55:          }
56:         } catch (StringIndexOutOfBoundsException e) {
57:          return folder;
58:         }
59: }
60: }
61:
62: class DefaultMailFile extends File implements MailFile {
63: protected transient RandomAccessFile file;
64:
65: private static final long serialVersionUID = 3713116697523761684L;
66:
67: DefaultMailFile(String name) {
68:         super(name);
69: }
70:
71: public boolean lock(String mode) {
72:         try {
73:          file = new RandomAccessFile(this, mode);
74:          return true;
75:         } catch (FileNotFoundException fe) {
76:          return false;
77:         } catch (IOException ie) {
78:          file = null;
79:          return false;
80:         }
81: }
82:
83: public void unlock() {
84:•        if (file != null) {
85:          try {
86:                 file.close();
87:          } catch (IOException e) {
88:                 // ignore it
89:          }
90:          file = null;
91:         }
92: }
93:
94: public void touchlock() {
95: }
96:
97: public FileDescriptor getFD() {
98:•        if (file == null)
99:          return null;
100:         try {
101:          return file.getFD();
102:         } catch (IOException e) {
103:          return null;
104:         }
105: }
106: }