PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/client/impl/annotation/Recoder.java

http://mobicents.googlecode.com/
Java | 658 lines | 578 code | 26 blank | 54 comment | 187 complexity | 9eef57f0d1710834f2ca83f4c544c30c MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2010, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jdiameter.client.impl.annotation;
  23. import org.jdiameter.api.*;
  24. import org.jdiameter.api.annotation.*;
  25. import org.jdiameter.client.impl.annotation.internal.ClassInfo;
  26. import org.jdiameter.client.impl.annotation.internal.ConstructorInfo;
  27. import org.jdiameter.client.impl.annotation.internal.MethodInfo;
  28. import org.jdiameter.client.impl.annotation.internal.Storage;
  29. import org.jdiameter.client.impl.RawSessionImpl;
  30. import org.jdiameter.client.api.annotation.IRecoder;
  31. import org.jdiameter.client.api.annotation.RecoderException;
  32. import org.jdiameter.client.api.IMessage;
  33. import org.slf4j.LoggerFactory;
  34. import org.slf4j.Logger;
  35. import java.lang.reflect.Constructor;
  36. import java.lang.reflect.InvocationTargetException;
  37. import java.util.*;
  38. /**
  39. *
  40. * @author erick.svenson@yahoo.com
  41. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  42. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  43. */
  44. public class Recoder implements IRecoder {
  45. // TODO full min/max/position constrains and optimization (caching)
  46. private static final Logger log = LoggerFactory.getLogger(Recoder.class);
  47. private Storage storage = new Storage();
  48. private final RawSessionImpl rawSession;
  49. private final MetaData metaData;
  50. public Recoder(SessionFactory factory, MetaData metaData) {
  51. this.metaData = metaData;
  52. try {
  53. this.rawSession = (RawSessionImpl) factory.getNewRawSession();
  54. } catch (InternalException e) {
  55. throw new IllegalArgumentException(e);
  56. }
  57. }
  58. // =======================================================================================
  59. //@Override
  60. public Message encodeToRequest(Object yourDomainMessageObject, Avp... additionalAvp) throws RecoderException {
  61. return encode(yourDomainMessageObject, null, 0, additionalAvp);
  62. }
  63. //@Override
  64. public Message encodeToAnswer(Object yourDomainMessageObject, Request request, long resultCode) throws RecoderException {
  65. return encode(yourDomainMessageObject, request, resultCode);
  66. }
  67. public Message encode(Object yourDomainMessageObject, Request request, long resultCode, Avp... addAvp) throws RecoderException {
  68. IMessage message = null;
  69. ClassInfo classInfo = storage.getClassInfo(yourDomainMessageObject.getClass());
  70. CommandDscr commandDscr = classInfo.getAnnotation(CommandDscr.class);
  71. if (commandDscr != null) {
  72. // Get command parameters
  73. if (request == null) {
  74. message = (IMessage) rawSession.createMessage(commandDscr.code(), ApplicationId.createByAccAppId(0));
  75. message.setRequest(true);
  76. message.getAvps().addAvp(addAvp);
  77. try {
  78. if (message.getAvps().getAvp(Avp.AUTH_APPLICATION_ID) != null) {
  79. message.setHeaderApplicationId(message.getAvps().getAvp(Avp.AUTH_APPLICATION_ID).getUnsigned32());
  80. }
  81. else if (message.getAvps().getAvp(Avp.ACCT_APPLICATION_ID) != null) {
  82. message.setHeaderApplicationId(message.getAvps().getAvp(Avp.ACCT_APPLICATION_ID).getUnsigned32());
  83. }
  84. else if (message.getAvps().getAvp(Avp.VENDOR_SPECIFIC_APPLICATION_ID) != null) {
  85. message.setHeaderApplicationId(message.getAvps().getAvp(Avp.VENDOR_SPECIFIC_APPLICATION_ID).
  86. getGrouped().getAvp(Avp.VENDOR_ID).getUnsigned32());
  87. }
  88. } catch (Exception exc) {
  89. throw new RecoderException(exc);
  90. }
  91. if (message.getAvps().getAvp(Avp.ORIGIN_HOST) == null) {
  92. message.getAvps().addAvp(Avp.ORIGIN_HOST, metaData.getLocalPeer().getUri().getFQDN(), true, false, true);
  93. }
  94. if (message.getAvps().getAvp(Avp.ORIGIN_REALM) == null) {
  95. message.getAvps().addAvp(Avp.ORIGIN_REALM, metaData.getLocalPeer().getRealmName(), true, false, true);
  96. }
  97. } else {
  98. message = (IMessage) request.createAnswer(resultCode);
  99. }
  100. for (CommandFlag f : commandDscr.flags()) {
  101. switch (f) {
  102. case E:
  103. message.setError(true);
  104. break;
  105. case P:
  106. message.setProxiable(true);
  107. break;
  108. case R:
  109. message.setRequest(true);
  110. break;
  111. case T:
  112. message.setReTransmitted(true);
  113. break;
  114. }
  115. }
  116. // Find top level avp in getter-annotation methods
  117. Map<String, Object> chMap = getChildInstance(yourDomainMessageObject, classInfo, null);
  118. // Fill
  119. for (Child ch : commandDscr.childs()) {
  120. fillChild(message.getAvps(), ch, chMap);
  121. }
  122. } else {
  123. log.debug("Can not found annotation for object {}", yourDomainMessageObject);
  124. }
  125. return message;
  126. }
  127. private Map<String, Object> getChildInstance(Object yourDomainMessageObject, ClassInfo c, Map<String, Object> chMap)
  128. throws RecoderException {
  129. if (chMap == null)
  130. chMap = new HashMap<String, Object>();
  131. for (MethodInfo mi : c.getMethodsInfo()) {
  132. if (mi.getAnnotation(Getter.class) != null) {
  133. try {
  134. Object value = mi.getMethod().invoke(yourDomainMessageObject);
  135. if (value != null) {
  136. Class mc = value.getClass().isArray() ? value.getClass().getComponentType() : value.getClass();
  137. chMap.put(mc.getName(), value);
  138. for (Class<?> i : mc.getInterfaces())
  139. chMap.put(i.getName(), value);
  140. }
  141. } catch (IllegalAccessException e) {
  142. throw new RecoderException(e);
  143. } catch (InvocationTargetException e) {
  144. throw new RecoderException(e);
  145. }
  146. }
  147. }
  148. return chMap;
  149. }
  150. private void fillChild(AvpSet as, Child ci, Map<String, Object> childs) throws RecoderException {
  151. Object c = childs.get(ci.ref().getName());
  152. if (c != null) {
  153. ClassInfo cc = storage.getClassInfo(ci.ref());
  154. AvpDscr ad = cc.getAnnotation(AvpDscr.class);
  155. if (ad != null) {
  156. boolean m = false, p = false;
  157. // cast <=> getter for primitive
  158. switch (ad.type()) {
  159. case Integer32:
  160. case Enumerated: {
  161. for (AvpFlag f : ad.must())
  162. if (AvpFlag.M.equals(f)) {
  163. m = true;
  164. }
  165. else if (AvpFlag.P.equals(f)) {
  166. p = true;
  167. }
  168. // find in getter
  169. Collection<Integer> cv = getValue(c, Integer.class);
  170. for (Integer v : cv)
  171. as.addAvp(ad.code(), v, ad.vendorId(), m, p);
  172. }
  173. break;
  174. case Unsigned32: {
  175. for (AvpFlag f : ad.must())
  176. if (AvpFlag.M.equals(f)) {
  177. m = true;
  178. }
  179. else if (AvpFlag.P.equals(f)) {
  180. p = true;
  181. }
  182. Collection<Long> cv = getValue(c, Long.class);
  183. for (Long v : cv)
  184. as.addAvp(ad.code(), v, ad.vendorId(), m, p, true);
  185. }
  186. break;
  187. case Unsigned64:
  188. case Integer64: {
  189. for (AvpFlag f : ad.must())
  190. if (AvpFlag.M.equals(f)) {
  191. m = true;
  192. }
  193. else if (AvpFlag.P.equals(f)) {
  194. p = true;
  195. }
  196. Collection<Long> cv = getValue(c, Long.class);
  197. for (Long v : cv)
  198. as.addAvp(ad.code(), v, ad.vendorId(), m, p);
  199. }
  200. break;
  201. case Float32: {
  202. for (AvpFlag f : ad.must())
  203. if (AvpFlag.M.equals(f)) {
  204. m = true;
  205. }
  206. else if (AvpFlag.P.equals(f)) {
  207. p = true;
  208. }
  209. Collection<Float> cv = getValue(c, Float.class);
  210. for (Float v : cv)
  211. as.addAvp(ad.code(), v, ad.vendorId(), m, p);
  212. }
  213. break;
  214. case Float64: {
  215. for (AvpFlag f : ad.must())
  216. if (AvpFlag.M.equals(f)) {
  217. m = true;
  218. }
  219. else if (AvpFlag.P.equals(f)) {
  220. p = true;
  221. }
  222. Collection<Double> cv = getValue(c, Double.class);
  223. for (Double v : cv)
  224. as.addAvp(ad.code(), v, ad.vendorId(), m, p);
  225. }
  226. break;
  227. case OctetString:
  228. case Address:
  229. case Time:
  230. case DiameterIdentity:
  231. case DiameterURI:
  232. case IPFilterRule:
  233. case QoSFilterRule: {
  234. for (AvpFlag f : ad.must())
  235. if (AvpFlag.M.equals(f)) {
  236. m = true;
  237. }
  238. else if (AvpFlag.P.equals(f)) {
  239. p = true;
  240. }
  241. Collection<String> cv = getValue(c, String.class);
  242. for (String v : cv)
  243. as.addAvp(ad.code(), v, ad.vendorId(), m, p, true);
  244. }
  245. break;
  246. case UTF8String: {
  247. for (AvpFlag f : ad.must())
  248. if (AvpFlag.M.equals(f)) {
  249. m = true;
  250. }
  251. else if (AvpFlag.P.equals(f)) {
  252. p = true;
  253. }
  254. Collection<String> cv = getValue(c, String.class);
  255. for (String v : cv)
  256. as.addAvp(ad.code(), v, ad.vendorId(), m, p, false);
  257. }
  258. break;
  259. case Grouped: {
  260. for (AvpFlag f : ad.must()) {
  261. if (AvpFlag.M.equals(f)) {
  262. m = true;
  263. }
  264. else if (AvpFlag.P.equals(f)) {
  265. p = true;
  266. }
  267. }
  268. Collection<Object> cv = new ArrayList<Object>();
  269. if (c.getClass().isArray()) {
  270. cv = Arrays.asList((Object[])c);
  271. }
  272. else {
  273. cv.add(c);
  274. }
  275. for (Object cj : cv) {
  276. AvpSet las = as.addGroupedAvp(ad.code(),ad.vendorId(), m, p);
  277. Map<String, Object> lchilds = getChildInstance(cj, storage.getClassInfo(cj.getClass()), null);
  278. for (Child lci : ad.childs()) {
  279. fillChild(las, lci, lchilds);
  280. }
  281. }
  282. }
  283. break;
  284. }
  285. }
  286. }
  287. }
  288. private <T> Collection<T> getValue(Object ic, Class<T> type) throws RecoderException {
  289. Collection<T> rc = new ArrayList<T>();
  290. Object[] xc = null;
  291. if (ic.getClass().isArray())
  292. xc = (Object[]) ic;
  293. else
  294. xc = new Object[] {ic};
  295. for (Object c : xc) {
  296. for (MethodInfo lm : storage.getClassInfo(c.getClass()).getMethodsInfo()) {
  297. if (lm.getAnnotation(Getter.class) != null) {
  298. try {
  299. rc.add((T) lm.getMethod().invoke(c));
  300. } catch (IllegalAccessException e) {
  301. throw new RecoderException(e);
  302. } catch (InvocationTargetException e) {
  303. throw new RecoderException(e);
  304. }
  305. }
  306. }
  307. }
  308. return rc;
  309. }
  310. // =======================================================================================
  311. public <T> T decode(Message message, java.lang.Class<T> yourDomainMessageObject) throws RecoderException {
  312. Object rc = null;
  313. ClassInfo c = storage.getClassInfo(yourDomainMessageObject);
  314. CommandDscr cd = c.getAnnotation(CommandDscr.class);
  315. if (cd != null) {
  316. try {
  317. if (message.getCommandCode() != cd.code())
  318. throw new IllegalArgumentException("Invalid message code " + message.getCommandCode());
  319. if (message.getApplicationId() != 0 && message.getApplicationId() != cd.appId())
  320. throw new IllegalArgumentException("Invalid Application-Id " + message.getApplicationId());
  321. for (CommandFlag f : cd.flags()) {
  322. switch (f) {
  323. case E:
  324. if (!message.isError())
  325. throw new IllegalArgumentException("Flag e is not set");
  326. break;
  327. case P:
  328. if (!message.isProxiable())
  329. throw new IllegalArgumentException("Flag p is not set");
  330. break;
  331. case R:
  332. if (!message.isRequest())
  333. throw new IllegalArgumentException("Flag m is not set");
  334. break;
  335. case T:
  336. if (!message.isReTransmitted())
  337. throw new IllegalArgumentException("Flag t is not set");
  338. break;
  339. }
  340. }
  341. // Find max constructor + lost avp set by setters
  342. int cacount = 0;
  343. Constructor<?> cm = null;
  344. Map<String, Class<?>> cmargs = new HashMap<String, Class<?>>();
  345. for (ConstructorInfo ci : c.getConstructorsInfo()) {
  346. if (ci.getAnnotation(Setter.class) != null) {
  347. // check params - all params must have avp annotation
  348. Class<?>[] params = ci.getConstructor().getParameterTypes();
  349. boolean correct = true;
  350. for (Class<?> j : params) {
  351. if (j.isArray())
  352. j = j.getComponentType();
  353. if (storage.getClassInfo(j).getAnnotation(AvpDscr.class) == null) {
  354. correct = false;
  355. break;
  356. }
  357. }
  358. if (!correct)
  359. continue;
  360. // find max args constructor
  361. if (cacount < params.length) {
  362. cacount = params.length;
  363. cm = ci.getConstructor();
  364. }
  365. }
  366. }
  367. // fill cm args
  368. List<Object> initargs = new ArrayList<Object>();
  369. if (cm != null) {
  370. for (Class<?> ac : cm.getParameterTypes()) {
  371. Class<?> lac = ac.isArray() ? ac.getComponentType() : ac;
  372. cmargs.put(lac.getName(), ac);
  373. // Create params
  374. initargs.add(createChildByAvp(findChildDscr(cd.childs(), ac), ac, message.getAvps()));
  375. }
  376. // Create instance class
  377. rc = cm.newInstance(initargs.toArray());
  378. } else {
  379. rc = yourDomainMessageObject.newInstance();
  380. }
  381. //
  382. for (MethodInfo mi : c.getMethodsInfo()) {
  383. if (mi.getAnnotation(Setter.class) != null) {
  384. Class<?>[] pt = mi.getMethod().getParameterTypes();
  385. if (pt.length == 1 && storage.getClassInfo(pt[0]).getAnnotation(AvpDscr.class) != null) {
  386. Class<?> ptc = pt[0].isArray() ? pt[0].getComponentType() : pt[0];
  387. if (!cmargs.containsKey(ptc.getName())) {
  388. cmargs.put(ptc.getName(), ptc);
  389. mi.getMethod().invoke(rc, createChildByAvp(findChildDscr(cd.childs(), pt[0]), pt[0], message.getAvps()));
  390. }
  391. }
  392. }
  393. }
  394. // Fill undefined avp
  395. setUndefinedAvp(message.getAvps(), rc, c, cmargs);
  396. } catch (InstantiationException e) {
  397. throw new RecoderException(e);
  398. } catch (InvocationTargetException e) {
  399. throw new RecoderException(e);
  400. } catch (IllegalAccessException e) {
  401. throw new RecoderException(e);
  402. }
  403. }
  404. return (T) rc;
  405. }
  406. private void setUndefinedAvp(AvpSet set, Object rc, ClassInfo c, Map<String, Class<?>> cmargs) throws RecoderException {
  407. try {
  408. for (MethodInfo mi : c.getMethodsInfo()) {
  409. Setter s = mi.getAnnotation(Setter.class);
  410. if (s != null && Setter.Type.UNDEFINED.equals(s.value())) {
  411. Map<Integer, Integer> known = new HashMap<Integer, Integer>();
  412. for (Class<?> argc : cmargs.values()) {
  413. AvpDscr argd = storage.getClassInfo((argc.isArray() ? argc.getComponentType() : argc)).getAnnotation(AvpDscr.class);
  414. known.put(argd.code(), argd.code());
  415. }
  416. for (Avp a : set) {
  417. if (!known.containsKey(a.getCode()))
  418. mi.getMethod().invoke(rc, new UnknownAvp(a.getCode(), a.isMandatory(), a.isVendorId(), a.isEncrypted(), a.getVendorId(), a.getRaw()));
  419. }
  420. break;
  421. }
  422. }
  423. } catch (IllegalAccessException e) {
  424. throw new RecoderException(e);
  425. } catch (InvocationTargetException e) {
  426. throw new RecoderException(e);
  427. } catch (AvpDataException e) {
  428. throw new RecoderException(e);
  429. }
  430. }
  431. private Child findChildDscr(Child[] childs, Class<?> m) {
  432. for (Child c : childs) {
  433. Class<?> t = c.ref();
  434. m = m.isArray() ? m.getComponentType() : m;
  435. if (m == t)
  436. return c;
  437. if (m.getSuperclass() == t)
  438. return c;
  439. for (Class<?> i : m.getInterfaces())
  440. if (i == t)
  441. return c;
  442. }
  443. return null;
  444. }
  445. private Object createChildByAvp(Child mInfo, Class<?> m, AvpSet parentSet) throws RecoderException {
  446. Object rc;
  447. AvpDscr ad = storage.getClassInfo((m.isArray() ? m.getComponentType() : m)).getAnnotation(AvpDscr.class);
  448. Avp av = parentSet.getAvp(ad.code());
  449. if (av != null) {
  450. for (AvpFlag i : ad.must())
  451. switch (i) {
  452. case M:
  453. if (!av.isMandatory()) throw new IllegalArgumentException("not set flag M");
  454. break;
  455. case V:
  456. if (!av.isVendorId()) throw new IllegalArgumentException("not set flag V");
  457. break;
  458. case P:
  459. if (!av.isEncrypted()) throw new IllegalArgumentException("not set flag P");
  460. break;
  461. }
  462. } else {
  463. if (mInfo.min() > 0)
  464. throw new IllegalArgumentException("Avp " + ad.code() + " is mandatory");
  465. }
  466. if (AvpType.Grouped.equals(ad.type())) {
  467. if (m.isArray()) {
  468. Class<?> arrayClass = m.getComponentType();
  469. AvpSet as = parentSet.getAvps(ad.code());
  470. Object[] array = (Object[]) java.lang.reflect.Array.newInstance(arrayClass, as.size());
  471. for (int ii = 0; ii < array.length; ii++) {
  472. array[ii] = newInstanceGroupedAvp(arrayClass, ad, as.getAvpByIndex(ii));
  473. }
  474. rc = array;
  475. } else {
  476. rc = newInstanceGroupedAvp(m, ad, parentSet.getAvp(ad.code()));
  477. }
  478. } else {
  479. if (m.isArray()) {
  480. Class<?> arrayClass = m.getComponentType();
  481. AvpSet as = parentSet.getAvps(ad.code());
  482. Object[] array = (Object[]) java.lang.reflect.Array.newInstance(arrayClass, as.size());
  483. for (int ii = 0; ii < array.length; ii++) {
  484. array[ii] = newInstanceSimpleAvp(arrayClass, ad, as.getAvpByIndex(ii));
  485. }
  486. rc = array;
  487. } else {
  488. rc = newInstanceSimpleAvp(m, ad, parentSet.getAvp(ad.code()));
  489. }
  490. }
  491. // =========
  492. return rc;
  493. }
  494. private Object newInstanceGroupedAvp(Class<?> m, AvpDscr ad, Avp avp) throws RecoderException {
  495. Object rc;
  496. int cacount = 0;
  497. ClassInfo c = storage.getClassInfo(m);
  498. Constructor<?> cm = null;
  499. Map<String, Class<?>> cmargs = new HashMap<String, Class<?>>();
  500. for (ConstructorInfo ci : c.getConstructorsInfo()) {
  501. if (ci.getAnnotation(Setter.class) != null) {
  502. // check params - all params must have avp annotation
  503. Class<?>[] params = ci.getConstructor().getParameterTypes();
  504. boolean correct = true;
  505. for (Class<?> j : params) {
  506. if (j.isArray())
  507. j = j.getComponentType();
  508. if (storage.getClassInfo(j).getAnnotation(AvpDscr.class) == null) {
  509. correct = false;
  510. break;
  511. }
  512. }
  513. if (!correct)
  514. continue;
  515. // find max args constructor
  516. if (cacount < params.length) {
  517. cacount = params.length;
  518. cm = ci.getConstructor();
  519. }
  520. }
  521. }
  522. // fill cm args
  523. try {
  524. List<Object> initargs = new ArrayList<Object>();
  525. if (cm != null) {
  526. for (Class<?> ac : cm.getParameterTypes()) {
  527. Class<?> lac = ac.isArray() ? ac.getComponentType() : ac;
  528. cmargs.put(lac.getName(), ac);
  529. // Create params
  530. initargs.add(createChildByAvp(findChildDscr(ad.childs(), ac), ac, avp.getGrouped()));
  531. }
  532. // Create instance class
  533. rc = cm.newInstance(initargs.toArray());
  534. } else {
  535. rc = m.newInstance();
  536. }
  537. //
  538. for (MethodInfo mi : c.getMethodsInfo()) {
  539. if (mi.getAnnotation(Setter.class) != null) {
  540. Class<?>[] pt = mi.getMethod().getParameterTypes();
  541. if (pt.length == 1 && storage.getClassInfo(pt[0]).getAnnotation(AvpDscr.class) != null) {
  542. Class<?> ptc = pt[0].isArray() ? pt[0].getComponentType() : pt[0];
  543. if (!cmargs.containsKey(ptc.getName())) {
  544. cmargs.put(ptc.getName(), ptc);
  545. mi.getMethod().invoke(rc, createChildByAvp(findChildDscr(ad.childs(), pt[0]), pt[0], avp.getGrouped()));
  546. }
  547. }
  548. }
  549. }
  550. // Fill undefined child
  551. setUndefinedAvp(avp.getGrouped(), rc, c, cmargs);
  552. } catch (InstantiationException e) {
  553. throw new RecoderException(e);
  554. } catch (InvocationTargetException e) {
  555. throw new RecoderException(e);
  556. } catch (AvpDataException e) {
  557. throw new RecoderException(e);
  558. } catch (IllegalAccessException e) {
  559. throw new RecoderException(e);
  560. }
  561. return rc;
  562. }
  563. private Object newInstanceSimpleAvp(Class<?> m, AvpDscr ad, Avp avp) {
  564. Object rc = null;
  565. if (avp == null) return null;
  566. ClassInfo c = storage.getClassInfo(m);
  567. try {
  568. for (ConstructorInfo ci : c.getConstructorsInfo()) {
  569. if (ci.getConstructor().getParameterTypes().length == 1 && ci.getAnnotation(Setter.class) != null) {
  570. List<Object> args = new ArrayList<Object>();
  571. if (ci.getConstructor().getParameterTypes()[0].isArray()) {
  572. args.add(getValue(ad.type(), avp));
  573. } else {
  574. args.add(getValue(ad.type(), avp));
  575. }
  576. rc = ci.getConstructor().newInstance(args.toArray());
  577. }
  578. }
  579. if (rc == null) {
  580. rc = m.newInstance();
  581. for (MethodInfo mi : c.getMethodsInfo()) {
  582. if (mi.getAnnotation(Setter.class) != null) {
  583. List<Object> args = new ArrayList<Object>();
  584. if (mi.getMethod().getParameterTypes()[0].isArray()) {
  585. args.add(getValue(ad.type(), avp));
  586. } else {
  587. args.add(getValue(ad.type(), avp));
  588. }
  589. mi.getMethod().invoke(rc, args);
  590. }
  591. }
  592. }
  593. } catch (InstantiationException e) {
  594. throw new RecoderException(e);
  595. } catch (InvocationTargetException e) {
  596. throw new RecoderException(e);
  597. } catch (AvpDataException e) {
  598. throw new RecoderException(e);
  599. } catch (IllegalAccessException e) {
  600. throw new RecoderException(e);
  601. }
  602. return rc;
  603. }
  604. private Object getValue(AvpType type, Avp avp) throws AvpDataException {
  605. switch (type) {
  606. case Integer32:
  607. case Enumerated:
  608. return avp.getInteger32();
  609. case Unsigned32:
  610. return avp.getUnsigned32();
  611. case Unsigned64:
  612. case Integer64:
  613. return avp.getInteger64();
  614. case Float32:
  615. return avp.getFloat32();
  616. case Float64:
  617. return avp.getFloat64();
  618. case OctetString:
  619. case Address:
  620. case Time:
  621. case DiameterIdentity:
  622. case DiameterURI:
  623. case IPFilterRule:
  624. case QoSFilterRule:
  625. return avp.getOctetString();
  626. case UTF8String:
  627. return avp.getUTF8String();
  628. }
  629. return null;
  630. }
  631. // =======================================================================================
  632. }