001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.config;
023
024import java.util.ArrayList;
025import java.util.HashMap;
026import java.util.List;
027
028import org.granite.config.flex.Channel;
029import org.granite.config.flex.Destination;
030import org.granite.config.flex.EndPoint;
031import org.granite.config.flex.Service;
032import org.granite.config.flex.ServicesConfig;
033import org.granite.logging.Logger;
034import org.granite.messaging.service.security.RemotingDestinationSecurizer;
035import org.granite.util.XMap;
036
037
038public class AbstractRemoteDestination {
039        
040    private static final Logger log = Logger.getLogger(AbstractRemoteDestination.class);
041
042
043    ///////////////////////////////////////////////////////////////////////////
044    // Instance fields.
045   
046    private String id = null;
047    private String source = null;
048    private RemotingDestinationSecurizer securizer = null;
049    private List<String> roles = null;
050    
051    
052    public String getId() {
053                return id;
054        }
055
056        public void setId(String id) {
057                this.id = id;
058        }
059
060    public String getSource() {
061                return source;
062        }
063
064        public void setSource(String source) {
065                this.source = source;
066        }
067        
068        public RemotingDestinationSecurizer getSecurizer() {
069                return securizer;
070        }
071
072        public void setSecurizer(RemotingDestinationSecurizer securizer) {
073                this.securizer = securizer;
074        }
075
076        public List<String> getRoles() {
077                return roles;
078        }
079        public void setRoles(List<String> roles) {
080                this.roles = roles;
081        }
082
083        
084    protected void init(AbstractFrameworkGraniteConfig graniteConfig) {
085        ServicesConfig servicesConfig = graniteConfig.getServicesConfig();
086        initServices(servicesConfig);
087    }
088    
089    public void initServices(ServicesConfig servicesConfig) {
090        Channel channel = servicesConfig.findChannelById("graniteamf");
091        if (channel == null) {
092                channel = new Channel("graniteamf", "mx.messaging.channels.AMFChannel",
093                                new EndPoint("http://{server.name}:{server.port}/{context.root}/graniteamf/amf", "flex.messaging.endpoints.AMFEndpoint"),
094                                new XMap());
095                servicesConfig.addChannel(channel);
096        }
097        
098        List<Service> services = servicesConfig.findServicesByMessageType("flex.messaging.messages.RemotingMessage");
099        Service service = null;
100        if (services == null || services.isEmpty()) {
101                service = new Service("granite-service", "flex.messaging.services.RemotingService", "flex.messaging.messages.RemotingMessage", 
102                                null, null, new HashMap<String, Destination>());
103                servicesConfig.addService(service);
104        }
105        else
106                service = services.get(0);
107        
108        service.getDestinations().put(source, buildDestination());
109        
110        log.info("Registered remote destination %s", source);
111    }
112        
113        protected Destination buildDestination() {
114        List<String> channelIds = new ArrayList<String>();
115        channelIds.add("graniteamf");
116        Destination destination = new Destination(source, channelIds, new XMap(), roles, null, null);
117        if (securizer != null)
118                destination.setSecurizer(securizer);
119        return destination;
120        }
121}