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
021 package org.granite.config.flex;
022
023 import java.io.Serializable;
024 import java.util.ArrayList;
025 import java.util.List;
026 import java.util.Map;
027
028 import org.granite.messaging.service.security.DestinationSecurizer;
029 import org.granite.util.ClassUtil;
030 import org.granite.util.XMap;
031
032 /**
033 * @author Franck WOLFF
034 */
035 public 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 final 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 = ClassUtil.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
114 public static Destination forElement(XMap element, Adapter defaultAdapter, Map<String, Adapter> adaptersMap) {
115 String id = element.get("@id");
116
117 List<String> channelRefs = new ArrayList<String>();
118 for (XMap channel : element.getAll("channels/channel[@ref]"))
119 channelRefs.add(channel.get("@ref"));
120
121 XMap properties = new XMap(element.getOne("properties"));
122
123 List<String> rolesList = null;
124 if (element.containsKey("security/security-constraint/roles/role")) {
125 rolesList = new ArrayList<String>();
126 for (XMap role : element.getAll("security/security-constraint/roles/role"))
127 rolesList.add(role.get("."));
128 }
129
130 XMap adapter = element.getOne("adapter[@ref]");
131 Adapter adapterRef = adapter != null && adaptersMap != null
132 ? adaptersMap.get(adapter.get("@ref"))
133 : defaultAdapter;
134
135 return new Destination(id, channelRefs, properties, rolesList, adapterRef, null);
136 }
137 }