All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspectran.core.component.AbstractComponent Maven / Gradle / Ivy

There is a newer version: 8.1.5
Show newest version
/*
 * Copyright (c) 2008-2024 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.utils.ObjectUtils;
import com.aspectran.utils.logging.Logger;
import com.aspectran.utils.logging.LoggerFactory;
import com.aspectran.utils.thread.AutoLock;

/**
 * Abstract Implementation of {@link Component}.
 *
 * 

Created: 2017. 7. 4.

*/ public abstract class AbstractComponent implements Component { private static final Logger logger = LoggerFactory.getLogger(AbstractComponent.class); private final AutoLock lock = new AutoLock(); 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 { try (AutoLock ignored = lock.lock()) { if (destroyed) { throw new IllegalStateException("Already destroyed " + getComponentName()); } if (initialized) { throw new IllegalStateException("Already initialized " + getComponentName()); } doInitialize(); initialized = true; if (logger.isDebugEnabled()) { logger.debug("Initialized " + getComponentName()); } } } @Override public void destroy() { try (AutoLock ignored = lock.lock()) { if (!initialized) { throw new IllegalStateException("Not yet initialized " + getComponentName()); } if (destroying || destroyed) { throw new IllegalStateException("Already destroyed " + getComponentName()); } try { destroying = true; doDestroy(); if (logger.isDebugEnabled()) { logger.debug("Destroyed " + getComponentName()); } } catch (Exception e) { logger.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 ObjectUtils.simpleIdentityToString(this); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy