PageRenderTime 22ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/xivo/ldap/AbstractLdapSynchronizer.java

https://gitlab.com/gesnaud/xivo-lib-ldap
Java | 252 lines | 230 code | 22 blank | 0 comment | 55 complexity | e051dcd62aa28425d29c1dd2dc7df905 MD5 | raw file
  1. package xivo.ldap;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.sql.SQLException;
  6. import java.util.Arrays;
  7. import java.util.List;
  8. import java.util.logging.Level;
  9. import java.util.logging.LogManager;
  10. import java.util.logging.Logger;
  11. import javax.naming.NamingException;
  12. import org.apache.commons.cli.ParseException;
  13. import xivo.ldap.asterisk.AsteriskManager;
  14. import xivo.ldap.configuration.ConfigLoader;
  15. import xivo.ldap.configuration.Configuration;
  16. import xivo.ldap.configuration.ExitCodes;
  17. import xivo.ldap.ldapconnection.LDAPConnector;
  18. import xivo.ldap.xivoconnection.EmptyContextException;
  19. import xivo.ldap.xivoconnection.NumberOutOfContextException;
  20. import xivo.ldap.xivoconnection.XivoConnector;
  21. import xivo.restapi.connection.WebServicesException;
  22. import xivo.restapi.model.CtiConfiguration;
  23. import xivo.restapi.model.CtiProfile;
  24. import xivo.restapi.model.User;
  25. import com.google.inject.Guice;
  26. import com.google.inject.Inject;
  27. import com.google.inject.Injector;
  28. public abstract class AbstractLdapSynchronizer {
  29. protected Configuration config = ConfigLoader.getConfig();
  30. @Inject
  31. protected AbstractDeltaGenerator deltaGenerator;
  32. @Inject
  33. protected XivoConnector xivoConnector;
  34. @Inject
  35. protected LDAPConnector ldapConnector;
  36. @Inject
  37. protected Initializer initializer;
  38. @Inject
  39. protected AsteriskManager asteriskManager;
  40. protected List<User> xivoUsers;
  41. protected List<User> ldapUsers;
  42. protected static Logger logger;
  43. public static void start(Class<? extends AbstractLDAPModule> moduleClass, String[] args) {
  44. initiateLogger();
  45. ParsingResult parsing = null;
  46. try {
  47. parsing = new ArgumentParser().parse(args);
  48. } catch (ParseException e) {
  49. logger.log(Level.SEVERE, "Error parsing the parameters : " + Arrays.toString(args), e);
  50. System.exit(ExitCodes.WRONG_CLI_PARAMETERS.ordinal());
  51. }
  52. AbstractLDAPModule module = getModuleInstance(moduleClass, parsing);
  53. Injector injector = Guice.createInjector(module);
  54. AbstractLdapSynchronizer synchronizer = injector.getInstance(AbstractLdapSynchronizer.class);
  55. synchronizer.setUp();
  56. try {
  57. synchronizer.retrieveUsers();
  58. } catch (WebServicesException e) {
  59. logger.log(Level.SEVERE, "Error with restapi when retrieving the users", e);
  60. System.exit(ExitCodes.REST_API_ERROR.ordinal());
  61. } catch (SQLException e) {
  62. logger.log(Level.SEVERE, "Error with postgres when retrieving the users", e);
  63. System.exit(ExitCodes.REST_API_ERROR.ordinal());
  64. } catch (NamingException e) {
  65. logger.log(Level.SEVERE, "Error with the LDAP when retrieving the users", e);
  66. System.exit(ExitCodes.REST_API_ERROR.ordinal());
  67. } catch (IOException e) {
  68. logger.log(Level.SEVERE, "Error when retrieving the users", e);
  69. System.exit(ExitCodes.REST_API_ERROR.ordinal());
  70. }
  71. try {
  72. if (parsing.isInitiatialization())
  73. synchronizer.initialize();
  74. else
  75. synchronizer.synchronize();
  76. } catch (WebServicesException e) {
  77. logger.log(Level.SEVERE, "Error with restapi when performing the synchronization", e);
  78. } catch (SQLException e) {
  79. logger.log(Level.SEVERE, "Error with postgres when performing the synchronization", e);
  80. } catch (IOException e) {
  81. logger.log(Level.SEVERE, "Error when performing the synchronization", e);
  82. }
  83. }
  84. private static void initiateLogger() {
  85. try {
  86. FileInputStream configFile = new FileInputStream("config/logging.properties");
  87. LogManager.getLogManager().readConfiguration(configFile);
  88. configFile.close();
  89. } catch (FileNotFoundException e) {
  90. System.err.println("Could not find the logging configuration file, exiting.");
  91. System.exit(ExitCodes.LOG_CONFIGURATION_ERROR.ordinal());
  92. } catch (IOException e) {
  93. System.err.println("Could not read the logging configuration file, exiting.");
  94. System.exit(ExitCodes.LOG_CONFIGURATION_ERROR.ordinal());
  95. }
  96. logger = Logger.getLogger(AbstractLdapSynchronizer.class.getName());
  97. }
  98. private static AbstractLDAPModule getModuleInstance(Class<? extends AbstractLDAPModule> moduleClass,
  99. ParsingResult parsing) {
  100. AbstractLDAPModule module = null;
  101. try {
  102. module = moduleClass.getConstructor(boolean.class).newInstance(parsing.isDryRun());
  103. } catch (Exception e) {
  104. logger.log(Level.SEVERE, "Error instantiating " + moduleClass.getName()
  105. + ", did you define a constructor with a single boolean parameter ?", e);
  106. System.exit(ExitCodes.CODE_STRUCTURE_ERROR.ordinal());
  107. }
  108. return module;
  109. }
  110. protected abstract void setUp();
  111. protected abstract void performSpecificActions();
  112. private void retrieveUsers() throws IOException, WebServicesException, SQLException, NamingException {
  113. xivoUsers = xivoConnector.getAllUsers();
  114. ldapUsers = ldapConnector.getUsers();
  115. }
  116. protected void initialize() throws IOException, WebServicesException, SQLException {
  117. initializer.initialize(xivoUsers, ldapUsers);
  118. xivoConnector.disableLiveReload();
  119. for (User u : initializer.getUsersToUpdate())
  120. xivoConnector.updateUser(u);
  121. xivoConnector.enableLiveReload();
  122. }
  123. protected void synchronize() throws IOException, WebServicesException, SQLException {
  124. deltaGenerator.generateDelta(xivoUsers, ldapUsers);
  125. xivoConnector.disableLiveReload();
  126. performCreations();
  127. performUpdates();
  128. performDeletions();
  129. performLdapUpdates();
  130. performSpecificActions();
  131. xivoConnector.enableLiveReload();
  132. asteriskManager.reloadCore();
  133. }
  134. protected void performLdapUpdates() {
  135. List<User> ldapUpdates = deltaGenerator.getUpdatesToLdap();
  136. if (config.updateLdap()) {
  137. ldapConnector.updateUsers(ldapUpdates, config.getUpdatedLdapFields());
  138. }
  139. }
  140. protected void performDeletions() throws IOException {
  141. for (User u : deltaGenerator.getUsersToDelete()) {
  142. try {
  143. if (config.deleteVoicemails())
  144. xivoConnector.deleteVoicemailForUser(u);
  145. if (config.deleteLines())
  146. xivoConnector.deleteLineForUser(u);
  147. if (config.deleteIncalls())
  148. xivoConnector.deleteIncallForUser(u);
  149. if (config.deleteUsers())
  150. xivoConnector.deleteUser(u);
  151. } catch (WebServicesException | SQLException e) {
  152. logger.log(Level.SEVERE, "Error deleting the user " + u, e);
  153. }
  154. }
  155. for(User u: deltaGenerator.getUsersToUpdate()) {
  156. try {
  157. User original = xivoConnector.getUser(u.getId());
  158. if(config.deleteLines() && u.getLine() == null && original.getLine() != null)
  159. xivoConnector.deleteLineForUser(original);
  160. if(config.deleteVoicemails() && u.getVoicemail() == null && original.getVoicemail() != null)
  161. xivoConnector.deleteVoicemailForUser(original);
  162. if(config.deleteIncalls() && u.getIncomingCall() == null && original.getIncomingCall() != null)
  163. xivoConnector.deleteIncallForUser(original);
  164. } catch (WebServicesException | SQLException e) {
  165. logger.log(Level.SEVERE, "Error deleting a subobject for user " + u, e);
  166. }
  167. }
  168. }
  169. protected void performUpdates() throws IOException {
  170. for (User u : deltaGenerator.getUsersToUpdate()) {
  171. try {
  172. if (config.updateUsers()) {
  173. xivoConnector.updateUser(u);
  174. }
  175. if (config.updateLines()) {
  176. xivoConnector.updateLineForUser(u);
  177. }
  178. if (config.updateVoicemails()) {
  179. xivoConnector.updateVoicemailForUser(u);
  180. }
  181. if (config.updateIncalls()) {
  182. xivoConnector.updateIncallForUser(u);
  183. }
  184. } catch (WebServicesException | SQLException | EmptyContextException | NumberOutOfContextException e) {
  185. logger.log(Level.SEVERE, "Error deleting the user " + u, e);
  186. }
  187. }
  188. }
  189. protected void performCreations() throws IOException {
  190. List<User> usersToCreate = deltaGenerator.getUsersToCreate();
  191. List<User> usersToUpdate = deltaGenerator.getUsersToUpdate();
  192. setDefaultCtiConfig(usersToCreate);
  193. for (User u : usersToCreate) {
  194. try {
  195. if (config.createUsers())
  196. xivoConnector.createUser(u);
  197. if (config.createLines())
  198. xivoConnector.createLineForUser(u);
  199. if (config.createVoicemails())
  200. xivoConnector.createVoicemailForUser(u);
  201. if (config.createIncalls())
  202. xivoConnector.createIncallForUser(u);
  203. } catch (WebServicesException | EmptyContextException | NumberOutOfContextException | SQLException e) {
  204. logger.log(Level.SEVERE, "Error creating the user " + u, e);
  205. }
  206. }
  207. for (User user : usersToUpdate) {
  208. try {
  209. if (config.createLines() && user.getLine() != null && user.getLine().getLineId() == null)
  210. xivoConnector.createLineForUser(user);
  211. if (config.createVoicemails() && user.getVoicemail() != null && user.getVoicemail().getId() == null)
  212. xivoConnector.createVoicemailForUser(user);
  213. } catch (WebServicesException e) {
  214. logger.log(Level.SEVERE, "Error updating the user " + user, e);
  215. }
  216. }
  217. }
  218. private void setDefaultCtiConfig(List<User> usersToCreate) throws IOException {
  219. CtiProfile defaultProfile = xivoConnector.getDefaultCtiProfile();
  220. CtiConfiguration defaultCtiConfig = new CtiConfiguration(true, defaultProfile);
  221. for (User user : usersToCreate) {
  222. if (user.getUsername() != null && user.getPassword() != null) {
  223. if (user.getCtiConfiguration() == null)
  224. user.setCtiConfiguration(defaultCtiConfig);
  225. else if (user.getCtiConfiguration().getCtiProfile() == null)
  226. user.getCtiConfiguration().setCtiProfile(defaultProfile);
  227. }
  228. }
  229. }
  230. }