PageRenderTime 87ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/diameter/core/jdiameter/impl/src/main/java/org/jdiameter/client/impl/transport/TransportLayerFactory.java

http://mobicents.googlecode.com/
Java | 109 lines | 68 code | 12 blank | 29 comment | 3 complexity | f6d9d6d65bf796227a3fc84192c13859 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 2006, 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.transport;
  23. import org.jdiameter.api.Configuration;
  24. import org.jdiameter.api.InternalException;
  25. import org.jdiameter.client.api.io.*;
  26. import org.jdiameter.client.api.parser.IMessageParser;
  27. import org.jdiameter.client.impl.helpers.AppConfiguration;
  28. import org.jdiameter.client.impl.helpers.ExtensionPoint;
  29. import org.jdiameter.client.impl.helpers.Parameters;
  30. import org.jdiameter.common.api.concurrent.DummyConcurrentFactory;
  31. import org.jdiameter.common.api.concurrent.IConcurrentFactory;
  32. import static java.lang.Class.forName;
  33. import java.lang.reflect.Constructor;
  34. import java.net.InetAddress;
  35. /**
  36. *
  37. * @author erick.svenson@yahoo.com
  38. * @author <a href="mailto:baranowb@gmail.com"> Bartosz Baranowski </a>
  39. * @author <a href="mailto:brainslog@gmail.com"> Alexandre Mendonca </a>
  40. */
  41. public class TransportLayerFactory implements ITransportLayerFactory {
  42. private Class<IConnection> connectionClass;
  43. private Constructor<IConnection> constructorIAi, constructorIAiCL;
  44. protected IMessageParser parser;
  45. protected Configuration config = null;
  46. public TransportLayerFactory(Configuration config, IMessageParser parser) throws TransportException {
  47. this.config = config;
  48. Configuration[] children = config.getChildren(Parameters.Extensions.ordinal());
  49. AppConfiguration internalExtensions = (AppConfiguration) children[ExtensionPoint.Internal.id()];
  50. String implName = internalExtensions.getStringValue(
  51. ExtensionPoint.InternalConnectionClass.ordinal(), (String) ExtensionPoint.InternalConnectionClass.defValue()
  52. );
  53. try {
  54. //TODO: this should be enough to check if class has interface!?
  55. this.connectionClass = (Class<IConnection>) forName(implName);
  56. if (!IConnection.class.isAssignableFrom(this.connectionClass))
  57. throw new TransportException("Specified class does not inherit IConnection interface " + this.connectionClass, TransportError.Internal);
  58. } catch (Exception e) {
  59. throw new TransportException("Cannot prepare specified connection class " + this.connectionClass, TransportError.Internal, e);
  60. }
  61. try {
  62. //TODO: this is bad practice, IConnection is interface and this code enforces constructor type to be present!
  63. constructorIAiCL = connectionClass.getConstructor(
  64. Configuration.class, IConcurrentFactory.class, InetAddress.class, Integer.TYPE, InetAddress.class,
  65. Integer.TYPE, IConnectionListener.class, IMessageParser.class, String.class);
  66. constructorIAi = connectionClass.getConstructor(
  67. Configuration.class, IConcurrentFactory.class, InetAddress.class, Integer.TYPE, InetAddress.class,
  68. Integer.TYPE, IMessageParser.class, String.class);
  69. }
  70. catch (Exception e) {
  71. throw new TransportException("Cannot find required constructor", TransportError.Internal, e);
  72. }
  73. this.parser = parser;
  74. }
  75. public IConnection createConnection(InetAddress remoteAddress, IConcurrentFactory factory, int remotePort, InetAddress localAddress, int localPort, String ref) throws TransportException {
  76. try {
  77. factory = factory == null ? new DummyConcurrentFactory() : factory;
  78. return constructorIAi.newInstance(config, factory, remoteAddress, remotePort, localAddress, localPort, parser, ref);
  79. } catch (Exception e) {
  80. throw new TransportException("Cannot create an instance of " + connectionClass, TransportError.Internal, e);
  81. }
  82. }
  83. public IConnection createConnection(InetAddress remoteAddress, IConcurrentFactory factory, int remotePort, InetAddress localAddress, int localPort, IConnectionListener listener, String ref) throws TransportException {
  84. try {
  85. factory = factory == null ? new DummyConcurrentFactory() : factory;
  86. return constructorIAiCL.newInstance(config, factory, remoteAddress, remotePort, localAddress, localPort, listener, parser, ref);
  87. } catch (Exception e) {
  88. throw new TransportException("Cannot create an instance of " + connectionClass, TransportError.Internal, e);
  89. }
  90. }
  91. public boolean isWrapperFor(Class<?> aClass) throws InternalException {
  92. return false;
  93. }
  94. public <T> T unwrap(Class<T> aClass) throws InternalException {
  95. return null;
  96. }
  97. }