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.configuration.easyconf;
016    
017    import com.germinus.easyconf.AggregatedProperties;
018    import com.germinus.easyconf.ComponentConfiguration;
019    import com.germinus.easyconf.ComponentProperties;
020    import com.germinus.easyconf.ConfigurationNotFoundException;
021    import com.germinus.easyconf.Conventions;
022    
023    import com.liferay.portal.kernel.exception.LoggedExceptionInInitializerError;
024    import com.liferay.portal.kernel.log.Log;
025    import com.liferay.portal.kernel.log.LogFactoryUtil;
026    import com.liferay.portal.kernel.util.SystemProperties;
027    
028    import java.lang.reflect.Constructor;
029    
030    /**
031     * @author Raymond Aug??
032     */
033    public class ClassLoaderComponentConfiguration extends ComponentConfiguration {
034    
035            public ClassLoaderComponentConfiguration(
036                    ClassLoader classLoader, String companyId, String componentName) {
037    
038                    super(companyId, componentName);
039    
040                    _classLoader = classLoader;
041                    _companyId = companyId;
042                    _componentName = componentName;
043            }
044    
045            @Override
046            public boolean equals(Object obj) {
047                    if (this == obj) {
048                            return true;
049                    }
050    
051                    if (!(obj instanceof ComponentConfiguration)) {
052                            return false;
053                    }
054    
055                    ComponentConfiguration componentConfiguration =
056                            (ComponentConfiguration)obj;
057    
058                    return _componentName.equals(componentConfiguration.getComponentName());
059            }
060    
061            @Override
062            public String getComponentName() {
063                    return _componentName;
064            }
065    
066            @Override
067            public Object getConfigurationObject() {
068                    throw new UnsupportedOperationException();
069            }
070    
071            @Override
072            public Object getConfigurationObject(String configurationName) {
073                    throw new UnsupportedOperationException();
074            }
075    
076            @Override
077            public ComponentProperties getProperties() {
078                    ComponentProperties componentProperties = _getAvailableProperties();
079    
080                    if (!componentProperties.hasBaseConfiguration()) {
081                            throw new ConfigurationNotFoundException(
082                                    _componentName, "The base properties file was not found");
083                    }
084    
085                    return componentProperties;
086            }
087    
088            @Override
089            public int hashCode() {
090                    return _componentName.hashCode();
091            }
092    
093            @Override
094            public void saveConfigurationObject(Object configurationObject) {
095                    throw new UnsupportedOperationException();
096            }
097    
098            @Override
099            public void saveConfigurationObject(
100                    String confName, Object configurationObject) {
101    
102                    throw new UnsupportedOperationException();
103            }
104    
105            private ComponentProperties _getAvailableProperties() {
106                    if (_properties != null) {
107                            return _properties;
108                    }
109    
110                    SystemProperties.set("base.path", ".");
111    
112                    ClassLoaderAggregateProperties classLoaderAggregateProperties =
113                            new ClassLoaderAggregateProperties(
114                                    _classLoader, _companyId, _componentName);
115    
116                    classLoaderAggregateProperties.addGlobalFileName(
117                            Conventions.GLOBAL_CONFIGURATION_FILE +
118                                    Conventions.PROPERTIES_EXTENSION);
119    
120                    classLoaderAggregateProperties.addBaseFileName(
121                            _componentName + Conventions.PROPERTIES_EXTENSION);
122    
123                    if (_log.isInfoEnabled()) {
124                            _log.info(
125                                    "Properties for " + _componentName + " loaded from " +
126                                            classLoaderAggregateProperties.loadedSources());
127                    }
128    
129                    try {
130                            _properties = _CONSTRUCTOR.newInstance(
131                                    new Object[] {classLoaderAggregateProperties});
132                    }
133                    catch (Exception e) {
134                            _log.error(e, e);
135                    }
136    
137                    return _properties;
138            }
139    
140            private static final Constructor<ComponentProperties> _CONSTRUCTOR;
141    
142            private static final Log _log = LogFactoryUtil.getLog(
143                    ClassLoaderComponentConfiguration.class);
144    
145            static {
146                    Constructor<ComponentProperties> constructor = null;
147    
148                    try {
149                            constructor = ComponentProperties.class.getDeclaredConstructor(
150                                    AggregatedProperties.class);
151    
152                            constructor.setAccessible(true);
153                    }
154                    catch (Exception e) {
155                            throw new LoggedExceptionInInitializerError(e);
156                    }
157    
158                    _CONSTRUCTOR = constructor;
159            }
160    
161            private final ClassLoader _classLoader;
162            private final String _companyId;
163            private final String _componentName;
164            private ComponentProperties _properties;
165    
166    }