|
Glassfish example source code file (GrizzlyProxy.java)
The Glassfish GrizzlyProxy.java source code
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2007-20120-2010 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 com.sun.enterprise.v3.services.impl;
import com.sun.enterprise.util.Result;
import com.sun.enterprise.v3.services.impl.monitor.GrizzlyMonitoring;
import com.sun.enterprise.config.serverbeans.VirtualServer;
import org.jvnet.hk2.config.types.Property;
import com.sun.grizzly.Controller;
import com.sun.grizzly.ControllerStateListener;
import com.sun.grizzly.config.GrizzlyEmbeddedHttp;
import com.sun.grizzly.config.dom.NetworkListener;
import com.sun.grizzly.config.dom.Protocol;
import com.sun.grizzly.http.SelectorThread;
import com.sun.grizzly.tcp.Adapter;
import com.sun.grizzly.tcp.StaticResourcesAdapter;
import com.sun.grizzly.util.http.mapper.Mapper;
import com.sun.grizzly.util.Grizzly;
import com.sun.hk2.component.ExistingSingletonInhabitant;
import org.glassfish.api.container.EndpointRegistrationException;
import org.glassfish.api.deployment.ApplicationContainer;
import org.glassfish.internal.grizzly.V3Mapper;
import org.jvnet.hk2.component.Inhabitant;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* This clas is responsible for configuring Grizzly {@link SelectorThread}.
*
* @author Jerome Dochez
* @author Jeanfrancois Arcand
*/
public class GrizzlyProxy implements NetworkProxy {
protected GrizzlyListener grizzlyListener;
final Logger logger;
final NetworkListener networkListener;
private int portNumber;
public final static String LEADER_FOLLOWER
= "com.sun.grizzly.useLeaderFollower";
public final static String AUTO_CONFIGURE
= "com.sun.grizzly.autoConfigure";
// <http-listener> 'address' attribute
private InetAddress address;
private GrizzlyService grizzlyService;
private VirtualServer vs;
public GrizzlyProxy(GrizzlyService service, NetworkListener listener) {
grizzlyService = service;
logger = service.getLogger();
networkListener = listener;
String port = networkListener.getPort();
portNumber = 8080;
if (port == null) {
logger.severe("Cannot find port information from domain.xml");
throw new RuntimeException("Cannot find port information from domain configuration");
}
try {
portNumber = Integer.parseInt(port);
} catch (NumberFormatException e) {
logger.severe("Cannot parse port value : " + port + ", using port 8080");
}
try {
address = InetAddress.getByName(networkListener.getAddress());
} catch (UnknownHostException ex) {
logger.log(Level.SEVERE, "Unknown address " + address, ex);
}
configureGrizzly();
}
/**
* Create a <code>GrizzlyServiceListener based on a NetworkListener
* configuration object.
*/
private void configureGrizzly() {
if(!("light-weight-listener".equals(networkListener.getProtocol()))) {
registerMonitoringStatsProviders();
}
grizzlyListener = new GrizzlyListener(grizzlyService.getMonitoring(), new Controller(){
public void logVersion(){}
}, networkListener.getName());
grizzlyListener.configure(networkListener, grizzlyService.habitat);
if(!grizzlyListener.isGenericListener()) {
final GrizzlyEmbeddedHttp embeddedHttp = grizzlyListener.getEmbeddedHttp();
final Protocol httpProtocol = networkListener.findHttpProtocol();
if (httpProtocol != null) {
final V3Mapper mapper = new V3Mapper(logger);
mapper.setPort(portNumber);
mapper.setId(networkListener.getName());
final ContainerMapper adapter = new ContainerMapper(grizzlyService, embeddedHttp);
adapter.setMapper(mapper);
adapter.setDefaultHost(grizzlyListener.getDefaultVirtualServer());
adapter.configureMapper();
embeddedHttp.setAdapter(adapter);
String ct = httpProtocol.getHttp().getDefaultResponseType();
adapter.setDefaultContentType(ct);
final Collection<VirtualServer> list = grizzlyService.getHabitat().getAllByContract(VirtualServer.class);
final String vsName = httpProtocol.getHttp().getDefaultVirtualServer();
for (VirtualServer virtualServer : list) {
if (virtualServer.getId().equals(vsName)) {
vs = virtualServer;
embeddedHttp.setWebAppRootPath(vs.getDocroot());
if (!grizzlyService.hasMapperUpdateListener() &&
vs.getProperty() != null && !vs.getProperty().isEmpty()) {
for (Property p: vs.getProperty()){
String name = p.getName();
if (name.startsWith("alternatedocroot")){
String value = p.getValue();
String[] mapping = value.split(" ");
if (mapping.length != 2){
logger.log(Level.WARNING, "Invalid alternate_docroot " + value);
continue;
}
String docBase = mapping[1].substring("dir=".length());
String urlPattern = mapping[0].substring("from=".length());
try {
StaticResourcesAdapter a = new StaticResourcesAdapter();
a.addRootFolder(docBase);
ArrayList<String> al = toArray(vs.getHosts(),";");
al.add(grizzlyListener.getDefaultVirtualServer());
registerEndpoint(urlPattern,al , a, null);
} catch (EndpointRegistrationException ex) {
logger.log(Level.SEVERE, "Unable to set alternate_docroot", ex);
}
}
}
}
break;
}
}
adapter.addRootFolder(embeddedHttp.getWebAppRootPath());
Inhabitant<Mapper> onePortMapper = new ExistingSingletonInhabitant
Other Glassfish examples (source code examples)Here is a short list of links related to this Glassfish GrizzlyProxy.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.