Recent Posts
None | 16 sec ago
HTML | 33 sec ago
None | 1 min ago
None | 1 min ago
None | 2 min ago
None | 2 min ago
None | 2 min ago
Lua | 2 min ago
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Domain Reports
Please check out our new and improved Firefox Add-on. hide message
By rb2k on the 8th of Apr 2008 05:13:48 AM Download | Raw | Embed | Report
  1. /*
  2. * HybridUserProvider.java
  3. *
  4. * Created on 16. April 2007, 21:48
  5. * by Marc Seeger
  6. * code works fine as far as my 10 User Test-Server goes
  7. * It basically checks different userproviders which are being set in the configuration xml file
  8. * I use it in combination with hybridauth providers to be able to get the usual users from ldap but still have some Bots in MySQL
  9. *
  10. * Changed on 14. Nov. 2007, 10:48
  11. * by Chris Neasbitt
  12. *  -changed getUsers(int startIndex, int numResults) method to return a subset of the total users from all providers
  13. *  -changed the getUsers() method to use a vector internally since addAll is an optional method of the collection
  14. *   interface we cannot assume that all classes that support the collection interface also support the addAll method
  15. *  -changed the getUserCount() method to iterate through an array of providers while calling a private helper method
  16. *   getUserCount(UserProvider provider) on each of them.
  17. */
  18.  
  19. package org.jivesoftware.openfire.user;
  20. import java.util.Collection;
  21. import java.util.Vector;
  22. import java.util.Date;
  23. import java.util.HashSet;
  24. import java.util.Set;
  25.  
  26. import org.jivesoftware.util.ClassUtils;
  27. import org.jivesoftware.util.JiveGlobals;
  28. import org.jivesoftware.util.Log;
  29. import org.jivesoftware.openfire.auth.AuthProvider;
  30. import org.jivesoftware.openfire.user.UserProvider;
  31. /**
  32. *
  33. * @author Marc Seeger
  34. * @author Chris Neasbitt
  35. */
  36.  
  37. public class HybridUserProvider implements UserProvider {
  38.  
  39.   private UserProvider primaryProvider = null;
  40.   private UserProvider secondaryProvider = null;
  41.   private UserProvider tertiaryProvider = null;
  42.   private UserProvider[] userproviders = {primaryProvider, secondaryProvider, tertiaryProvider};
  43.  
  44.   private Set<String> primaryOverrides = new HashSet<String>();
  45.   private Set<String> secondaryOverrides = new HashSet<String>();
  46.   private Set<String> tertiaryOverrides = new HashSet<String>();
  47.  
  48.  
  49.   public HybridUserProvider() {
  50. // Load primary, secondary, and tertiary user providers.
  51.       String primaryClass = JiveGlobals.getXMLProperty("hybridUserProvider.primaryProvider.className");
  52.       if (primaryClass == null) {
  53.           Log.error("A primary UserProvider must be specified in the openfire.xml.");
  54.           return;
  55.       }
  56.       try {
  57.           Class c = ClassUtils.forName(primaryClass);
  58.           primaryProvider = (UserProvider)c.newInstance();
  59.           Log.debug("Primary user provider: " + primaryClass);
  60.       } catch (Exception e) {
  61.           Log.error("Unable to load primary user provider: " + primaryClass +
  62.                   ". Users in this provider will be disabled.", e);
  63.           return;
  64.       }
  65.       String secondaryClass = JiveGlobals.getXMLProperty("hybridUserProvider.secondaryProvider.className");
  66.       if (secondaryClass != null) {
  67.           try {
  68.               Class c = ClassUtils.forName(secondaryClass);
  69.               secondaryProvider = (UserProvider)c.newInstance();
  70.               Log.debug("Secondary user provider: " + secondaryClass);
  71.           } catch (Exception e) {
  72.               Log.error("Unable to load secondary user provider: " + secondaryClass, e);
  73.           }
  74.       }
  75.       String tertiaryClass = JiveGlobals.getXMLProperty("hybridUserProvider.tertiaryProvider.className");
  76.       if (secondaryClass != null) {
  77.           try {
  78.               Class c = ClassUtils.forName(secondaryClass);
  79.               tertiaryProvider = (UserProvider)c.newInstance();
  80.               Log.debug("Secondary user provider: " + secondaryClass);
  81.           } catch (Exception e) {
  82.               Log.error("Unable to load tertiary user provider: " + tertiaryClass, e);
  83.           }
  84.       }
  85.  
  86.       // Now, load any overrides.
  87.       String overrideList = JiveGlobals.getXMLProperty(
  88.               "hybridUserProvider.primaryProvider.overrideList", "");
  89.       for (String user: overrideList.split(",")) {
  90.           primaryOverrides.add(user.trim().toLowerCase());
  91.       }
  92.  
  93.       if (secondaryProvider != null) {
  94.           overrideList = JiveGlobals.getXMLProperty(
  95.                   "hybridUserProvider.secondaryProvider.overrideList", "");
  96.           for (String user: overrideList.split(",")) {
  97.               secondaryOverrides.add(user.trim().toLowerCase());
  98.           }
  99.       }
  100.  
  101.       if (tertiaryProvider != null) {
  102.           overrideList = JiveGlobals.getXMLProperty(
  103.                   "hybridUserProvider.tertiaryProvider.overrideList", "");
  104.           for (String user: overrideList.split(",")) {
  105.               tertiaryOverrides.add(user.trim().toLowerCase());
  106.           }
  107.       }
  108.  
  109.   }
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.   public User createUser(String username, String password, String name, String email) throws UserAlreadyExistsException {
  119.       //initialize our returnvalue
  120.       User returnvalue = null;
  121.  
  122.       //try to use the providers to create a user and change the return value to that user
  123.       if (!primaryProvider.isReadOnly()) {
  124.           try{
  125.               returnvalue = primaryProvider.createUser(username, password, name, email);
  126.           }
  127.  
  128.           finally{}
  129.  
  130.       } else if (secondaryProvider!=null){
  131.           if (!secondaryProvider.isReadOnly()) {
  132.               try{
  133.                   returnvalue = secondaryProvider.createUser(username, password, name, email);
  134.               }
  135.  
  136.               finally{}
  137.  
  138.           }
  139.       } else if (tertiaryProvider!=null){
  140.           if (!tertiaryProvider.isReadOnly()) {
  141.               try{
  142.                   returnvalue = tertiaryProvider.createUser(username, password, name, email);
  143.               }
  144.  
  145.               finally{}
  146.           }
  147.       }
  148.  
  149.       //return our created user
  150.       if (returnvalue!=null){
  151.           return returnvalue;} else{
  152.           throw new UnsupportedOperationException();
  153.           }
  154.   }
  155.  
  156.  
  157.  
  158.   public void deleteUser(String username) {
  159.       if (!primaryProvider.isReadOnly()) {
  160.           try{
  161.               primaryProvider.deleteUser(username);
  162.               return;
  163.           }
  164.  
  165.           finally{}
  166.  
  167.       } else if (secondaryProvider!=null){
  168.           if (!secondaryProvider.isReadOnly()) {
  169.               try{
  170.                   secondaryProvider.deleteUser(username);
  171.                   return;
  172.               }
  173.  
  174.               finally{}
  175.  
  176.           }
  177.       } else if (tertiaryProvider!=null){
  178.           if (!tertiaryProvider.isReadOnly()) {
  179.               try{
  180.                   tertiaryProvider.deleteUser(username);
  181.                   return;
  182.               }
  183.  
  184.               finally{}
  185.  
  186.           } else{
  187. // Reject the operation since all of the providers seem to be read-only
  188.               throw new UnsupportedOperationException();}
  189.  
  190.       }
  191.   }
  192.  
  193.  
  194.   public Collection<User> findUsers(Set<String> fields, String query) throws UnsupportedOperationException {
  195.  
  196.  
  197.       Collection<User> returnvalue = null;
  198.       try{
  199.           returnvalue = primaryProvider.findUsers(fields, query);
  200.       }
  201.  
  202.       finally{}
  203.  
  204.       if (secondaryProvider!=null){
  205.           try{
  206.  
  207.               returnvalue = secondaryProvider.findUsers(fields, query);
  208.           }
  209.  
  210.           finally{}
  211.       }
  212.       if (tertiaryProvider!=null){
  213.           try{
  214.               returnvalue = tertiaryProvider.findUsers(fields, query);
  215.           }
  216.  
  217.           finally{}
  218.       }
  219.  
  220.       //return our collection of users
  221.       if (returnvalue!=null){
  222.           return returnvalue;} else{
  223.           throw new UnsupportedOperationException();
  224.           }
  225.   }
  226.  
  227.  
  228.  
  229.  
  230.  
  231.   public Collection<User> findUsers(Set<String> fields, String query, int startIndex, int numResults) throws UnsupportedOperationException {
  232.       Collection<User> returnvalue = null;
  233.       try{
  234.           returnvalue = primaryProvider.findUsers(fields, query,startIndex,numResults);
  235.       }
  236.  
  237.       finally{}
  238.  
  239.       if (secondaryProvider!=null){
  240.           try{
  241.  
  242.               returnvalue = secondaryProvider.findUsers(fields, query,startIndex,numResults);
  243.           }
  244.  
  245.           finally{}
  246.       }
  247.       if (tertiaryProvider!=null){
  248.           try{
  249.               returnvalue = tertiaryProvider.findUsers(fields, query,startIndex,numResults);
  250.           }
  251.  
  252.           finally{}
  253.       }
  254.  
  255.       //return our Collection of Users
  256.       if (returnvalue!=null){
  257.           return returnvalue;} else{
  258.           throw new UnsupportedOperationException();
  259.           }
  260.   }
  261.  
  262.  
  263.   public Set<String> getSearchFields() throws UnsupportedOperationException {
  264.       Set<String> returnvalue = null;
  265.       try{
  266.           returnvalue = primaryProvider.getSearchFields();
  267.       }
  268.  
  269.       finally{}
  270.  
  271.       if (secondaryProvider!=null){
  272.           try{
  273.  
  274.               returnvalue = secondaryProvider.getSearchFields();
  275.           }
  276.  
  277.           finally{}
  278.       }
  279.       if (tertiaryProvider!=null){
  280.           try{
  281.               returnvalue = tertiaryProvider.getSearchFields();
  282.           }
  283.  
  284.           finally{}
  285.       }
  286.  
  287.       //return our Set of Strings
  288.       if (returnvalue!=null){
  289.           return returnvalue;} else{
  290.           throw new UnsupportedOperationException();
  291.           }
  292.   }
  293.  
  294.  
  295.  public int getUserCount() {
  296. int count = 0;
  297. for(UserProvider provider : userproviders) {
  298. count = count + this.getUserCount(provider);
  299. }
  300. return count;
  301.   }
  302.  
  303. private int getUserCount(UserProvider provider){
  304. int returnvalue = 0;
  305.       if (provider!=null){
  306.           try{
  307.  
  308.               returnvalue = returnvalue + provider.getUserCount();
  309.           }
  310.  
  311.           finally{}
  312.       }
  313.       return returnvalue;
  314. }
  315.  
  316.  
  317.   public Collection<String> getUsernames() {
  318.       Collection<String> returnvalue = null;
  319.       try{
  320.           returnvalue = primaryProvider.getUsernames();
  321.       }
  322.  
  323.       finally{}
  324.  
  325.       if (secondaryProvider!=null){
  326.           try{
  327.               returnvalue.addAll(secondaryProvider.getUsernames());
  328.           }
  329.  
  330.           finally{}
  331.       }
  332.       if (tertiaryProvider!=null){
  333.           try{
  334.               returnvalue.addAll(tertiaryProvider.getUsernames());
  335.           }
  336.  
  337.           finally{}
  338.       }
  339.  
  340.       //return our Set of Strings
  341.       if (returnvalue!=null){
  342.           return returnvalue;} else{
  343.           throw new UnsupportedOperationException();
  344.           }
  345.   }
  346.  
  347.  
  348.  
  349.  
  350.   public Collection<User> getUsers() {
  351.       Vector<User> returnvalue = null;
  352.       try{
  353.           returnvalue = new Vector<User>(primaryProvider.getUsers());
  354.       }
  355.  
  356.       finally{}
  357.  
  358.       if (secondaryProvider!=null){
  359.           try{
  360.               returnvalue.addAll(secondaryProvider.getUsers());
  361.           }
  362.  
  363.           finally{}
  364.       }
  365.       if (tertiaryProvider!=null){
  366.           try{
  367.               returnvalue.addAll(tertiaryProvider.getUsers());
  368.           }
  369.  
  370.           finally{}
  371.       }
  372.  
  373.       //return our Set of Strings
  374.       if (returnvalue!=null){
  375.           return returnvalue;} else{
  376.           throw new UnsupportedOperationException();
  377.           }
  378.   }
  379.  
  380.  
  381. /*
  382. *Changed by Chris Neasbitt to more accurately represent the intent of the method
  383. *
  384. *This method now removes a sub set of the combined users from all providers.  This
  385. *is done in places as to avoid copying collections of users in memory.
  386. */
  387.  
  388. public Collection<User> getUsers(int startIndex, int numResults) {
  389. Vector<User> returnresult = new Vector<User>();
  390. int numResultsLeft = numResults;
  391. int currentStartIndex = startIndex;
  392. for(UserProvider provider : userproviders)
  393. {
  394. if(numResultsLeft == 0) {
  395. break;
  396. }
  397.  
  398. int pusercount = this.getUserCount(provider);
  399.  
  400. if(pusercount == 0 || currentStartIndex >= pusercount) {
  401.  
  402. currentStartIndex = currentStartIndex - pusercount;
  403. continue;
  404.  
  405. } else {
  406.  
  407. Collection<User> subresult = provider.getUsers(currentStartIndex, numResultsLeft);
  408. currentStartIndex = currentStartIndex - subresult.size();
  409. numResultsLeft = numResultsLeft - subresult.size();
  410. returnresult.addAll(subresult);
  411. }
  412. }
  413.  
  414. return returnresult;
  415. }
  416.  
  417.   public boolean isReadOnly() {
  418.       return false;
  419.   }
  420.  
  421.   public User loadUser(String username) throws UserNotFoundException {
  422.       try{
  423.           return primaryProvider.loadUser(username);
  424.  
  425.       }
  426.  
  427.       catch (Exception e) {}
  428.  
  429.       if (secondaryProvider!=null) {
  430.           try{
  431.  
  432.               return secondaryProvider.loadUser(username);
  433.           }
  434.  
  435.           catch (Exception e) {}
  436.       }
  437.  
  438.  
  439.       if (tertiaryProvider!=null) {
  440.           try{
  441.               return tertiaryProvider.loadUser(username);
  442.           }
  443.  
  444.           catch (Exception e) {}
  445.       }
  446.  
  447.       //if we get this far, no provider seems to successfully have loaded the user
  448.       throw new UserNotFoundException();
  449.   }
  450.  
  451.   public void setCreationDate(String username, Date creationDate) throws UserNotFoundException {
  452.  
  453.       //without it eclipse goes apeshit
  454.       if (primaryProvider!=null)
  455.           //apeshit I say!
  456.  
  457.  
  458.           try{
  459.               primaryProvider.setCreationDate(username, creationDate);
  460.               return;
  461.           } catch (Exception e) {}
  462.  
  463.       if (secondaryProvider!=null){
  464.           try{
  465.               secondaryProvider.setCreationDate(username, creationDate);
  466.               return;
  467.           }
  468.  
  469.           catch (Exception e) {}
  470.       }
  471.       if (tertiaryProvider!=null){
  472.           try{
  473.               tertiaryProvider.setCreationDate(username, creationDate);
  474.               return;
  475.           }
  476.  
  477.           catch (Exception e) {throw new UserNotFoundException();}
  478.       }
  479.  
  480.  
  481.   }
  482.   public void setEmail(String username, String email) throws UserNotFoundException {
  483.       //without it eclipse goes apeshit
  484.       if (primaryProvider!=null)
  485.           //apeshit I say!
  486.  
  487.  
  488.           try{
  489.               primaryProvider.setEmail(username, email);
  490.               return;
  491.           } catch (Exception e) {}
  492.  
  493.       if (secondaryProvider!=null){
  494.           try{
  495.               secondaryProvider.setEmail(username, email);
  496.               return;
  497.           }
  498.  
  499.           catch (Exception e) {}
  500.       }
  501.       if (tertiaryProvider!=null){
  502.           try{
  503.               tertiaryProvider.setEmail(username, email);
  504.               return;
  505.           }
  506.  
  507.           catch (Exception e) {throw new UserNotFoundException();}
  508.       }
  509.  
  510.   }
  511.  
  512.  
  513.   public void setModificationDate(String username, Date modificationDate) throws UserNotFoundException {
  514.  
  515.       //without it eclipse goes apeshit
  516.       if (primaryProvider!=null)
  517.           //apeshit I say!
  518.  
  519.  
  520.           try{
  521.               primaryProvider.setModificationDate(username, modificationDate);
  522.               return;
  523.           } catch (Exception e) {}
  524.  
  525.       if (secondaryProvider!=null){
  526.           try{
  527.               secondaryProvider.setModificationDate(username, modificationDate);
  528.               return;
  529.           }
  530.  
  531.           catch (Exception e) {}
  532.       }
  533.       if (tertiaryProvider!=null){
  534.           try{
  535.               tertiaryProvider.setModificationDate(username, modificationDate);
  536.               return;
  537.           }
  538.  
  539.           catch (Exception e) {throw new UserNotFoundException();}
  540.       }
  541.  
  542.  
  543.   }
  544.   public void setName(String username, String name) throws UserNotFoundException {
  545.       //without it eclipse goes apeshit
  546.       if (primaryProvider!=null)
  547.           //apeshit I say!
  548.  
  549.  
  550.           try{
  551.               primaryProvider.setName(username, name);
  552.               return;
  553.           } catch (Exception e) {}
  554.  
  555.       if (secondaryProvider!=null){
  556.           try{
  557.               secondaryProvider.setName(username, name);
  558.               return;
  559.           }
  560.  
  561.           catch (Exception e) {}
  562.       }
  563.       if (tertiaryProvider!=null){
  564.           try{
  565.               tertiaryProvider.setName(username, name);
  566.               return;
  567.           }
  568.  
  569.           catch (Exception e) {throw new UserNotFoundException();}
  570.       }
  571.   }
  572. }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: