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.messaging.service;
023
024import java.util.Map;
025import java.util.concurrent.locks.ReentrantLock;
026
027import org.granite.config.flex.Destination;
028import org.granite.config.flex.Factory;
029import org.granite.context.GraniteContext;
030import org.granite.logging.Logger;
031import org.granite.util.TypeUtil;
032import org.granite.util.XMap;
033
034import flex.messaging.messages.RemotingMessage;
035
036/**
037 * @author Franck WOLFF
038 */
039public abstract class ServiceFactory {
040
041        private static final Logger log = Logger.getLogger(ServiceFactory.class);
042    private static final ReentrantLock lock = new ReentrantLock();
043
044    private ServiceExceptionHandler serviceExceptionHandler;
045
046    public static ServiceFactory getFactoryInstance(RemotingMessage request) throws ServiceException {
047
048        GraniteContext context = GraniteContext.getCurrentInstance();
049
050        String messageType = request.getClass().getName();
051        String destinationId = request.getDestination();
052
053        log.debug(">> Finding factoryId for messageType: %s and destinationId: %s", messageType, destinationId);
054
055        Destination destination = context.getServicesConfig().findDestinationById(messageType, destinationId);
056        if (destination == null)
057                throw new ServiceException("Destination not found: " + destinationId);
058        String factoryId = destination.getProperties().get("factory");
059
060        log.debug(">> Found factoryId: %s", factoryId);
061
062        Map<String, Object> cache = context.getApplicationMap();
063        String key = ServiceFactory.class.getName() + '.' + factoryId;
064
065        return getServiceFactory(cache, context, factoryId, key);
066    }
067
068    private static ServiceFactory getServiceFactory(Map<String, Object> cache, GraniteContext context, String factoryId, String key) {
069        lock.lock();
070        try {
071            ServiceFactory factory = (ServiceFactory)cache.get(key);
072            if (factory == null) {
073
074                log.debug(">> No cached factory for: %s", factoryId);
075
076                Factory config = context.getServicesConfig().findFactoryById(factoryId);
077                if (config == null)
078                    config = getDefaultFactoryConfig();
079                try {
080                    Class<? extends ServiceFactory> clazz = TypeUtil.forName(config.getClassName(), ServiceFactory.class);
081                    factory = clazz.newInstance();
082                    factory.configure(config.getProperties());
083                } 
084                catch (Exception e) {
085                    throw new ServiceException("Could not instantiate factory: " + factoryId + " of type " + config.getClassName(), e);
086                }
087                cache.put(key, factory);
088            }
089            else
090                log.debug(">> Found a cached factory for: %s", factoryId);
091
092            log.debug("<< Returning factory: %s", factory);
093
094            return factory;
095        } finally {
096            lock.unlock();
097        }
098    }
099
100    private static Factory getDefaultFactoryConfig() {
101        return Factory.DEFAULT_FACTORY;
102    }
103
104    public void configure(XMap properties) throws ServiceException {
105
106        log.debug(">> Configuring factory with: %s", properties);
107
108        // service exception handler
109        String sServiceExceptionHandler = properties.get("service-exception-handler");
110        String enableLogging = properties.get("enable-exception-logging");
111        if (sServiceExceptionHandler != null) {
112            try {
113                if (Boolean.TRUE.toString().equals(enableLogging) || Boolean.FALSE.toString().equals(enableLogging))
114                    this.serviceExceptionHandler = (ServiceExceptionHandler)TypeUtil.newInstance(sServiceExceptionHandler.trim(), 
115                                new Class<?>[] { boolean.class }, new Object[] { Boolean.valueOf(enableLogging) });
116                else
117                        this.serviceExceptionHandler = (ServiceExceptionHandler)TypeUtil.newInstance(sServiceExceptionHandler.trim());
118            } 
119            catch (Exception e) {
120                throw new ServiceException("Could not instantiate service exception handler: " + sServiceExceptionHandler, e);
121            }
122        }
123        else {
124                if (Boolean.TRUE.toString().equals(enableLogging) || Boolean.FALSE.toString().equals(enableLogging))
125                        this.serviceExceptionHandler = new DefaultServiceExceptionHandler(Boolean.valueOf(enableLogging));
126                else
127                        this.serviceExceptionHandler = new DefaultServiceExceptionHandler();
128        }
129
130        log.debug("<< Configuring factory done: %s", this);
131    }
132
133    public abstract ServiceInvoker<?> getServiceInstance(RemotingMessage request) throws ServiceException;
134
135    public ServiceExceptionHandler getServiceExceptionHandler() {
136        return serviceExceptionHandler;
137    }
138
139    @Override
140    public String toString() {
141        return toString(null);
142    }
143
144    public String toString(String append) {
145        return super.toString() + " {" +
146            (append != null ? append : "") +
147            "\n  serviceExceptionHandler: " + serviceExceptionHandler +
148        "\n}";
149    }
150}