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