001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package edu.sampleu.travel.infrastructure;
017
018import org.apache.commons.lang.StringUtils;
019import org.apache.log4j.Logger;
020import org.springframework.context.ConfigurableApplicationContext;
021import org.springframework.context.support.ClassPathXmlApplicationContext;
022
023/**
024 * Initializes the Travel App Spring context.
025 * @author Kuali Rice Team (rice.collab@kuali.org)
026 */
027public class TravelServiceLocator {
028    private static final Logger LOG = Logger.getLogger(TravelServiceLocator.class);
029
030    private static final String STANDARD_CONTEXT = "classpath:SampleAppBeans.xml";
031    private static final String TEST_CONTEXT = "classpath:SampleAppBeans-test.xml";
032
033    private static ConfigurableApplicationContext appContext;
034
035        public static synchronized void initialize(boolean test) {
036                if (appContext == null) {
037                    final String[] resources;
038                    // check if we are running in unit tests, and if so, add the test context resource
039                    if (test) {
040                        resources = new String[] { STANDARD_CONTEXT, TEST_CONTEXT };
041                    } else {
042                        resources = new String[] { STANDARD_CONTEXT };
043                    }
044                    LOG.info("Loading contexts: " + StringUtils.join(resources, ", "));
045                        appContext = new ClassPathXmlApplicationContext(resources);
046                }
047        }
048
049        public static synchronized ConfigurableApplicationContext getAppContext() {
050                return appContext;
051        }       
052}