001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }