
org.fabric3.implementation.pojo.component.PojoComponent Maven / Gradle / Ivy
The newest version!
/*
* Fabric3
* Copyright (c) 2009-2015 Metaform Systems
*
* 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.
*
* Portions originally based on Apache Tuscany 2007
* licensed under the Apache 2.0 license.
*/
package org.fabric3.implementation.pojo.component;
import javax.xml.namespace.QName;
import java.net.URI;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.fabric3.api.annotation.monitor.MonitorLevel;
import org.fabric3.implementation.pojo.manager.ImplementationManager;
import org.fabric3.implementation.pojo.manager.ImplementationManagerFactory;
import org.fabric3.implementation.pojo.objectfactory.ComponentObjectFactory;
import org.fabric3.api.model.type.component.Scope;
import org.fabric3.spi.container.component.ComponentException;
import org.fabric3.spi.container.component.InstanceDestructionException;
import org.fabric3.spi.container.component.InstanceInitException;
import org.fabric3.spi.container.component.InstanceLifecycleException;
import org.fabric3.spi.container.component.ScopeContainer;
import org.fabric3.spi.container.component.ScopedComponent;
import org.fabric3.api.model.type.java.Injectable;
import org.fabric3.spi.container.objectfactory.InjectionAttributes;
import org.fabric3.spi.container.objectfactory.ObjectCreationException;
import org.fabric3.spi.container.objectfactory.ObjectFactory;
/**
* Base class for Java component implementations.
*/
public abstract class PojoComponent implements ScopedComponent {
private URI uri;
private ImplementationManagerFactory factory;
private ScopeContainer scopeContainer;
private QName deployable;
private boolean eager;
private ImplementationManager implementationManager;
private URI classLoaderId;
private MonitorLevel level = MonitorLevel.INFO;
private AtomicBoolean recreate = new AtomicBoolean(true);
private Object cachedInstance;
public PojoComponent(URI componentId, ImplementationManagerFactory factory, ScopeContainer scopeContainer, QName deployable, boolean eager) {
this.uri = componentId;
this.factory = factory;
this.scopeContainer = scopeContainer;
this.deployable = deployable;
this.eager = eager;
}
public void start() throws ComponentException {
scopeContainer.register(this);
}
public void stop() throws ComponentException {
implementationManager = null;
cachedInstance = null;
scopeContainer.unregister(this);
}
public void startUpdate() {
factory.startUpdate();
}
public void endUpdate() {
factory.endUpdate();
}
public URI getUri() {
return uri;
}
public QName getDeployable() {
return deployable;
}
public URI getClassLoaderId() {
return classLoaderId;
}
public void setClassLoaderId(URI classLoaderId) {
this.classLoaderId = classLoaderId;
}
public String getName() {
return uri.toString();
}
public MonitorLevel getLevel() {
return level;
}
public void setLevel(MonitorLevel level) {
this.level = level;
}
public boolean isEagerInit() {
return eager;
}
public Object getInstance() throws InstanceLifecycleException {
if (cachedInstance != null) {
return cachedInstance;
}
return scopeContainer.getInstance(this);
}
public void releaseInstance(Object instance) throws InstanceDestructionException {
scopeContainer.releaseInstance(this, instance);
}
public Object createInstance() throws ObjectCreationException {
if (recreate.getAndSet(false)) {
implementationManager = null;
}
Object instance = getImplementationManager().newInstance();
if (Scope.COMPOSITE == scopeContainer.getScope()) {
cachedInstance = instance;
}
return instance;
}
public ObjectFactory
© 2015 - 2025 Weber Informatics LLC | Privacy Policy