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.tide.simple;
023
024import java.io.IOException;
025import java.io.ObjectInputStream;
026import java.io.ObjectOutputStream;
027import java.util.ArrayList;
028import java.util.HashSet;
029import java.util.List;
030import java.util.Map;
031import java.util.Set;
032import java.util.concurrent.ConcurrentHashMap;
033
034import org.granite.logging.Logger;
035import org.granite.messaging.service.ServiceException;
036import org.granite.messaging.service.ServiceInvocationContext;
037import org.granite.tide.IInvocationCall;
038import org.granite.tide.IInvocationResult;
039import org.granite.tide.TidePersistenceManager;
040import org.granite.tide.TideServiceContext;
041import org.granite.tide.annotations.BypassTideMerge;
042import org.granite.tide.async.AsyncPublisher;
043import org.granite.tide.data.DataContext;
044import org.granite.tide.invocation.ContextUpdate;
045import org.granite.tide.invocation.InvocationResult;
046import org.granite.tide.util.AbstractContext;
047import org.granite.util.TypeUtil;
048
049
050/**
051 * @author William DRAI
052 */
053public class SimpleServiceContext extends TideServiceContext {
054
055    private static final long serialVersionUID = 1L;
056    
057    private static final Logger log = Logger.getLogger(SimpleServiceContext.class);
058
059    private Map<String, SimpleComponent> cache = new ConcurrentHashMap<String, SimpleComponent>();
060
061    private final SimpleIdentity identity;
062
063    
064    public SimpleServiceContext() throws ServiceException {
065        super();
066        this.identity = new SimpleIdentity();
067    }
068    
069    
070    @Override
071    protected AsyncPublisher getAsyncPublisher() {
072        return null;
073    }
074
075    @Override
076    protected TidePersistenceManager getTidePersistenceManager(boolean create) {
077        return null;
078    }
079
080    @Override
081    public Object findComponent(String componentName, Class<?> componentClass) {
082        if ("identity".equals(componentName))
083                return identity;
084        
085        SimpleComponent component = null;
086        if (componentName != null)
087            component = cache.get(componentName);
088        else {
089            for (Map.Entry<String, SimpleComponent> entry : cache.entrySet()) {
090                if (entry.getValue().classes.contains(componentClass)) {
091                    component = entry.getValue();
092                    break;
093                }
094            }
095        }
096        if (component != null)
097            return component.instance;
098
099        log.debug(">> New SimpleServiceContext looking up: %s", componentName);
100
101        try {
102            SimpleScannedItemHandler itemHandler = SimpleScannedItemHandler.instance();
103            Class<?> clazz = null;
104            if (componentClass != null)
105                clazz = itemHandler.getScannedClasses().get(componentClass);
106            else
107                clazz = itemHandler.getScannedClassesById().get(componentName);
108
109            if (clazz == null)
110                return null;
111
112            component = new SimpleComponent();
113            component.instance = TypeUtil.newInstance(clazz, Object.class);
114            component.classes = new HashSet<Class<?>>();
115            component.classes.add(clazz);
116            cache.put(componentName, component);
117
118            return component.instance;
119        }
120        catch (Exception e) {
121                log.error(e, "Component not found %s", componentName);
122            throw new ServiceException("Could not lookup for: " + componentName, e);
123        }
124    }
125    
126    /* (non-Javadoc)
127         * @see org.granite.tide.ejb.EJBServiceContextIntf#findComponentClass(java.lang.String)
128         */
129    @Override
130    public Set<Class<?>> findComponentClasses(String componentName, Class<?> componentClass) {
131        if ("identity".equals(componentName)) {
132                Set<Class<?>> classes = new HashSet<Class<?>>(1);
133                classes.add(SimpleIdentity.class);
134                return classes;
135        }
136        
137        SimpleComponent component = cache.get(componentName);
138        if (component == null)
139            findComponent(componentName, componentClass);
140        return cache.get(componentName).classes;
141    }
142
143    /* (non-Javadoc)
144         * @see org.granite.tide.ejb.EJBServiceContextIntf#prepareCall(org.granite.messaging.service.ServiceInvocationContext, org.granite.tide.IInvocationCall, java.lang.String)
145         */
146    @Override
147    public void prepareCall(ServiceInvocationContext context, IInvocationCall c, String componentName, Class<?> componentClass) {
148        // Initialize an empty data context
149        DataContext.init();
150    }
151
152    
153    private static class SimpleComponent {
154        public Object instance;
155        public Set<Class<?>> classes;
156    }
157    
158    /* (non-Javadoc)
159         * @see org.granite.tide.ejb.EJBServiceContextIntf#postCall(org.granite.messaging.service.ServiceInvocationContext, java.lang.Object, java.lang.String)
160         */
161    @Override
162    public IInvocationResult postCall(ServiceInvocationContext context, Object result, String componentName, Class<?> componentClass) {
163        List<ContextUpdate> results = new ArrayList<ContextUpdate>();
164        DataContext dataContext = DataContext.get();
165        Object[][] updates = dataContext != null ? dataContext.getUpdates() : null;
166
167        InvocationResult ires = new InvocationResult(result, results);
168        if (componentName != null || componentClass != null) {
169            Set<Class<?>> componentClasses = findComponentClasses(componentName, componentClass);
170            if (isBeanAnnotationPresent(componentClasses, context.getMethod().getName(), context.getMethod().getParameterTypes(), BypassTideMerge.class))
171                ires.setMerge(false);
172        }
173
174        ires.setUpdates(updates);
175        return ires;
176    }
177
178    /* (non-Javadoc)
179         * @see org.granite.tide.ejb.EJBServiceContextIntf#postCallFault(org.granite.messaging.service.ServiceInvocationContext, java.lang.Throwable, java.lang.String)
180         */
181    @Override
182    public void postCallFault(ServiceInvocationContext context, Throwable t, String componentName, Class<?> componentClass) {
183        AbstractContext.remove();
184    }
185    
186    private void writeObject(ObjectOutputStream out) throws IOException {
187        out.defaultWriteObject(); 
188    }
189    
190    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
191        in.defaultReadObject();
192        cache = new ConcurrentHashMap<String, SimpleComponent>();
193    }
194}