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.flex;
022
023import java.io.Serializable;
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Map;
027
028import org.granite.messaging.service.security.DestinationSecurizer;
029import org.granite.util.TypeUtil;
030import org.granite.util.XMap;
031
032/**
033 * @author Franck WOLFF
034 */
035public class Destination implements Serializable {
036
037    private static final long serialVersionUID = 1L;
038
039    private static final String SECURIZER_PROPERTY_KEY = "securizer";
040
041    private final String id;
042    private final List<String> channelRefs;
043    private final XMap properties;
044    private final List<String> roles;
045    private final Adapter adapter;
046    private final Class<?> scannedClass;
047    private DestinationSecurizer securizer;
048    
049    private DestinationRemoveListener removeListener;
050
051    
052    public Destination(String id, List<String> channelRefs, XMap properties, List<String> roles, Adapter adapter, Class<?> scannedClass) {
053        this.id = id;
054        this.channelRefs = new ArrayList<String>(channelRefs);
055        this.properties = properties;
056        this.roles = (roles != null ? new ArrayList<String>(roles) : null);
057        this.adapter = adapter;
058        this.scannedClass = scannedClass;
059
060        final String securizerClassName = properties.get(SECURIZER_PROPERTY_KEY);
061        if (securizerClassName != null)  {
062            try {
063                this.securizer = TypeUtil.newInstance(securizerClassName.trim(), DestinationSecurizer.class);
064            } catch (Exception e) {
065                throw new RuntimeException("Could not instantiate securizer: " + securizerClassName, e);
066            }
067        } else
068            this.securizer = null;
069    }
070    
071    public void addRemoveListener(DestinationRemoveListener listener) {
072        this.removeListener = listener;
073    }
074    
075    public void remove() {
076        if (removeListener != null)
077                removeListener.destinationRemoved(this);
078    }
079    
080
081    public String getId() {
082        return id;
083    }
084
085    public List<String> getChannelRefs() {
086        return channelRefs;
087    }
088
089    public XMap getProperties() {
090        return properties;
091    }
092
093    public boolean isSecured() {
094        return roles != null;
095    }
096
097    public List<String> getRoles() {
098        return roles;
099    }
100
101    public Adapter getAdapter() {
102        return adapter;
103    }
104
105    public Class<?> getScannedClass() {
106                return scannedClass;
107        }
108
109        public DestinationSecurizer getSecurizer() {
110        return securizer;
111    }
112        
113        public void setSecurizer(DestinationSecurizer securizer) {
114                this.securizer = securizer;
115        }
116
117
118    public static Destination forElement(XMap element, Adapter defaultAdapter, Map<String, Adapter> adaptersMap) {
119        String id = element.get("@id");
120
121        List<String> channelRefs = new ArrayList<String>();
122        for (XMap channel : element.getAll("channels/channel[@ref]"))
123            channelRefs.add(channel.get("@ref"));
124
125        XMap properties = new XMap(element.getOne("properties"));
126
127        List<String> rolesList = null;
128        if (element.containsKey("security/security-constraint/roles/role")) {
129            rolesList = new ArrayList<String>();
130            for (XMap role : element.getAll("security/security-constraint/roles/role"))
131                rolesList.add(role.get("."));
132        }
133
134        XMap adapter = element.getOne("adapter[@ref]");
135        Adapter adapterRef = adapter != null && adaptersMap != null
136            ? adaptersMap.get(adapter.get("@ref"))
137            : defaultAdapter;
138
139        return new Destination(id, channelRefs, properties, rolesList, adapterRef, null);
140    }
141}