001/*
002 * Copyright (c) 2015 Maxim Yunusov
003 *    Licensed under the Apache License, Version 2.0 (the "License");
004 *    you may not use this file except in compliance with the License.
005 *    You may obtain a copy of the License at
006 *
007 *        http://www.apache.org/licenses/LICENSE-2.0
008 *
009 *    Unless required by applicable law or agreed to in writing, software
010 *    distributed under the License is distributed on an "AS IS" BASIS,
011 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 *    See the License for the specific language governing permissions and
013 *    limitations under the License.
014 */
015
016package org.maxur.perfmodel.backend;
017
018import org.glassfish.hk2.api.Factory;
019import org.glassfish.hk2.api.ServiceLocator;
020import org.maxur.perfmodel.backend.infrastructure.DataSourceLevelDbImpl;
021
022import javax.inject.Inject;
023
024/**
025 * @author myunusov
026 * @version 1.0
027 * @since <pre>12.09.2015</pre>
028 */
029public class DataSourceProvider implements Factory<DataSourceLevelDbImpl> {
030
031    private static DataSourceLevelDbImpl ds;
032
033    @Inject
034    private ServiceLocator locator;
035
036    @Override
037    public DataSourceLevelDbImpl provide() {
038        if (ds == null) {
039            ds = new DataSourceLevelDbImpl();
040            locator.inject(ds);
041            ds.init();
042        }
043        return ds;
044    }
045
046    @Override
047    public void dispose(final DataSourceLevelDbImpl instance) {
048        instance.stop();
049    }
050}