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.HashMap; 025import java.util.Map; 026 027import org.granite.config.flex.Destination; 028import org.granite.logging.Logger; 029import org.granite.util.TypeUtil; 030 031import flex.messaging.messages.RemotingMessage; 032 033/** 034 * @author Franck WOLFF 035 */ 036public class SimpleServiceInvoker extends ServiceInvoker<SimpleServiceFactory> { 037 038 private static final Logger log = Logger.getLogger(SimpleServiceInvoker.class); 039 040 private final Map<String, Object> sources; 041 042 protected SimpleServiceInvoker(Destination destination, SimpleServiceFactory factory) throws ServiceException { 043 super(destination, factory); 044 045 String className = destination.getProperties().get("source"); 046 if (className == null) 047 throw new ServiceException("No source property for destination: " + destination); 048 className = className.trim(); 049 050 log.debug(">> New SimpleServiceInvoker constructing new: %s", className); 051 052 // Invokee class set at runtime (RemoteObject.source). 053 if ("*".equals(className)) 054 sources = new HashMap<String, Object>(); 055 else { 056 try { 057 if (destination.getScannedClass() != null) 058 this.invokee = destination.getScannedClass().newInstance(); 059 else 060 this.invokee = TypeUtil.newInstance(className); 061 } catch (Exception e) { 062 throw new ServiceException("Invalid source property for destination: " + destination, e); 063 } 064 sources = null; 065 } 066 } 067 068 @Override 069 protected Object adjustInvokee(RemotingMessage request, String methodName, Object[] args) throws ServiceException { 070 071 if (sources == null) 072 return super.adjustInvokee(request, methodName, args); 073 074 String className = request.getSource(); 075 if (className == null) 076 throw new ServiceException("No source property in request for '*' destination: " + destination); 077 className = className.trim(); 078 079 Object invokee = null; 080 081 synchronized (sources) { 082 invokee = sources.get(className); 083 if (invokee == null) { 084 try { 085 invokee = TypeUtil.newInstance(className); 086 } catch (Exception e) { 087 throw new ServiceException("Invalid source property in request for '*' destination: " + destination, e); 088 } 089 sources.put(className, invokee); 090 } 091 } 092 093 return invokee; 094 } 095}