]> git.argeo.org Git - gpl/argeo-slc.git/blob - ext/javax.mail.mbox/src/com/sun/mail/remote/RemoteStore.java
Make logging synchronous during native image build
[gpl/argeo-slc.git] / ext / javax.mail.mbox / src / com / sun / mail / remote / RemoteStore.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.remote;
18
19 import java.io.*;
20 import javax.mail.*;
21 import com.sun.mail.mbox.*;
22
23 /**
24 * A wrapper around a local <code>MboxStore</code> that fetches data
25 * from the Inbox in a remote store and adds it to our local Inbox.
26 */
27 public abstract class RemoteStore extends MboxStore {
28
29 protected Store remoteStore;
30 protected Folder remoteInbox;
31 protected Folder inbox;
32 protected String host, user, password;
33 protected int port;
34 protected long lastUpdate = 0;
35
36 public RemoteStore(Session session, URLName url) {
37 super(session, url);
38 remoteStore = getRemoteStore(session, url);
39 }
40
41 /**
42 * Subclasses override this method to return the appropriate
43 * <code>Store</code> object. This method will be called by
44 * the <code>RemoteStore</code> constructor.
45 */
46 protected abstract Store getRemoteStore(Session session, URLName url);
47
48 /**
49 * Connect to the store.
50 */
51 public void connect(String host, int port, String user, String password)
52 throws MessagingException {
53 this.host = host;
54 this.port = port;
55 this.user = user;
56 this.password = password;
57 updateInbox();
58 }
59
60 /**
61 * Fetch any new mail in the remote INBOX and add it to the local INBOX.
62 */
63 protected void updateInbox() throws MessagingException {
64 // is it time to do an update yet?
65 // XXX - polling frequency, rules, etc. should be in properties
66 if (System.currentTimeMillis() < lastUpdate + (5 * 1000))
67 return;
68 try {
69 /*
70 * Connect to the remote store, using the saved
71 * authentication information.
72 */
73 remoteStore.connect(host, port, user, password);
74
75 /*
76 * If this store isn't connected yet, do it now, because
77 * it needs to be connected to get the INBOX folder.
78 */
79 if (!isConnected())
80 super.connect(host, port, user, password);
81 if (remoteInbox == null)
82 remoteInbox = remoteStore.getFolder("INBOX");
83 if (inbox == null)
84 inbox = getFolder("INBOX");
85 remoteInbox.open(Folder.READ_WRITE);
86 Message[] msgs = remoteInbox.getMessages();
87 inbox.appendMessages(msgs);
88 remoteInbox.setFlags(msgs, new Flags(Flags.Flag.DELETED), true);
89 remoteInbox.close(true);
90 remoteStore.close();
91 } catch (MessagingException ex) {
92 try {
93 if (remoteInbox != null && remoteInbox.isOpen())
94 remoteInbox.close(false);
95 } finally {
96 if (remoteStore != null && remoteStore.isConnected())
97 remoteStore.close();
98 }
99 throw ex;
100 }
101 }
102
103 public Folder getDefaultFolder() throws MessagingException {
104 checkConnected();
105
106 return new RemoteDefaultFolder(this, null);
107 }
108
109 public Folder getFolder(String name) throws MessagingException {
110 checkConnected();
111
112 if (name.equalsIgnoreCase("INBOX"))
113 return new RemoteInbox(this, name);
114 else
115 return super.getFolder(name);
116 }
117
118 public Folder getFolder(URLName url) throws MessagingException {
119 checkConnected();
120 return getFolder(url.getFile());
121 }
122
123 private void checkConnected() throws MessagingException {
124 if (!isConnected())
125 throw new MessagingException("Not connected");
126 }
127 }