001 /**
002 *
003 * Copyright 2004 Protique Ltd
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 **/
018 package org.codehaus.activesoap.handler.xmlbeans;
019
020 import org.apache.xmlbeans.SchemaType;
021 import org.apache.xmlbeans.XmlObject;
022 import org.codehaus.activesoap.handler.DefaultHandlerRegistry;
023 import org.codehaus.activesoap.handler.DefaultHandlerRegistry;
024 import org.codehaus.activesoap.handler.DefaultHandlerRegistry;
025
026 import javax.xml.namespace.QName;
027 import java.lang.reflect.Field;
028 import java.lang.reflect.Method;
029
030 /**
031 * @version $Revision: 1.6 $
032 */
033 public class XMLBeansRegistry extends DefaultHandlerRegistry {
034
035 /**
036 * Allows simple services to be registered so that they can automatically handle
037 * requests of the correct schema type.
038 *
039 * @param instance
040 */
041 public void registerService(Object instance) throws IllegalAccessException, NoSuchFieldException {
042 if (instance == null) {
043 throw new IllegalArgumentException("instance should not be null");
044 }
045 registerService(instance.getClass(), instance);
046 }
047
048 /**
049 * Allows simple services to be registered so that they can automatically handle
050 * requests of the correct schema type.
051 *
052 * @param serviceClass
053 */
054 public void registerService(Class serviceClass) throws IllegalAccessException, NoSuchFieldException {
055 registerService(serviceClass, null);
056 }
057
058 protected void registerService(Class serviceClass, Object instance) throws NoSuchFieldException, IllegalAccessException {
059 Method[] methods = serviceClass.getMethods();
060 for (int i = 0, size = methods.length; i < size; i++) {
061 Method method = methods[i];
062 Class[] parameterTypes = method.getParameterTypes();
063 if (parameterTypes.length == 1) {
064 Class parameterType = parameterTypes[0];
065 if (XmlObject.class.isAssignableFrom(parameterType)) {
066 registerServiceMethod(serviceClass, instance, method, parameterType);
067 }
068 }
069 }
070 }
071
072 protected void registerServiceMethod(Class serviceClass, Object instance, Method method, Class parameterType) throws NoSuchFieldException, IllegalAccessException {
073 // lets access to the SchemaType
074 Field field = parameterType.getField("type");
075 SchemaType type = (SchemaType) field.get(null);
076 QName name = type.getDocumentElementName();
077 addHandler(name, new XMLBeanInvokeMethodHandler(serviceClass, instance, method));
078 }
079 }