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.guice;
022    
023    import javax.servlet.ServletContext;
024    
025    import org.granite.config.flex.Destination;
026    import org.granite.context.GraniteContext;
027    import org.granite.messaging.service.ServiceException;
028    import org.granite.messaging.service.ServiceInvoker;
029    import org.granite.messaging.service.SimpleServiceFactory;
030    import org.granite.messaging.webapp.HttpGraniteContext;
031    import org.granite.util.XMap;
032    
033    import com.google.inject.Injector;
034    
035    import flex.messaging.messages.RemotingMessage;
036    
037    /**
038     * @author Matt GIACOMI
039     */
040    public class GuiceServiceFactory extends SimpleServiceFactory {
041    
042        private static final long serialVersionUID = 1L;
043    
044        private Injector injector = null;
045    
046        @Override
047        public void configure(XMap properties)throws ServiceException {
048            super.configure(properties);
049    
050            // getting guice injector from container
051            GraniteContext context = GraniteContext.getCurrentInstance();
052            ServletContext sc = ((HttpGraniteContext)context).getServletContext();
053            injector = (Injector) sc.getAttribute(Injector.class.getName());
054        }
055    
056        @Override
057        public ServiceInvoker<?> getServiceInstance(RemotingMessage request) throws ServiceException {
058            String messageType = request.getClass().getName();
059            String destinationId = request.getDestination();
060    
061            GraniteContext context = GraniteContext.getCurrentInstance();
062            Destination destination = context.getServicesConfig().findDestinationById(messageType, destinationId);
063            if (destination == null)
064                throw new ServiceException("No matching destination: " + destinationId);
065    
066            String className = destination.getProperties().get("source");
067    
068            try {
069                Class<?> gclass = Class.forName(className, true, getClass().getClassLoader());
070                Object gobject = injector.getInstance(gclass);
071                return new GuiceServiceInvoker(destination, this, gobject);
072            }
073            catch(Exception e) {
074                throw new ServiceException("Class not found in classloader: "+ className, e);
075            }
076        }
077    
078        @Override
079        public String toString() {
080            return toString("\n  Guice Injector: " + injector);
081        }
082    }