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