1 20 package org.apache.beehive.wsm.registration; 21 22 import java.io.File ; 23 import java.lang.reflect.Array ; 24 import java.lang.reflect.Field ; 25 import java.lang.reflect.GenericArrayType ; 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.ParameterizedType ; 28 import java.lang.reflect.Type ; 29 import java.rmi.Remote ; 30 import java.util.HashMap ; 31 import java.util.Map ; 32 33 import org.apache.log4j.*; 34 35 import javax.jws.soap.SOAPBinding; 36 import javax.xml.namespace.QName ; 37 import javax.xml.rpc.encoding.TypeMapping ; 38 import javax.xml.rpc.holders.Holder ; 39 40 import org.apache.beehive.wsm.databinding.BindingLookupService; 41 import org.apache.beehive.wsm.databinding.GenericHolder; 42 43 public abstract class TypeRegistrar { 44 static Logger logger = Logger.getLogger(TypeRegistrar.class); 45 46 protected TypeMapping mTypeMapping; 47 48 protected BindingLookupService lookupService; 49 50 54 public TypeRegistrar(TypeMapping typeMapping, 55 BindingLookupService lookupService) { 56 super(); 57 mTypeMapping = typeMapping; 58 this.lookupService = lookupService; 59 } 60 61 65 protected abstract QName getRegisteredQName(Class cls); 66 67 71 abstract protected void registerClassAsWithDefaultSearialization(Class cls, 72 QName q, SOAPBinding.Style style, 73 SOAPBinding.Use use); 74 75 79 abstract protected void registerClassAsXMLBeans(Class cls, QName q); 80 81 90 abstract protected void registerClassAsDataHandler(Class cls, QName q) 91 throws ClassNotFoundException , NoSuchMethodException , 92 InstantiationException , IllegalAccessException , 93 InvocationTargetException ; 94 95 99 abstract protected void registerClassAsSoapEncodedArray(Class cls, QName q); 100 101 106 abstract protected boolean classIsRegistered(Class cls, QName q); 107 108 111 abstract public QName getVoidType(); 112 113 abstract protected boolean isBuiltInType(Class cls); 114 115 abstract protected QName getBuiltInTypeQname(Class cls); 116 117 public QName registerType(Class cls, SOAPBinding.Style style, 118 SOAPBinding.Use use) { 119 QName q; 120 if (SOAPBinding.Use.ENCODED == use && cls.isArray()) { 121 q = lookupService.class2qname(cls.getComponentType()); 122 } 123 else { 124 q = lookupService.class2qname(cls); 125 } 126 return registerType(cls, q, style, use); 127 } 128 129 public QName registerType(Class cls, QName q, SOAPBinding.Style style, 130 SOAPBinding.Use use) { 131 try { 132 logger.debug("Register class: " + cls.getCanonicalName() 133 + " qName: " + q); 134 if (Void.TYPE.equals(cls)) 135 return null; 136 137 if (isBuiltInType(cls)) 138 return getBuiltInTypeQname(cls); 139 if (q == null) 140 throw new RuntimeException ( 141 "Invalid registeration requestion qname is null"); 142 143 if (cls.isArray()) { 144 if (SOAPBinding.Use.ENCODED == use) { 145 146 QName arrayQname = new QName (q.getNamespaceURI(), q.getLocalPart()+"Array"); 154 155 if (!classIsRegistered(cls, arrayQname)) 156 registerClassAsSoapEncodedArray(cls, arrayQname); 157 } 158 q = registerType(cls.getComponentType(), q, style, use); 160 161 } else if (!classIsRegistered(cls, q)) { 162 if (org.apache.xmlbeans.XmlObject.class.isAssignableFrom(cls)) { 163 registerClassAsXMLBeans(cls, q); 164 } 165 169 else if (isActivationEnabled() 170 && (java.awt.Image .class.isAssignableFrom(cls) 171 || getMultipartClass().isAssignableFrom(cls) || getDataHandlerClass() 172 .isAssignableFrom(cls))) { 173 try { 174 registerClassAsDataHandler(cls, q); 175 } catch (Exception e) { 176 180 e.printStackTrace(); 181 } 182 } else if (!Remote .class.isAssignableFrom(cls) 183 187 188 194 && !File .class.isAssignableFrom(cls)) { 195 registerClassAsWithDefaultSearialization(cls, q, style, use); 196 } else { 197 throw new RuntimeException ("failed to register " 198 + cls.getName() 199 + " as a valid web service datatype," 200 + " consider using a custom type mapping"); 201 } 202 } 203 logger.info("Registered class: " + cls.getCanonicalName() 206 + " qName: " + q); 207 return q; 208 } catch (RuntimeException e) { 209 logger.error("Failed to register class: " + cls.getCanonicalName() 210 + " type: " + q.getNamespaceURI() + ":" + q.getLocalPart()); 211 212 e.printStackTrace(); 213 throw e; 214 } 215 } 217 private boolean isActivationEnabled() { 218 return null != getDataHandlerClass() && null != getMultipartClass(); 219 } 220 221 private Class getDataHandlerClass() { 222 try { 223 return getClass().getClassLoader().loadClass( 224 "javax.activation.DataHandler"); 225 } catch (Exception e) { 226 } 227 return null; 228 } 229 230 private Class getMultipartClass() { 231 try { 232 return getClass().getClassLoader().loadClass( 233 "javax.mail.internet.MimeMultipart"); 234 } catch (Exception e) { 235 } 236 return null; 237 } 238 239 243 static public Class getHoldersValueClass(Type t) { 244 Class res = null; 245 246 if (t instanceof ParameterizedType ) { 247 ParameterizedType pt = (ParameterizedType ) t; 248 Type raw = pt.getRawType(); 249 if (GenericHolder.class.isAssignableFrom((Class ) raw)) { 250 Type [] typeArgs = pt.getActualTypeArguments(); 251 if (typeArgs[0] instanceof GenericArrayType ) { 252 Class arrayElementType = (Class ) ((GenericArrayType ) typeArgs[0]) 253 .getGenericComponentType(); 254 res = Array.newInstance(arrayElementType, 0).getClass(); 255 } else { 256 res = (Class ) typeArgs[0]; 257 } 258 } else { 259 throw new RuntimeException ( 260 "Invalid parameterized type for holder: " + t); 261 } 262 263 } else if (t instanceof Class ) { 264 if (Holder .class.isAssignableFrom((Class ) t)) { 265 Field [] publicFields = ((Class ) t).getFields(); 268 for (int i = 0; i < publicFields.length; i++) { 269 if (0 == "value".compareTo(publicFields[i].getName())) { 270 res = publicFields[i].getType(); 271 break; 272 } 273 } 274 } else { 275 throw new RuntimeException ("Invalid class. Type: " + t 276 + " is not a holder."); 277 } 278 } 279 return res; 281 } 282 } 283 | Popular Tags |