001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.service;
020
021import org.apache.oozie.store.StoreException;
022import org.apache.oozie.store.SLAStore;
023import org.apache.oozie.store.Store;
024import org.apache.oozie.store.WorkflowStore;
025import org.apache.oozie.ErrorCode;
026import javax.persistence.EntityManager;
027
028/**
029 * Base service for persistency of jobs and actions.
030 */
031public class StoreService implements Service {
032
033    /**
034     * Return instance of store.
035     *
036     * @return {@link Store}.
037     */
038    @SuppressWarnings("unchecked")
039    public <S extends Store> S getStore(Class<S> klass) throws StoreException {
040        if (WorkflowStore.class.equals(klass)) {
041            return (S) Services.get().get(WorkflowStoreService.class).create();
042        }
043        else if (SLAStore.class.equals(klass)) {
044            return (S) Services.get().get(SLAStoreService.class).create();
045        }
046        // to do add checks for other stores - coordinator and SLA stores
047        throw new StoreException(ErrorCode.E0607, " can not get store StoreService.getStore(Class)", "");
048    }
049
050    /**
051     * Return instance of store with an EntityManager pointing to an existing Store.
052     *
053     * @return {@link Store}.
054     */
055    @SuppressWarnings("unchecked")
056    public <S extends Store, T extends Store> S getStore(Class<S> klass, T store) throws StoreException {
057        if (WorkflowStore.class.equals(klass)) {
058            return (S) Services.get().get(WorkflowStoreService.class).create(store);
059        }
060        else if (SLAStore.class.equals(klass)) {
061            return (S) Services.get().get(SLAStoreService.class).create(store);
062        }
063        throw new StoreException(ErrorCode.E0607, " StoreService.getStore(Class, store)", "");
064    }
065
066    /**
067     * Return the public interface of the service.
068     *
069     * @return {@link StoreService}.
070     */
071    public Class<? extends Service> getInterface() {
072        return StoreService.class;
073    }
074
075    private JPAService jpaService;
076
077    /**
078     * Initializes the {@link StoreService}.
079     *
080     * @param services services instance.
081     */
082    public void init(Services services) throws ServiceException {
083        jpaService = Services.get().get(JPAService.class);
084        if (jpaService == null) {
085            throw new ServiceException(ErrorCode.E0610);
086        }
087    }
088
089    /**
090     * Destroy the StoreService
091     */
092    public void destroy() {
093    }
094
095    /**
096     * Return EntityManager
097     */
098    public EntityManager getEntityManager() {
099        return jpaService.getEntityManager();
100    }
101}