1/*2 * Copyright 2001-2008 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 *16 */17package org.apache.juddi.config;
1819import java.io.File;
20import java.net.MalformedURLException;
21import java.net.URL;
22import java.util.List;
23import java.util.Properties;
2425import javax.persistence.EntityManager;
26import javax.persistence.EntityTransaction;
2728import org.apache.commons.configuration.CompositeConfiguration;
29import org.apache.commons.configuration.Configuration;
30import org.apache.commons.configuration.ConfigurationException;
31import org.apache.commons.configuration.MapConfiguration;
32import org.apache.commons.configuration.SystemConfiguration;
33import org.apache.commons.configuration.XMLConfiguration;
34import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
35import org.apache.commons.logging.Log;
36import org.apache.commons.logging.LogFactory;
37import org.apache.juddi.ClassUtil;
38import org.apache.juddi.Registry;
39import org.apache.juddi.keygen.KeyGenerator;
40import org.apache.juddi.model.UddiEntityPublisher;
4142/**43 * Handles the application level configuration for jUDDI. By default it first44 * looks at system properties (juddi.propertiesFile)45 * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a>46 * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>47 */48publicclassAppConfig49 {
50/**51 * This system property's value should be a path to a configuration file52 */53publicstaticfinal String JUDDI_CONFIGURATION_FILE_SYSTEM_PROPERTY="juddi.propertiesFile";
54/**55 * The default configuration file name for juddi56 */57publicstaticfinal String JUDDI_PROPERTIES = "juddiv3.xml";
58private Log log = LogFactory.getLog(AppConfig.class);
59private Configuration config;
60privatestaticAppConfig instance=null;
61privatestatic URL loadedFrom=null;
62privatestatic XMLConfiguration propConfig=null;
6364/**65 * Enables an administrator to identify the physical location of the configuration file from which it was loaded.<br>66 * Always call via the singleton function AppConfig.getInstance().getConfigFileURL()67 * @since 3.268 * @return may return null if no config file was found69 */70publicstatic URL getConfigFileURL()
71 {
72return loadedFrom;
73 }
7475/**76 * Constructor (note Singleton pattern).77 * @throws ConfigurationException78 */79privateAppConfig() throws ConfigurationException
80 {
81 loadConfiguration();
82 }
83publicstaticvoid setJuddiProperty(String key, Object val) throws ConfigurationException{
84if (instance==null) {
85 instance = newAppConfig();
86 }
87 propConfig.setProperty(key, val);
88 propConfig.save();
89 }
9091publicstaticvoid saveConfiguration() throws ConfigurationException{
92 getConfiguration(); //findbugs will flag this as useless, but its not93 propConfig.save();
94 }
959697/**98 * Does the actual work of reading the configuration from System99 * Properties and/or juddiv3.xml file. When the juddiv3.xml100 * file is updated the file will be reloaded. By default the reloadDelay is101 * set to 1 second to prevent excessive date stamp checking.102 */103privatevoid loadConfiguration() throws ConfigurationException
104 {
105//Properties from system properties106 CompositeConfiguration compositeConfig = new CompositeConfiguration();
107 compositeConfig.addConfiguration(new SystemConfiguration());
108//Properties from file109//changed 7-19-2013 AO for JUDDI-627110 propConfig = null;
111final String filename = System.getProperty(JUDDI_CONFIGURATION_FILE_SYSTEM_PROPERTY);
112if (filename != null) {
113 propConfig = new XMLConfiguration (filename);
114try {
115 loadedFrom = new File(filename).toURI().toURL();
116// propConfig = new PropertiesConfiguration(filename);117 } catch (MalformedURLException ex) {
118try {
119 loadedFrom = new URL("file://" + filename);
120 } catch (MalformedURLException ex1) {
121 log.warn("unable to get an absolute path to " + filename + ". This may be ignorable if everything works properly.", ex1);
122 }
123 }
124 } else {
125//propConfig = new PropertiesConfiguration(JUDDI_PROPERTIES);126 propConfig = new XMLConfiguration(JUDDI_PROPERTIES);
127 loadedFrom = ClassUtil.getResource(JUDDI_PROPERTIES, this.getClass());
128 }
129//Hey! this may break things130 propConfig.setAutoSave(true);
131132 log.info("Reading from jUDDI config file from: " + loadedFrom);
133long refreshDelay = propConfig.getLong(Property.JUDDI_CONFIGURATION_RELOAD_DELAY, 1000l);
134 log.debug("Setting refreshDelay to " + refreshDelay);
135 FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
136 fileChangedReloadingStrategy.setRefreshDelay(refreshDelay);
137 propConfig.setReloadingStrategy(fileChangedReloadingStrategy);
138 compositeConfig.addConfiguration(propConfig);
139140141 Properties properties = new Properties();
142if ("Hibernate".equals(propConfig.getString(Property.PERSISTENCE_PROVIDER))) {
143if (propConfig.containsKey(Property.DATASOURCE))
144 properties.put("hibernate.connection.datasource",propConfig.getString(Property.DATASOURCE));
145if (propConfig.containsKey(Property.HBM_DDL_AUTO))
146 properties.put("hibernate.hbm2ddl.auto",propConfig.getString(Property.HBM_DDL_AUTO));
147if (propConfig.containsKey(Property.DEFAULT_SCHEMA))
148 properties.put("hibernate.default_schema",propConfig.getString(Property.DEFAULT_SCHEMA));
149if (propConfig.containsKey(Property.HIBERNATE_DIALECT))
150 properties.put("hibernate.dialect",propConfig.getString(Property.HIBERNATE_DIALECT));
151 }
152// initialize the entityManagerFactory.153 PersistenceManager.initializeEntityManagerFactory(propConfig.getString(Property.JUDDI_PERSISTENCEUNIT_NAME), properties);
154// Properties from the persistence layer 155 MapConfiguration persistentConfig = new MapConfiguration(getPersistentConfiguration(compositeConfig));
156157 compositeConfig.addConfiguration(persistentConfig);
158//Making the new configuration globally accessible.159 config = compositeConfig;
160 }
161162/*163 * This method will build any "persisted" properties. Persisted properties are those that are stored in the database. These values164 * should be stored when the application is installed. If they don't exist, then an error should occur.165 */166private Properties getPersistentConfiguration(Configuration config) throws ConfigurationException {
167 Properties result = new Properties();
168169 EntityManager em = PersistenceManager.getEntityManager();
170 EntityTransaction tx = em.getTransaction();
171try {
172boolean seedAlways = config.getBoolean("juddi.seed.always",false);
173if (seedAlways || !Install.alreadyInstalled(config)) {
174if (seedAlways) {
175 log.info("Installing UDDI seed data, loading...");
176 } else {
177 log.info("The 'root' publisher was not found, loading...");
178 }
179try {
180 Install.install(config);
181 } catch (Exception e) {
182thrownew ConfigurationException(e);
183 } catch (Throwable t) {
184thrownew ConfigurationException(t);
185 }
186 }
187188 tx.begin();
189190 String rootPublisherStr = config.getString(Property.JUDDI_ROOT_PUBLISHER);
191 UddiEntityPublisher rootPublisher = new UddiEntityPublisher(rootPublisherStr);
192 rootPublisher.populateKeyGeneratorKeys(em);
193 List<String> rootKeyGenList = rootPublisher.getKeyGeneratorKeys();
194if (rootKeyGenList == null || rootKeyGenList.size() == 0)
195thrownew ConfigurationException("The 'root' publisher key generator was not found. Please make sure that the application is properly installed.");
196197 String rootKeyGen = rootKeyGenList.iterator().next();
198//rootKeyGen = rootKeyGen.substring((KeyGenerator.UDDI_SCHEME + KeyGenerator.PARTITION_SEPARATOR).length());199 rootKeyGen = rootKeyGen.substring(0, rootKeyGen.length() - (KeyGenerator.PARTITION_SEPARATOR + KeyGenerator.KEYGENERATOR_SUFFIX).length());
200 log.debug("root partition: " + rootKeyGen);
201202 result.setProperty(Property.JUDDI_ROOT_PARTITION, rootKeyGen);
203204// The node Id is defined as the business key of the business entity categorized as a node. This entity is saved as part of the install.205// Only one business entity should be categorized as a node.206 String nodeId = config.getString(Property.JUDDI_NODE_ID);
207if (nodeId==null)
208 log.fatal("Error! " + Property.JUDDI_NODE_ID + " is not defined in the config!");
209else210 result.setProperty(Property.JUDDI_NODE_ID, nodeId);
211/*212 CategoryBag categoryBag = new CategoryBag();213 KeyedReference keyedRef = new KeyedReference();214 keyedRef.setTModelKey(Constants.NODE_CATEGORY_TMODEL);215 keyedRef.setKeyValue(Constants.NODE_KEYVALUE);216 categoryBag.getKeyedReference().add(keyedRef);217 List<?> keyList = FindBusinessByCategoryQuery.select(em, new FindQualifiers(), categoryBag, null);218 if (keyList != null && keyList.size() > 1)219 {220 StringBuilder sb = new StringBuilder();221 Iterator<?> iterator = keyList.iterator();222 while(iterator.hasNext()){223 sb.append(iterator.next()).append(",");224 }225 //226 //throw new ConfigurationException("Only one business entity can be categorized as the node. Config loaded from " + loadedFrom + " Key's listed at the node: " + sb.toString());227 //unless of course, we are in a replicated environment228 }229 if (keyList != null && keyList.size() > 0) {230 nodeId = (String)keyList.get(0);231 }232 else233 throw new ConfigurationException("A node business entity was not found. Please make sure that the application is properly installed.");234 */235 String rootbiz=config.getString(Property.JUDDI_NODE_ROOT_BUSINESS);
236if (rootbiz==null)
237 log.fatal("Error! " + Property.JUDDI_NODE_ROOT_BUSINESS + " is not defined in the config");
238else239 result.setProperty(Property.JUDDI_NODE_ROOT_BUSINESS, rootbiz);
240241242243 tx.commit();
244return result;
245 } finally {
246if (tx.isActive()) {
247 tx.rollback();
248 }
249 em.close();
250 }
251 }
252253254/**255 * Obtains the reference to the Singleton instance.256 * 257 * @return the APplicationConfuration Singleton Instance.258 * @throws ConfigurationException259 */260publicstaticAppConfig getInstance() throws ConfigurationException
261 {
262if (instance==null) {
263 instance = newAppConfig();
264 }
265return instance;
266 }
267/**268 * Hook to receive configuration reload events from an external application.269 * 270 * @throws ConfigurationException271 */272publicstaticvoid reloadConfig() throws ConfigurationException
273 {
274 Registry.stop();
275 getInstance().loadConfiguration();
276 Registry.start();
277 }
278279publicstaticvoid triggerReload() throws ConfigurationException{
280 getInstance().loadConfiguration();
281 }
282/**283 * The object from which property values can be obtained.284 * @return the commons Configuration interface285 * @throws ConfigurationException 286 */287publicstatic Configuration getConfiguration() throws ConfigurationException
288 {
289return getInstance().config;
290 }
291 }