001/*
002 * Copyright (c) 2015 Maxim Yunusov
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.
014 */
015
016package org.maxur.perfmodel.backend.service;
017
018import org.jvnet.hk2.annotations.Contract;
019import org.slf4j.Logger;
020import org.slf4j.LoggerFactory;
021
022import javax.annotation.PostConstruct;
023import javax.inject.Inject;
024
025/**
026 * This interface represents Perfomance Model Standalone Application
027 *
028 * @author myunusov
029 * @version 1.0
030 * @since <pre>30.08.2015</pre>
031 */
032@Contract
033public abstract class Application {
034
035    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
036
037    @Inject
038    private WebServer webServer;
039
040    @Inject
041    private Database db;
042
043    @PostConstruct
044    public final void init() {
045        onInit();
046    }
047
048    public final void start() {
049        webServer.start();
050        onStart();
051        LOGGER.info("Performance Model Calculator Server is started");
052    }
053
054    public final void stop() {
055        webServer.stop();
056        db.stop();
057        onStop();
058        LOGGER.info("Performance Model Calculator Server is stopped");
059    }
060
061    public String version() {
062        return this.getClass().getPackage().getImplementationVersion();
063    }
064
065
066    protected final  WebServer webServer() {
067        return webServer;
068    }
069
070    /**
071     * Returns true if Application is applicable.
072     *
073     * @return true if Application is applicable
074     */
075    public abstract boolean isApplicable();
076
077    /**
078     * Hook on Init
079     */
080    protected void onInit() {
081    }
082
083    /**
084     * Hook on Start
085     */
086    protected void onStart() {
087    }
088
089    /**
090     * Hook on Stop
091     */
092    protected void onStop() {
093    }
094}