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.config;
022    
023    import java.util.ArrayList;
024    import java.util.HashMap;
025    import java.util.List;
026    
027    import org.granite.config.flex.Channel;
028    import org.granite.config.flex.Destination;
029    import org.granite.config.flex.EndPoint;
030    import org.granite.config.flex.Service;
031    import org.granite.config.flex.ServicesConfig;
032    import org.granite.logging.Logger;
033    import org.granite.util.XMap;
034    
035    
036    public class AbstractRemoteDestination {
037            
038        private static final Logger log = Logger.getLogger(AbstractRemoteDestination.class);
039    
040    
041        ///////////////////////////////////////////////////////////////////////////
042        // Instance fields.
043       
044        private String id = null;
045        private String source = null;
046        private List<String> roles = null;
047        
048        
049        public String getId() {
050                    return id;
051            }
052    
053            public void setId(String id) {
054                    this.id = id;
055            }
056    
057        public String getSource() {
058                    return source;
059            }
060    
061            public void setSource(String source) {
062                    this.source = source;
063            }
064            
065            public List<String> getRoles() {
066                    return roles;
067            }
068            public void setRoles(List<String> roles) {
069                    this.roles = roles;
070            }
071    
072            
073        protected void init(AbstractFrameworkGraniteConfig graniteConfig) {
074            ServicesConfig servicesConfig = graniteConfig.getServicesConfig();
075            initServices(servicesConfig);
076        }
077        
078        public void initServices(ServicesConfig servicesConfig) {
079            Channel channel = servicesConfig.findChannelById("graniteamf");
080            if (channel == null) {
081                    channel = new Channel("graniteamf", "mx.messaging.channels.AMFChannel",
082                                    new EndPoint("http://{server.name}:{server.port}/{context.root}/graniteamf/amf", "flex.messaging.endpoints.AMFEndpoint"),
083                                    new XMap());
084                    servicesConfig.addChannel(channel);
085            }
086            
087            List<Service> services = servicesConfig.findServicesByMessageType("flex.messaging.messages.RemotingMessage");
088            Service service = null;
089            if (services == null || services.isEmpty()) {
090                    service = new Service("granite-service", "flex.messaging.services.RemotingService", "flex.messaging.messages.RemotingMessage", 
091                                    null, null, new HashMap<String, Destination>());
092                    servicesConfig.addService(service);
093            }
094            else
095                    service = services.get(0);
096            
097            service.getDestinations().put(source, buildDestination());
098            
099            log.info("Registered remote destination %s", source);
100        }
101            
102            protected Destination buildDestination() {
103            List<String> channelIds = new ArrayList<String>();
104            channelIds.add("graniteamf");
105            Destination destination = new Destination(source, channelIds, new XMap(), roles, null, null);
106            return destination;
107            }
108    }