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