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    
021    package org.granite.seam;
022    
023    import org.granite.config.flex.Destination;
024    import org.granite.context.GraniteContext;
025    import org.granite.messaging.service.ServiceException;
026    import org.granite.messaging.service.ServiceFactory;
027    import org.granite.messaging.service.ServiceInvoker;
028    import org.granite.util.XMap;
029    import org.jboss.seam.Component;
030    import org.jboss.seam.log.Log;
031    import org.jboss.seam.log.Logging;
032    
033    import flex.messaging.messages.RemotingMessage;
034    
035    /**
036     * @author Cameron INGRAM, Stuart Robertson
037     */
038    public class SeamServiceFactory extends ServiceFactory {
039    
040        private static final Log log = Logging.getLog(SeamServiceFactory.class);
041    
042        @Override
043        public void configure(XMap properties) throws ServiceException {
044            super.configure(properties);
045        }
046    
047        @Override
048        public ServiceInvoker<?> getServiceInstance(RemotingMessage request) throws ServiceException {
049            String messageType = request.getClass().getName();
050            String destinationId = request.getDestination();
051    
052            GraniteContext context = GraniteContext.getCurrentInstance();
053            Destination destination = context.getServicesConfig().findDestinationById(messageType, destinationId);
054            if (destination == null)
055                throw new ServiceException("No matching destination: " + destinationId);
056    
057            // all we need is to get bean name
058            String componentName = destination.getProperties().get("source");
059    
060            // Find the component we're calling
061            Component component = Component.forName(componentName);
062    
063            if (component == null) {
064                String msg = "Unable to create a Seam component named [" + componentName + "]";
065                log.error(msg);
066                ServiceException e = new ServiceException(msg);
067                throw e;
068            }
069    
070            //Create an instance of the component
071            Object instance = Component.getInstance(componentName, true);
072    
073            return new SeamServiceInvoker(destination, this, instance);
074        }
075    
076    }