]> git.argeo.org Git - gpl/argeo-slc.git/blob - ext/javax.mail.mbox/src/com/sun/mail/mbox/UNIXInbox.java
Make logging synchronous during native image build
[gpl/argeo-slc.git] / ext / javax.mail.mbox / src / com / sun / mail / mbox / UNIXInbox.java
1 /*
2 * Copyright (c) 1997, 2018 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 com.sun.mail.mbox;
18
19 import java.io.*;
20
21 public class UNIXInbox extends UNIXFolder implements InboxFile {
22 private final String user;
23
24 private static final long serialVersionUID = 651261842162777620L;
25
26 /*
27 * Superclass UNIXFile loads the library containing all the
28 * native code and sets the "loaded" flag if successful.
29 */
30
31 public UNIXInbox(String user, String name) {
32 super(name);
33 this.user = user;
34 if (user == null)
35 throw new NullPointerException("user name is null in UNIXInbox");
36 }
37
38 public boolean lock(String mode) {
39 if (lockType == NATIVE) {
40 if (!loaded)
41 return false;
42 if (!maillock(user, 5))
43 return false;
44 }
45 if (!super.lock(mode)) {
46 if (loaded)
47 mailunlock();
48 return false;
49 }
50 return true;
51 }
52
53 public void unlock() {
54 super.unlock();
55 if (loaded)
56 mailunlock();
57 }
58
59 public void touchlock() {
60 if (loaded)
61 touchlock0();
62 }
63
64 private transient RandomAccessFile lockfile; // the user's ~/.Maillock file
65 private transient String lockfileName; // its name
66
67 public boolean openLock(String mode) {
68 if (mode.equals("r"))
69 return true;
70 if (lockfileName == null) {
71 String home = System.getProperty("user.home");
72 lockfileName = home + File.separator + ".Maillock";
73 }
74 try {
75 lockfile = new RandomAccessFile(lockfileName, mode);
76 boolean ret;
77 switch (lockType) {
78 case NONE:
79 ret = true;
80 break;
81 case NATIVE:
82 default:
83 ret = UNIXFile.lock(lockfile.getFD(), mode);
84 break;
85 case JAVA:
86 ret = lockfile.getChannel().
87 tryLock(0L, Long.MAX_VALUE, !mode.equals("rw")) != null;
88 break;
89 }
90 if (!ret)
91 closeLock();
92 return ret;
93 } catch (IOException ex) {
94 }
95 return false;
96 }
97
98 public void closeLock() {
99 if (lockfile == null)
100 return;
101 try {
102 lockfile.close();
103 } catch (IOException ex) {
104 } finally {
105 lockfile = null;
106 }
107 }
108
109 public boolean equals(Object o) {
110 if (!(o instanceof UNIXInbox))
111 return false;
112 UNIXInbox other = (UNIXInbox)o;
113 return user.equals(other.user) && super.equals(other);
114 }
115
116 public int hashCode() {
117 return super.hashCode() + user.hashCode();
118 }
119
120 private native boolean maillock(String user, int retryCount);
121 private native void mailunlock();
122 private native void touchlock0();
123 }