HybridUserProvider.java 17.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
/*
* HybridUserProvider.java
*
* Created on 16. April 2007, 21:48
* by Marc Seeger
* code works fine as far as my 10 User Test-Server goes
* It basically checks different userproviders which are being set in the configuration xml file
* 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
*
* Changed on 14. Nov. 2007, 10:48
* by Chris Neasbitt
*  -changed getUsers(int startIndex, int numResults) method to return a subset of the total users from all providers
*  -changed the getUsers() method to use a vector internally since addAll is an optional method of the collection
*   interface we cannot assume that all classes that support the collection interface also support the addAll method
*  -changed the getUserCount() method to iterate through an array of providers while calling a private helper method
*   getUserCount(UserProvider provider) on each of them.
*/

package org.jivesoftware.openfire.user;

import java.util.Collection;
import java.util.Vector;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.jivesoftware.util.ClassUtils;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.Log;

/**
 * @author Marc Seeger
 * @author Chris Neasbitt
 */

public class HybridUserProvider implements UserProvider {

    private UserProvider primaryProvider = null;
    private UserProvider secondaryProvider = null;
    private UserProvider tertiaryProvider = null;
    private UserProvider[] userproviders = {primaryProvider, secondaryProvider, tertiaryProvider};

    private Set<String> primaryOverrides = new HashSet<String>();
    private Set<String> secondaryOverrides = new HashSet<String>();
    private Set<String> tertiaryOverrides = new HashSet<String>();


    public HybridUserProvider() {
// Load primary, secondary, and tertiary user providers.
        String primaryClass = JiveGlobals.getXMLProperty("hybridUserProvider.primaryProvider.className");
        if (primaryClass == null) {
            Log.error("A primary UserProvider must be specified in the openfire.xml.");
            return;
        }
        try {
            Class c = ClassUtils.forName(primaryClass);
            primaryProvider = (UserProvider) c.newInstance();
            Log.debug("Primary user provider: " + primaryClass);
        } catch (Exception e) {
            Log.error("Unable to load primary user provider: " + primaryClass +
                    ". Users in this provider will be disabled.", e);
            return;
        }
        String secondaryClass = JiveGlobals.getXMLProperty("hybridUserProvider.secondaryProvider.className");
        if (secondaryClass != null) {
            try {
                Class c = ClassUtils.forName(secondaryClass);
                secondaryProvider = (UserProvider) c.newInstance();
                Log.debug("Secondary user provider: " + secondaryClass);
            } catch (Exception e) {
                Log.error("Unable to load secondary user provider: " + secondaryClass, e);
            }
        }
        String tertiaryClass = JiveGlobals.getXMLProperty("hybridUserProvider.tertiaryProvider.className");
        if (secondaryClass != null) {
            try {
                Class c = ClassUtils.forName(secondaryClass);
                tertiaryProvider = (UserProvider) c.newInstance();
                Log.debug("Secondary user provider: " + secondaryClass);
            } catch (Exception e) {
                Log.error("Unable to load tertiary user provider: " + tertiaryClass, e);
            }
        }

        // Now, load any overrides.
        String overrideList = JiveGlobals.getXMLProperty(
                "hybridUserProvider.primaryProvider.overrideList", "");
        for (String user : overrideList.split(",")) {
            primaryOverrides.add(user.trim().toLowerCase());
        }

        if (secondaryProvider != null) {
            overrideList = JiveGlobals.getXMLProperty(
                    "hybridUserProvider.secondaryProvider.overrideList", "");
            for (String user : overrideList.split(",")) {
                secondaryOverrides.add(user.trim().toLowerCase());
            }
        }

        if (tertiaryProvider != null) {
            overrideList = JiveGlobals.getXMLProperty(
                    "hybridUserProvider.tertiaryProvider.overrideList", "");
            for (String user : overrideList.split(",")) {
                tertiaryOverrides.add(user.trim().toLowerCase());
            }
        }

    }


    public User createUser(String username, String password, String name, String email) throws UserAlreadyExistsException {
        //initialize our returnvalue
        User returnvalue = null;

        //try to use the providers to create a user and change the return value to that user
        if (!primaryProvider.isReadOnly()) {
            try {
                returnvalue = primaryProvider.createUser(username, password, name, email);
            }

            finally {
            }

        } else if (secondaryProvider != null) {
            if (!secondaryProvider.isReadOnly()) {
                try {
                    returnvalue = secondaryProvider.createUser(username, password, name, email);
                }

                finally {
                }

            }
        } else if (tertiaryProvider != null) {
            if (!tertiaryProvider.isReadOnly()) {
                try {
                    returnvalue = tertiaryProvider.createUser(username, password, name, email);
                }

                finally {
                }
            }
        }

        //return our created user
        if (returnvalue != null) {
            return returnvalue;
        } else {
            throw new UnsupportedOperationException();
        }
    }


    public void deleteUser(String username) {
        if (!primaryProvider.isReadOnly()) {
            try {
                primaryProvider.deleteUser(username);
                return;
            }

            finally {
            }

        } else if (secondaryProvider != null) {
            if (!secondaryProvider.isReadOnly()) {
                try {
                    secondaryProvider.deleteUser(username);
                    return;
                }

                finally {
                }

            }
        } else if (tertiaryProvider != null) {
            if (!tertiaryProvider.isReadOnly()) {
                try {
                    tertiaryProvider.deleteUser(username);
                    return;
                }

                finally {
                }

            } else {
// Reject the operation since all of the providers seem to be read-only
                throw new UnsupportedOperationException();
            }

        }
    }


    public Collection<User> findUsers(Set<String> fields, String query) throws UnsupportedOperationException {


        Collection<User> returnvalue = null;
        try {
            returnvalue = primaryProvider.findUsers(fields, query);
        }

        finally {
        }

        if (secondaryProvider != null) {
            try {

                returnvalue = secondaryProvider.findUsers(fields, query);
            }

            finally {
            }
        }
        if (tertiaryProvider != null) {
            try {
                returnvalue = tertiaryProvider.findUsers(fields, query);
            }

            finally {
            }
        }

        //return our collection of users
        if (returnvalue != null) {
            return returnvalue;
        } else {
            throw new UnsupportedOperationException();
        }
    }


    public Collection<User> findUsers(Set<String> fields, String query, int startIndex, int numResults) throws UnsupportedOperationException {
        Collection<User> returnvalue = null;
        try {
            returnvalue = primaryProvider.findUsers(fields, query, startIndex, numResults);
        }

        finally {
        }

        if (secondaryProvider != null) {
            try {

                returnvalue = secondaryProvider.findUsers(fields, query, startIndex, numResults);
            }

            finally {
            }
        }
        if (tertiaryProvider != null) {
            try {
                returnvalue = tertiaryProvider.findUsers(fields, query, startIndex, numResults);
            }

            finally {
            }
        }

        //return our Collection of Users
        if (returnvalue != null) {
            return returnvalue;
        } else {
            throw new UnsupportedOperationException();
        }
    }


    public Set<String> getSearchFields() throws UnsupportedOperationException {
        Set<String> returnvalue = null;
        try {
            returnvalue = primaryProvider.getSearchFields();
        }

        finally {
        }

        if (secondaryProvider != null) {
            try {

                returnvalue = secondaryProvider.getSearchFields();
            }

            finally {
            }
        }
        if (tertiaryProvider != null) {
            try {
                returnvalue = tertiaryProvider.getSearchFields();
            }

            finally {
            }
        }

        //return our Set of Strings
        if (returnvalue != null) {
            return returnvalue;
        } else {
            throw new UnsupportedOperationException();
        }
    }


    public int getUserCount() {
        int count = 0;
        for (UserProvider provider : userproviders) {
            count = count + this.getUserCount(provider);
        }
        return count;
    }

    private int getUserCount(UserProvider provider) {
        int returnvalue = 0;
        if (provider != null) {
            try {

                returnvalue = returnvalue + provider.getUserCount();
            }

            finally {
            }
        }
        return returnvalue;
    }


    public Collection<String> getUsernames() {
        Collection<String> returnvalue = null;
        try {
            returnvalue = primaryProvider.getUsernames();
        }

        finally {
        }

        if (secondaryProvider != null) {
            try {
                returnvalue.addAll(secondaryProvider.getUsernames());
            }

            finally {
            }
        }
        if (tertiaryProvider != null) {
            try {
                returnvalue.addAll(tertiaryProvider.getUsernames());
            }

            finally {
            }
        }

        //return our Set of Strings
        if (returnvalue != null) {
            return returnvalue;
        } else {
            throw new UnsupportedOperationException();
        }
    }


    public Collection<User> getUsers() {
        Vector<User> returnvalue = null;
        try {
            returnvalue = new Vector<User>(primaryProvider.getUsers());
        }

        finally {
        }

        if (secondaryProvider != null) {
            try {
                returnvalue.addAll(secondaryProvider.getUsers());
            }

            finally {
            }
        }
        if (tertiaryProvider != null) {
            try {
                returnvalue.addAll(tertiaryProvider.getUsers());
            }

            finally {
            }
        }

        //return our Set of Strings
        if (returnvalue != null) {
            return returnvalue;
        } else {
            throw new UnsupportedOperationException();
        }
    }

/*
*Changed by Chris Neasbitt to more accurately represent the intent of the method
*
*This method now removes a sub set of the combined users from all providers.  This
*is done in places as to avoid copying collections of users in memory.
*/

    public Collection<User> getUsers(int startIndex, int numResults) {
        Vector<User> returnresult = new Vector<User>();
        int numResultsLeft = numResults;
        int currentStartIndex = startIndex;
        for (UserProvider provider : userproviders) {
            if (numResultsLeft == 0) {
                break;
            }

            int pusercount = this.getUserCount(provider);

            if (pusercount == 0 || currentStartIndex >= pusercount) {

                currentStartIndex = currentStartIndex - pusercount;
                continue;

            } else {

                Collection<User> subresult = provider.getUsers(currentStartIndex, numResultsLeft);
                currentStartIndex = currentStartIndex - subresult.size();
                numResultsLeft = numResultsLeft - subresult.size();
                returnresult.addAll(subresult);
            }
        }

        return returnresult;
    }

    public boolean isReadOnly() {
        return false;
    }

    public boolean isNameRequired() {
        return false;
    }

    public boolean isEmailRequired() {
        return false;
    }

    public User loadUser(String username) throws UserNotFoundException {
        try {
            return primaryProvider.loadUser(username);

        }

        catch (Exception e) {
        }

        if (secondaryProvider != null) {
            try {

                return secondaryProvider.loadUser(username);
            }

            catch (Exception e) {
            }
        }


        if (tertiaryProvider != null) {
            try {
                return tertiaryProvider.loadUser(username);
            }

            catch (Exception e) {
            }
        }

        //if we get this far, no provider seems to successfully have loaded the user
        throw new UserNotFoundException();
    }

    public void setCreationDate(String username, Date creationDate) throws UserNotFoundException {
        if (primaryProvider != null)
            try {
                primaryProvider.setCreationDate(username, creationDate);
                return;
            } catch (Exception e) {
            }

        if (secondaryProvider != null) {
            try {
                secondaryProvider.setCreationDate(username, creationDate);
                return;
            }

            catch (Exception e) {
            }
        }
        if (tertiaryProvider != null) {
            try {
                tertiaryProvider.setCreationDate(username, creationDate);
                return;
            }

            catch (Exception e) {
                throw new UserNotFoundException();
            }
        }


    }

    public void setEmail(String username, String email) throws UserNotFoundException {
        if (primaryProvider != null)
            try {
                primaryProvider.setEmail(username, email);
                return;
            } catch (Exception e) {
            }

        if (secondaryProvider != null) {
            try {
                secondaryProvider.setEmail(username, email);
                return;
            }

            catch (Exception e) {
            }
        }
        if (tertiaryProvider != null) {
            try {
                tertiaryProvider.setEmail(username, email);
                return;
            }

            catch (Exception e) {
                throw new UserNotFoundException();
            }
        }

    }


    public void setModificationDate(String username, Date modificationDate) throws UserNotFoundException {

        //without it eclipse goes apeshit
        if (primaryProvider != null)
            //apeshit I say!


            try {
                primaryProvider.setModificationDate(username, modificationDate);
                return;
            } catch (Exception e) {
            }

        if (secondaryProvider != null) {
            try {
                secondaryProvider.setModificationDate(username, modificationDate);
                return;
            }

            catch (Exception e) {
            }
        }
        if (tertiaryProvider != null) {
            try {
                tertiaryProvider.setModificationDate(username, modificationDate);
                return;
            }

            catch (Exception e) {
                throw new UserNotFoundException();
            }
        }


    }

    public void setName(String username, String name) throws UserNotFoundException {
        if (primaryProvider != null)
            try {
                primaryProvider.setName(username, name);
                return;
            } catch (Exception e) {
            }

        if (secondaryProvider != null) {
            try {
                secondaryProvider.setName(username, name);
                return;
            }

            catch (Exception e) {
            }
        }
        if (tertiaryProvider != null) {
            try {
                tertiaryProvider.setName(username, name);
                return;
            }

            catch (Exception e) {
                throw new UserNotFoundException();
            }
        }
    }
}