001 /*
002 * Copyright 2009-2013 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
017 package org.codehaus.griffon.runtime.core;
018
019 import griffon.core.GriffonApplication;
020 import griffon.core.GriffonService;
021 import griffon.core.ServiceManager;
022 import griffon.core.ShutdownHandler;
023 import griffon.util.ConfigUtils;
024 import groovy.util.ConfigObject;
025 import org.codehaus.groovy.runtime.InvokerHelper;
026 import org.slf4j.Logger;
027 import org.slf4j.LoggerFactory;
028
029 import java.util.Map;
030
031 import static griffon.core.GriffonServiceClass.TRAILING;
032 import static java.util.Arrays.asList;
033
034 /**
035 * Base implementation of the {@code ServiceManager} interface.
036 *
037 * @author Andres Almiray
038 * @since 0.9.4
039 */
040 public abstract class AbstractServiceManager implements ServiceManager {
041 private final GriffonApplication app;
042 private static final Logger LOG = LoggerFactory.getLogger(AbstractServiceManager.class);
043
044 public AbstractServiceManager(GriffonApplication app) {
045 this.app = app;
046 app.addShutdownHandler(new ServiceManagerShutdownHandler());
047 }
048
049 public GriffonApplication getApp() {
050 return app;
051 }
052
053 @Override
054 public GriffonService findService(String name) {
055 if (!name.endsWith(TRAILING)) {
056 name += TRAILING;
057 }
058
059 GriffonService service = doFindService(name);
060 if (null == service) {
061 if (LOG.isInfoEnabled()) {
062 LOG.info("Instantiating service identified by '" + name + "'");
063 }
064 service = doInstantiateService(name);
065 if (null != service) {
066 InvokerHelper.setProperty(service, "app", getApp());
067 doSetConfigProperties(name, service);
068 if (LOG.isInfoEnabled()) {
069 LOG.info("Initializing service identified by '" + name + "'");
070 }
071 service.serviceInit();
072 }
073 }
074 return service;
075 }
076
077 protected void doSetConfigProperties(String name, GriffonService service) {
078 if (LOG.isInfoEnabled()) {
079 LOG.info("Applying configuration to service identified by '" + name + "'");
080 }
081 name = name.substring(0, name.length() - TRAILING.length());
082 ConfigObject config = (ConfigObject) ConfigUtils.getConfigValue(getApp().getConfig(), "services." + name);
083 if (config != null && !config.isEmpty()) InvokerHelper.setProperties(service, config);
084 }
085
086 protected abstract GriffonService doFindService(String name);
087
088 protected abstract GriffonService doInstantiateService(String name);
089
090 private class ServiceManagerShutdownHandler implements ShutdownHandler {
091 @Override
092 public boolean canShutdown(GriffonApplication application) {
093 return true;
094 }
095
096 @Override
097 public void onShutdown(GriffonApplication application) {
098 for (Map.Entry<String, GriffonService> entry : getServices().entrySet()) {
099 if (LOG.isInfoEnabled()) {
100 LOG.info("Destroying service identified by '" + entry.getKey() + "'");
101 }
102 GriffonService service = entry.getValue();
103 application.removeApplicationEventListener(service);
104 application.event(GriffonApplication.Event.DESTROY_INSTANCE.getName(), asList(service.getClass(), service.getGriffonClass().getArtifactType(), service));
105 service.serviceDestroy();
106 }
107 }
108 }
109 }
|