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 */
018package org.apache.oozie.service;
019
020import org.apache.oozie.store.StoreException;
021import org.apache.oozie.service.Service;
022import org.apache.oozie.service.Services;
023import org.apache.oozie.store.SLAStore;
024import org.apache.oozie.store.Store;
025import org.apache.oozie.store.WorkflowStore;
026import org.apache.oozie.store.CoordinatorStore;
027import org.apache.oozie.ErrorCode;
028import javax.persistence.EntityManager;
029
030/**
031 * Base service for persistency of jobs and actions.
032 */
033public class StoreService implements Service {
034
035    /**
036     * Return instance of store.
037     *
038     * @return {@link Store}.
039     */
040    @SuppressWarnings("unchecked")
041    public <S extends Store> S getStore(Class<S> klass) throws StoreException {
042        if (WorkflowStore.class.equals(klass)) {
043            return (S) Services.get().get(WorkflowStoreService.class).create();
044        }
045        else {
046            if (CoordinatorStore.class.equals(klass)) {
047                return (S) Services.get().get(CoordinatorStoreService.class).create();
048            }
049            else {
050                if (SLAStore.class.equals(klass)) {
051                    return (S) Services.get().get(SLAStoreService.class).create();
052                }
053            }
054        }
055        // to do add checks for other stores - coordinator and SLA stores
056        throw new StoreException(ErrorCode.E0607, " can not get store StoreService.getStore(Class)", "");
057    }
058
059    /**
060     * Return instance of store with an EntityManager pointing to an existing Store.
061     *
062     * @return {@link Store}.
063     */
064    @SuppressWarnings("unchecked")
065    public <S extends Store, T extends Store> S getStore(Class<S> klass, T store) throws StoreException {
066        if (WorkflowStore.class.equals(klass)) {
067            return (S) Services.get().get(WorkflowStoreService.class).create(store);
068        }
069        else {
070            if (CoordinatorStore.class.equals(klass)) {
071                return (S) Services.get().get(CoordinatorStoreService.class).create(store);
072            }
073            else {
074                if (SLAStore.class.equals(klass)) {
075                    return (S) Services.get().get(SLAStoreService.class).create(store);
076                }
077            }
078        }
079        throw new StoreException(ErrorCode.E0607, " StoreService.getStore(Class, store)", "");
080    }
081
082    /**
083     * Return the public interface of the service.
084     *
085     * @return {@link StoreService}.
086     */
087    public Class<? extends Service> getInterface() {
088        return StoreService.class;
089    }
090
091    private JPAService jpaService;
092
093    /**
094     * Initializes the {@link StoreService}.
095     *
096     * @param services services instance.
097     */
098    public void init(Services services) throws ServiceException {
099        jpaService = Services.get().get(JPAService.class);
100        if (jpaService == null) {
101            throw new ServiceException(ErrorCode.E0610);
102        }
103    }
104
105    /**
106     * Destroy the StoreService
107     */
108    public void destroy() {
109    }
110
111    /**
112     * Return EntityManager
113     */
114    public EntityManager getEntityManager() {
115        return jpaService.getEntityManager();
116    }
117}