001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2007-2010 ADEQUATE SYSTEMS SARL
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 long serialVersionUID = 1L;
041    
042        private static final Log log = Logging.getLog(SeamServiceFactory.class);
043    
044        @Override
045        public void configure(XMap properties) throws ServiceException {
046            super.configure(properties);
047        }
048    
049        @Override
050        public ServiceInvoker<?> getServiceInstance(RemotingMessage request) throws ServiceException {
051            String messageType = request.getClass().getName();
052            String destinationId = request.getDestination();
053    
054            GraniteContext context = GraniteContext.getCurrentInstance();
055            Destination destination = context.getServicesConfig().findDestinationById(messageType, destinationId);
056            if (destination == null)
057                throw new ServiceException("No matching destination: " + destinationId);
058    
059            // all we need is to get bean name
060            String componentName = destination.getProperties().get("source");
061    
062            // Find the component we're calling
063            Component component = Component.forName(componentName);
064    
065            if (component == null) {
066                String msg = "Unable to create a Seam component named [" + componentName + "]";
067                log.error(msg);
068                ServiceException e = new ServiceException(msg);
069                throw e;
070            }
071    
072            //Create an instance of the component
073            Object instance = Component.getInstance(componentName, true);
074    
075            return new SeamServiceInvoker(destination, this, instance);
076        }
077    
078    }