001    /**
002     * Copyright (c) 2000-present 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.portal.convert.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.model.BaseModel;
020    import com.liferay.portal.model.ModelHintsUtil;
021    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    /**
026     * @author Cristina Gonz??lez
027     */
028    public class HibernateModelUtil {
029    
030            public static List<Class<? extends BaseModel<?>>> getModelClassNames(
031                    ClassLoader classLoader, String regex) {
032    
033                    List<String> modelNames = ModelHintsUtil.getModels();
034    
035                    List<Class<? extends BaseModel<?>>> implClassNames = new ArrayList<>();
036    
037                    for (String modelName : modelNames) {
038                            if (!modelName.contains(".model.")) {
039                                    continue;
040                            }
041    
042                            String implClassName = modelName.replaceFirst(
043                                    "(\\.model\\.)(\\p{Upper}.*)", "$1impl.$2Impl");
044    
045                            if (implClassName.matches(regex)) {
046                                    Class<? extends BaseModel<?>> implClass = getImplClass(
047                                            classLoader, implClassName);
048    
049                                    if (implClass != null) {
050                                            implClassNames.add(implClass);
051                                    }
052                            }
053                    }
054    
055                    return implClassNames;
056            }
057    
058            protected static Class<? extends BaseModel<?>> getImplClass(
059                    ClassLoader classLoader, String implClassName) {
060    
061                    try {
062                            return (Class<? extends BaseModel<?>>)classLoader.loadClass(
063                                    implClassName);
064                    }
065                    catch (Exception e) {
066                            if (_log.isDebugEnabled()) {
067                                    _log.debug(e, e);
068                            }
069                    }
070    
071                    return null;
072            }
073    
074            private static final Log _log = LogFactoryUtil.getLog(
075                    HibernateModelUtil.class);
076    
077    }