001 /*
002 * Copyright 2014-2017 the original author or authors.
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 */
016 package org.codehaus.griffon.runtime.ebean;
017
018 import com.avaje.ebean.EbeanServer;
019 import griffon.plugins.ebean.EbeanServerCallback;
020 import griffon.plugins.ebean.EbeanServerHandler;
021 import griffon.plugins.ebean.EbeanServerFactory;
022 import griffon.plugins.ebean.EbeanServerStorage;
023 import griffon.plugins.ebean.exceptions.RuntimeEbeanServerException;
024 import org.slf4j.Logger;
025 import org.slf4j.LoggerFactory;
026
027 import javax.annotation.Nonnull;
028 import javax.annotation.Nullable;
029 import javax.inject.Inject;
030
031 import static griffon.util.GriffonNameUtils.requireNonBlank;
032 import static java.util.Objects.requireNonNull;
033
034 /**
035 * @author Andres Almiray
036 */
037 public class DefaultEbeanServerHandler implements EbeanServerHandler {
038 private static final Logger LOG = LoggerFactory.getLogger(DefaultEbeanServerHandler.class);
039 private static final String ERROR_SESSION_FACTORY_NAME_BLANK = "Argument 'ebeanServerName' must not be blank";
040 private static final String ERROR_CALLBACK_NULL = "Argument 'callback' must not be null";
041
042 private final EbeanServerFactory ebeanServerFactory;
043 private final EbeanServerStorage ebeanServerStorage;
044
045 @Inject
046 public DefaultEbeanServerHandler(@Nonnull EbeanServerFactory ebeanServerFactory, @Nonnull EbeanServerStorage ebeanServerStorage) {
047 this.ebeanServerFactory = requireNonNull(ebeanServerFactory, "Argument 'ebeanServerFactory' must not be null");
048 this.ebeanServerStorage = requireNonNull(ebeanServerStorage, "Argument 'ebeanStorage' must not be null");
049 }
050
051 @Nullable
052 @Override
053 public <R> R withEbean(@Nonnull EbeanServerCallback<R> callback) throws RuntimeEbeanServerException {
054 return withEbean(DefaultEbeanServerFactory.KEY_DEFAULT, callback);
055 }
056
057 @Nullable
058 @Override
059 @SuppressWarnings("ThrowFromFinallyBlock")
060 public <R> R withEbean(@Nonnull String ebeanServerName, @Nonnull EbeanServerCallback<R> callback) throws RuntimeEbeanServerException {
061 requireNonBlank(ebeanServerName, ERROR_SESSION_FACTORY_NAME_BLANK);
062 requireNonNull(callback, ERROR_CALLBACK_NULL);
063
064 EbeanServer ebeanServer = getEbeanServer(ebeanServerName);
065 if (LOG.isDebugEnabled()) {
066 LOG.debug("Executing statements on ebeanServer '{}'", ebeanServerName);
067 }
068
069 try {
070 return callback.handle(ebeanServerName, ebeanServer);
071 } catch (Exception e) {
072 throw new RuntimeEbeanServerException(ebeanServerName, e);
073 }
074 }
075
076 @Override
077 public void closeEbean() {
078 closeEbean(DefaultEbeanServerFactory.KEY_DEFAULT);
079 }
080
081 @Override
082 public void closeEbean(@Nonnull String ebeanServerName) {
083 requireNonBlank(ebeanServerName, ERROR_SESSION_FACTORY_NAME_BLANK);
084 EbeanServer ebean = ebeanServerStorage.get(ebeanServerName);
085 if (ebean != null) {
086 ebeanServerFactory.destroy(ebeanServerName, ebean);
087 ebeanServerStorage.remove(ebeanServerName);
088 }
089 }
090
091 @Nonnull
092 private EbeanServer getEbeanServer(@Nonnull String ebeanServerName) {
093 EbeanServer ebeanServer = ebeanServerStorage.get(ebeanServerName);
094 if (ebeanServer == null) {
095 ebeanServer = ebeanServerFactory.create(ebeanServerName);
096 ebeanServerStorage.set(ebeanServerName, ebeanServer);
097 }
098 return ebeanServer;
099 }
100 }
|