01 /*
02 * Copyright 2014-2017 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.codehaus.griffon.runtime.ebean;
17
18 import com.avaje.ebean.EbeanServer;
19 import griffon.core.GriffonApplication;
20 import griffon.core.env.Metadata;
21 import griffon.inject.DependsOn;
22 import griffon.plugins.ebean.EbeanServerCallback;
23 import griffon.plugins.ebean.EbeanServerHandler;
24 import griffon.plugins.ebean.EbeanServerFactory;
25 import griffon.plugins.ebean.EbeanServerStorage;
26 import griffon.plugins.monitor.MBeanManager;
27 import org.codehaus.griffon.runtime.core.addon.AbstractGriffonAddon;
28 import org.codehaus.griffon.runtime.jmx.EbeanServerStorageMonitor;
29
30 import javax.annotation.Nonnull;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import java.util.Map;
34
35 import static griffon.util.ConfigUtils.getConfigValueAsBoolean;
36
37 /**
38 * @author Andres Almiray
39 */
40 @DependsOn("datasource")
41 @Named("ebean")
42 public class EbeanAddon extends AbstractGriffonAddon {
43 @Inject
44 private EbeanServerHandler ebeanServerHandler;
45
46 @Inject
47 private EbeanServerFactory ebeanServerFactory;
48
49 @Inject
50 private EbeanServerStorage ebeanServerStorage;
51
52 @Inject
53 private MBeanManager mbeanManager;
54
55 @Inject
56 private Metadata metadata;
57
58 @Override
59 public void init(@Nonnull GriffonApplication application) {
60 mbeanManager.registerMBean(new EbeanServerStorageMonitor(metadata, ebeanServerStorage));
61 }
62
63 public void onStartupStart(@Nonnull GriffonApplication application) {
64 for (String ebeanServerName : ebeanServerFactory.getEbeanServerNames()) {
65 Map<String, Object> config = ebeanServerFactory.getConfigurationFor(ebeanServerName);
66 if (getConfigValueAsBoolean(config, "connect_on_startup", false)) {
67 ebeanServerHandler.withEbean(ebeanServerName, new EbeanServerCallback<Void>() {
68 @Override
69 public Void handle(@Nonnull String ebeanServerName, @Nonnull EbeanServer ebeanServer) {
70 return null;
71 }
72 });
73 }
74 }
75 }
76
77 public void onShutdownStart(@Nonnull GriffonApplication application) {
78 for (String ebeanServerName : ebeanServerFactory.getEbeanServerNames()) {
79 ebeanServerHandler.closeEbean(ebeanServerName);
80 }
81 }
82 }
|