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