001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.admin.messaging;
016    
017    import com.liferay.portal.kernel.messaging.BaseMessageListener;
018    import com.liferay.portal.kernel.messaging.Message;
019    import com.liferay.portal.model.Company;
020    import com.liferay.portal.security.ldap.LDAPSettingsUtil;
021    import com.liferay.portal.security.ldap.PortalLDAPImporterUtil;
022    import com.liferay.portal.service.CompanyLocalServiceUtil;
023    
024    import java.util.List;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class LDAPImportMessageListener extends BaseMessageListener {
030    
031            protected void doImportOnStartup() throws Exception {
032                    List<Company> companies = CompanyLocalServiceUtil.getCompanies(false);
033    
034                    for (Company company : companies) {
035                            long companyId = company.getCompanyId();
036    
037                            if (LDAPSettingsUtil.isImportOnStartup(companyId)) {
038                                    PortalLDAPImporterUtil.importFromLDAP(companyId);
039                            }
040                    }
041            }
042    
043            @Override
044            protected void doReceive(Message message) throws Exception {
045                    if (_startup) {
046                            _startup = false;
047    
048                            doImportOnStartup();
049                    }
050                    else {
051                            PortalLDAPImporterUtil.importFromLDAP();
052                    }
053            }
054    
055            private boolean _startup = true;
056    
057    }