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.gravity.config;
022    
023    import java.util.ArrayList;
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    import org.granite.config.AbstractFrameworkGraniteConfig;
029    import org.granite.config.flex.Adapter;
030    import org.granite.config.flex.Channel;
031    import org.granite.config.flex.Destination;
032    import org.granite.config.flex.EndPoint;
033    import org.granite.config.flex.Service;
034    import org.granite.config.flex.ServicesConfig;
035    import org.granite.logging.Logger;
036    import org.granite.util.XMap;
037    
038    
039    public class AbstractMessagingDestination {
040            
041        private static final Logger log = Logger.getLogger(AbstractMessagingDestination.class);
042    
043    
044        ///////////////////////////////////////////////////////////////////////////
045        // Instance fields.
046       
047        private String id = null;
048        private List<String> roles = null;
049        private boolean noLocal = false;
050        private boolean sessionSelector = false;
051        
052    
053        public String getId() {
054                    return id;
055            }
056    
057            public void setId(String id) {
058                    this.id = id;
059            }
060            
061            public List<String> getRoles() {
062                    return roles;
063            }
064            public void setRoles(List<String> roles) {
065                    this.roles = roles;
066            }
067    
068            public boolean isNoLocal() {
069                    return noLocal;
070            }
071    
072            public void setNoLocal(boolean noLocal) {
073                    this.noLocal = noLocal;
074            }
075    
076            public boolean isSessionSelector() {
077                    return sessionSelector;
078            }
079    
080            public void setSessionSelector(boolean sessionSelector) {
081                    this.sessionSelector = sessionSelector;
082            }
083    
084            
085        protected void init(AbstractFrameworkGraniteConfig graniteConfig) {
086            ServicesConfig servicesConfig = graniteConfig.getServicesConfig();
087            initServices(servicesConfig);
088        }
089        
090        public void initServices(ServicesConfig servicesConfig) {
091            Channel channel = servicesConfig.findChannelById("gravityamf");
092            if (channel == null) {
093                    channel = new Channel("gravityamf", "org.granite.gravity.channels.GravityChannel",
094                                    new EndPoint("http://{server.name}:{server.port}/{context.root}/gravityamf/amf", "flex.messaging.endpoints.AMFEndpoint"),
095                                    new XMap());
096                    servicesConfig.addChannel(channel);
097            }
098            
099            List<Service> services = servicesConfig.findServicesByMessageType("flex.messaging.messages.AsyncMessage");
100            Service service = null;
101            Adapter adapter = null;
102            if (services == null || services.isEmpty()) {
103                    adapter = buildAdapter();
104                    Map<String, Adapter> adapters = new HashMap<String, Adapter>();
105                    adapters.put(adapter.getId(), adapter);
106                    service = new Service("gravity-service", "flex.messaging.services.MessagingService", "flex.messaging.messages.AsyncMessage", 
107                                    adapter, adapters, new HashMap<String, Destination>());
108                    servicesConfig.addService(service);
109            }
110            else {
111                    service = services.get(0);
112                    Adapter ad = buildAdapter();
113                            adapter = service.findAdapterById(ad.getId());
114                            if (adapter == null) {
115                                    adapter = ad;
116                                    service.addAdapter(adapter);
117                            }
118            }
119            
120            service.getDestinations().put(id, buildDestination(adapter));
121            
122            log.info("Registered messaging destination %s", id);
123        }
124            
125            protected Adapter buildAdapter() {
126                    return new Adapter("simple-adapter", "org.granite.gravity.adapters.SimpleServiceAdapter", new XMap());
127            }
128            
129            protected Destination buildDestination(Adapter adapter) {
130            List<String> channelIds = new ArrayList<String>();
131            channelIds.add("gravityamf");
132            Destination destination = new Destination(id, channelIds, new XMap(), roles, adapter, null);
133            destination.getProperties().put("no-local", String.valueOf(noLocal));
134            destination.getProperties().put("session-selector", String.valueOf(sessionSelector));
135            return destination;
136            }
137    }