|
| 1 | +package org.restcomm.protocols.ss7.cap; |
| 2 | + |
| 3 | +import javolution.text.TextBuilder; |
| 4 | +import javolution.xml.XMLBinding; |
| 5 | +import javolution.xml.XMLObjectReader; |
| 6 | +import javolution.xml.XMLObjectWriter; |
| 7 | +import javolution.xml.stream.XMLStreamException; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.FileNotFoundException; |
| 12 | +import java.io.FileOutputStream; |
| 13 | + |
| 14 | +public class CAPStackConfigurationManagement { |
| 15 | + private static final String PERSIST_FILE_NAME = "management.xml"; |
| 16 | + private static final String CAP_MANAGEMENT_PERSIST_DIR_KEY = "capmanagement.persist.dir"; |
| 17 | + private static final String USER_DIR_KEY = "user.dir"; |
| 18 | + private static final String TAB_INDENT = "\t"; |
| 19 | + private static final String DEFAULT_CONFIG_FILE_NAME = "CapStack"; |
| 20 | + |
| 21 | + private static final String TIMER_CIRCUITSWITCHEDCALLCONTROL_SHORT = "timercircuitswitchedcallcontrolshort"; |
| 22 | + private static final String TIMER_CIRCUITSWITCHEDCALLCONTROL_MEDIUM = "timercircuitswitchedcallcontrolmedium"; |
| 23 | + private static final String TIMER_CIRCUITSWITCHEDCALLCONTROL_LONG = "timercircuitswitchedcallcontrollong"; |
| 24 | + private static final String TIMER_SMS_SHORT = "timersmsshort"; |
| 25 | + private static final String TIMER_GPRS_SHORT = "timergprsshort"; |
| 26 | + |
| 27 | + private static final XMLBinding binding = new XMLBinding(); |
| 28 | + private static CAPStackConfigurationManagement instance = new CAPStackConfigurationManagement(); |
| 29 | + |
| 30 | + private final TextBuilder persistFile = TextBuilder.newInstance(); |
| 31 | + private String configFileName = DEFAULT_CONFIG_FILE_NAME; |
| 32 | + private String persistDir = null; |
| 33 | + |
| 34 | + private int _Timer_CircuitSwitchedCallControl_Short = 6000; // 1 - 10 sec |
| 35 | + private int _Timer_CircuitSwitchedCallControl_Medium = 30000; // 1 - 60 sec |
| 36 | + private int _Timer_CircuitSwitchedCallControl_Long = 300000; // 1 s - 30 minutes |
| 37 | + private int _Timer_Sms_Short = 10000; // 1 - 20 sec |
| 38 | + private int _Timer_Gprs_Short = 10000; // 1 - 20 sec |
| 39 | + |
| 40 | + private CAPStackConfigurationManagement() { |
| 41 | + } |
| 42 | + |
| 43 | + public static CAPStackConfigurationManagement getInstance() { |
| 44 | + return instance; |
| 45 | + } |
| 46 | + |
| 47 | + public void setPersistDir(String persistDir) { |
| 48 | + this.persistDir = persistDir; |
| 49 | + this.setPersistFile(); |
| 50 | + } |
| 51 | + |
| 52 | + private void setPersistFile() { |
| 53 | + this.persistFile.clear(); |
| 54 | + |
| 55 | + if (persistDir != null) { |
| 56 | + this.persistFile.append(persistDir).append(File.separator).append(this.configFileName).append("_").append(PERSIST_FILE_NAME); |
| 57 | + } else { |
| 58 | + persistFile.append(System.getProperty(CAP_MANAGEMENT_PERSIST_DIR_KEY, System.getProperty(USER_DIR_KEY))).append(File.separator).append(this.configFileName) |
| 59 | + .append("_").append(PERSIST_FILE_NAME); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Persist |
| 65 | + */ |
| 66 | + public void store() { |
| 67 | + // TODO : Should we keep reference to Objects rather than recreating |
| 68 | + // everytime? |
| 69 | + try { |
| 70 | + XMLObjectWriter writer = XMLObjectWriter.newInstance(new FileOutputStream(persistFile.toString())); |
| 71 | + |
| 72 | + writer.setBinding(binding); |
| 73 | + writer.setIndentation(TAB_INDENT); |
| 74 | + |
| 75 | + writer.write(this._Timer_CircuitSwitchedCallControl_Short, TIMER_CIRCUITSWITCHEDCALLCONTROL_SHORT, Integer.class); |
| 76 | + writer.write(this._Timer_CircuitSwitchedCallControl_Medium, TIMER_CIRCUITSWITCHEDCALLCONTROL_MEDIUM, Integer.class); |
| 77 | + writer.write(this._Timer_CircuitSwitchedCallControl_Long, TIMER_CIRCUITSWITCHEDCALLCONTROL_LONG, Integer.class); |
| 78 | + writer.write(this._Timer_Sms_Short, TIMER_SMS_SHORT, Integer.class); |
| 79 | + writer.write(this._Timer_Gprs_Short, TIMER_GPRS_SHORT, Integer.class); |
| 80 | + |
| 81 | + writer.close(); |
| 82 | + } catch (Exception e) { |
| 83 | + System.err.println(String.format("Error while persisting the CAP Resource state in file=%s", persistFile.toString())); |
| 84 | + e.printStackTrace(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Load and create LinkSets and Link from persisted file |
| 90 | + * <p> |
| 91 | + * load() is called from CAPStackImpl |
| 92 | + */ |
| 93 | + public void load() { |
| 94 | + try { |
| 95 | + setPersistFile(); |
| 96 | + XMLObjectReader reader = XMLObjectReader.newInstance(new FileInputStream(persistFile.toString())); |
| 97 | + reader.setBinding(binding); |
| 98 | + load(reader); |
| 99 | + } catch (XMLStreamException | FileNotFoundException e) { |
| 100 | + System.err.println(String.format("Error while load the CAP Resource state from file=%s", persistFile.toString())); |
| 101 | + e.printStackTrace(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void load(XMLObjectReader reader) throws XMLStreamException { |
| 106 | + Integer val = reader.read(TIMER_CIRCUITSWITCHEDCALLCONTROL_SHORT, Integer.class); |
| 107 | + if (val != null) |
| 108 | + this._Timer_CircuitSwitchedCallControl_Short = val; |
| 109 | + |
| 110 | + val = reader.read(TIMER_CIRCUITSWITCHEDCALLCONTROL_MEDIUM, Integer.class); |
| 111 | + if (val != null) |
| 112 | + this._Timer_CircuitSwitchedCallControl_Medium = val; |
| 113 | + |
| 114 | + val = reader.read(TIMER_CIRCUITSWITCHEDCALLCONTROL_LONG, Integer.class); |
| 115 | + if (val != null) |
| 116 | + this._Timer_CircuitSwitchedCallControl_Long = val; |
| 117 | + |
| 118 | + val = reader.read(TIMER_SMS_SHORT, Integer.class); |
| 119 | + if (val != null) |
| 120 | + this._Timer_Sms_Short = val; |
| 121 | + |
| 122 | + val = reader.read(TIMER_GPRS_SHORT, Integer.class); |
| 123 | + if (val != null) |
| 124 | + this._Timer_Gprs_Short = val; |
| 125 | + |
| 126 | + reader.close(); |
| 127 | + } |
| 128 | + |
| 129 | + public void setConfigFileName(String configFileName) { |
| 130 | + this.configFileName = configFileName; |
| 131 | + } |
| 132 | + |
| 133 | + public int getTimerCircuitSwitchedCallControlShort() { |
| 134 | + return _Timer_CircuitSwitchedCallControl_Short; |
| 135 | + } |
| 136 | + |
| 137 | + public int getTimerCircuitSwitchedCallControlMedium() { |
| 138 | + return _Timer_CircuitSwitchedCallControl_Medium; |
| 139 | + } |
| 140 | + |
| 141 | + public int getTimerCircuitSwitchedCallControlLong() { |
| 142 | + return _Timer_CircuitSwitchedCallControl_Long; |
| 143 | + } |
| 144 | + |
| 145 | + public int getTimerSmsShort() { |
| 146 | + return _Timer_Sms_Short; |
| 147 | + } |
| 148 | + |
| 149 | + public int getTimerGprsShort() { |
| 150 | + return _Timer_Gprs_Short; |
| 151 | + } |
| 152 | + |
| 153 | + public void set_Timer_CircuitSwitchedCallControl_Short(int _Timer_CircuitSwitchedCallControl_Short) { |
| 154 | + this._Timer_CircuitSwitchedCallControl_Short = _Timer_CircuitSwitchedCallControl_Short; |
| 155 | + this.store(); |
| 156 | + } |
| 157 | + |
| 158 | + public void set_Timer_CircuitSwitchedCallControl_Medium(int _Timer_CircuitSwitchedCallControl_Medium) { |
| 159 | + this._Timer_CircuitSwitchedCallControl_Medium = _Timer_CircuitSwitchedCallControl_Medium; |
| 160 | + this.store(); |
| 161 | + } |
| 162 | + |
| 163 | + public void set_Timer_CircuitSwitchedCallControl_Long(int _Timer_CircuitSwitchedCallControl_Long) { |
| 164 | + this._Timer_CircuitSwitchedCallControl_Long = _Timer_CircuitSwitchedCallControl_Long; |
| 165 | + this.store(); |
| 166 | + } |
| 167 | + |
| 168 | + public void set_Timer_Sms_Short(int _Timer_Sms_Short) { |
| 169 | + this._Timer_Sms_Short = _Timer_Sms_Short; |
| 170 | + this.store(); |
| 171 | + } |
| 172 | + |
| 173 | + public void set_Timer_Gprs_Short(int _Timer_Gprs_Short) { |
| 174 | + this._Timer_Gprs_Short = _Timer_Gprs_Short; |
| 175 | + this.store(); |
| 176 | + } |
| 177 | +} |
0 commit comments