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