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