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.gravity.adapters;
022
023import org.granite.config.flex.Destination;
024import org.granite.gravity.Channel;
025import org.granite.gravity.Gravity;
026import org.granite.logging.Logger;
027import org.granite.messaging.service.ServiceException;
028import org.granite.util.TypeUtil;
029import org.granite.util.XMap;
030
031import flex.messaging.messages.AsyncMessage;
032import flex.messaging.messages.CommandMessage;
033
034/**
035 * @author William DRAI
036 */
037public abstract class ServiceAdapter {
038
039    private static final Logger log = Logger.getLogger(ServiceAdapter.class);
040
041    private String id;
042    private Gravity gravity;
043    private Destination destination;
044    private Object adapterState;
045    private SecurityPolicy securityPolicy = new DefaultSecurityPolicy();
046
047
048    public String getId() {
049        return id;
050    }
051    public void setId(String id) {
052        this.id = id;
053    }
054
055    public Gravity getGravity() {
056        return gravity;
057    }
058    public void setGravity(Gravity gravity) {
059        this.gravity = gravity;
060    }
061
062    public Destination getDestination() {
063        return destination;
064    }
065    public void setDestination(Destination destination) {
066        this.destination = destination;
067    }
068
069    public Object getAdapterState() {
070        return adapterState;
071    }
072    public void setAdapterState(Object adapterState) {
073        this.adapterState = adapterState;
074    }
075
076    public SecurityPolicy getSecurityPolicy() {
077        return securityPolicy;
078    }
079    public void setSecurityPolicy(SecurityPolicy securityPolicy) {
080        this.securityPolicy = securityPolicy;
081    }
082
083
084    public void configure(XMap adapterProperties, XMap destinationProperties) throws ServiceException {
085        String securityPolicy = adapterProperties.get("security-policy");
086        if (securityPolicy != null) {
087                try {
088                        this.securityPolicy = TypeUtil.newInstance(securityPolicy, SecurityPolicy.class);
089                }
090                catch (Exception e) {
091                        log.error(e, "Could not create instance of %s (using default security policy)", securityPolicy);
092                }
093        }
094    }
095
096    public void start() throws ServiceException {
097    }
098
099    public void stop() throws ServiceException {
100    }
101
102
103    public abstract Object manage(Channel fromClient, CommandMessage message);
104
105    public abstract Object invoke(Channel fromClient, AsyncMessage message);
106}