1 /* 2 * hunt-proton: AMQP Protocol library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 module hunt.proton.engine.TransportResultFactory; 12 13 import hunt.proton.engine.TransportResult; 14 import hunt.proton.engine.TransportResult; 15 16 import std.concurrency : initOnce; 17 import hunt.proton.engine.TransportException; 18 /** 19 * Creates TransportResults. 20 * Only intended for use by internal Proton classes. 21 * This class resides in the api module so it can be used by both proton-j-impl and proton-jni. 22 */ 23 class TransportResultFactory 24 { 25 26 // private static TransportResult _okResult = new TransportResultImpl(OK, null, null); 27 28 29 static TransportResult _okResult() { 30 __gshared TransportResult inst; 31 return initOnce!inst(new TransportResultImpl(TransportResult.Status.OK, null, null)); 32 } 33 34 35 //static TransportResult ok() { 36 // __gshared TransportResult inst; 37 // return initOnce!inst(_okResult); 38 //} 39 40 public static TransportResult ok() 41 { 42 return _okResult; 43 } 44 45 //static TransportResult error(string format) { 46 // __gshared TransportResult inst; 47 // return initOnce!inst(new TransportResultImpl(TransportResult.Status.ERROR, format, null)); 48 //} 49 50 //public static TransportResult error(string format) 51 //{ 52 // //string errorDescription; 53 // //try 54 // //{ 55 // // errorDescription = String.format(format, args); 56 // //} 57 // //catch(IllegalFormatException e) 58 // //{ 59 // // LOGGER.log(Level.SEVERE, "Formating error in string " + format, e); 60 // // errorDescription = format; 61 // //} 62 // return new TransportResultImpl(ERROR, format, null); 63 //} 64 65 static TransportResult error( string errorDescription) 66 { 67 return new TransportResultImpl(TransportResult.Status.ERROR, errorDescription, null); 68 } 69 70 //static TransportResult error(Exception e) { 71 // __gshared TransportResult inst; 72 // return initOnce!inst(new TransportResultImpl(TransportResult.Status.ERROR, e is null ? null : e.toString(), e)); 73 //} 74 75 76 public static TransportResult error( Exception e) 77 { 78 return new TransportResultImpl(TransportResult.Status.ERROR, e is null ? null : e.toString(), e); 79 } 80 81 82 } 83 84 class TransportResultImpl : TransportResult 85 { 86 private string _errorDescription; 87 private TransportResult.Status _status; 88 private Exception _exception; 89 90 this(TransportResult.Status status, string errorDescription, Exception exception) 91 { 92 _status = status; 93 _errorDescription = errorDescription; 94 _exception = exception; 95 } 96 97 public bool isOk() 98 { 99 return _status == TransportResult.Status.OK; 100 } 101 102 public TransportResult.Status getStatus() 103 { 104 return _status; 105 } 106 107 public string getErrorDescription() 108 { 109 return _errorDescription; 110 } 111 112 public Exception getException() 113 { 114 return _exception; 115 } 116 117 public void checkIsOk() 118 { 119 if (!isOk()) 120 { 121 Exception e = getException(); 122 if (e !is null) 123 { 124 throw new TransportException(e); 125 } 126 else 127 { 128 throw new TransportException(getErrorDescription()); 129 } 130 } 131 } 132 }