|
Glassfish example source code file (ResourceManager.java)
The Glassfish ResourceManager.java source code
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2011 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.glassfish.javaee.services;
import org.glassfish.resource.common.ResourceInfo;
import org.glassfish.api.admin.ServerEnvironment;
import org.jvnet.hk2.annotations.Scoped;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.component.*;
import org.jvnet.hk2.config.*;
import org.jvnet.hk2.config.types.Property;
import org.glassfish.api.naming.GlassfishNamingManager;
import org.glassfish.internal.api.*;
import java.beans.PropertyChangeEvent;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.*;
import com.sun.enterprise.config.serverbeans.*;
import com.sun.appserv.connectors.internal.api.ConnectorsUtil;
import com.sun.appserv.connectors.internal.api.ConnectorRuntime;
import com.sun.appserv.connectors.internal.api.ConnectorConstants;
import com.sun.appserv.connectors.internal.spi.ResourceDeployer;
import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.logging.LogDomains;
import org.jvnet.hk2.config.ObservableBean;
import javax.naming.NamingException;
/**
* Resource manager to bind various resources during start-up, create/update/delete of resource/pool
* @author Jagadish Ramu
*/
@Scoped(Singleton.class)
@Service(name="ResourceManager") // this name is used in ApplicationLoaderService
public class ResourceManager implements PostStartup, PostConstruct, PreDestroy, ConfigListener {
private static final Logger logger =
LogDomains.getLogger(ResourceManager.class,LogDomains.RESOURCE_BUNDLE);
private static LocalStringManagerImpl localStrings =
new LocalStringManagerImpl(ResourceManager.class);
@Inject
private ResourcesBinder resourcesBinder;
@Inject
private Habitat connectorRuntimeHabitat;
@Inject
private Habitat deployerHabitat;
@Inject
private Habitat habitat;
private ConnectorRuntime runtime;
@Inject
private GlassfishNamingManager namingMgr;
@Inject
private ServerEnvironment environment;
@Inject
private Domain domain;
//Listen to config changes of resource refs.
//@Inject
//private ResourceRef[] resourceRefs;
//private Server server;
public void postConstruct() {
bindConnectorDescriptors();
deployResources(domain.getResources().getResources());
addListenerToResources();
addListenerToResourceRefs();
addListenerToServer();
}
private void addListenerToResources(){
Resources resources = domain.getResources();
ObservableBean bean = (ObservableBean) ConfigSupport.getImpl((ConfigBeanProxy)resources);
bean.addListener(this);
}
private void addListenerToServer() {
Server server = domain.getServerNamed(environment.getInstanceName());
ObservableBean bean = (ObservableBean) ConfigSupport.getImpl((ConfigBeanProxy)server);
bean.addListener(this);
}
private void bindConnectorDescriptors() {
for(String rarName : ConnectorConstants.systemRarNames){
bindConnectorDescriptorProxies(rarName);
}
}
private void bindConnectorDescriptorProxies(String rarName) {
//these proxies are needed as appclient container may lookup descriptors
String jndiName = ConnectorsUtil.getReservePrefixedJNDINameForDescriptor(rarName);
ConnectorDescriptorProxy proxy = habitat.getComponent(ConnectorDescriptorProxy.class);
proxy.setJndiName(jndiName);
proxy.setRarName(rarName);
try {
namingMgr.publishObject(jndiName, proxy, true);
} catch (NamingException e) {
Object[] params = new Object[]{rarName, e};
logger.log(Level.WARNING, "resources.resource-manager.connector-descriptor.bind.failure", params);
}
}
/**
* deploy resources
* @param resources list
*/
public void deployResources(Collection<Resource> resources){
for(Resource resource : resources){
if(resource instanceof BindableResource){
BindableResource bindableResource = (BindableResource)resource;
if(isBindableResourceEnabled(bindableResource.getJndiName())){
ResourceInfo resourceInfo = new ResourceInfo(bindableResource.getJndiName());
resourcesBinder.deployResource(resourceInfo, resource);
}
} else if (resource instanceof ResourcePool) {
// ignore, as they are loaded lazily
} else{
// only other resource types left are RAC, CWSM
try{
getResourceDeployer(resource).deployResource(resource);
}catch(Exception e){
Object[] params = {ConnectorsUtil.getGenericResourceInfo(resource), e};
logger.log(Level.WARNING, "resources.resource-manager.deploy-resource-failed", params);
}
}
}
/* TODO V3
will there be a chance of double listener registration for a resource ?
eg: allresources added during startup, resources of a particular
connector during connector startup / redeploy ?
*/
addListenerToResources(resources);
}
private boolean isBindableResourceEnabled(String jndiName) {
boolean resourceEnabled = false;
BindableResource res = (BindableResource)
domain.getResources().getResourceByName(BindableResource.class, jndiName);
if(res != null){
resourceEnabled = Boolean.valueOf(res.getEnabled());
}
boolean refEnabled = false;
if(resourceEnabled){
ResourceRef ref = domain.getServerNamed(environment.getInstanceName()).getResourceRef(jndiName);
if(ref != null){
refEnabled = Boolean.valueOf(ref.getEnabled());
}
}
return resourceEnabled && refEnabled;
}
public Resources getAllResources(){
return domain.getResources();
}
/**
* Do cleanup of system-resource-adapter, resources, pools
*/
public void preDestroy() {
if (isConnectorRuntimeInitialized()) {
Collection<Resource> resources = ConnectorsUtil.getAllSystemRAResourcesAndPools(domain.getResources());
undeployResources(resources);
ConnectorRuntime cr = getConnectorRuntime();
if (cr != null) {
// clean up will take care of any system RA resources, pools
// (including pools via datasource-definition)
cr.cleanUpResourcesAndShutdownAllActiveRAs();
}
} else {
if(logger.isLoggable(Level.FINEST)) {
logger.finest("ConnectorRuntime not initialized, hence skipping " +
"resource-adapters shutdown, resources, pools cleanup");
}
}
removeListenerForAllResources();
removeListenerForResources();
removeListenerForResourceRefs();
removeListenerForServer();
}
private void removeListenerForServer() {
Server server = domain.getServerNamed(environment.getInstanceName());
ObservableBean bean = (ObservableBean) ConfigSupport.getImpl((ConfigBeanProxy)server);
bean.removeListener(this);
}
private ConnectorRuntime getConnectorRuntime() {
if(runtime == null){
runtime = connectorRuntimeHabitat.getComponent(ConnectorRuntime.class, null);
}
return runtime;
}
/**
* undeploy the given set of resources<br>
* <b>care has to be taken for the case of dependent resources
Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish ResourceManager.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.