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.service.impl;
016    
017    import com.liferay.portal.kernel.cache.CacheRegistryItem;
018    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019    import com.liferay.portal.kernel.spring.aop.Skip;
020    import com.liferay.portal.kernel.transaction.Propagation;
021    import com.liferay.portal.kernel.transaction.Transactional;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.ClassName;
024    import com.liferay.portal.model.ModelHintsUtil;
025    import com.liferay.portal.model.impl.ClassNameImpl;
026    import com.liferay.portal.service.base.ClassNameLocalServiceBaseImpl;
027    
028    import java.util.List;
029    import java.util.Map;
030    import java.util.concurrent.ConcurrentHashMap;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class ClassNameLocalServiceImpl
036            extends ClassNameLocalServiceBaseImpl implements CacheRegistryItem {
037    
038            @Override
039            public ClassName addClassName(String value) {
040                    ClassName className = classNamePersistence.fetchByValue(value);
041    
042                    if (className == null) {
043                            long classNameId = counterLocalService.increment();
044    
045                            className = classNamePersistence.create(classNameId);
046    
047                            className.setValue(value);
048    
049                            classNamePersistence.update(className);
050                    }
051    
052                    return className;
053            }
054    
055            @Override
056            public void afterPropertiesSet() {
057                    super.afterPropertiesSet();
058    
059                    CacheRegistryUtil.register(this);
060            }
061    
062            @Override
063            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
064            public void checkClassNames() {
065                    List<ClassName> classNames = classNamePersistence.findAll();
066    
067                    for (ClassName className : classNames) {
068                            _classNames.put(className.getValue(), className);
069                    }
070    
071                    List<String> models = ModelHintsUtil.getModels();
072    
073                    for (String model : models) {
074                            getClassName(model);
075                    }
076            }
077    
078            @Override
079            public ClassName fetchClassName(String value) {
080                    if (Validator.isNull(value)) {
081                            return _nullClassName;
082                    }
083    
084                    ClassName className = _classNames.get(value);
085    
086                    if (className == null) {
087                            className = classNamePersistence.fetchByValue(value);
088    
089                            if (className == null) {
090                                    return _nullClassName;
091                            }
092    
093                            _classNames.put(value, className);
094                    }
095    
096                    return className;
097            }
098    
099            @Override
100            @Skip
101            public ClassName getClassName(String value) {
102                    if (Validator.isNull(value)) {
103                            return _nullClassName;
104                    }
105    
106                    // Always cache the class name. This table exists to improve
107                    // performance. Create the class name if one does not exist.
108    
109                    ClassName className = _classNames.get(value);
110    
111                    if (className == null) {
112                            try {
113                                    className = classNameLocalService.addClassName(value);
114    
115                                    _classNames.put(value, className);
116                            }
117                            catch (Throwable t) {
118                                    className = classNameLocalService.fetchClassName(value);
119    
120                                    if (className == _nullClassName) {
121                                            throw t;
122                                    }
123                            }
124                    }
125    
126                    return className;
127            }
128    
129            @Override
130            @Skip
131            public long getClassNameId(Class<?> clazz) {
132                    return getClassNameId(clazz.getName());
133            }
134    
135            @Override
136            @Skip
137            public long getClassNameId(String value) {
138                    ClassName className = getClassName(value);
139    
140                    return className.getClassNameId();
141            }
142    
143            @Override
144            public String getRegistryName() {
145                    return ClassNameLocalServiceImpl.class.getName();
146            }
147    
148            @Override
149            public void invalidate() {
150                    _classNames.clear();
151            }
152    
153            private static final Map<String, ClassName> _classNames =
154                    new ConcurrentHashMap<>();
155            private static final ClassName _nullClassName = new ClassNameImpl();
156    
157    }