aboutsummaryrefslogtreecommitdiff
path: root/subsonic-main/src/main/java/net/sourceforge/subsonic/ajax/ChatService.java
blob: 8905c8a68ec9a61e171057d305248b567ec6a2c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2009 (C) Sindre Mehus
 */
package net.sourceforge.subsonic.ajax;

import net.sourceforge.subsonic.Logger;
import net.sourceforge.subsonic.service.SecurityService;
import net.sourceforge.subsonic.util.BoundedList;
import org.apache.commons.lang.StringUtils;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Provides AJAX-enabled services for the chatting.
 * This class is used by the DWR framework (http://getahead.ltd.uk/dwr/).
 *
 * @author Sindre Mehus
 */
public class ChatService {

    private static final Logger LOG = Logger.getLogger(ChatService.class);
    private static final String CACHE_KEY = "1";
    private static final int MAX_MESSAGES = 10;
    private static final long TTL_MILLIS = 3L * 24L * 60L * 60L * 1000L; // 3 days.

    private final LinkedList<Message> messages = new BoundedList<Message>(MAX_MESSAGES);
    private SecurityService securityService;

    private long revision = System.identityHashCode(this);

    /**
     * Invoked by Spring.
     */
    public void init() {
        // Delete old messages every hour.
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        Runnable runnable = new Runnable() {
            public void run() {
                removeOldMessages();
            }
        };
        executor.scheduleWithFixedDelay(runnable, 0L, 3600L, TimeUnit.SECONDS);
    }

    private synchronized void removeOldMessages() {
        long now = System.currentTimeMillis();
        for (Iterator<Message> iterator = messages.iterator(); iterator.hasNext();) {
            Message message = iterator.next();
            if (now - message.getDate().getTime() > TTL_MILLIS) {
                iterator.remove();
                revision++;
            }
        }
    }

    public synchronized void addMessage(String message) {
        WebContext webContext = WebContextFactory.get();
        doAddMessage(message, webContext.getHttpServletRequest());
    }

    public synchronized void doAddMessage(String message, HttpServletRequest request) {

        String user = securityService.getCurrentUsername(request);
        message = StringUtils.trimToNull(message);
        if (message != null && user != null) {
            messages.addFirst(new Message(message, user, new Date()));
            revision++;
        }
    }

    public synchronized void clearMessages() {
        messages.clear();
        revision++;
    }

    /**
     * Returns all messages, but only if the given revision is different from the
     * current revision.
     */
    public synchronized Messages getMessages(long revision) {
        if (this.revision != revision) {
            return new Messages(new ArrayList<Message>(messages), this.revision);
        }
        return null;
    }

    public void setSecurityService(SecurityService securityService) {
        this.securityService = securityService;
    }

    public static class Messages implements Serializable {

        private static final long serialVersionUID = -752602719879818165L;
        private final  List<Message> messages;
        private final long revision;

        public Messages(List<Message> messages, long revision) {
            this.messages = messages;
            this.revision = revision;
        }

        public List<Message> getMessages() {
            return messages;
        }

        public long getRevision() {
            return revision;
        }
    }

    public static class Message implements Serializable {

        private static final long serialVersionUID = -1907101191518133712L;
        private final String content;
        private final String username;
        private final Date date;

        public Message(String content, String username, Date date) {
            this.content = content;
            this.username = username;
            this.date = date;
        }

        public String getContent() {
            return content;
        }

        public String getUsername() {
            return username;
        }

        public Date getDate() {
            return date;
        }

    }
}