001    /**
002     * Copyright (c) 2000-2012 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    import com.liferay.portal.security.lang.PortalSecurityManagerThreadLocal;
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 (!(obj instanceof ComponentConfiguration)) {
048                            return false;
049                    }
050    
051                    if (this == obj) {
052                            return true;
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                    boolean enabled = PortalSecurityManagerThreadLocal.isEnabled();
130    
131                    try {
132                            PortalSecurityManagerThreadLocal.setEnabled(false);
133    
134                            _properties = _constructor.newInstance(
135                                    new Object[] {classLoaderAggregateProperties});
136                    }
137                    catch (Exception e) {
138                            _log.error(e, e);
139                    }
140                    finally {
141                            PortalSecurityManagerThreadLocal.setEnabled(enabled);
142                    }
143    
144                    return _properties;
145            }
146    
147            private static Log _log = LogFactoryUtil.getLog(
148                    ClassLoaderComponentConfiguration.class);
149    
150            private static Constructor<ComponentProperties> _constructor;
151    
152            static {
153                    try {
154                            _constructor = ComponentProperties.class.getDeclaredConstructor(
155                                    AggregatedProperties.class);
156    
157                            _constructor.setAccessible(true);
158                    }
159                    catch (Exception e) {
160                            _log.error(e, e);
161                    }
162            }
163    
164            private ClassLoader _classLoader;
165            private String _companyId;
166            private String _componentName;
167            private ComponentProperties _properties;
168    
169    }