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