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
021package org.granite.messaging.service;
022
023import java.lang.reflect.Method;
024import java.util.HashMap;
025import java.util.Map;
026
027import javax.ejb.Remove;
028import javax.ejb.Stateful;
029
030import org.granite.util.TypeUtil;
031import org.granite.util.XMap;
032
033/**
034 * @author Franck WOLFF
035 */
036public class EjbServiceMetadata {
037
038        private boolean stateful = false;
039        private final Map<Method, Boolean> removeMethods = new HashMap<Method, Boolean>();
040        
041        /**
042         * Default constructor. Should only be used by the externalization mechanism.
043         */
044        public EjbServiceMetadata() {
045        }
046        
047        public EjbServiceMetadata(Class<?> scannedClass, Class<?> invokeeClass) {
048                stateful = scannedClass.isAnnotationPresent(Stateful.class);
049                
050                if (stateful) {
051                        for (Method method : scannedClass.getMethods()) {
052                                Remove remove = method.getAnnotation(Remove.class);
053                                if (remove != null) {
054                                        try {
055                                                method = invokeeClass.getMethod(method.getName(), method.getParameterTypes());
056                                                removeMethods.put(method, Boolean.valueOf(remove.retainIfException()));
057                                        } catch (Exception e) {
058                                                // ignore (invokee interface may not expose this remove method)...
059                                        }
060                                }
061                        }
062                }
063        }
064        
065        public EjbServiceMetadata(XMap properties, Class<?> invokeeClass) {
066                stateful = properties.containsKey("ejb-stateful");
067                
068                if (stateful) {
069                        for (XMap removeMethod : properties.getAll("ejb-stateful/remove-method")) {
070                                
071                                String signature = removeMethod.get("signature");
072                                if (signature == null)
073                                        throw new ServiceException("Missing signature in remove-method declaration: " + properties);
074                                
075                                Boolean retainIfException = Boolean.valueOf(removeMethod.get("retain-if-exception"));
076
077                                try {
078                                        removeMethods.put(TypeUtil.getMethod(invokeeClass, signature), retainIfException);
079                                }
080                                catch (NoSuchMethodException e) {
081                                        throw new ServiceException("Could not find method: " + invokeeClass.getName() + "." + signature);
082                                }
083                        }
084                }
085        }
086
087        public boolean isStateful() {
088                return stateful;
089        }
090        
091        public boolean isRemoveMethod(Method method) {
092                return removeMethods.containsKey(method);
093        }
094        
095        public boolean getRetainIfException(Method method) {
096                return removeMethods.get(method).booleanValue();
097        }
098}