001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.tide.data;
022    
023    import java.util.ArrayList;
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    import java.util.Map.Entry;
028    
029    import javax.servlet.http.HttpSession;
030    
031    import org.granite.clustering.GraniteDistributedData;
032    import org.granite.clustering.GraniteDistributedDataFactory;
033    import org.granite.context.GraniteContext;
034    import org.granite.logging.Logger;
035    import org.granite.messaging.webapp.HttpGraniteContext;
036    
037    import flex.messaging.messages.AsyncMessage;
038    
039    
040    /**
041     *  Base implementation for data update dispatchers.
042     *  It should be built at beginning of each request during initialization of <code>DataContext</code>.
043     *  The dispatch is a three step process :
044     * 
045     *      <ul>
046     *  <li>Initialization in the constructor</li>
047     *  <li><code>observe()</code> builds the server selector depending on the data that are processed</li>
048     *  <li><code>publish()</code> handles the actual publishing</li>
049     *  </ul>
050     *  
051     *  Actual implementations should only override <code>changeDataSelector</code> and <code>publishUpdate</code>/
052     * 
053     *  @see DataDispatcher
054     *  @see DataContext
055     * 
056     *  @author William Drai
057     */
058    public abstract class AbstractDataDispatcher implements DataDispatcher {
059        
060        private static final String TIDE_DATA_SELECTORS_KEY_PREFIX = "org.granite.tide.dataSelectors.";
061    
062    
063            private static final Logger log = Logger.getLogger(AbstractDataDispatcher.class);
064    
065        
066        protected boolean enabled;
067        protected String topicName = null;
068        protected DataTopicParams paramsProvider = null;
069        protected String sessionId = null;
070        protected String clientId = null;
071        protected String subscriptionId = null;
072        
073        
074            public AbstractDataDispatcher(String topicName, Class<? extends DataTopicParams> dataTopicParamsClass) {
075                    this.topicName = topicName;
076                    
077                    try {
078                            paramsProvider = dataTopicParamsClass.newInstance();
079                    }
080                    catch (Exception e) {
081                            log.error("Could not instantiate class " + dataTopicParamsClass, e);
082                    }
083            }
084            
085            
086            public void observe() {
087                    // Prepare the selector even if we are not yet subscribed
088                    DataObserveParams params = null;
089                    if (paramsProvider != null) {
090                            // Collect selector parameters from component
091                            params = new DataObserveParams();
092                            paramsProvider.observes(params);
093                    }
094                    
095                    
096                    // Ensure that the current Gravity consumer listens about this data topic and params
097                    GraniteContext graniteContext = GraniteContext.getCurrentInstance();
098                    if (!(graniteContext instanceof HttpGraniteContext))
099                            return;
100                    
101                    GraniteDistributedData gdd = GraniteDistributedDataFactory.getInstance();
102                    HttpSession session = ((HttpGraniteContext)graniteContext).getSession(false);
103                    
104                    @SuppressWarnings("unchecked")
105                    List<DataObserveParams> selectors = (List<DataObserveParams>)session.getAttribute(TIDE_DATA_SELECTORS_KEY_PREFIX + topicName);
106                    if (selectors == null) {
107                            selectors = new ArrayList<DataObserveParams>();
108                            session.setAttribute(TIDE_DATA_SELECTORS_KEY_PREFIX + topicName, selectors);
109                    }
110                    
111                    boolean dataSelectorChanged = false;
112                    String dataSelector = gdd.getDestinationSelector(topicName);
113                    if (params != null) {
114                            String newDataSelector = params.updateDataSelector(dataSelector, selectors);
115                            dataSelectorChanged = !newDataSelector.equals(dataSelector);
116                            if (dataSelectorChanged) {
117                                    gdd.setDestinationSelector(topicName, newDataSelector);
118                                    dataSelector = newDataSelector;
119                            }
120                    }
121                    
122                    if (!enabled)
123                            return;
124                    
125                    if (dataSelectorChanged)
126                            changeDataSelector(dataSelector);
127            }
128            
129            protected abstract void changeDataSelector(String dataSelector);
130            
131            
132            public void publish(Object[][] dataUpdates) {
133                    if (!enabled)
134                            return;
135                    
136                    try {
137                            Map<Map<String, String>, List<Object>> updates = new HashMap<Map<String, String>, List<Object>>();
138                            if (paramsProvider != null) {
139                                    for (Object[] dataUpdate : dataUpdates) {
140                                            DataPublishParams params = new DataPublishParams();
141                                            paramsProvider.publishes(params, dataUpdate[1]);
142                                            
143                                            Map<String, String> headers = params.getHeaders();
144                                            List<Object> list = updates.get(headers);
145                                            if (list == null) {
146                                                    list = new ArrayList<Object>();
147                                                    updates.put(headers, list);
148                                            }
149                                            list.add(dataUpdate);
150                                    }
151                            }
152                            
153                            for (Entry<Map<String, String>, List<Object>> me : updates.entrySet()) {
154                                    Map<String, String> headers = new HashMap<String, String>(me.getKey());
155                                    headers.put(AsyncMessage.SUBTOPIC_HEADER, TIDE_DATA_SUBTOPIC);
156                                    headers.put(GDS_SESSION_ID, sessionId);
157                                    headers.put(TIDE_DATA_TYPE_KEY, TIDE_DATA_TYPE_VALUE);
158                                    publishUpdate(headers, me.getValue().toArray());
159                            }
160                    }
161                    catch (Exception e) {
162                            log.error(e, "Could not publish data update on topic %s", topicName);
163                    }
164            }
165            
166            protected abstract void publishUpdate(Map<String, String> params, Object body);
167    }