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.config.flex;
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.granite.util.XMap;
028
029/**
030 * @author Franck WOLFF
031 */
032public class Service {
033
034    private final String id;
035    private final String className;
036    private final String messageTypes;
037    private final Map<String, Adapter> adapters;
038    private final Adapter defaultAdapter;
039    private final Map<String, Destination> destinations;
040
041
042    public Service(String id, String className, String messageTypes, Adapter defaultAdapter, Map<String, Adapter> adapters, Map<String, Destination> destinations) {
043        this.id = id;
044        this.className = className;
045        this.messageTypes = messageTypes;
046        this.defaultAdapter = defaultAdapter;
047        this.adapters = adapters;
048        this.destinations = destinations;
049    }
050
051    public String getId() {
052        return id;
053    }
054
055    public String getClassName() {
056        return className;
057    }
058
059    public String getMessageTypes() {
060        return messageTypes;
061    }
062
063    public Destination findDestinationById(String id) {
064        return destinations.get(id);
065    }
066    public Map<String, Destination> getDestinations() {
067        return destinations;
068    }
069
070    public Adapter findAdapterById(String id) {
071        return adapters.get(id);
072    }
073
074    public Adapter getDefaultAdapter() {
075        return defaultAdapter;
076    }
077    
078    public void addAdapter(Adapter adapter) {
079        adapters.put(adapter.getId(), adapter);
080    }
081
082    public static Service forElement(XMap element) {
083        String id = element.get("@id");
084        String className = element.get("@class");
085        String messageTypes = element.get("@messageTypes");
086
087        Adapter defaultAdapter = null;
088        Map<String, Adapter> adaptersMap = new HashMap<String, Adapter>();
089        for (XMap adapter : element.getAll("adapters/adapter-definition")) {
090            Adapter ad = Adapter.forElement(adapter);
091            if (Boolean.TRUE.toString().equals(adapter.get("@default")))
092                defaultAdapter = ad;
093            adaptersMap.put(ad.getId(), ad);
094        }
095
096        Map<String, Destination> destinations = new HashMap<String, Destination>();
097        for (XMap destinationElt : element.getAll("destination")) {
098            Destination destination = Destination.forElement(destinationElt, defaultAdapter, adaptersMap);
099            destinations.put(destination.getId(), destination);
100        }
101
102        return new Service(id, className, messageTypes, defaultAdapter, adaptersMap, destinations);
103    }
104}