com.aspectran.core.component.AbstractComponent Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2019 The Aspectran Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.aspectran.core.component;
import com.aspectran.core.util.logging.Log;
import com.aspectran.core.util.logging.LogFactory;
/**
* Abstract Implementation of {@link Component}.
*
* Created: 2017. 7. 4.
*/
public abstract class AbstractComponent implements Component {
private static final Log log = LogFactory.getLog(AbstractComponent.class);
private final Object lock = new Object();
private volatile boolean initialized;
private volatile boolean destroying;
private volatile boolean destroyed;
protected abstract void doInitialize() throws Exception;
protected abstract void doDestroy() throws Exception;
@Override
public void initialize() throws Exception {
synchronized (lock) {
if (destroyed) {
throw new IllegalStateException("Already destroyed " + getComponentName());
}
if (initialized) {
throw new IllegalStateException("Already initialized " + getComponentName());
}
doInitialize();
log.debug("Initialized " + getComponentName());
initialized = true;
}
}
@Override
public void destroy() {
synchronized (lock) {
if (!initialized) {
throw new IllegalStateException("Not yet initialized " + getComponentName());
}
if (destroying || destroyed) {
throw new IllegalStateException("Already destroyed " + getComponentName());
}
try {
destroying = true;
doDestroy();
log.debug("Destroyed " + getComponentName());
} catch (Exception e) {
log.warn("Failed to destroy " + getComponentName(), e);
} finally {
destroying = false;
}
destroyed = true;
}
}
@Override
public boolean isAvailable() {
return (initialized && !destroying && !destroyed);
}
@Override
public boolean isInitialized() {
return initialized;
}
@Override
public boolean isDestroying() {
return destroying;
}
@Override
public boolean isDestroyed() {
return destroyed;
}
@Override
public String getComponentName() {
return getClass().getSimpleName() + '@' + Integer.toString(hashCode(), 16);
}
}